# ilovedurianhhh/ansa-mcp architecture

> Architecture diagram of the GitHub repository ilovedurianhhh/ansa-mcp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ilovedurianhhh/ansa-mcp
- Repository: https://github.com/ilovedurianhhh/ansa-mcp

## Overview

#### 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: [`src/ansa_mcp/plugin.py`](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/plugin.py) (ANSA plugin), [`src/ansa_mcp/server.py`](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/server.py) (MCP tools), [`src/ansa_mcp/ipc.py`](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/ipc.py) (File IPC), [`src/ansa_mcp/config.py`](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/config.py) (Workspace resolver)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["MCP Client"]
  node_mcp_client(("MCP client"))
  node_mcp_config["Launch config<br/>MCP configuration"]
end

subgraph group_server["External Python"]
  node_server_entry["MCP server<br/>Python entry point<br/>[server.py]"]
  node_tool_operations["MCP tools<br/>tool adapter<br/>[server.py]"]
  node_workspace_config["Workspace resolver<br/>configuration<br/>[config.py]"]
  node_ipc["File IPC<br/>command protocol<br/>[ipc.py]"]
  node_packaging["Server package<br/>packaging<br/>[pyproject.toml]"]
end

subgraph group_workspace["ANSA MCP Workspace"]
  node_runs[("Run directories<br/>IPC artifacts")]
  node_plugin_status["Plugin status<br/>IPC artifact"]
  node_imports["Imports area<br/>managed input"]
  node_outputs["Managed artifacts<br/>workspace output"]
  node_stop_flag["Stop flag<br/>plugin control"]
end

subgraph group_ansa["ANSA Runtime"]
  node_plugin_loader["Plugin loader<br/>ANSA startup script"]
  node_plugin_loop_start["Loop starter<br/>ANSA startup script"]
  node_plugin["ANSA plugin<br/>polling dispatcher<br/>[plugin.py]"]
  node_ansa_api{{"ANSA model API<br/>embedded API"}}
end

node_mcp_config -->|"launches"| node_server_entry
node_packaging -->|"installs entry point"| node_server_entry
node_mcp_client -->|"tool requests"| node_server_entry
node_server_entry -->|"routes calls"| node_tool_operations
node_tool_operations -->|"resolves workspace"| node_workspace_config
node_tool_operations -->|"dispatches and waits"| node_ipc
node_workspace_config -->|"locates"| node_runs
node_ipc -->|"atomic command/result I/O"| node_runs
node_ipc -->|"reads status"| node_plugin_status
node_tool_operations -->|"copies external inputs"| node_imports
node_tool_operations -->|"returns managed outputs"| node_outputs
node_plugin_loader -->|"loads"| node_plugin
node_plugin_loop_start -->|"starts polling"| node_plugin
node_plugin -->|"polls commands and writes results"| node_runs
node_plugin -->|"publishes capabilities and ping"| node_plugin_status
node_stop_flag -.->|"stops loop"| node_plugin
node_plugin -->|"receives staged files"| node_imports
node_plugin -->|"writes autosaves and exports"| node_outputs
node_plugin -->|"executes operations"| node_ansa_api
node_tool_operations -->|"structured response"| node_mcp_client

click node_mcp_config "https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/examples/mcp_config.example.json"
click node_server_entry "https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/server.py"
click node_tool_operations "https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/server.py"
click node_workspace_config "https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/config.py"
click node_ipc "https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/ipc.py"
click node_packaging "https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/pyproject.toml"
click node_plugin_loader "https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/scripts/load_plugin_in_ansa.py"
click node_plugin_loop_start "https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/scripts/start_plugin_loop_in_ansa.py"
click node_plugin "https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/plugin.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_mcp_client,node_mcp_config toneBlue
class node_server_entry,node_tool_operations,node_workspace_config,node_ipc,node_packaging toneAmber
class node_runs,node_plugin_status,node_imports,node_outputs,node_stop_flag toneMint
class node_plugin_loader,node_plugin_loop_start,node_plugin,node_ansa_api toneRose
```

## Components

### MCP Client

- **MCP client**: client
- [Launch config](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/examples/mcp_config.example.json): MCP configuration · `examples/mcp_config.example.json`

### External Python

Normal Python runtime with no ANSA imports.

- [MCP server](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/server.py): Python entry point · Implements tools, validates calls, and orchestrates runs. · `src/ansa_mcp/server.py`
- [MCP tools](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/server.py): tool adapter · Model, geometry, import/export, recovery, quality, benchmark, and script tools. · `src/ansa_mcp/server.py`
- [Workspace resolver](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/config.py): configuration · Resolves ANSA_MCP_HOME or the default workspace. · `src/ansa_mcp/config.py`
- [File IPC](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/ipc.py): command protocol · Creates runs and atomically writes commands, results, status, and logs. · `src/ansa_mcp/ipc.py`
- [Server package](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/pyproject.toml): packaging · Defines the ansa-mcp-server installed entry point. · `pyproject.toml`

### ANSA MCP Workspace

Shared file-based IPC and managed artifacts.

- **Run directories**: IPC artifacts · Independent per-call commands, results, and logs.
- **Plugin status**: IPC artifact · Status and capability publication used with ping validation.
- **Imports area**: managed input · External files are copied here before ANSA receives them.
- **Managed artifacts**: workspace output · Autosaves, exports, and benchmark reports remain under the workspace.
- **Stop flag**: plugin control

### ANSA Runtime

ANSA embedded Python environment.

- [Plugin loader](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/scripts/load_plugin_in_ansa.py): ANSA startup script · Loads the polling plugin into ANSA. · `scripts/load_plugin_in_ansa.py`
- [Loop starter](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/scripts/start_plugin_loop_in_ansa.py): ANSA startup script · Starts the plugin polling loop in ANSA. · `scripts/start_plugin_loop_in_ansa.py`
- [ANSA plugin](https://github.com/ilovedurianhhh/ansa-mcp/blob/auto/2026-06-30-initial-upload/src/ansa_mcp/plugin.py): polling dispatcher · Only component that imports and calls the ANSA Python API. · `src/ansa_mcp/plugin.py`
- **ANSA model API**: embedded API · ANSA v25.1.0 model operations.

## Connections

- 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
