Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ needs no extra dependencies. See the [Studio docs](docs/studio.md).

![PythonFMU3 Studio](docs/assets/studio-simulate.png)

### AI agents (MCP)

`pythonfmu3 mcp` lets AI agents (Claude, GitHub Copilot in VS Code, Cursor, ...) write,
check, simulate and build FMUs for you:

```bash
pip install "pythonfmu3[mcp]"
claude mcp add pythonfmu3 -- pythonfmu3 mcp ~/fmu-models
```

Then ask, for example: "Make me a bouncing ball FMU and simulate it for 5 s." See
[AI agents (MCP)](docs/mcp.md).

### How do I build an FMU from python code?

1. Install `pythonfmu3` package:
Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 1.2.0 (unreleased)

### Added
- **MCP server for AI agents** (`pythonfmu3 mcp`): Claude, GitHub Copilot in VS Code,
Cursor and other MCP clients can write, check, simulate and build FMUs, using the same
checks and simulators as the Studio. Install with `pip install "pythonfmu3[mcp]"`
(Python 3.10+). See [AI agents (MCP)](mcp.md).

## 1.1.0 (2026-11-14)

### Added
Expand Down
114 changes: 114 additions & 0 deletions docs/mcp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# AI agents (MCP server)

`pythonfmu3 mcp` runs PythonFMU3 as an [MCP](https://modelcontextprotocol.io) server. AI
agents such as Claude (Desktop or Code), GitHub Copilot in VS Code and Cursor can then
write, check, simulate and build FMUs for you.

The agent doesn't just write code: it runs the same checks and simulations as
[PythonFMU3 Studio](studio.md), reads the results and fixes what's wrong before it builds
the FMU.

## Install

The server needs Python 3.10 or later:

```bash
pip install "pythonfmu3[mcp]"
```

This adds the `mcp` package. Building FMUs needs
PythonFMU3's native library for your platform, as for any FMU build (see
[Installation](install.md)).

## Connect an agent

The server takes a workspace folder, where it reads and writes models:

```bash
pythonfmu3 mcp ~/fmu-models
```

You don't run this yourself: the agent starts it. Use the full path to `pythonfmu3`
(`which pythonfmu3` in your virtual environment) if the agent doesn't find it.

### Claude Desktop

In `claude_desktop_config.json` (**Settings → Developer → Edit Config**):

```json
{
"mcpServers": {
"pythonfmu3": {
"command": "/path/to/.venv/bin/pythonfmu3",
"args": ["mcp", "/Users/you/fmu-models""]
}
}
}
```

### Claude Code

```bash
claude mcp add pythonfmu3 -- /path/to/.venv/bin/pythonfmu3 mcp /Users/you/fmu-models
```

### VS Code (GitHub Copilot)

In `.vscode/mcp.json`, then use Copilot Chat in agent mode:

```json
{
"servers": {
"pythonfmu3": {
"type": "stdio",
"command": "/path/to/.venv/bin/pythonfmu3",
"args": ["mcp", "${workspaceFolder}/models"]
}
}
}
```

Other MCP clients work the same way: the command is `pythonfmu3`, the arguments are
`mcp <folder>`, and the transport is stdio.

## What the agent can do

| Tool | What it does |
| --- | --- |
| `get_guide` | How to write PythonFMU3 models, and the recommended workflow |
| `list_files`, `read_file` | See the models, FMUs and results in the workspace |
| `write_model` | Save a whole model and return its checks |
| `edit_model` | Replace part of a model (much cheaper than rewriting it) and return its checks |
| `copy_template` | Start a model from a template |
| `run_checks` | The Studio's checks: variables, causality, settable inputs, the model description |
| `simulate` | Run the model (Python preview, or the built FMU in fmusim or FMPy) and summarise its outputs (or chosen variables); the full result is saved as CSV in `results/` |
| `build_fmu` | Build the FMU into `build/` |
| `inspect_fmu`, `validate_fmu` | Describe or validate any FMU |
| `list_templates`, `read_template` | Working example models, for reference |

Try asking:

- "Write a Co-Simulation FMU of a water tank with an inflow input and a drain valve, check
it and simulate it for 100 s."
- "Fix the problems in `controller.py`."
- "Make me a bouncing ball and simulate it for 5 s."

## Keeping it quick and cheap

The tools are designed to keep what the agent reads and writes small: changes go through
`edit_model` instead of rewriting files, templates are copied rather than retyped, every
change returns the checks, and simulations return a summary of the outputs rather than the
data. On your side:

- Be specific in the request (interface, parameters, units, stop time), so the agent needs
fewer rounds.
- Say "don't build yet" while iterating; building is only needed for the final FMU.
- Start a new chat for each new model, and use a smaller, faster model for routine work.

## Security

- The server reads and writes only inside the workspace folder.
- Model code runs in a separate Python process for each request.
- The server runs the code in your models, as building an FMU does, and the agent writes
that code. Use a workspace folder for agent work, and review models before using them
elsewhere.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ nav:
- Model Exchange: usageMX.md
- Dependencies: dependencies.md
- Studio: studio.md
- AI agents (MCP): mcp.md
- Migrating from PythonFMU: migration.md
- Changelog: changelog.md
theme: readthedocs
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ dependencies = []

[project.optional-dependencies]
tests = ["pytest", "fmpy"]
# The MCP server for AI agents (`pythonfmu3 mcp`)
mcp = ["mcp>=1.2; python_version >= '3.10'"]

[project.urls]
Homepage = "https://github.com/StephenSmith25/PythonFMU3"
Expand Down
10 changes: 10 additions & 0 deletions pythonfmu3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def default_execution(**kwargs):
from .studio.server import create_command_parser as create_studio_parser
create_studio_parser(studio_parser)

mcp_parser = subparsers.add_parser(
"mcp",
description="Run PythonFMU3 as an MCP server (over stdio) so AI agents can write, check, "
"simulate and build FMUs. "
'Needs: pip install "pythonfmu3[mcp]"',
help="Run the MCP server for AI agents."
)
from .mcp_server import create_command_parser as create_mcp_parser
create_mcp_parser(mcp_parser)

options = vars(parser.parse_args())
execute = options.pop("execute")
execute(**options)
Expand Down
2 changes: 1 addition & 1 deletion pythonfmu3/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.0"
__version__ = "1.2.0"
Loading
Loading