OpenCode MCP servers are added with opencode mcp add, which writes the server to your project config (or your global one with --global) and connects it automatically. Remote servers sign in with OAuth by default, and in current OpenCode config every server lives under mcp.servers, not directly under mcp.
Applies to: OpenCode 2 docs (latest changelog entry v2.4.3, November 29, 2026), read on December 2, 2026. Last checked: December 2, 2026.
On this page
Add a server from the command line
OpenCode’s docs add a remote server from the project that should use it, then check the connection (MCP servers):
opencode mcp add context7 --url https://mcp.context7.com/mcp
opencode mcp list
The command writes the server to the project configuration. Add --global to make it available in every project:
opencode mcp add context7 --global --url https://mcp.context7.com/mcp
If opencode mcp list shows needs authentication, open OpenCode, run /mcps, select the server and sign in. A connected server is listed as ✓ context7 connected.
For a local server, which OpenCode starts over the MCP stdio transport, put the command after --, and pass environment variables with repeated --env flags (MCP servers):
opencode mcp add everything -- npx -y @modelcontextprotocol/server-everything
opencode mcp add everything --env LOG_LEVEL=info -- npx -y @modelcontextprotocol/server-everything
A remote server that wants a header instead of OAuth takes --header. The docs say to quote {env:NAME} references so the shell writes them literally:
opencode mcp add context7 --url https://mcp.context7.com/mcp --header 'CONTEXT7_API_KEY={env:CONTEXT7_API_KEY}'
OpenCode’s docs warn that MCP tools consume model context, so add only the servers you need. The Context7 and Sentry listings show what those two servers expose.
The config shape: mcp.servers
To write a server by hand, give it a unique name under mcp.servers in opencode.jsonc. The docs state that V2 does not place server names directly under mcp (MCP servers):
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"servers": {
"my-server": {
"type": "local",
"command": ["npx", "-y", "example-mcp-server"],
},
},
},
}
Servers connect automatically. Use disabled, not an enabled field, to keep one configured without connecting it. The global file is ~/.config/opencode/opencode.json(c), and a project can use opencode.json(c) or .opencode/opencode.json(c) (config).
Warning
A higher-precedence project config replaces the entire server object with the same name, not just the fields it sets. Per the docs, use different names for separate connections or accounts; otherwise repeat every required field in the override (MCP servers).
| Field | Local server | Remote server |
|---|---|---|
type | "local" (required) | "remote" (required) |
| Where it points | command: executable followed by its arguments (required) | url: absolute Streamable HTTP endpoint (required) |
| Extra settings | cwd (defaults to the workspace), environment | headers, oauth (settings, or false to turn OAuth off) |
| Both | disabled, codemode, timeout, protocol, pool | disabled, codemode, timeout, protocol, pool |
For the difference between the two transports, see MCP stdio vs Streamable HTTP.
Older tutorials: the V1 shape
Many tutorials show V1 config, where the server name sits directly under mcp and carries enabled. OpenCode’s migration page lays the two side by side (Migrate from V1):
// V1
{
"mcp": {
"playwright": {
"type": "local",
"command": ["npx", "@playwright/mcp"],
"enabled": true,
"timeout": 30000
}
}
}
// V2
{
"mcp": {
"servers": {
"playwright": {
"type": "local",
"command": ["npx", "@playwright/mcp"],
"disabled": false,
"timeout": {
"catalog": 30000,
"execution": 30000
}
}
}
}
}
The same page says the V1 config format remains supported and the native V2 format is optional, and that V1 and native V2 members may be mixed within mcp. An enabled-only V1 entry without a type is listed among the unsupported ones. V1 and V2 read the same config locations, so the page advises against pointing V1 at files you have converted to V2-only shapes. The OAuth fields also change from camel case to snake case: clientId becomes client_id, clientSecret becomes client_secret, callbackPort becomes callback_port and redirectUri becomes redirect_uri.
Secrets and headers
Store secrets in environment variables, not in the config file. OpenCode substitutes {env:NAME}; shell syntax such as $NAME is not expanded inside JSON strings (MCP servers):
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"servers": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp",
"oauth": false,
"headers": {
"CONTEXT7_API_KEY": "{env:CONTEXT7_API_KEY}"
}
}
}
}
}
Set oauth: false only when the server uses an API key or another header credential exclusively.
Warning
For calls made on behalf of a session, OpenCode sends the session ID in
_meta["ai.opencode/sessionID"]. The docs say remote servers receive the raw ID and may log or retain it, and that it must not be used by itself for authentication or authorization (MCP servers).
OAuth sign-in
OAuth is on for every remote server unless oauth is false. OpenCode discovers the authorization server, signs in with PKCE and refreshes tokens, and keeps credentials outside the project configuration. Most servers need only the URL. Sign in from /mcps, or from the CLI (MCP servers):
opencode mcp auth sentry
Run opencode mcp auth without a name to pick from the OAuth-capable servers, and opencode mcp logout sentry to remove stored credentials when you need to sign in again or switch accounts.
Over SSH or in a container. The browser cannot reach OpenCode after you sign in and shows a page that does not load. Copy that page’s URL from the address bar and paste it into the sign-in prompt in /mcps, opencode mcp auth, or the desktop and web app’s dialog. It looks like http://127.0.0.1:52817/callback?code=...&state=....
How OpenCode registers itself. It uses the first method that applies:
| Method | Used when |
|---|---|
| Pre-registered client | oauth.client_id is configured |
| Client ID Metadata Document (CIMD) | The authorization server supports CIMD and public clients, and oauth.redirect_uri is not set |
| Dynamic client registration (DCR) | Neither of the above applies |
If the authorization server supports neither CIMD nor DCR, configure a pre-registered client. The docs give this shape, with the credentials read from environment variables:
{
"mcp": {
"servers": {
"company-tools": {
"type": "remote",
"url": "https://mcp.example.com/mcp",
"oauth": {
"client_id": "{env:MCP_CLIENT_ID}",
"client_secret": "{env:MCP_CLIENT_SECRET}",
"scope": "tools:read tools:execute",
"callback_port": 19876,
"redirect_uri": "http://127.0.0.1:19876/callback"
}
}
}
}
}
Setting redirect_uri disables CIMD. Omit client_secret for a public client. For what happens on the wire, see MCP OAuth.
Timeouts
Timeouts are positive integer milliseconds. Set defaults under mcp.timeout; a server’s own timeout object overrides the matching defaults (MCP servers):
{
"mcp": {
"timeout": {
"startup": 45000,
"catalog": 30000,
"execution": 600000
},
"servers": {
"slow-tools": {
"type": "remote",
"url": "https://mcp.example.com/mcp",
"timeout": {
"catalog": 60000
}
}
}
}
}
| Timeout | Default | Applies to |
|---|---|---|
startup | 30 seconds | Transport connection and server initialization |
catalog | 30 seconds | Listing tools, prompts, resources and resource templates |
execution | 12 hours | Tool calls, prompt retrieval and resource reads |
Protocol version and shared connections
By default OpenCode probes each server for the 2026-07-28 MCP revision and falls back to the classic initialize handshake. The docs warn that probing an older local server adds a short-lived extra process and can wait up to the startup timeout, so set "protocol": "legacy" on servers you know predate that revision (MCP servers).
One OpenCode server can have many directories open, and each connects to every MCP server on its own. "pool": true lets directories with identical server settings share one connection and one tool list. Only connections on the 2026-07-28 revision are shared, and a local server is shared across projects only with a fixed cwd such as "~".
Tool names, prompts and permissions
OpenCode names a tool <server>_<tool> and replaces characters other than letters, numbers, _ and - with _, so server context 7 with tool resolve.library/id becomes context_7_resolve_library_id. MCP prompts become commands named <server>:<prompt>, for example /context_7:find_docs react "server components". Choose short server names that stay unique after this normalization (MCP servers).
Code Mode is the default way tools are exposed. Set "codemode": false on a server to keep its tools on the provider’s native tool list. To hide or deny a server’s tools without disconnecting it, match the normalized name in a permission rule:
{
"permissions": [
{
"action": "context7_*",
"resource": "*",
"effect": "deny"
}
]
}
Agents read MCP resources through the built-in list_mcp_resources and read_mcp_resource tools, controlled by the opencode_list_mcp_resources and opencode_read_mcp_resource actions. The docs also say a server’s instructions are added to the system prompt inside <mcp_instructions>, and that servers can ask for input through MCP elicitation, shown as a form; dismissing it cancels the request.
Check and remove servers
opencode mcp list shows each server and its connection state, and /mcps in OpenCode lets you view, connect, disconnect or authenticate. Disconnecting in /mcps does not change the config. To remove a server, delete its entry from the project or global config where it was added; to keep it configured but disconnected, set "disabled": true (MCP servers). The same commands side by side with Claude Code’s claude mcp add are in Claude Code vs OpenCode.





