Metadata-Version: 2.4
Name: mcpdoctor-py
Version: 0.1.1
Summary: Diagnose why an MCP server won't connect: audit your client configs, resolve commands the way GUI apps see them, run the real handshake, and get the exact cause and fix.
Author: Kamal Mosadegh
License: MIT
Project-URL: Homepage, https://github.com/kamalmosadegh/mcpdoctor
Project-URL: Repository, https://github.com/kamalmosadegh/mcpdoctor
Project-URL: Issues, https://github.com/kamalmosadegh/mcpdoctor/issues
Keywords: mcp,model-context-protocol,cli,diagnostics,claude,cursor,developer-tools
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# mcpdoctor

![Python](https://img.shields.io/badge/python-3.9%2B-blue)
![License](https://img.shields.io/badge/license-MIT-green)
![Dependencies](https://img.shields.io/badge/dependencies-0-brightgreen)

**Your MCP server won't connect, and the client won't tell you why. mcpdoctor will.**

You add a server to Claude Desktop / Claude Code / Cursor / VS Code, restart, and get *silence* — no error, no server. Install guides put the average first-time MCP install at up to **3 hours** of guessing (mcpindex.dev, *How to Install MCP Servers — "The Installation Trap"*). mcpdoctor finds your client configs, checks every server entry stage by stage, and prints **which step failed, why, and the exact fix** — usually in under a second per server.

Zero dependencies. Read-only: it never modifies your configs. Env values are never printed (key names only).

## The four classic causes (from every troubleshooting guide)

1. **Wrong config file / wrong key** — e.g. servers written under `mcpServers` in a VS Code file, which reads `servers`. The client loads nothing and says nothing.
2. **Silent JSON failure** — a trailing comma or a `//` comment makes the whole file unparseable; most clients respond by loading zero servers, with no error dialog.
3. **GUI apps don't inherit your shell PATH** — `npx` works in your terminal, but the GUI app was launched with a minimal PATH, so spawning the server fails with a silent ENOENT.
4. **The server itself dies or misbehaves on startup** — it crashes before the handshake, prints log lines to stdout (which *is* the JSON-RPC channel), never answers, or rejects `initialize`.

## What it checks

For every server entry in every config it finds, in order:

| Stage | Checks |
|---|---|
| **parse** | JSON/TOML syntax — with the exact line/column, and specific detection of trailing commas, comments, UTF-8 BOMs, invalid UTF-8, and duplicate keys (a repeated server name silently shadows the earlier definition in every JSON parser) |
| **schema** | The client's top-level key, entry shape, `command` present and a single executable (a whole command line in `command` — `"npx -y srv"` — is caught and the split shown), `args` a list of strings, `env` an object whose values are strings/numbers/booleans |
| **resolve** | The command resolved twice: against your shell PATH *and* against the minimal PATH a GUI app inherits — plus existence/executability for absolute paths |
| **run** | The server is actually spawned (with its `env` merged) and spoken to in raw JSON-RPC over stdio: `initialize` → `notifications/initialized` → `tools/list`, with stderr captured separately |

Failures are classified into cause codes: `TRAILING_COMMA`, `COMMENT_IN_JSON`, `CONFIG_PARSE_ERROR`, `WRONG_TOP_LEVEL_KEY`, `SCHEMA_ERROR`, `MISSING_COMMAND`, `COMMAND_EMBEDDED_ARGS`, `INVALID_FIELD`, `COMMAND_NOT_FOUND`, `GUI_PATH`, `NOT_EXECUTABLE`, `PROCESS_EXITED_EARLY` (with the missing env var named when stderr names one), `STDOUT_POLLUTION`, `TIMEOUT`, `INVALID_RESPONSE`, `INIT_ERROR`, `TOOLS_LIST_FAILED`.

Healthy servers also get a **tool tax** line: tool count, tool-definition size in characters, and an approximate token estimate — clearly labelled as an estimate (chars ÷ 4; real tokenizers differ per model).

## What it does NOT do

- It does **not** modify, fix, or reformat your configs. It tells you the fix; you apply it.
- It does **not** check remote (URL/HTTP) servers in v0.1 — they are reported as skipped.
- It does **not** guarantee a server works *inside* a specific client version — it reproduces the client's config location, PATH environment, and handshake as documented; client bugs are out of scope.
- The token figure is a rough estimate, not a billing number.

## Known limitations

- **First fault wins.** Diagnosis stops at the first failing stage per server, and within the run stage a server that prints a banner to stdout *and then crashes* is reported for the banner (`INVALID_RESPONSE`/`STDOUT_POLLUTION`) — fix that and re-run to see the next fault.
- **GUI PATH is a simulation** of the documented minimal PATH per OS, not a query of your actual GUI session. If you customized the GUI environment (e.g. `launchctl setenv PATH …`), a `GUI_PATH` verdict can be wrong for your machine — the prescribed fix (use the absolute path) is still correct and harmless.
- **Windows paths are untested on Windows.** The path table is implemented and resolved via `%APPDATA%` / the user profile, but the test suite runs on POSIX.
- **Relative paths resolve against mcpdoctor's working directory** — a relative `command` or a relative script in `args` is interpreted from wherever you run the doctor. Clients differ (project root, home directory), so run the doctor from the directory your client would use.
- **Worst-case time per server is ~2× `--timeout` plus a few seconds** (one timeout for `initialize`, one for `tools/list`, plus drain/terminate grace). A config whose first server hangs still gets every later server diagnosed.
- **Secret scrubbing has a floor.** Env values and secret-flag arguments of 4+ characters are scrubbed from all output; shorter values are not (replacing them would mangle unrelated text). A server that prints a secret it obtained some other way (a file, a derived token) can still echo it — the doctor cannot know that string is secret.
- **Single-line messages over ~1 MB** are treated as invalid output: stream reads are size-capped so a broken server cannot flood the doctor's memory. No real MCP server sends a 1 MB single-line handshake response.
- **Daemonizing servers**: if the entry point forks and the parent exits, the doctor reports the parent's exit (correctly — a stdio server must stay attached), but it cannot kill or reap the detached grandchild; that process belongs to the OS.
- **Duplicate keys are only flagged at the top level and server-name level.** A repeated key deep inside one entry (e.g. two `"command"` keys) is collapsed silently, as in every JSON parser.
- **A config path that is a directory** (e.g. `~/.cursor/mcp.json/` exists as a folder) is reported as "not found" during discovery; pass it via `--config` to get the explicit "not a file" error.

## Quickstart

```bash
pip install mcpdoctor-py        # (not yet published — install from source for now)
# from source:
pip install -e .

mcpdoctor                    # discover and check all client configs
mcpdoctor --json             # machine-readable report
mcpdoctor --config ~/.cursor/mcp.json --client cursor
mcpdoctor --timeout 5        # shorter handshake timeout (default 10s)
```

Exit codes: `0` = every checked server healthy · `1` = at least one broken · `2` = no client configs found (nothing to check).

## Demo

A real run against deliberately broken configs (full capture in [demo.txt](demo.txt)):

```
Claude Desktop — ~/.config/Claude/claude_desktop_config.json
  ✗ (config file) [parse/TRAILING_COMMA] JSON syntax error at line 4, column 3…
      Fix: Remove the trailing comma at line 4 of … — JSON does not allow a
      comma before a closing brace/bracket, and most clients respond by
      silently loading zero servers.

Claude Code — ~/.claude.json
  ✗ chatty-server [run/STDOUT_POLLUTION] The server answered initialize, but
    printed 1 non-JSON line(s) to stdout first — strict clients refuse the
    connection.
  ✗ crashy-server [run/PROCESS_EXITED_EARLY] The server process exited with
    code 1 before answering the initialize handshake. (stderr traceback shown)
  ✓ notes-server: Healthy — fixture-healthy 1.2.3 answered initialize and
    tools/list with 2 tool(s); tool definitions are 359 chars ≈ ~90 tokens
    (rough estimate, chars/4).

Cursor — ~/.cursor/mcp.json
  ✗ ghost-server [resolve/GUI_PATH] "npx" resolves in your shell … but is
    invisible on the minimal PATH a GUI app inherits — the classic silent
    spawn ENOENT.
      Fix: … Replace "command" with the absolute path "…/bin/npx".
```

## Config locations

macOS/Linux are fully working. Windows paths are included in the table below (resolved via `%APPDATA%` / the user profile when run on Windows):

| Client | macOS | Linux | Windows |
|---|---|---|---|
| Claude Desktop | `~/Library/Application Support/Claude/claude_desktop_config.json` | `~/.config/Claude/claude_desktop_config.json` | `%APPDATA%\Claude\claude_desktop_config.json` |
| Claude Code | `~/.claude.json` (+ project `.mcp.json`) | same | `%USERPROFILE%\.claude.json` |
| Cursor | `~/.cursor/mcp.json` | same | `%USERPROFILE%\.cursor\mcp.json` |
| VS Code | `~/Library/Application Support/Code/User/mcp.json` | `~/.config/Code/User/mcp.json` | `%APPDATA%\Code\User\mcp.json` |
| Codex | `~/.codex/config.toml` | same | `%USERPROFILE%\.codex\config.toml` |

## Tests & benchmark

51 real tests — the original 20 planted cases plus 31 hardening regression tests from an adversarial probe pass (BOM and invalid-UTF-8 configs, duplicate server names, secret leakage through crash stderr, a 30 MB stderr line, embedded arguments in `command`, missing/bad shebangs, `tools/list` failures, Codex TOML, double-forking servers). Real config files and real server processes on disk throughout; no mocked subprocess for the live-run cases:

```bash
pip install -e . pytest
python -m pytest tests/ -q
python benchmarks/run_benchmark.py
```

Benchmark result (this machine, 2026-11-29): **20/20 diagnoses correct**, median diagnosis time **0.08s**, total **8.64s** for all 20 cases. Full table: [benchmarks/BENCHMARK.md](benchmarks/BENCHMARK.md). Framing honesty: the "up to 3 hours" figure is the install guides' claim about manual first-time installs — it is not measured here; what is measured is the doctor's accuracy and speed on these fixtures.

## License

MIT — see [LICENSE](LICENSE).
