sim-tools-mcp
by zuhalsicim
README.md
# sim-tools-mcp
An MCP server that lets an AI agent use my [cooling-surrogate](https://github.com/zuhalsicim/cooling-surrogate)
pipeline: run the finite-volume solver, ask the surrogate, sweep one input, check an answer against
physics, and compare surrogate and solver. I built it to answer one question.
## The question
Can an agent work with a simulation and its fast surrogate through tool calls, without ever getting a
surrogate number it should not trust, and what does each call cost?
The surrogate in cooling-surrogate is much faster than the solver, but it is only valid inside the box of
inputs it was trained on, and only when its answer passes the physics checks. A person reads that in the
README. An agent only sees the tool list and what the calls return, so the tools have to
carry those rules themselves.
## What I measured
Every number in this section is written by `python -m bench.readme` from the files in `results/`, which
`python -m bench.run_all` produces. CI fails if the two disagree.
<!-- results:begin -->
I asked the scripted agent for the warmest mould temperature that keeps a 2 mm part at or under 8 s of cooling. It answered 44.00 °C after 7 tool calls. The solver's own answer, found by bisection without the tools, is 44.032 °C, so the agent is 0.032 K below it, inside the 0.05 K step it searched with. Its last calls ran the solver on both sides of the answer to confirm it. On this run the surrogate put the crossing one step too high and the solver check moved it down. With one predict_cooling call per bisection step it took 17 calls. The solver alone took 13 calls and 2.59 s, where the sweep plan took 475 ms.
With a 12 s budget the answer lies above 60 °C, where the surrogate was never trained. The same plan answered 76.15 °C (solver: 76.156 °C) in 6 calls; 56 of its 97 sweep points were answered by the solver, each one marked as such. It still took only 1.37 s, against 3.01 s for the solver alone, because a sweep hands all its out-of-box points to the solver as one batch.
Of 694 calls the surrogate must not answer (invalid, malformed, outside the training box, or failing a physics check), it answered 0. 74 hand-written calls of that kind were all handled as planned (74 of 74): 59 refused with a code, a reason and a hint, the rest answered by the solver. 1,000 random calls matched an independent check of the limits every time (0 mismatches). With a broken model loaded on purpose, the int8 copy my gate in cooling-surrogate blocked, 71 of 200 answers inside the box failed a physics check and went to the solver; 0 answers that fail a check came back.
Every tool answer equals the direct library call: 134 of 134 comparisons on 22 fixed cases, exact float equality after the JSON round trip, including against cooling-surrogate's own HTTP service. A second server process gave byte-identical answers and result ids (45 of 45), and repeated calls came from the result store (45 of 45, 0 solver runs).
Over stdio a surrogate call takes 2.30 ms at the median against 0.31 ms for the bare library call, so the protocol and the guards add about 1.99 ms. A solver call takes 221 ms (library 221 ms). Starting the server takes 4.01 s.
### 1. The agent session
A deterministic client follows a fixed plan, no language model. One fresh server per run. Time is what the client waited for tool calls, without server start.
**A 2 mm wall, melt 230 °C: what is the warmest mould temperature between 20 and 60 °C that keeps the cooling time at or below 8 s?** Solver answer: 44.0317 °C.
| plan | tool calls | solver runs | time | answer | off by | confirmed by the solver |
|---|---:|---:|---:|---:|---:|---|
| two sweeps, then check | 7 | 2 | 475 ms | 44.0000 °C | -0.0317 K | yes (stepped down) |
| one point per call | 17 | 2 | 474 ms | 44.0234 °C | -0.0082 K | yes (stepped down) |
| solver only | 13 | 12 | 2.59 s | 44.0234 °C | -0.0082 K | yes |
**The same part with a 12 s cycle budget: what is the warmest mould temperature between 20 and 95 °C that keeps the cooling time at or below 12 s? The surrogate was trained up to 60 °C only.** Solver answer: 76.1559 °C.
| plan | tool calls | solver runs | time | answer | off by | confirmed by the solver |
|---|---:|---:|---:|---:|---:|---|
| two sweeps, then check | 6 | 58 | 1.37 s | 76.1500 °C | -0.0059 K | yes |
| one point per call | 17 | 13 | 3.08 s | 76.1401 °C | -0.0158 K | yes |
| solver only | 14 | 13 | 3.01 s | 76.1401 °C | -0.0158 K | yes |
### 2. Safety
Hand-written bad calls, by kind. The surrogate column must be zero everywhere.
| kind | calls | as planned | refused | solver | surrogate |
|---|---:|---:|---:|---:|---:|
| physically meaningless numbers | 35 | 35 | 35 | 0 | 0 |
| wrong types, missing or extra fields, bad ids | 21 | 21 | 21 | 0 | 0 |
| valid, outside the training box | 16 | 16 | 2 | 14 | 0 |
| inside the box, surrogate answer fails a check | 1 | 1 | 0 | 1 | 0 |
| valid, but the wall never cools enough | 1 | 1 | 1 | 0 | 0 |
Random calls to predict_cooling (seed 7): each input in the box, outside it, physically invalid, malformed or missing, at fixed odds.
| outcome | calls |
|---|---:|
| refused, invalid_input | 395 |
| surrogate | 380 |
| solver, out_of_box | 225 |
Physics guard, inside the box only, every answer then put through check_physics:
| model served | calls | surrogate answers failing a check | sent to the solver | answers returned failing a check |
|---|---:|---:|---:|---:|
| version 1, promoted | 200 | 1 | 1 | 0 |
| int8 copy, blocked by the gate | 200 | 71 | 71 | 0 |
### 3. Correctness
| comparison | checked | equal |
|---|---:|---:|
| run_solver = physics.solve | 22 | 22 |
| predict_cooling = library guard and fallback | 22 | 22 |
| predict_cooling = cooling-surrogate HTTP service | 22 | 22 |
| compare_with_solver = metrics.errors | 22 | 22 |
| check_physics = checks.run_all | 44 | 44 |
| sweep_parameter = library guard on the batch | 2 | 2 |
A sweep answers in one batch, and batching changes the floating-point path a little: sweep points differ from the same points asked one by one by at most 2.9e-07 relative.
### 4. Latency
Median and 95th percentile over 200 calls (15 for the solver rows), new inputs on every call. Library is cooling_surrogate called directly; tool layer is the same call through ToolBox in-process; MCP is the client's wait for the answer from the server subprocess over stdio.
| call | library | tool layer | MCP, median | MCP, p95 | answer size |
|---|---:|---:|---:|---:|---:|
| describe_model | - | 0.01 ms | 1.57 ms | 1.96 ms | 3,236 chars |
| predict_cooling, inside the box | 0.31 ms | 0.40 ms | 2.30 ms | 3.48 ms | 609 chars |
| predict_cooling, same call again (result store) | - | 0.02 ms | 1.59 ms | 2.28 ms | 605 chars |
| sweep_parameter, 41 points inside the box | 0.94 ms | 1.29 ms | 4.35 ms | 6.00 ms | 2,481 chars |
| check_physics on a stored answer | 0.11 ms | 0.15 ms | 2.12 ms | 3.00 ms | 1,151 chars |
| predict_cooling, outside the box (solver) | 220 ms | 221 ms | 230 ms | 249 ms | 649 chars |
| run_solver | 221 ms | 217 ms | 221 ms | 249 ms | 507 chars |
| compare_with_solver | 222 ms | 217 ms | 224 ms | 256 ms | 921 chars |
Server start, three runs: 3.93 s, 4.01 s, 4.04 s. Machine: AMD64 Family 25 Model 80 Stepping 0, AuthenticAMD, Windows 11, Python 3.12.16, mcp 2.3.0, torch 2.15.0+cpu, numpy 2.5.5, one torch thread. Whole measurement: 239 s.
<!-- results:end -->
## How it works
| tool | what it does | runs the solver |
|---|---|---|
| `describe_model` | every input with unit, allowed range and training box; model version and hashes; the checks | no |
| `predict_cooling` | cooling time and heat removed for one case, surrogate with solver fallback | only when needed |
| `sweep_parameter` | the same over a grid of one input, in one call | for the points that need it, in one batch |
| `run_solver` | the finite-volume solver, verified against the exact series in cooling-surrogate | yes |
| `check_physics` | the five physics checks on an earlier answer, by its `result_id`, with the worst value of each | no |
| `compare_with_solver` | surrogate and solver on one case, and the surrogate's error | yes |
Every input carries its unit in its name (`melt_temp_c`, `thickness_mm`, `htc_w_m2k`, ...), and the JSON
schema the agent sees gives its allowed range and the training box. Types are strict: `"230"` or `true`
for a temperature is refused, not converted. A refused call comes back with `ok: false`, MCP's `is_error`
flag, a code, a reason that names the field and what is allowed, and a hint for the next call. For an
invalid call nothing is computed.
`predict_cooling` and `sweep_parameter` use the same guard as cooling-surrogate's HTTP service, in the same
order. If any input is outside the training box, the solver answers. If the surrogate's answer fails one of
the five physics checks, the solver answers. The answer says which one answered and why (`source`,
`fallback_reason`, `outside_training_box`, `surrogate_failed_checks`). With `on_out_of_box: "refuse"` an
agent that cannot wait for the solver gets a refusal instead, and still no surrogate number.
Each answer reports its `cost`: wall time, solver runs, surrogate evaluations, and whether it came from the
result store. Answers depend only on the arguments and the model, so each one gets a `result_id`, a hash
of the tool, the arguments, the weights and the library commit. The same call again returns the same id
and the same answer from the store without running anything. `check_physics` takes that id, so an agent
never has to send profiles back.
I wrote the server on the SDK's low-level `Server`, not the decorator layer. That way the schemas the agent
sees are exactly the ones in `tools.py`, and a bad argument comes back as a refusal the agent can read,
not as a protocol error. All tools are marked read-only and idempotent.
`src/cooling_surrogate` is an unchanged copy of eight files of cooling-surrogate at commit `58b7397` (MIT
licence, copied with it). It is copied, not installed, because that repository is not packaged for pip.
`UPSTREAM.json` holds the SHA-256 of every file, a test checks them, and CI compares them with a fresh
clone of that commit. The model in `src/sim_tools_mcp/model_v1` is version 1 of cooling-surrogate's full
run, the one its gate promoted. The server refuses to start if the weights' hash is not the registered one.
The measurements live in `bench/`: `correctness.py`, `safety.py` (with the bad calls in `cases.py`),
`latency.py`, and `agent.py` with `session.py` for the scripted agent.
## Run it
Python 3.12, CPU only.
```sh
python -m venv .venv
.venv/bin/pip install -r requirements-dev.txt # Windows: .venv\Scripts\pip
.venv/bin/pip install -e . --no-deps
.venv/bin/python -m pytest -q
.venv/bin/python -m bench.run_all # writes results/
.venv/bin/python -m bench.readme # rewrites the results part of this README
```
To use it from an MCP host, start it over stdio:
```json
{
"mcpServers": {
"cooling": { "command": "/path/to/sim-tools-mcp/.venv/bin/python", "args": ["-m", "sim_tools_mcp"] }
}
}
```
CI (`.github/workflows/ci.yml`) runs lint, the tests, the README check, the comparison of the copied files
with cooling-surrogate, and a small version of the whole measurement.
## Limits
- The agent is a script, not a language model. It shows that the tools are enough to answer the question
and how many calls each plan needs. It says nothing about how well a model would choose a plan. I did
not run it with a model.
- I ran the server only with the SDK's own client, not inside a desktop host.
- The physics is cooling-surrogate's: a one-dimensional wall with constant properties. Its README lists
what that leaves out.
- The latency numbers come from one machine, one torch thread and stdio. Over HTTP or on other hardware
they will differ.
- The result store lives in the server process. It is lost on restart, and `check_physics` only knows ids
from the same process. It keeps the last 4,096 answers.
- The guard checks each input against its own range, as in cooling-surrogate. A combination that is rare
inside the box is not flagged.
- The physical limits are those of cooling-surrogate's HTTP service. A wall that loses heat very slowly
passes them, and the solver then runs to its time limit before the tool refuses with `solver_failed`.
- The bad calls and the fuzz odds are mine. They cover the mistakes I could think of, not every input.
MIT licence.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues