Metadata-Version: 2.4
Name: ros2-doctor-mcp
Version: 0.1.1
Summary: MCP server that diagnoses why a ROS 2 robot is not moving (Nav2 and ros2_control arms). Read-only.
License: Apache-2.0
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: mcp<2,>=1.2
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# ros2-doctor-mcp

[![CI](https://github.com/darshmenon/ros2_doctor_mcp/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/darshmenon/ros2_doctor_mcp/actions/workflows/ci.yml)
[![License](https://img.shields.io/badge/license-Apache--2.0-green)](LICENSE)

An MCP server that answers **"why isn't my robot moving?"** for ROS 2. Most ROS MCP servers *control* a robot (publish, call services, send goals). This one only *diagnoses*: it is read-only, never publishes, and never changes anything, so it is safe to point an agent at a running robot.

![diagnose_robot on a UR3 with MoveIt: healthy, then the arm controller deactivated](docs/demo.gif)

*The tool results are real output captured through MCP on a UR3 + MoveIt Gazebo sim (healthy, then `arm_controller` deactivated). The question and the agent's reply are illustrative. Source: `docs/demo_raw.txt`, `docs/make_demo_gif.py`.*

![An agent calls ros2-doctor-mcp, which fans out to the read-only doctors](docs/architecture.png)

It wraps existing read-only doctors and returns one normalised result, `{ok, findings: [{level, code, msg, hint}]}`:

| Tool | Wraps | Answers |
|---|---|---|
| `diagnose_robot` | all of the below | **start here**: detects which stacks are on the domain (Nav2, an arm, MoveIt, SLAM, cameras), runs every doctor that applies in parallel, returns one merged verdict, errors first, each finding tagged with its source |
| `diagnose_nav2` | [nav2_doctor](https://github.com/darshmenon/nav2_doctor) | stale TF, dead odom or scan, inactive Nav2 servers, no `cmd_vel`, flapping goals, no progress |
| `nav2_ready` | `nav2_doctor --ready` | pre-flight: can this stack navigate right now? |
| `diagnose_arm` | [armdoc](https://github.com/darshmenon/armdoc) | inactive controller, silent `/joint_states`, joint at a limit, stalled trajectory, missing `move_group` |
| `arm_ready` | `armdoc --ready` | pre-flight for a ros2_control arm |
| `diagnose_perception` | [percept_doctor](https://github.com/darshmenon/percept_doctor) | cameras, depth and point clouds: dead or stale streams, low rate, dark or frozen images, invalid depth, empty clouds, missing `camera_info` |
| `diagnose_slam` | [slam_doctor](https://github.com/darshmenon/slam_doctor) | SLAM health: map stalls, lost tracking, odometry drift, no loop closures (slam_toolbox, Cartographer, RTAB-Map, ORB-SLAM3) |
| `analyze_force` | [forcetrace](https://github.com/darshmenon/forcetrace) | offline: did a peg-in-hole or press run jam or insert, peak and steady force, time in contact |
| `check_reach` | [reachlens](https://github.com/darshmenon/reachlens) | offline: can a UR3 or UR5e reach this tool-down pose? IK error, manipulability, joint-limit margin |
| `score_map` | [nav_explore_eval](https://github.com/darshmenon/nav_explore_eval) | offline: coverage, wall recall and precision of a saved map against ground truth |
| `ros_graph` | `ros2 node list`, `ros2 topic list -t` | what is visible on a domain; start here when a doctor says nothing is visible |
| `learn_baseline`, `list_baselines` | the doctors' `--learn` | record what normal looks like for this robot (message rates) while it is healthy; then pass `baseline=<name>` to any `diagnose_*` tool to be warned with `RATE_DROP` / `RATE_SPIKE` when a stream drifts. Writes one small file in `~/.ros2_doctor/baselines` (override with `ROS2_DOCTOR_BASELINE_DIR`), nothing on the robot |
| `explain_code` | every doctor's hint table | offline, instant: what a finding code (`CMD_ZERO`, `TF_FREEZE`, `RATE_DROP` ...) means, which doctor raises it, and the next step; unknown codes return the closest known ones |
| `analyze_trajectory` | [trajlens](https://github.com/darshmenon/trajlens) | offline: jerk spikes, limit violations, slow profiles in a recorded trajectory (CSV, JSON, rosbag2); no robot needed |

The server also offers a resource, `doctor://codes` (the full finding-code catalog), and a prompt, `why_isnt_my_robot_moving`, that walks an agent through diagnose, explain and propose. The catalog is generated from the doctors' hint tables with `scripts/build_catalog.py`; a test fails when a doctor adds a code the catalog lacks.

Every diagnose tool takes `ns` (namespace), `seconds` (watch time, clamped to 1 to 120) and `domain_id` (sets `ROS_DOMAIN_ID` for that call only). Each finding carries a `hint` with a suggested next step.

## What it has found

![Findings from live Gazebo runs and from testing](docs/findings.png)

Every row except `by design` comes from a run on a UR3 + MoveIt or Nav2 Gazebo sim, or from a bug the testing caught. Nothing here was run on real hardware.

## Install

**Docker** (ROS 2 Humble and the doctors are built in; nothing else to install):

```bash
docker build -t ros2-doctor-mcp https://github.com/darshmenon/ros2_doctor_mcp.git
claude mcp add ros2-doctor -- docker run -i --rm --net=host -e ROS_DOMAIN_ID=0 ros2-doctor-mcp
```

`--net=host` lets the container see robots on this machine; set `ROS_DOMAIN_ID` to match yours (or pass `domain_id` in a tool call). The image uses UDP-only Fast DDS, because shared memory does not work between a container and the host (discovery succeeds but no data arrives).

**From source** (you already have ROS 2):

```bash
# a ROS 2 install with the doctors you want
cd ~/ros2_ws/src && git clone https://github.com/darshmenon/nav2_doctor.git && git clone https://github.com/darshmenon/armdoc.git
cd .. && colcon build --packages-select nav2_doctor armdoc

pip install ros2-doctor-mcp      # latest main instead: pip install git+https://github.com/darshmenon/ros2_doctor_mcp.git
```

Run the server from a shell where ROS 2 and your workspace are sourced.

## Use with Claude Code

```bash
claude mcp add ros2-doctor -- ros2-doctor-mcp
```

or in `.mcp.json`:

```json
{"mcpServers": {"ros2-doctor": {"command": "ros2-doctor-mcp"}}}
```

Then ask: *"The robot on domain 77 won't move. Why?"* and the agent calls `diagnose_robot`.

## Configuration

The doctors are found on `PATH` (`nav2_doctor`, `armdoc`), else through `ros2 run`. Override either with an environment variable:

```bash
ROS2_DOCTOR_NAV2_CMD="python3 -m nav2_doctor.doctor"
ROS2_DOCTOR_ARM_CMD="ros2 run armdoc armdoc"
```

## Not included

`nav2_fault_inject` and `armchaos` deliberately break a running robot, so they stay out of a read-only server. `ctrlbench` is a long benchmark, not a quick question.

## Safety

- The wrapped doctors only subscribe to topics and query lifecycle or controller state.
- The server runs them as subprocesses with a hard timeout and a clamped watch time. There is no tool that publishes, calls a mutating service, or sends a goal.
- Pairs well with a control-oriented ROS MCP server: let that one move the robot, and use this one to find out why it did not.

## Scoreboard

[docs/scoreboard.md](docs/scoreboard.md): 23 injected faults across nav2_doctor, armdoc and percept_doctor, each with the finding code fixed in advance, plus 5 healthy controls. All 23 were named by the doctor that owns them and all 5 controls stayed free of errors and warnings. The graphs are the synthetic fakes from each doctor's integration tests, so this is a regression table for the rules on the fault patterns they model, not recall on a real robot (those faults were chosen with the rules in hand). A few scenarios also raise a second, related code (a wrong namespace cascades into `ODOM_NONE`, `SCAN_NONE`, `NODE_MISSING`); the table lists everything reported. Re-run it with `python3 scripts/scoreboard.py` in a sourced ROS 2 shell with the doctors cloned next to each other.

## Tests

```bash
pip install ".[test]" && pytest -q
```

The unit tests stub the subprocess, so they need neither ROS nor a robot. The Nav2 and arm tools were also run end to end through the MCP protocol against live Gazebo sims (see the changelog).

## Roadmap

- fault injectors (nav2_fault_inject, armchaos) are deliberately not exposed: this server stays read-only
- merged node logs tied to a finding
- multi-robot: one call per namespace

License: Apache-2.0.
