| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- ---
- title: MCP servers
- description: Add local and remote MCP tools.
- ---
- You can add external tools to OpenCode using the _Model Context Protocol_, or MCP. OpenCode supports both local and remote servers.
- Once added, MCP tools are automatically available to the LLM alongside built-in tools.
- ---
- #### Caveats
- When you use an MCP server, it adds to the context. This can quickly add up if you have a lot of tools. So we recommend being careful with which MCP servers you use.
- :::tip
- MCP servers add to your context, so you want to be careful with which ones you enable.
- :::
- Certain MCP servers, like the GitHub MCP server, tend to add a lot of tokens and can easily exceed the context limit.
- ---
- ## Enable
- You can define MCP servers in your [OpenCode Config](https://opencode.ai/docs/config/) under `mcp`. Add each MCP with a unique name. You can refer to that MCP by name when prompting the LLM.
- ```jsonc title="opencode.jsonc" {6}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "name-of-mcp-server": {
- // ...
- "enabled": true,
- },
- "name-of-other-mcp-server": {
- // ...
- },
- },
- }
- ```
- You can also disable a server by setting `enabled` to `false`. This is useful if you want to temporarily disable a server without removing it from your config.
- ---
- ### Overriding remote defaults
- Organizations can provide default MCP servers via their `.well-known/opencode` endpoint. These servers may be disabled by default, allowing users to opt-in to the ones they need.
- To enable a specific server from your organization's remote config, add it to your local config with `enabled: true`:
- ```json title="opencode.json"
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "jira": {
- "type": "remote",
- "url": "https://jira.example.com/mcp",
- "enabled": true
- }
- }
- }
- ```
- Your local config values override the remote defaults. See [config precedence](/docs/config#precedence-order) for more details.
- ---
- ## Local
- Add local MCP servers using `type` to `"local"` within the MCP object.
- ```jsonc title="opencode.jsonc" {15}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "my-local-mcp-server": {
- "type": "local",
- // Or ["bun", "x", "my-mcp-command"]
- "command": ["npx", "-y", "my-mcp-command"],
- "enabled": true,
- "environment": {
- "MY_ENV_VAR": "my_env_var_value",
- },
- },
- },
- }
- ```
- The command is how the local MCP server is started. You can also pass in a list of environment variables as well.
- For example, here's how you can add the test [`@modelcontextprotocol/server-everything`](https://www.npmjs.com/package/@modelcontextprotocol/server-everything) MCP server.
- ```jsonc title="opencode.jsonc"
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "mcp_everything": {
- "type": "local",
- "command": ["npx", "-y", "@modelcontextprotocol/server-everything"],
- },
- },
- }
- ```
- And to use it I can add `use the mcp_everything tool` to my prompts.
- ```txt "mcp_everything"
- use the mcp_everything tool to add the number 3 and 4
- ```
- ---
- #### Options
- Here are all the options for configuring a local MCP server.
- | Option | Type | Required | Description |
- | ------------- | ------- | -------- | ----------------------------------------------------------------------------------- |
- | `type` | String | Y | Type of MCP server connection, must be `"local"`. |
- | `command` | Array | Y | Command and arguments to run the MCP server. |
- | `environment` | Object | | Environment variables to set when running the server. |
- | `enabled` | Boolean | | Enable or disable the MCP server on startup. |
- | `timeout` | Number | | Timeout in ms for fetching tools from the MCP server. Defaults to 5000 (5 seconds). |
- ---
- ## Remote
- Add remote MCP servers by setting `type` to `"remote"`.
- ```json title="opencode.json"
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "my-remote-mcp": {
- "type": "remote",
- "url": "https://my-mcp-server.com",
- "enabled": true,
- "headers": {
- "Authorization": "Bearer MY_API_KEY"
- }
- }
- }
- }
- ```
- The `url` is the URL of the remote MCP server and with the `headers` option you can pass in a list of headers.
- ---
- #### Options
- | Option | Type | Required | Description |
- | --------- | ------- | -------- | ----------------------------------------------------------------------------------- |
- | `type` | String | Y | Type of MCP server connection, must be `"remote"`. |
- | `url` | String | Y | URL of the remote MCP server. |
- | `enabled` | Boolean | | Enable or disable the MCP server on startup. |
- | `headers` | Object | | Headers to send with the request. |
- | `oauth` | Object | | OAuth authentication configuration. See [OAuth](#oauth) section below. |
- | `timeout` | Number | | Timeout in ms for fetching tools from the MCP server. Defaults to 5000 (5 seconds). |
- ---
- ## OAuth
- OpenCode automatically handles OAuth authentication for remote MCP servers. When a server requires authentication, OpenCode will:
- 1. Detect the 401 response and initiate the OAuth flow
- 2. Use **Dynamic Client Registration (RFC 7591)** if supported by the server
- 3. Store tokens securely for future requests
- ---
- ### Automatic
- For most OAuth-enabled MCP servers, no special configuration is needed. Just configure the remote server:
- ```json title="opencode.json"
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "my-oauth-server": {
- "type": "remote",
- "url": "https://mcp.example.com/mcp"
- }
- }
- }
- ```
- If the server requires authentication, OpenCode will prompt you to authenticate when you first try to use it. If not, you can [manually trigger the flow](#authenticating) with `opencode mcp auth <server-name>`.
- ---
- ### Pre-registered
- If you have client credentials from the MCP server provider, you can configure them:
- ```json title="opencode.json" {7-11}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "my-oauth-server": {
- "type": "remote",
- "url": "https://mcp.example.com/mcp",
- "oauth": {
- "clientId": "{env:MY_MCP_CLIENT_ID}",
- "clientSecret": "{env:MY_MCP_CLIENT_SECRET}",
- "scope": "tools:read tools:execute"
- }
- }
- }
- }
- ```
- ---
- ### Authenticating
- You can manually trigger authentication or manage credentials.
- Authenticate with a specific MCP server:
- ```bash
- opencode mcp auth my-oauth-server
- ```
- List all MCP servers and their auth status:
- ```bash
- opencode mcp list
- ```
- Remove stored credentials:
- ```bash
- opencode mcp logout my-oauth-server
- ```
- The `mcp auth` command will open your browser for authorization. After you authorize, OpenCode will store the tokens securely in `~/.local/share/opencode/mcp-auth.json`.
- ---
- #### Disabling OAuth
- If you want to disable automatic OAuth for a server (e.g., for servers that use API keys instead), set `oauth` to `false`:
- ```json title="opencode.json" {7}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "my-api-key-server": {
- "type": "remote",
- "url": "https://mcp.example.com/mcp",
- "oauth": false,
- "headers": {
- "Authorization": "Bearer {env:MY_API_KEY}"
- }
- }
- }
- }
- ```
- ---
- #### OAuth Options
- | Option | Type | Description |
- | -------------- | --------------- | -------------------------------------------------------------------------------- |
- | `oauth` | Object \| false | OAuth config object, or `false` to disable OAuth auto-detection. |
- | `clientId` | String | OAuth client ID. If not provided, dynamic client registration will be attempted. |
- | `clientSecret` | String | OAuth client secret, if required by the authorization server. |
- | `scope` | String | OAuth scopes to request during authorization. |
- #### Debugging
- If a remote MCP server is failing to authenticate, you can diagnose issues with:
- ```bash
- # View auth status for all OAuth-capable servers
- opencode mcp auth list
- # Debug connection and OAuth flow for a specific server
- opencode mcp debug my-oauth-server
- ```
- The `mcp debug` command shows the current auth status, tests HTTP connectivity, and attempts the OAuth discovery flow.
- ---
- ## Manage
- Your MCPs are available as tools in OpenCode, alongside built-in tools. So you can manage them through the OpenCode config like any other tool.
- ---
- ### Global
- This means that you can enable or disable them globally.
- ```json title="opencode.json" {14}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "my-mcp-foo": {
- "type": "local",
- "command": ["bun", "x", "my-mcp-command-foo"]
- },
- "my-mcp-bar": {
- "type": "local",
- "command": ["bun", "x", "my-mcp-command-bar"]
- }
- },
- "tools": {
- "my-mcp-foo": false
- }
- }
- ```
- We can also use a glob pattern to disable all matching MCPs.
- ```json title="opencode.json" {14}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "my-mcp-foo": {
- "type": "local",
- "command": ["bun", "x", "my-mcp-command-foo"]
- },
- "my-mcp-bar": {
- "type": "local",
- "command": ["bun", "x", "my-mcp-command-bar"]
- }
- },
- "tools": {
- "my-mcp*": false
- }
- }
- ```
- Here we are using the glob pattern `my-mcp*` to disable all MCPs.
- ---
- ### Per agent
- If you have a large number of MCP servers you may want to only enable them per agent and disable them globally. To do this:
- 1. Disable it as a tool globally.
- 2. In your [agent config](/docs/agents#tools), enable the MCP server as a tool.
- ```json title="opencode.json" {11, 14-18}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "my-mcp": {
- "type": "local",
- "command": ["bun", "x", "my-mcp-command"],
- "enabled": true
- }
- },
- "tools": {
- "my-mcp*": false
- },
- "agent": {
- "my-agent": {
- "tools": {
- "my-mcp*": true
- }
- }
- }
- }
- ```
- ---
- #### Glob patterns
- The glob pattern uses simple regex globbing patterns:
- - `*` matches zero or more of any character (e.g., `"my-mcp*"` matches `my-mcp_search`, `my-mcp_list`, etc.)
- - `?` matches exactly one character
- - All other characters match literally
- :::note
- MCP server tools are registered with server name as prefix, so to disable all tools for a server simply use:
- ```
- "mymcpservername_*": false
- ```
- :::
- ---
- ## Examples
- Below are examples of some common MCP servers. You can submit a PR if you want to document other servers.
- ---
- ### Sentry
- Add the [Sentry MCP server](https://mcp.sentry.dev) to interact with your Sentry projects and issues.
- ```json title="opencode.json" {4-8}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "sentry": {
- "type": "remote",
- "url": "https://mcp.sentry.dev/mcp",
- "oauth": {}
- }
- }
- }
- ```
- After adding the configuration, authenticate with Sentry:
- ```bash
- opencode mcp auth sentry
- ```
- This will open a browser window to complete the OAuth flow and connect OpenCode to your Sentry account.
- Once authenticated, you can use Sentry tools in your prompts to query issues, projects, and error data.
- ```txt "use sentry"
- Show me the latest unresolved issues in my project. use sentry
- ```
- ---
- ### Context7
- Add the [Context7 MCP server](https://github.com/upstash/context7) to search through docs.
- ```json title="opencode.json" {4-7}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "context7": {
- "type": "remote",
- "url": "https://mcp.context7.com/mcp"
- }
- }
- }
- ```
- If you have signed up for a free account, you can use your API key and get higher rate-limits.
- ```json title="opencode.json" {7-9}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "context7": {
- "type": "remote",
- "url": "https://mcp.context7.com/mcp",
- "headers": {
- "CONTEXT7_API_KEY": "{env:CONTEXT7_API_KEY}"
- }
- }
- }
- }
- ```
- Here we are assuming that you have the `CONTEXT7_API_KEY` environment variable set.
- Add `use context7` to your prompts to use Context7 MCP server.
- ```txt "use context7"
- Configure a Cloudflare Worker script to cache JSON API responses for five minutes. use context7
- ```
- Alternatively, you can add something like this to your [AGENTS.md](/docs/rules/).
- ```md title="AGENTS.md"
- When you need to search docs, use `context7` tools.
- ```
- ---
- ### Grep by Vercel
- Add the [Grep by Vercel](https://grep.app) MCP server to search through code snippets on GitHub.
- ```json title="opencode.json" {4-7}
- {
- "$schema": "https://opencode.ai/config.json",
- "mcp": {
- "gh_grep": {
- "type": "remote",
- "url": "https://mcp.grep.app"
- }
- }
- }
- ```
- Since we named our MCP server `gh_grep`, you can add `use the gh_grep tool` to your prompts to get the agent to use it.
- ```txt "use the gh_grep tool"
- What's the right way to set a custom domain in an SST Astro component? use the gh_grep tool
- ```
- Alternatively, you can add something like this to your [AGENTS.md](/docs/rules/).
- ```md title="AGENTS.md"
- If you are unsure how to do something, use `gh_grep` to search code examples from GitHub.
- ```
|