PyAnsysBaseMCP#

class ansys.common.mcp.server.PyAnsysBaseMCP(python_executable: str | None = None, working_directory: str | None = None, need_python: bool = True, *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.

run_cli

Parse CLI arguments and run the MCP server with the selected transport.

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.

PyAnsysBaseMCP.run_cli(argv: List[str] | None = None) None#

Parse CLI arguments and run the MCP server with the selected transport.

This method provides a ready-to-use entry point for product-specific MCP servers, handling transport selection and HTTP configuration so that downstream packages do not need to duplicate this boilerplate.

The method follows the template method pattern: two hooks let subclasses extend the CLI without rewriting the transport dispatch logic:

  • _add_cli_arguments() — add product-specific arguments to the parser.

  • _configure_from_cli() — process parsed product-specific arguments (for example, store them in _cli_config so create_context can read them).

Parameters:
argvlist[str] or None, default: None

Argument list to parse. If None, sys.argv[1:] is used. Pass an explicit list in tests to avoid reading the real command line.

Examples

Minimal usage — call from a product’s __main__.py:

>>> app.run_cli()

Product with extra CLI arguments:

>>> class PyMAPDLMCP(PyAnsysBaseMCP):
...     def _add_cli_arguments(self, parser):
...         parser.add_argument("--ip", dest="mapdl_ip", default="127.0.0.1")
...         parser.add_argument("--port", dest="mapdl_port", type=int, default=50052)
...
...     def _configure_from_cli(self, args):
...         self._cli_config = {"mapdl_ip": args.mapdl_ip, "mapdl_port": args.mapdl_port}