Skip to main content
Glama
opensim-bio-mcp

opensim-bio-mcp

by jrayo
README.md
# opensim-bio-mcp

Typed, reproducible access to [OpenSim](https://opensim.stanford.edu/) musculoskeletal models for
humans and AI agents, through a Python library, a CLI and a local
[Model Context Protocol](https://modelcontextprotocol.io) (MCP) server.

> **Unofficial.** This project is not affiliated with or endorsed by Stanford University or the
> OpenSim project. It uses the official OpenSim Python bindings.

> **Status: pre-alpha (0.1.0.dev0).** Tested against OpenSim 4.6 on macOS arm64. The interface may
> change. Read [Known limitations](#known-limitations) before using any output.

## What it does

An agent (or you, from the CLI) can inspect a model, compute muscle geometry at a posture, and run
**coordinate-sweep experiments** that record musculotendon length and moment arm for chosen muscles
while one coordinate moves through a range. Every run is a self-contained directory:

* the exact input spec, the model's hash, software versions and the source commit;
* CSV results and figures;
* `verification.json`, with automatic checks on every run: the moment arm against a finite
  difference of length (r = −dL/dq), whether the requested posture was actually held, whether
  muscles span the coordinate, and optional invariance checks;
* `REPRODUCE.md`, with the CLI command that reruns it without any agent.

`scripts/verify_direct_opensim.py` independently recomputes a run with the raw OpenSim API, without
this package's analysis code.

## Principles

* **OpenSim is the only source of numbers.** Nothing is estimated or filled in. Every result traces
  to a run directory.
* **Failures are reported, not hidden.** A run with a failed check completes with status
  `completed_with_failed_checks` and quotes the check.
* **Numerical agreement is not biomechanical validity.** The checks show that the software reports
  what OpenSim computes, and that OpenSim's quantities are self-consistent. Whether the model
  represents real people for your question is not established by any automated check.
* **No clinical, diagnostic or injury-risk claims.** Moment arms and lengths say nothing about
  activation, force or joint loading.
* **Safe by design.** No shell or code-execution tools. All paths are confined to a workspace
  directory. OpenSim runs in a worker subprocess with timeouts, never in the server process. Run
  size is capped.

## Install (macOS Apple Silicon or Linux x86_64)

OpenSim comes from the official `opensim-org` conda channel, so install with conda:

```bash
bash scripts/setup_macos.sh
```

or step by step:

```bash
conda env create -f environment.yml          # OpenSim 4.6, Python 3.12, MCP SDK v2
conda activate opensim-bio-mcp
pip install -e ".[dev]" --no-deps
opensim-bio-mcp init-workspace -w ./workspace    # layout + example experiment spec
opensim-bio-mcp fetch-models   -w ./workspace    # pinned models, hash-verified, written read-only
OPENSIM_BIO_MCP_TEST_WORKSPACE=./workspace pytest -v
```

There is no Linux aarch64 OpenSim build, so ARM Linux VMs and containers can't run it.

## Use from an MCP client

Claude Code:

```bash
claude mcp add opensim -- /path/to/envs/opensim-bio-mcp/bin/opensim-bio-mcp serve --workspace /path/to/workspace
```

Codex (`~/.codex/config.toml`):

```toml
[mcp_servers.opensim]
command = "/path/to/envs/opensim-bio-mcp/bin/opensim-bio-mcp"
args = ["serve", "--workspace", "/path/to/workspace"]
tool_timeout_sec = 900
```

Tools: `get_environment_info`, `list_models`, `inspect_model`, `list_coordinates`, `list_muscles`,
`compute_muscle_geometry`, `get_experiment_schema`, `create_experiment`, `validate_experiment`,
`run_experiment`, `list_runs`, `get_run_results`.

Typical flow: `get_environment_info` → `list_models` → `list_coordinates` / `list_muscles` →
`get_experiment_schema` → `create_experiment` → `validate_experiment` → `run_experiment` →
`get_run_results`.

## Use without an agent

```bash
opensim-bio-mcp env -w ./workspace
opensim-bio-mcp inspect rajagopal2016 -w ./workspace --section coordinates
opensim-bio-mcp validate-experiment experiments/knee_flexion_muscle_geometry/spec.json -w ./workspace
opensim-bio-mcp run-experiment      experiments/knee_flexion_muscle_geometry/spec.json -w ./workspace
python scripts/verify_direct_opensim.py workspace/runs/knee-flexion-muscle-geometry/<run_id> --every 5
```

Exit codes for `run-experiment`: 0 succeeded, 3 completed with failed checks, 1 failed.

## Example experiment

`experiments/knee_flexion_muscle_geometry/spec.json` sweeps right knee flexion from 0 to 110° in the
Rajagopal 2016 model under three hip/ankle conditions. It records musculotendon length and knee
moment arm for 12 muscles, checks every moment arm against a finite difference of length, and checks
that the monoarticular muscles are invariant to hip and ankle posture.

## Models

| Registry id | Model | Notes |
|---|---|---|
| `rajagopal2016` | Rajagopal et al. 2016 full-body model | |
| `rajagopal_lai_uhlrich2023` | Rajagopal 2016 with the Lai 2017 high-flexion and Uhlrich 2022 passive-curve changes | |
| `arm26` | OpenSim 2-DOF arm example | test fixture only, not for scientific conclusions |

Models are downloaded from pinned commits of
[opensim-org/opensim-models](https://github.com/opensim-org/opensim-models) into your workspace and
verified by hash. They are **not** redistributed here, and their licenses are independent of this
project's. Check them before publishing anything derived from a model.

## Known limitations

* **Geometry only.** No forces, activations, inverse kinematics, inverse dynamics or Moco yet.
* **F-001: moment arm vs −dL/dq while wrapping** (`docs/DECISIONS.md` D-016, D-024). The
  `moment_arm_fd_agreement` check flags rows where OpenSim's `computeMomentArm` differs from the
  finite-difference derivative of `getLength`, and both quantities are written to `moment_arms.csv`.
  * **Two engaged wrap objects** (gastrocnemii near knee extension; up to 18 mm in the example
    experiment): an OpenSim 4.6 defect in `getLength`. It is **fixed upstream** in
    [opensim-core #4485](https://github.com/opensim-org/opensim-core/pull/4485) but not yet
    released. This package uses the 4.6 release, so its results still show it.
  * **Single wrap objects** (bfsh_r at 96–110°, grac_r at 0°; up to 0.3 mm): unchanged by that fix,
    cause unknown.
* **No regression baselines yet.** They will only be recorded from reviewed real runs.
* Tested on macOS arm64 only so far. Linux x86_64 should work but has not been tested.

## Layout

```
src/opensim_bio_mcp/   core/ models/ analysis/ experiments/ visualization/ server/ jobs.py worker.py cli.py
experiments/           versioned experiment specs (inputs)
scripts/               setup, independent cross-check, baseline recording
tests/                 unit (incl. a fenced OpenSim test double), integration (real OpenSim), regression
docs/                  ARCHITECTURE, DECISIONS, FEASIBILITY, LEARNING, REFERENCES, ROADMAP
workspace/             (git-ignored) models, runs, logs
```

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md). The rules for AI agents working in this repository are in
[AGENTS.md](AGENTS.md); they apply to human contributors too.

## License and citation

Apache-2.0 ([LICENSE](LICENSE)). If you use this software, cite it with
[CITATION.cff](CITATION.cff) and cite OpenSim and the models you used.