PyAnsysBaseMCP#

class ansys.common.mcp.server.PyAnsysBaseMCP(python_executable: str | None = None, working_directory: str | None = None, *args, **kwargs)#

Bases: fastmcp.FastMCP, abc.ABC

Base MCP server for PyAnsys libraries.

Overview#

product_cleanup

Cleanup routine before shutting down the server.

product_startup

Startup routine to initialize resources when the server starts.

create_context

Create product-specific context.

start_python_session

Start a persistent Python session for executing generated code.

cleanup_python_session

Clean up the persistent Python session.

product_lifespan

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 PyMAPDLContext with an MAPDL field).

Returns:
PyAnsysBaseAppContext

Context 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:
serverFastMCP

MCP server instance.

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.