How ilovedurianhhh/ansa-mcp works
Purpose and Entry Points
ANSA MCP is a Python bridge that exposes ANSA model operations to an MCP client without importing ANSA APIs outside the ANSA runtime. The external process starts through ansa_mcp.server (src/ansa_mcp/server.py); ANSA loads and runs the polling plugin through scripts/load_plugin_in_ansa.py or scripts/start_plugin_loop_in_ansa.py.
Process Boundary and File IPC
The core architectural boundary is between a normal Python MCP server and ANSA’s embedded Python environment. The server writes JSON commands under an ANSA MCP workspace; the plugin polls, executes them through ANSA APIs, and writes matching JSON results. IPC mechanics, atomic .tmp/os.replace() writes, command IDs, status, and run artifacts are centralized in src/ansa_mcp/ipc.py, with workspace resolution in src/ansa_mcp/config.py.
External MCP Server
src/ansa_mcp/server.py is the client-facing adapter and orchestration layer. It implements MCP tools, validates inputs, creates per-call runs, dispatches commands through file IPC, waits for results, and returns structured responses. It deliberately does not import ansa, preserving runtime isolation. Its tools cover connection checks, model inspection, geometry selection/deletion, import/export, autosave/restore, quality checks, benchmarks, and controlled script execution.
ANSA-Resident Plugin
src/ansa_mcp/plugin.py runs only inside ANSA. It owns the polling loop, publishes plugin status/capabilities, reads command files, dispatches operation names, calls ANSA APIs, and serializes results or errors. This is the only component intended to access the ansa Python API. The loop can be stopped by ANSA-side control functions or a workspace stop.flag.
Command Execution Flow
A typical request flows as: MCP client → server tool in src/ansa_mcp/server.py → command JSON via src/ansa_mcp/ipc.py → plugin dispatcher in src/ansa_mcp/plugin.py → result JSON → server response. Each call has an independent run directory, supporting later inspection through the read_last_log tool. Connection validation combines plugin status with a plugin ping, avoiding reliance on file presence alone.
Model and Artifact Boundaries
The server constrains outputs to ANSA_MCP_HOME; imported external files are copied into its imports area before ANSA receives them. Autosaves, exports, benchmark reports, commands, results, and logs remain workspace artifacts. Destructive face deletion is designed around a staged flow: enumerate/filter faces, preview a selection, then execute deletion; autosave and restore provide recovery paths.
Runtime and Infrastructure
The primary runtime is Python, with an MCP SDK expected for the server deployment and ANSA’s embedded Python for the plugin. Verified ANSA operation targets ANSA v25.1.0, including -nogui startup. The workspace location is configured by ANSA_MCP_HOME (defaulting to a user-home .ansa-mcp directory). MCP client launch configuration is represented by examples/mcp_config.example.json; packaging and the installed ansa-mcp-server entry point are defined in pyproject.toml.
Main entry points: plugin.py ANSA plugin · server.py MCP tools · ipc.py File IPC · config.py Workspace resolver
Components 16
MCP Client
- MCP client
- Launch config
examples/mcp_config.example.json
External Python
Normal Python runtime with no ANSA imports.
- MCP server · Implements tools, validates calls, and orchestrates runs.
src/ansa_mcp/server.py - MCP tools · Model, geometry, import/export, recovery, quality, benchmark, and script tools.
src/ansa_mcp/server.py - Workspace resolver · Resolves ANSA_MCP_HOME or the default workspace.
src/ansa_mcp/config.py - File IPC · Creates runs and atomically writes commands, results, status, and logs.
src/ansa_mcp/ipc.py - Server package · Defines the ansa-mcp-server installed entry point.
pyproject.toml
ANSA MCP Workspace
Shared file-based IPC and managed artifacts.
- Run directories · Independent per-call commands, results, and logs.
- Plugin status · Status and capability publication used with ping validation.
- Imports area · External files are copied here before ANSA receives them.
- Managed artifacts · Autosaves, exports, and benchmark reports remain under the workspace.
- Stop flag
ANSA Runtime
ANSA embedded Python environment.
- Plugin loader · Loads the polling plugin into ANSA.
scripts/load_plugin_in_ansa.py - Loop starter · Starts the plugin polling loop in ANSA.
scripts/start_plugin_loop_in_ansa.py - ANSA plugin · Only component that imports and calls the ANSA Python API.
src/ansa_mcp/plugin.py - ANSA model API · ANSA v25.1.0 model operations.
Connections 20
- Launch config → MCP server: launches
- Server package → MCP server: installs entry point
- MCP client → MCP server: tool requests
- MCP server → MCP tools: routes calls
- MCP tools → Workspace resolver: resolves workspace
- MCP tools → File IPC: dispatches and waits
- Workspace resolver → Run directories: locates
- File IPC → Run directories: atomic command/result I/O
- File IPC → Plugin status: reads status
- MCP tools → Imports area: copies external inputs
- MCP tools → Managed artifacts: returns managed outputs
- Plugin loader → ANSA plugin: loads
- Loop starter → ANSA plugin: starts polling
- ANSA plugin → Run directories: polls commands and writes results
- ANSA plugin → Plugin status: publishes capabilities and ping
- Stop flag → ANSA plugin: stops loop
- ANSA plugin → Imports area: receives staged files
- ANSA plugin → Managed artifacts: writes autosaves and exports
- ANSA plugin → ANSA model API: executes operations
- MCP tools → MCP client: structured response