Develop PyMAPDL-MCP#

Set up your development environment and start contributing code to PyMAPDL-MCP.

Architecture#

PyMAPDL-MCP is built on the PyAnsysBaseMCP framework (from ansys-common-mcp), which is itself built on top of FastMCP. The server lifecycle has three phases:

Startup

  • Initializes a persistent Python session for custom code execution.

  • If --connect-on-startup is used, connects to an existing MAPDL instance.

  • Otherwise, waits for a dynamic connection through the connect_to_mapdl or launch_mapdl_session tools.

Runtime

  • Exposes MCP tools for MAPDL interaction.

  • Manages dynamic MAPDL connections through the tools.

  • Executes commands in both the MAPDL session and the persistent Python session.

  • Provides workflow guidance through the get_guidelines_for context tool.

  • Dynamically enables and disables tools based on the MAPDL connection state.

Shutdown

  • Gracefully disconnects from MAPDL.

  • Cleans up the persistent Python session resources.

App context#

The server uses a strongly typed PyMAPDLAppContext dataclass that holds:

  • The active MAPDL instance connection

  • The persistent Python session for custom code execution

  • Transport configuration (STDIO or HTTP)

  • Connection settings (IP, port, auto-connect flags)

  • Command history tracking

Check prerequisites#

Before you begin, ensure you have:

  • Python 3.10 or higher

  • Git installed

  • A text editor or IDE (such as Visual Studio Code or PyCharm)

  • A GitHub account

Clone the repository#

  1. Fork the GitHub repository.

  2. Clone your fork locally:

    git clone https://github.com/YOUR_USERNAME/pymapdl-mcp.git
    cd pymapdl-mcp
    
  3. Add the upstream repository as a remote:

    git remote add upstream https://github.com/ansys/pymapdl-mcp.git
    

Set up your development environment#

  1. Create a virtual environment:

    python -m venv .venv
    
  2. Activate the virtual environment:

    On Windows:

    .venv\Scripts\activate
    

    On macOS/Linux:

    source .venv/bin/activate
    
  3. Install the package in editable mode with development dependencies:

    pip install -e .[tests]
    
  4. Install pre-commit hooks:

    pre-commit install
    

    This ensures code quality checks run automatically before each commit.

Explore the project structure#

pymapdl-mcp/
├── src/ansys/mapdl/mcp/          # Main package source
│   ├── __init__.py
│   ├── __main__.py               # Entry point
│   ├── contexts.py               # App context and guidelines
│   ├── helpers.py                # Utility functions
│   ├── mcp.py                    # MCP server setup
│   ├── prompts.py                # Prompt definitions
│   ├── server.py                 # Server implementation
│   ├── tools.py                  # MCP tools
│   └── python_helper/            # Python session helpers
├── tests/                        # Test suite
├── doc/                          # Documentation
├── docker/                       # Docker configuration
├── pyproject.toml               # Project metadata
└── README.md                    # Main README

Follow the development workflow#

  1. Create a feature branch:

    git checkout -b feature/your-feature-name
    
  2. Make your changes:

    • Add or modify relevant files.

    • Add or modify tests as needed.

    • Update documentation if applicable.

  3. Run tests:

    # Run unit tests (no MAPDL required)
    pytest -m "not integration"
    
    # Run all tests with coverage
    pytest --cov=ansys.mapdl.mcp --cov-report=html
    
  4. Check code quality:

    # Run pre-commit checks manually
    pre-commit run --all-files
    
  5. Commit your changes:

    git add .
    git commit -m "feat: Add your feature description"
    
    # Pre-commit hooks will run automatically
    
  6. Push to your fork:

    git push origin feature/your-feature-name
    
  7. Create a pull request (PR) on GitHub:

    • Go to the main repository on GitHub.

    • Click New Pull Request.

    • Select your feature branch.

    • Fill in the PR description.

    • Submit the PR.

Follow code conventions#

Branch naming

  • Features: feature/short-description

  • Fixes: fix/short-description

  • Documentation: doc/short-description

  • Tests: test/short-description

Commit messages

Use conventional commits format:

feat: Add new tool for XYZ feature.
fix: Correct handling of edge case.
docs: Update installation instructions.
test: Add tests for feature ABC.
refactor: Simplify tool implementation.
chore: Update dependencies.

Code style

  • Follow PEP 8 and the Coding style.

  • Use type hints for all functions.

  • Write docstrings following NumPy style.

  • Keep line lengths to 100 characters or less.

  • Format code with Black and isort (run automatically via pre-commit).

Add a new tool#

  1. Edit the src/ansys/mapdl/mcp/tools.py file.

  2. Add your tool using the @app.tool() decorator:

    @app.tool()
    def your_new_tool(ctx: Context, param1: str, param2: int = 10) -> str:
        """
        Brief description of what this tool does.
    
        Parameters
        ----------
        ctx : Context
            Tool execution context with access to MAPDL.
        param1 : str
            Description of param1.
        param2 : int, default: 10
            Description of param2.
    
        Returns
        -------
        str
            Description of what this tool returns
        """
        mapdl = ctx.application_context.mapdl
    
        if mapdl is None:
            return "Error: No MAPDL connection available"
    
        try:
            # Your implementation here
            result = mapdl.your_operation()
            return f"Success: {result}"
        except Exception as e:
            return f"Error: {str(e)}"
    
  3. Write tests in the tests/test_tools.py file.

  4. Document the tool in the doc/source/api/tools.rst file.

  5. Add a usage example if appropriate in the doc/source/examples/ directory.

Add or modify documentation#

You can tag tools with the @app.tool() decorator to selectively turn them off at runtime. After applying a tag, call app.disable() with that tag when the condition applies (for example, when --connect-on-startup locks the connection):

# Tag the tool so it can be turned off as a group
@app.tool(tags={"locked_connection"})
def connect_to_mapdl(ctx: Context, port: int = 50052, ip: str = "localhost") -> str:
    ...

# Turn off all tools with this tag when the connection is locked
app.disable(tags={"locked_connection"})
  1. Edit RST files in the doc/source/ directory.

  2. Build documentation locally:

    cd doc
    make.bat html    # On Windows
    make html        # On Linux/macOS
    
  3. View the documentation by opening the _build/html/index.html file in your browser.

  4. Commit changes to your documentation files.

Run tests#

PyMAPDL-MCP includes a comprehensive test suite with more than 40 tests.

Run unit tests (recommended for development):

pytest -m "not integration"

Run all tests with coverage:

pytest --cov=ansys.mapdl.mcp --cov-report=html

Run a specific test file:

pytest tests/test_tools.py -v

Run integration tests (requires MAPDL):

pytest -m integration

Test coverage goal#

Aim for greater than 80% test coverage on new code. Open the htmlcov/index.html file after report generation to view the report:

pytest --cov=ansys.mapdl.mcp --cov-report=html

Get help#

If you need help:

Submit your work#

When your feature is ready:

  1. Use the pytest -m "not integration" command to ensure all tests pass.

  2. Use the pre-commit run --all-files command to ensure code quality.

  3. Update relevant documentation.

  4. Add tests for new features to ensure greater than 80% coverage.

  5. Create a PR with:

    • A clear description of changes

    • References to related issues (such as Fixes #123)

    • A list of changes made

    • A note of any breaking changes

Follow PR guidelines#

  • Keep PRs focused on a single feature or fix.

  • Include tests for new features.

  • Update documentation as needed.

  • Respond to review feedback.

  • Keep the PR up to date with the main branch.

  • Use GitHub’s Squash and merge option when possible to keep the history clean.

Earn recognition#

Contributors are recognized in:

  • PR comments

  • Release notes

  • Contributors file (as applicable)

See also#