# MCP

Each connected server's tools appear in chat as `<name>__<tool>`. Its resources and prompts reach the model through the `mcp_meta` tools in [Built-in tools](https://fzkuji.github.io/OpenProgram/docs/capabilities/tools.html).

```
openprogram mcp add drawio -- npx -y @drawio/mcp
openprogram mcp list
openprogram mcp show drawio
```

| Command | Effect |
| --- | --- |
| `openprogram mcp add NAME COMMAND…` | Adds a stdio server, effective at once. `--env KEY=VALUE` (repeatable), `--timeout` (startup and per-call seconds), `--disabled` (write config, do not start). |
| `openprogram mcp list` | Status of every server |
| `openprogram mcp show NAME` | Its tools and full schemas |
| `openprogram mcp rm NAME` | Stops it and deletes its config |
| `openprogram mcp restart NAME` | Stops and starts it |
| `openprogram mcp enable`, `disable NAME` | Toggles; disable keeps the config |
| `openprogram mcp test` | Starts an ad-hoc server, confirms a tool list, writes nothing |
| `openprogram mcp edit` | Compatibility only: reports that raw editing was removed because it exposed stored secrets. Use `add`, `rm` or the MCP settings page. |

Management commands talk to the background worker: `openprogram worker start`, `openprogram status`. Servers connect in the background when the worker starts; the App and core API stay available meanwhile. Until ready its tools are unavailable; `mcp list` and the settings page show the status.

## Configuration

`~/.openprogram/mcp_servers.json` (with `--profile <name>`, `~/.openprogram-<name>/mcp_servers.json`):

```
{
  "servers": {
    "drawio": {
      "type": "local",
      "command": ["npx", "-y", "@drawio/mcp"],
      "env": {},
      "enabled": true,
      "timeout_seconds": 30
    },
    "linear": {
      "type": "http",
      "url": "https://mcp.linear.app/mcp",
      "auth": {"kind": "oauth", "client_name": "OpenProgram"},
      "enabled": true
    }
  }
}
```

## Transports and auth

| `type` | Transport | Fields |
| --- | --- | --- |
| `local` | stdio subprocess | `command`, `env` |
| `http` | Streamable HTTP | `url`, `headers`, `auth` |
| `sse` | Legacy SSE | `url`, `headers`, `auth` |

| `auth.kind` | Needs |
| --- | --- |
| `none` | Nothing |
| `bearer` | `token` |
| `oauth` | OAuth 2.1 PKCE. Servers with dynamic client registration need nothing; others need `client_id` and `client_secret`. |

### OAuth sign-in happens once

| When | What happens |
| --- | --- |
| First connection | Opens the consent page and captures the redirect on localhost |
| Saved | Tokens, the client registration and discovered endpoints, in `~/.openprogram/mcp_tokens/<server>.json` (mode `0600`) |
| Later connections | Reuse them; an expired token renews silently |
| Refresh token rejected | The browser returns, and the management UI shows the server as needing re-authentication |
| Switch accounts | `POST /api/mcp/servers/{name}/auth/clear` wipes the state and restarts the server |

## Serve OpenProgram over MCP

```
openprogram mcp token create
export OPENPROGRAM_MCP_TOKEN="<paste the token printed above>"
openprogram mcp serve
```

`token create` prints the token as one raw line and does not set the environment. If one exists, reuse it. `serve` rejects a missing or mismatched token.
