twincat-project-mcp
README.md
# twincat-project-mcp
Inspect a saved Beckhoff TwinCAT 3 project from Codex, Claude Code or another local stdio MCP client. Open a system or PLC project to see which files it includes, which references could be resolved and the SHA-256 of each file and of the project manifest. The Python 3.12+ reader runs without XAE, an ADS route or a PLC.
## First read
From this checkout, use uv to install the locked development environment and exercise the actual stdio server with the bundled synthetic program.
```sh
uv sync --locked
uv run twincat-project-mcp-doctor
```
Expect a PASS with the hash of `MAIN.TcPOU`. To check your own input, pass `--project /absolute/path/to/MAIN.TcPOU`. Compare its reported digest with `Get-FileHash -Algorithm SHA256` on Windows or `sha256sum` on Linux.
Configure your client's stdio launcher with the absolute path to `.venv/Scripts/twincat-project-mcp.exe` on Windows or `.venv/bin/twincat-project-mcp` on Linux. The server captures its inputs at startup. Restart it to inspect a new saved revision.
```json
{
"mcpServers": {
"twincat-project": {
"command": "C:/path/to/twincat-project-mcp/.venv/Scripts/twincat-project-mcp.exe",
"args": ["--project", "C:/path/to/MAIN.TcPOU"]
}
}
}
```
This is the JSON form for clients using `mcpServers`. For Codex, register the same executable and arguments with `codex mcp add twincat-project -- <executable> --project <absolute-file>`. Ask “Where is `ready` declared and which line sets it?” when using the bundled example.
## Inspect a project
Pass a saved system or PLC project to the same launcher. To verify a project before opening your MCP client, run the doctor against it.
```sh
uv run twincat-project-mcp-doctor --project /checkout/plc/Cell.tsproj
```
By default, capture stays inside the entry file’s directory. If the project includes a sibling folder, pass `--root /checkout` to authorize the common parent. The doctor reports the entry hash, manifest hash, captured file count and unresolved reference count.
| Tool | Result |
| --- | --- |
| `get_capabilities` | Supported formats, scope and whether a comparison input was selected. |
| `get_project` | Entry identity, object kind, visible sections and direct references. For project entries, includes the manifest hash and counts of captured files and unresolved references. |
| `list_inventory` | Page through files, references, diagnostics, projects, instances, tasks, libraries, objects or coverage notes. Each page identifies its manifest. |
| `read_object` | For project entries, pass the object ID from a search or inventory result. Returns the requested section lines, owning PLC, file hash and locator. For a single source file, select its top-level section directly. |
| `search_project` | Paged literal search in captured declarations and ST bodies. Filter by PLC path, object kind or section. Each project hit identifies its owner, object, file hash and one-based section line. |
| `compare_projects` | Enabled by `--compare-project <second-file>`. Returns both file hashes and the fields that changed in the supported projection. |
From a `.tsproj`, capture follows explicit PLC project paths and recognized split PLC descriptors. For example, a PLC project with `File="Cell.xti"` resolves under `_Config/PLC`. Its `PrjFilePath` resolves relative to that XTI. From each `.plcproj`, capture follows literal, unconditional compile items for POUs, GVLs, DUTs, PLC tasks and interfaces. Task inventory distinguishes system tasks, PLC tasks and their saved program calls. Library rows show saved references and placeholder resolutions.
For example, from the demo system project, call `search_project` with the following arguments to find uses of the status buffer in method bodies.
```json
{"request":{"query":"StatusBuffer[","plc":"CartonPLC/CartonPLC.plcproj","kind":"Method","section":"implementation","limit":5}}
```
To continue, repeat the request with the returned `next_cursor` in `request.cursor`. The query and filters must match. You can change the page size (1–100). A null cursor marks the last page. On an empty project search, `searched_objects` counts objects eligible under the filters. The unresolved-reference and coverage counts describe the entire capture. Use `list_inventory` with `diagnostics` or `coverage` to inspect those gaps.
Check `isError` before interpreting a tool response. An inspection failure carries `error.code`, `hint`, `retryable` and the selected source identity in both structured content and JSON text. After `invalid_cursor`, reuse the original query or start a new search. After `snapshot_mismatch`, repeat the lookup on the current snapshot. For a project `read_object`, pass the hit’s `object_id` and optional `snapshot_sha256` to verify the revision.
### Try the public CartonPLC demo
Open `plc/CartonCell.tsproj` and ask **“Where is the status array declared, where does the heartbeat advance, and where is the array published?”** In revision `172b616`, the declaration is on line 5 of the `GVL_HMI` declaration. In the `PublishStatus` method, implementation line 13 advances word 9 and line 17 publishes the array. These are section-relative lines. The links open the corresponding XML lines. Starting at `plc/CartonPLC/CartonPLC.plcproj` yields the same three source locations. [Public demo repository](https://github.com/sergiogallegos/demo-twincat) · [Status declaration](https://github.com/sergiogallegos/demo-twincat/blob/172b616/plc/CartonPLC/GVL_HMI.TcGVL#L8) · [Heartbeat and publication](https://github.com/sergiogallegos/demo-twincat/blob/172b616/plc/CartonPLC/FB_CartonCell.TcPOU#L308-L312)
Capture accepts local regular files up to 2 MiB each, 32 MiB and 2,048 files in total. It rejects DTDs and links, and compares two captures before serving a session. Restart the server after a save to inspect the new revision. Requested source excerpts are visible to the MCP client you choose.
## Optional physical CX check
On a Windows engineering PC with the TwinCAT ADS library and an existing route to a CX running the CartonPLC demo, install the extra and explicitly select the target. In XAE, **SYSTEM → Routes** lists remote AMS Net IDs. Use the PLC instance’s AMS port. The demo uses 851.
```sh
uv sync --locked --extra ads
uv run --no-sync twincat-carton-probe --net-id <six-octet-net-id> --port 851
```
This separate CLI reads only `GVL_HMI.Status`. It checks the declared 106-DINT array, transfer length and signature 51003 before reporting two host-timestamped state and heartbeat samples. An unchanged heartbeat is reported as observed. PLC writes, subscriptions and route creation are outside the probe. [Physical bench result](docs/bench-verification.md)
## Development
```sh
uv run ruff format --check .
uv run ruff check .
uv run pyright
uv run pytest
uv build --wheel
```
CI tests the built wheel on Windows and Linux with Python 3.12 and 3.13. The automated tests use synthetic data and local stdio. [Architecture and design](docs/architecture.md) · [Peer feature analysis](docs/feature-inventory.md) · [Contributor rules](AGENTS.md)
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues