页面简介
Connect OpenCode to local and remote Model Context Protocol servers with opencode mcp add or in config, including OAuth sign-in.
正文摘录
MCP serversOpenCode connects to Model Context Protocol servers and exposes capabilities such as tools, prompts, resources, and instructions. MCP tools consume model context, so add only the servers you need. Setup Add a remote server from the project that should use it, then check its connection: Terminalopencode 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: Terminalopencode mcp add context7 --global --url https://mcp.context7.com/mcp Remote servers use OAuth by default. If the list shows needs authentication, open OpenCode, run /mcps, select the server, and sign in. A connected server is ready for an agent to use: ✓ context7 connected Config To configure a server by hand, give it a unique name under mcp.servers. V2 does not place server names directly under mcp. opencode.jsonc{ "$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: JSONC{ "mcp": { "servers": { "my-server": { "type": "local", "command": ["npx", "-y", "example-mcp-server"], "disabled": true, }, }, }, } A higher-precedence project config replaces the entire server object with the same name. Use different names for separate connections or accounts; otherwise repeat every required field in the override: opencode.jsonc{ "mcp": { "servers": { "my-server": { "type": "remote", "url": "https://mcp.example.com/mcp", }, }, }, } Local A local server is a command that OpenCode starts over the MCP stdio transport. Add one with a command after --: Terminalopencode mcp add everything -- npx -y @modelcontextprotocol/server-everything Pass environment variables with repeated --env flags: Terminalopencode mcp add everything --env LOG_LEVEL=info -- npx -y @modelcontextprotocol/server-everything Use configuration for other process options such as a working directory: opencode.jsonc{ "$schema": "https://opencode.ai/config.json", "mcp": { "servers": { "everything": { "type": "local", "command": ["npx", "-y", "@modelcontextprotocol/server-everything"], "cwd": ".", "environment": { "LOG_LEVEL": "info", "MCP_API_KEY": "{env:MCP_API_KEY}", }, }, }, }, } FieldRequiredDescriptiontypeYesMust be "local".commandYesExecutable followed by its arguments.cwdNoProcess directory. Relative paths resolve from the workspace, which is also the default.environmentNoString variables added to OpenCode’s inherited process environment.disabledNoPrevents connection when true. Defaults to false.codemodeNoSet to false to expose tools directly instead of through Code Mode. Defaults to true.timeoutNoPer-server timeout overrides.protocolNoauto (default), legacy, or 2026-07-28. See Protocol version.poolNoShare one connection across directories. Defaults to false. See Shared connections. Use {env:NAME} for environment substitution. Shell expressions such as $NAME are not expanded in JSON strings: JSONC{ "environment": { "MCP_API_KEY": "{env:MCP_API_KEY}", }, } Remote A remote server uses the MCP Streamable HTTP transport and requires an absolute URL: Terminalopencode mcp add context7 --url https://mcp.context7.com/mcp Pass request headers with repeated --header flags. Quote {env:NAME} references so the shell writes them literally: Terminalopencode mcp add context7 --url https://mcp.context7.com/mcp --header 'CONTEXT7_API_KEY={env:CONTEXT7_API_KEY}' Use configuration for other options. Store secrets in environment variables rather than in the file: opencode.jsonc{ "$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}", }, }, }, }, } FieldRequiredDescriptiontypeYesMust be "remote".urlYesAbsolute Streamable HTTP endpoint.headersNoString HTTP headers sent to the endpoint.oauthNoOAuth settings, or false to disable OAuth.disabledNoPrevents connection when true. Defaults to false.codemodeNoSet to false to expose tools directly instead of through Code Mode. Defaults to true.timeoutNoPer-server timeout overrides.protocolNoauto (default), legacy, or 2026-07-28. See Protocol version.poolNoShare one connection across directories. Defaults to false. See Shared connections. Use oauth: false only when the server exclusively uses an API key or another header credential: JSONC{ "type": "remote", "url": "https://mcp.example.com/mcp", "oauth": false, "headers": { "Authorization": "Bearer {env:MCP_API_KEY}" }, } OAuth OAuth is enabled for remote servers unless oauth is false. OpenCode discovers the authorization server, signs in with PKCE, and refreshes tokens. Credentials are stored outside project configuration. Most servers need only the URL: opencode.jsonc{ "$schema": "https://opencode.ai/config.json", "mcp": { "servers": { "sentry": { "type": "remote", "url": "https://mcp.sentry.dev/mcp", }, }, }, } If the server needs authentication, run /mcps, select it, and complete authorization in the browser. The CLI can start the same flow: Terminalopencode mcp auth sentry Browser on another machine When OpenCode runs over SSH, in a container, or on a remote server, 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: http://127.0.0.1:52817/callback?code=...&state=... The prompt appears in /mcps, in opencode mcp auth, and in the desktop and web app’s sign-in dialog while OpenCode waits for the browser. Client registration OpenCode identifies itself to the authorization server with the first method that applies: MethodUsed whenPre-registered clientoauth.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. OpenCode registers a client and reuses it on later sign-ins, listening on the port it registered. If that port is taken, OpenCode registers again. An authorization server opts into CIMD through its metadata: JSON{ "issuer": "https://auth.example.com", "client_id_metadata_document_supported": true, "token_endpoint_auth_methods_supported": ["none"] } With CIMD, OpenCode sends its metadata document URL as the client_id and skips registration: https://opencode.ai/oauth/opencode/client.json The document allows these loopback redirects without a port: http://127.0.0.1/callback http://localhost/callback http://127.0.0.1/mcp/oauth/callback for older clients Authorization servers accept any loopback port for these redirects, as RFC 8252 requires, so callback_port works with CIMD. A custom redirect_uri does not, so OpenCode falls back to DCR. If the authorization server supports neither CIMD nor DCR, configure a pre-registered client. Pre-registered clients When a provider gives you client credentials, use V2’s snake_case OAuth fields: opencode.jsonc{ "$schema": "https://opencode.ai/config.json", "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", }, }, }, }, } FieldDescriptionclient_idPre-registered client ID. Takes priority over CIMD and DCR.client_secretSecret for a confidential client, sent in the token request body. Omit it for public clients.scopeSpace-delimited scopes to request. Defaults to the scope the server names when it rejects an unauthenticated request. OpenCode sends the granted scope again when it refreshes the token.callback_portLocal callback port from 1 through 65535. Defaults to the redirect_uri port, then the port of a client registered at an earlier sign-in, then an available ephemeral port.redirect_uriPre-registered loopback URI whose path and port reach the local callback listener. Setting it disables CIMD.auth_server_metadata_urlURL of the authorization server’s OAuth or OpenID Connect metadata document. Set it when the MCP server does not publish protected resource metadata that names its authorization server. Remove stored OAuth credentials when you need to sign in again or switch accounts: Terminalopencode mcp logout sentry Timeouts Timeouts are positive integer milliseconds. Set defaults under mcp.timeout; a server’s timeout object overrides matching defaults. opencode.jsonc{ "$schema": "https://opencode.ai/config.json", "mcp": { "timeout": { "startup": 45000, "catalog": 30000, "execution": 600000, }, "servers": { "slow-tools": { "type": "remote", "url": "https://mcp.example.com/mcp", "timeout": { "catalog": 60000, }, }, }, }, } TimeoutDefaultApplies tostartup30 secondsTransport connection and server initialization.catalog30 secondsListing tools, prompts, resources, and resource templates.execution12 hoursTool calls, prompt retrieval, and resource reads. Protocol version OpenCode probes every server for the 2026-07-28 revision by default and falls back to the classic MCP initialize handshake when the server does not support it. Set a server’s protocol to legacy to skip the probe: opencode.jsonc{ "$schema": "https://opencode.ai/config.json", "mcp": { "servers": { "older": { "type": "remote", "url": "https://mcp.example.com/mcp", "protocol": "legacy", }, }, }, } ValueBehaviorautoDefault. Probes with server/discover for the 2026-07-28 revision and falls back to legacy when the server does not support it, including when a remote server answers the probe with an HTTP 5xx error.legacySends initialize and speaks protocol revisions up to 2025-11-25.2026-07-28Requires the 2026-07-28 revision. The connection fails against older servers. Probing an older local server adds a short-lived extra proc