Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCode MCP Server Configuration

OpenCode MCP Server Configuration

MCP (Model Context Protocol) is an open protocol that allows you to connect external tools and data sources to OpenCode. After adding an MCP server, the tools it provides automatically appear alongside OpenCode's built-in tools, and the LLM can call them directly in the conversation.

OpenCode supports two types of MCP servers:

Context Consumption Reminder:Each MCP server occupies conversation context space. The more tools enabled, the more Tokens consumed per conversation, and the easier it is to reach the model's context limit. It is recommended to enable only the MCP servers actually needed for the current task and turn them off when done. Some servers (such as GitHub MCP) have a large number of tools and consume especially significant Tokens, so use them with extra care.


Basic Configuration Structure

All MCP server configurations are written under the opencode.json 's mcp field. Each server needs aunique nameas its key; this name can also be used in prompts to specify which MCP to call:


{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "my-mcp-server": {         // 服务器名称(自定义,全局唯一)
      "type": "local",         // 连接类型:local 或 remote
      // ...other configuration items
      "enabled": true          // 是否启用,默认为 true
    },
    "another-mcp-server": {    // 可同时配置多个 MCP 服务器
      "type": "remote",
      // ...
    }
  }
}

data to the child component via enabled to false You can temporarily disable a server without removing it from the configuration file, making it easy to toggle as needed:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"my-mcp-server"

:

{

     

"type"

:

"local"

,

     

"command"

:

[

"npx"

,

"-y"

,

"my-mcp-command"

]

,

     

"enabled"

:

false

         

// temporarily disabled; the configuration is kept, just change it back to true when needed

   

}

 

}

}


Local MCP Servers

A local MCP server provides tools by running a command process on your machine. Set the type to "local"and use the command to specify the startup command:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"my-local-mcp-server"

:

{

     

"type"

:

"local"

,

                                 

// connection type: local

     

"command"

:

[

"npx"

,

"-y"

,

"my-mcp-command"

]

,

       

// startup command array:

                                                         

// the first element is the executable,

                                                         

// the following elements are arguments

                                                         

// can also be written as ["bun", "x", "my-mcp-command"]

     

"enabled"

:

true

,

     

"environment"

:

{

                                 

// environment variables injected at runtime (optional)

       

"MY_ENV_VAR"

:

"my_env_var_value"

     

}

   

}

 

}

}

Local Server Configuration Items

Configuration Item Type Required Description
type String Is Connection type; must be specified for local servers "local"
command arrays Is Command and arguments to start the MCP server, as an array, e.g. ["npx", "-y", "my-mcp"]
environment object No Environment variables injected when running the server, as key-value pairs
enabled Boolean No Whether to enable the server at startup; defaults to true
timeout Digit No Timeout for fetching the tool list from the MCP server (in milliseconds); defaults to 5000(5 seconds)

Example: Adding a Test MCP Server

The following is an example of adding the official test MCP server @modelcontextprotocol/server-everything , which can be used to verify that MCP functionality is working properly:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"mcp_everything"

:

{

     

"type"

:

"local"

,

     

"command"

:

[

"npx"

,

"-y"

,

"@modelcontextprotocol/server-everything"

]

     

// npx -y means automatically confirm installation, no manual npm install needed

   

}

 

}

}

After configuration, add the server name in the prompt to let the LLM call this MCP's tools:

use the mcp_everything tool to add the number 3 and 4

Remote MCP Servers

A remote MCP server connects to a cloud service via HTTPS. Set the type to "remote"and use the url to specify the server address:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"my-remote-mcp"

:

{

     

"type"

:

"remote"

,

                             

// connection type: remote

     

"url"

:

"https://my-mcp-server.com"

,

           

// remote server address (required)

     

"enabled"

:

true

,

     

"headers"

:

{

                                   

// HTTP headers sent with requests (optional)

       

"Authorization"

:

"Bearer MY_API_KEY"

         

// commonly used for API key authentication

     

}

   

}

 

}

}

Remote Server Configuration Items

Configuration Item Type Required Description
type String Is Connection type; must be specified for remote servers "remote"
url String Is Full HTTPS address of the remote MCP server
enabled Boolean No Whether to enable the server at startup; defaults to true
headers object No HTTP headers sent with each request, commonly used for API key authentication
oauth object / false No OAuth authentication configuration, or set to false to disable automatic OAuth detection
timeout Digit No Timeout for fetching the tool list from the MCP server (in milliseconds); defaults to 5000(5 seconds)

Referencing Environment Variables

In the configuration file,sensitive information such as API keys should not be written directly in plain text. OpenCode supports reading system environment variables at runtime through the {env:变量名} syntax:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"my-remote-mcp"

:

{

     

"type"

:

"remote"

,

     

"url"

:

"https://my-mcp-server.com"

,

     

"headers"

:

{

       

"Authorization"

:

"Bearer {env:MY_API_KEY}"

   

// automatically read the value of the environment variable MY_API_KEY at runtime

                                                     

// the environment variable needs to be set in the system in advance:

                                                     

// export MY_API_KEY=your_actual_key

     

}

   

}

 

}

}


OAuth Authentication

OpenCode has built-in OAuth authentication support for remote MCP servers; the entire process is nearly fully automatic, with no need to handle Tokens manually.

Automatic Authentication Flow

When a server requires OAuth authentication, OpenCode automatically completes the following steps:

  1. Detect a 401 (Unauthorized) response from the server
  2. Automatically start the OAuth authorization flow and open a browser to guide you through login
  3. When supported by the server, automatically register the app using Dynamic Client Registration (RFC 7591)
  4. Securely store the obtained Token in the ~/.local/share/opencode/mcp-auth.json Medium
  5. Subsequent requests automatically carry the Token, with no need to log in again

1. Automatic OAuth (No Extra Configuration Needed)

For most MCP servers that support OAuth, simply configuring url is enough; OpenCode will automatically guide you through authentication on first use:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"my-oauth-server"

:

{

     

"type"

:

"remote"

,

     

"url"

:

"https://mcp.example.com/mcp"

     

// no extra configuration needed; OpenCode handles the OAuth flow automatically

   

}

 

}

}

You can also trigger the authentication flow manually (e.g., to log in again after a Token expires):

opencode mcp auth my-oauth-server

2. Pre-registered Client Credentials

If you have already obtained a fixed client ID and secret from the service provider, you can specify them directly in the configuration to skip the dynamic registration step:

Example

{

 

"$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}"

,

         

// read from environment variables to avoid exposing them in plain text

       

"clientSecret"

:

"{env:MY_MCP_CLIENT_SECRET}"

,

       

"scope"

:

"tools:read tools:execute"

           

// requested permission scopes; the specific values are defined by the service provider

     

}

   

}

 

}

}

3. Disable OAuth (Use API Key Authentication)

If the server uses API key instead of OAuth authentication, you can set the oauth Set to false to disable automatic OAuth detection and use headers to pass the key:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"my-api-key-server"

:

{

     

"type"

:

"remote"

,

     

"url"

:

"https://mcp.example.com/mcp"

,

     

"oauth"

:

false

,

                                 

// disable automatic OAuth detection

     

"headers"

:

{

       

"Authorization"

:

"Bearer {env:MY_API_KEY}"

   

// authenticate directly with an API key

     

}

   

}

 

}

}

OAuth Configuration Items

Configuration Item Type Description
oauth object / false OAuth configuration object; set to false to completely disable automatic OAuth detection
clientId String OAuth client ID. If not provided, OpenCode will attempt Dynamic Client Registration
clientSecret String OAuth client secret (if required by the authorization server)
scope String Permission scopes requested during authorization; multiple scopes are separated by spaces, and the specific values are defined by the service provider

Authentication Management Commands

OpenCode provides a set of command-line tools for managing MCP authentication status:


# Authenticate with the specified MCP server (will open a browser to complete OAuth authorization)
opencode mcp auth my-oauth-server

# List all configured MCP servers and their current authentication status
opencode mcp list

# Delete credentials stored on a specified server (requires re-login next time)
opencode mcp logout my-oauth-server

# Debug connection and OAuth flow for a specific server (display authentication status, test connection, execute OAuth discovery flow)
opencode mcp debug my-oauth-server

# Check the authentication status of all servers supporting OAuth
opencode mcp auth list

Overriding Remote Default Configuration

Organizations can provide members with preset MCP server configurations through the .well-known/opencode endpoint; these servers may be disabled by default. If you need to enable one of them, add an entry with the same name in the local configuration and set enabled: true to, the local configuration overrides the remote default:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"jira"

:

{

                                       

// same as the server name in the organization's remote configuration

     

"type"

:

"remote"

,

     

"url"

:

"https://jira.example.com/mcp"

,

     

"enabled"

:

true

                               

// set to true locally to override the remote default disabled state

   

}

 

}

}


Tool Management

After an MCP server is registered, its tools appear in OpenCode's tool list in the form of 服务器名称_工具名称 , and can be managed uniformly through the tools field.

1. Globally Disable Tools of a Specified MCP

Example

{

 

"$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

   

// disable all tools of the my-mcp-foo server (the server still starts, but its tools are unavailable)

 

}

}

2. Batch Disabling Using Glob Patterns

Glob wildcards can be used to match and disable tools of multiple MCP servers at once:

Example

{

 

"$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

   

// Glob pattern: disable tools of all servers whose names start with my-mcp

 

}

}

3. Enabling Specific MCP Tools per Agent

If you have configured a large number of MCP servers but each agent only needs a subset, you can first disable all MCP tools globally and then enable them as needed in specific agents' configurations. This allows precise control over which tools each agent can access, avoiding context waste:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"my-mcp"

:

{

     

"type"

:

"local"

,

     

"command"

:

[

"bun"

,

"x"

,

"my-mcp-command"

]

,

     

"enabled"

:

true

       

// the server itself remains running (otherwise agents cannot use it either)

   

}

 

}

,

 

"tools"

:

{

   

"my-mcp*"

:

false

         

// step 1: globally disable all tools of this MCP, unavailable in default conversations

 

}

,

 

"agent"

:

{

   

"my-agent"

:

{

     

"tools"

:

{

       

"my-mcp*"

:

true

     

// step 2: enable only in the my-agent agent; other agents are unaffected

     

}

   

}

 

}

}

Glob Pattern Rules

Wildcard Meaning Example
* Matches zero or more of any characters my-mcp* Matches my-mcp_search、my-mcp_list etc.
? Matches exactly one of any character my-mcp? Matches my-mcpA, but does not match my-mcpAB
Other Characters Matches exactly by literal value my-mcp-foo Matches only servers with exactly the same name

When registered, an MCP server's tools are prefixed with the server name; for example, a server myserver 's tools are registered as myserver_toolname. Therefore, to disable all tools of a server, use "myserver_*": false for an exact match.


Configuration example

Sentry

Adding the Sentry MCP server allows you to directly query error, issue, and event data of Sentry projects in the conversation:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"sentry"

:

{

     

"type"

:

"remote"

,

     

"url"

:

"https://mcp.sentry.dev/mcp"

,

     

"oauth"

:

{

}

     

// an empty object enables automatic OAuth authentication; OpenCode handles the entire OAuth flow

   

}

 

}

}

After adding the configuration, run the following command to complete account authorization (a browser will open for OAuth login):

opencode mcp auth sentry

After authentication, add use sentry in the prompt to call the Sentry tools:

Show me the latest unresolved issues in my project. use sentry

Context7

Adding the Context7 MCP server allows you to search various technical documents in the conversation, helping the LLM obtain the latest and most accurate API references:

Example

// basic version (no rate limit optimization)

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"context7"

:

{

     

"type"

:

"remote"

,

     

"url"

:

"https://mcp.context7.com/mcp"

   

}

 

}

}

Example

// after registering a free account, using an API key gives higher rate limits

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"context7"

:

{

     

"type"

:

"remote"

,

     

"url"

:

"https://mcp.context7.com/mcp"

,

     

"headers"

:

{

       

"CONTEXT7_API_KEY"

:

"{env:CONTEXT7_API_KEY}"

   

// read the API key from an environment variable

                                                       

// needs to be set in advance: export CONTEXT7_API_KEY=your_key

     

}

   

}

 

}

}

Add use context7 in the prompt to call the document search feature:

Configure a Cloudflare Worker script to cache JSON API responses for five minutes. use context7

You can also add a global rule in the project's AGENTS.md so that the LLM automatically uses Context7 when it needs to consult documentation, without having to specify it manually each time:

When you need to search docs, use `context7` tools.

Grep by Vercel

Adding the Grep by Vercel MCP server allows you to directly search real code snippets on GitHub in the conversation, helping the LLM find correct API usage and implementation references:

Example

{

 

"$schema"

:

"https://opencode.ai/config.json"

,

 

"mcp"

:

{

   

"gh_grep"

:

{

         

// the server is named gh_grep, referenced by this name in prompts

     

"type"

:

"remote"

,

     

"url"

:

"https://mcp.grep.app"

   

}

 

}

}

Add use the gh_grep tool to search code examples on GitHub:

What's the right way to set a custom domain in an SST Astro component? use the gh_grep tool

You can also set a global rule in AGENTS.md so that the LLM automatically searches code examples when unsure about usage:

If you are unsure how to do something, use `gh_grep` to search code examples from GitHub.