comsol-mph-mcp
by ai55798-oss
README.md
# comsol-mph-mcp
An MCP server that lets an AI assistant drive COMSOL Multiphysics through the
[MPh](https://mph.readthedocs.io) Python library. Aimed at COMSOL 6.2 and 6.4
on Windows, but nothing in the code is Windows-only.
## What it solves
Repeating a transient heat-transfer study over several materials, thicknesses
and loads means a lot of clicking, or a pile of one-off scripts. This project
puts the common steps behind a small set of tools: load a model, change
parameters, solve, check whether the solver converged, evaluate expressions,
export data, and run a whole parameter grid with a manifest that records what
happened. It also has helpers for the usual `comsolmphserver` problems
(port not open, missing login file, memory or convergence errors in the log).
It has no runtime dependencies besides `mph`, which is optional: an offline
mock backend ships with the package so tests and CI run without a license.
## Architecture
```
MCP client (Claude Desktop, ...) --stdio JSON-RPC--> server.py (ToolRegistry)
|
sweep.py diagnostics.py |
\ | /
backend.py: ComsolBackend
|-- MphBackend (real COMSOL via mph)
`-- MockComsolBackend (offline, deterministic)
```
The server speaks MCP's JSON-RPC 2.0 over stdio directly, so it does not
depend on a particular version of the `mcp` SDK.
## Installation
```bash
git clone https://github.com/ai55798-oss/comsol-mph-mcp.git
cd comsol-mph-mcp
python -m venv .venv && . .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -e ".[comsol]" # add ,dev for pytest
```
Try it without COMSOL: `comsol-mph-mcp --backend mock`.
## Claude Desktop configuration
```json
{
"mcpServers": {
"comsol": {
"command": "C:\\path\\to\\comsol-mph-mcp\\.venv\\Scripts\\comsol-mph-mcp.exe",
"args": []
}
}
}
```
Use `"args": ["--backend", "mock"]` to test the wiring first.
## Windows setup for COMSOL 6.2 / 6.4
1. Install COMSOL with a license that allows client-server mode, and install
`mph` (`pip install "mph>=1.2"`) in the same environment as this package.
2. Run `comsolmphserver` once by hand so it creates your login credentials
(`%USERPROFILE%\.comsol\v62` or `v64\login.properties`).
3. Either let the tools start a local session (`comsol_connect` without a
host), or start a server and connect to it:
```powershell
.\scripts\start_comsol_mphserver.ps1 -Port 2036
.\scripts\start_comsol_mphserver.ps1 -Port 2036 -TestOnly
```
The script keeps preferences, temp files and logs in `.comsol_runtime\`
inside the repo (git-ignored). Then call `comsol_connect` with
`host: "localhost"`, `port: 2036`.
4. `comsol_status` with `check_runtime: true` reports whether the port is open
and the login file looks sane.
## Tools
| Tool | Purpose |
| --- | --- |
| `comsol_status` | Backend, connection and model state; optional runtime check |
| `comsol_connect` | Start a local session or connect to `comsolmphserver` |
| `comsol_disconnect` | Close the session |
| `model_load` | Load a `.mph` file |
| `model_inspect` | Parameters, studies, datasets (and physics/materials on the real backend) |
| `model_save` | Save, optionally to a new path |
| `set_parameters` | Set global parameters, e.g. `{"q": "8000[W/m^2]"}` |
| `run_parametric_sweep` | Grid sweep with convergence tracking; writes `manifest.json` and `results.csv` |
| `check_convergence` | Solve a study and report status, and/or parse a solver log |
| `export_results` | Evaluate expressions to CSV or JSON |
A sweep config looks like `examples/heat_transfer_sweep.json`: `variants`
(for example materials), a `parameters` grid, and `outputs` expressions.
The offline example needs no license:
```bash
python examples/run_example_sweep.py sweep_output
```
The mock backend uses a closed-form semi-infinite-solid heat estimate. It is
there to exercise the plumbing, not to produce engineering numbers.
## Running tests
```bash
pip install -e ".[dev]"
pytest
```
## Roadmap
- Verify `MphBackend` against more 6.2 and 6.4 models and report what breaks.
- Mesh-convergence study tool (refine, re-solve, compare an output).
- Plot export and selection-based evaluation.
- Optional adapter for the official `mcp` SDK once its API settles.
## License
MIT
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessOutdated data