PyAnsysBaseMCP#
- class ansys.common.mcp.server.PyAnsysBaseMCP(python_executable: str | None = None, working_directory: str | None = None, *args, **kwargs)#
Bases:
fastmcp.FastMCP,abc.ABCBase MCP server for PyAnsys libraries.
Overview#
Cleanup routine before shutting down the server. |
|
Startup routine to initialize resources when the server starts. |
Create product-specific context. |
|
Start a persistent Python session for executing generated code. |
|
Clean up the persistent Python session. |
|
Define default lifespan for PyAnsys MCP servers. |
Import detail#
from ansys.common.mcp.server import PyAnsysBaseMCP
Attribute detail#
- PyAnsysBaseMCP.python_executable = None#
- PyAnsysBaseMCP.working_directory = None#
Method detail#
- abstractmethod PyAnsysBaseMCP.product_cleanup()#
Cleanup routine before shutting down the server.
This abstract method must be implemented by subclasses to handle product-specific cleanup.
- abstractmethod PyAnsysBaseMCP.product_startup()#
Startup routine to initialize resources when the server starts.
This abstract method must be implemented by subclasses to handle product-specific initialization.
- PyAnsysBaseMCP.create_context() ansys.common.mcp.context.PyAnsysBaseAppContext#
Create product-specific context.
Override this method in subclasses to return custom context types (such as in
PyMAPDLContextwith an MAPDL field).- Returns:
PyAnsysBaseAppContextContext instance for this server. The default implementation creates a base context with Python session support.
Examples
Override in a product-specific server:
>>> class PyMAPDLMCP(PyAnsysBaseMCP): ... def create_context(self) -> PyMAPDLContext: ... return PyMAPDLContext( ... python_session=PersistentPythonSession(self.python_executable), ... command_history=[], ... )
- PyAnsysBaseMCP.start_python_session()#
Start a persistent Python session for executing generated code.
- PyAnsysBaseMCP.cleanup_python_session()#
Clean up the persistent Python session.
- async PyAnsysBaseMCP.product_lifespan(server: fastmcp.FastMCP) AsyncIterator[ansys.common.mcp.context.PyAnsysBaseAppContext]#
Define default lifespan for PyAnsys MCP servers.
Product-specific servers can override this method if needed.
- Parameters:
- server
FastMCP MCP server instance.
- server
- Yields:
AsyncIterator[PyAnsysBaseAppContext]Application context for the MCP server.
Notes
This method orchestrates the complete lifecycle: 1. Creates context (via factory method - extensible by subclasses). 2. Initializes Python session (managed by base class). 3. Calls product-specific startup. 4. Yields context to the application. 5. Cleans up in reverse order on shutdown.