|
| 1 | +--- |
| 2 | +title: MCP Servers |
| 3 | +sidebar: |
| 4 | + order: 10 |
| 5 | +--- |
| 6 | + |
| 7 | +OCR can act as a **Model Context Protocol (MCP) client**. You point it at |
| 8 | +one or more external MCP servers, and the tools those servers expose |
| 9 | +become available to the review agent — right alongside the |
| 10 | +[built-in tools](../tools/) like `file_read` and `code_search`. |
| 11 | + |
| 12 | +This is the *client* side of MCP. OCR does **not** run as an MCP server |
| 13 | +that other agents call — see the note in |
| 14 | +[Integrations](../integrations/#what-about-mcp) for that direction. This |
| 15 | +page is about the opposite: giving OCR's own reviewer extra capabilities. |
| 16 | + |
| 17 | +## When to use it |
| 18 | + |
| 19 | +Reach for an MCP server when the reviewer would benefit from context that |
| 20 | +lives outside the diff: |
| 21 | + |
| 22 | +- **Issue / ticket lookup** — let the agent fetch the linked Jira / GitHub |
| 23 | + issue to check whether the change matches the stated requirement. |
| 24 | +- **Docs / knowledge base** — pull internal API docs or coding standards |
| 25 | + so comments cite the real house rules. |
| 26 | +- **Custom analysis** — expose a linter, a schema validator, or a |
| 27 | + dependency checker as a tool the reviewer can invoke on demand. |
| 28 | + |
| 29 | +If all you need is a plain read of the repo, the built-in tools already |
| 30 | +cover it — MCP is for reaching *beyond* the checkout. |
| 31 | + |
| 32 | +## How it works |
| 33 | + |
| 34 | +- OCR connects to each configured server over the **stdio transport**: it |
| 35 | + launches the server as a subprocess and speaks MCP over its |
| 36 | + stdin/stdout. |
| 37 | +- The subprocess runs with its **working directory set to the repository |
| 38 | + root**, and inherits OCR's environment plus any `env` you configure. |
| 39 | +- On startup OCR lists the server's tools and registers them into the same |
| 40 | + tool registry the built-in tools use. Registered MCP tools are available |
| 41 | + in **both the plan phase and the main task**. |
| 42 | +- Servers stay alive for the duration of the review and are shut down when |
| 43 | + it finishes. |
| 44 | + |
| 45 | +If a server fails to start (or its `setup` command fails), OCR prints a |
| 46 | +warning and **continues the review without it** — a broken MCP server |
| 47 | +never blocks a review. |
| 48 | + |
| 49 | +## Configuration |
| 50 | + |
| 51 | +MCP servers live under the `mcp_servers` key in your user config file |
| 52 | +(`~/.opencodereview/config.json`). Each entry is keyed by a name you |
| 53 | +choose and accepts these fields: |
| 54 | + |
| 55 | +| Field | Type | Required | Description | |
| 56 | +|---|---|---|---| |
| 57 | +| `command` | string | ✓ | Executable that starts the MCP server (e.g. `npx`, `uvx`, an absolute path). | |
| 58 | +| `args` | string array | | Arguments passed to `command`. | |
| 59 | +| `env` | string array | | Extra environment variables in `KEY=VALUE` form. | |
| 60 | +| `tools` | string array | | Allowlist of tool names to register. Empty = register every tool the server offers. | |
| 61 | +| `setup` | string | | Shell command run once before the server starts (e.g. install deps). Runs in the repo root with a 5-minute timeout. | |
| 62 | + |
| 63 | +### With the CLI |
| 64 | + |
| 65 | +The `ocr config set` command writes these fields non-interactively. Array |
| 66 | +fields (`args`, `env`, `tools`) take a JSON array string: |
| 67 | + |
| 68 | +```bash |
| 69 | +# Minimal: just a command |
| 70 | +ocr config set mcp_servers.docs.command npx |
| 71 | + |
| 72 | +# Arguments |
| 73 | +ocr config set mcp_servers.docs.args '["-y", "@acme/docs-mcp-server"]' |
| 74 | + |
| 75 | +# Environment variables (KEY=VALUE entries) |
| 76 | +ocr config set mcp_servers.docs.env '["DOCS_TOKEN=secret", "DOCS_REGION=eu"]' |
| 77 | + |
| 78 | +# Restrict which tools are exposed to the reviewer |
| 79 | +ocr config set mcp_servers.docs.tools '["search_docs", "get_page"]' |
| 80 | + |
| 81 | +# A setup command to run before the server starts |
| 82 | +ocr config set mcp_servers.docs.setup "npm install -g @acme/docs-mcp-server" |
| 83 | +``` |
| 84 | + |
| 85 | +Remove a server with `unset`: |
| 86 | + |
| 87 | +```bash |
| 88 | +ocr config unset mcp_servers.docs |
| 89 | +``` |
| 90 | + |
| 91 | +### By hand |
| 92 | + |
| 93 | +The same configuration as JSON: |
| 94 | + |
| 95 | +```json |
| 96 | +{ |
| 97 | + "mcp_servers": { |
| 98 | + "docs": { |
| 99 | + "command": "npx", |
| 100 | + "args": ["-y", "@acme/docs-mcp-server"], |
| 101 | + "env": ["DOCS_TOKEN=secret", "DOCS_REGION=eu"], |
| 102 | + "tools": ["search_docs", "get_page"], |
| 103 | + "setup": "npm install -g @acme/docs-mcp-server" |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Filtering tools |
| 110 | + |
| 111 | +By default every tool a server advertises is registered. Set `tools` to an |
| 112 | +allowlist when a server exposes more than the reviewer needs — fewer, |
| 113 | +sharper tools keep the agent focused and cut token cost. Names in the list |
| 114 | +that the server doesn't actually offer are skipped with a warning, so a |
| 115 | +typo surfaces on stderr rather than silently doing nothing. |
| 116 | + |
| 117 | +## Name conflicts |
| 118 | + |
| 119 | +MCP tool names share one namespace with the built-in tools. If a server |
| 120 | +advertises a tool whose name collides with a **built-in/reserved** tool |
| 121 | +(`file_read`, `code_search`, `task_done`, …) or with a tool already |
| 122 | +registered by another MCP server, OCR **skips** it and logs a warning. |
| 123 | +First registration wins; give servers distinct tool names to avoid losing |
| 124 | +tools this way. |
| 125 | + |
| 126 | +## The `setup` command |
| 127 | + |
| 128 | +`setup` runs once, before the server subprocess starts, from the |
| 129 | +repository root. Use it to install or build the server on demand: |
| 130 | + |
| 131 | +```json |
| 132 | +"setup": "npm install -g @acme/docs-mcp-server" |
| 133 | +``` |
| 134 | + |
| 135 | +It has a **5-minute timeout**. If it exits non-zero, OCR logs the command, |
| 136 | +working directory, and output, then skips that server and proceeds with |
| 137 | +the review. |
| 138 | + |
| 139 | +## Troubleshooting |
| 140 | + |
| 141 | +All MCP diagnostics go to **stderr**, prefixed with `[ocr]`, so they never |
| 142 | +pollute `--format json` output on stdout: |
| 143 | + |
| 144 | +- `Running setup for MCP server "x": …` — the setup command is executing. |
| 145 | +- `failed to start MCP server "x": …` — the subprocess didn't connect |
| 146 | + within the 30-second init timeout, or `command` isn't on `PATH`. |
| 147 | +- `tool "y" conflicts with built-in tool, skipping` — rename the server's |
| 148 | + tool or drop it from `tools`. |
| 149 | +- `allowed tool "y" not found in server's tool list` — the name in `tools` |
| 150 | + doesn't match anything the server offers; check spelling. |
| 151 | + |
| 152 | +## See also |
| 153 | + |
| 154 | +- [Tools](../tools/) — the six built-in tools MCP tools sit beside. |
| 155 | +- [Configuration](../configuration/) — the full config file and every key. |
| 156 | +- [CLI Reference](../cli-reference/) — `ocr config` and the review flags. |
0 commit comments