你可以通过 Model Context Protocol(MCP) 向 OpenCode 添加外部工具。OpenCode 同时支持本地(local)和远程(remote)server。

一旦添加完成,MCP 工具会与内置工具一起,自动提供给 LLM 使用。


注意事项(Caveats)

当你使用 MCP server 时,它会占用 context(上下文)。如果你启用了很多工具,context 会很快被占满。因此,我们建议谨慎选择要使用的 MCP server。

某些 MCP server(例如 GitHub MCP server)通常会增加大量 token,很容易超过 context limit(上下文长度限制)。


启用(Enable)

你可以在 OpenCode Config 中的 mcp 节点下定义 MCP server。每一个 MCP 都需要一个唯一名称。你可以在 prompt(提示词)中通过名称引用该 MCP。

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "name-of-mcp-server": {
  5. // ...
  6. "enabled": true,
  7. },
  8. "name-of-other-mcp-server": {
  9. // ...
  10. },
  11. },
  12. }

你也可以通过将 enabled 设置为 false 来禁用某个 server。这在你希望临时关闭某个 server,但又不想从配置中删除它时非常有用。


覆盖远程默认配置(Overriding remote defaults)

组织可以通过其 .well-known/opencode endpoint 提供默认的 MCP server。这些 server 可能默认是禁用的,允许用户按需选择启用。

如果你想启用组织远程配置中的某个 server,可以在本地配置中添加该 server,并设置 enabled: true

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "jira": {
  5. "type": "remote",
  6. "url": "https://jira.example.com/mcp",
  7. "enabled": true
  8. }
  9. }
  10. }

你的本地配置会覆盖远程默认配置。更多细节请参考 config precedence


本地 MCP(Local)

在 MCP 对象中,将 type 设置为 "local",即可添加本地 MCP server。

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "my-local-mcp-server": {
  5. "type": "local",
  6. // ["bun", "x", "my-mcp-command"]
  7. "command": ["npx", "-y", "my-mcp-command"],
  8. "enabled": true,
  9. "environment": {
  10. "MY_ENV_VAR": "my_env_var_value",
  11. },
  12. },
  13. },
  14. }

command 用于指定如何启动本地 MCP server。你也可以通过 environment 传入一组环境变量。

例如,下面演示了如何添加测试用的 @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”], }, }, }

  1. 使用时,可以在 prompt 中添加 `use the mcp_everything tool`
  2. ```txt "mcp_everything"
  3. use the mcp_everything tool to add the number 3 and 4
  4. 使用 mcp_everything tool 计算 3 和 4 的和

配置项(Options)

下面是配置本地 MCP server 的所有可选项:

Option Type Required Description
type String Y MCP server 连接类型,必须为 "local"
command Array Y 启动 MCP server 的命令及参数
environment Object 运行 server 时设置的环境变量
enabled Boolean 是否在启动时启用 MCP server
timeout Number 从 MCP server 获取 tools 的超时时间(毫秒),默认 5000(5 秒)

远程 MCP(Remote)

type 设置为 "remote",即可添加远程 MCP server。

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "my-remote-mcp": {
  5. "type": "remote",
  6. "url": "https://my-mcp-server.com",
  7. "enabled": true,
  8. "headers": {
  9. "Authorization": "Bearer MY_API_KEY"
  10. }
  11. }
  12. }
  13. }

url 表示远程 MCP server 的地址,通过 headers 可以传入自定义 HTTP header。


配置项(Options)

Option Type Required Description
type String Y MCP server 连接类型,必须为 "remote"
url String Y 远程 MCP server 的 URL
enabled Boolean 是否在启动时启用 MCP server
headers Object 请求时携带的 header
oauth Object OAuth 认证配置,详见下方 OAuth
timeout Number 从 MCP server 获取 tools 的超时时间(毫秒),默认 5000(5 秒)

OAuth

OpenCode 会自动处理远程 MCP server 的 OAuth 认证流程。当 server 需要认证时,OpenCode 会:

  1. 检测到 401 响应并自动启动 OAuth 流程
  2. 如果 server 支持,则使用 Dynamic Client Registration(RFC 7591)
  3. 安全地存储 token,供后续请求使用

自动模式(Automatic)

对于大多数支持 OAuth 的 MCP server,不需要额外配置,只需配置远程 server 即可:

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "my-oauth-server": {
  5. "type": "remote",
  6. "url": "https://mcp.example.com/mcp"
  7. }
  8. }
  9. }

如果 server 需要认证,当你第一次使用时,OpenCode 会提示你完成认证。如果没有自动触发,也可以通过 opencode mcp auth <server-name> 手动触发认证流程。


预注册模式(Pre-registered)

如果你已经从 MCP server 提供方获取了 client credentials,可以手动配置:

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "my-oauth-server": {
  5. "type": "remote",
  6. "url": "https://mcp.example.com/mcp",
  7. "oauth": {
  8. "clientId": "{env:MY_MCP_CLIENT_ID}",
  9. "clientSecret": "{env:MY_MCP_CLIENT_SECRET}",
  10. "scope": "tools:read tools:execute"
  11. }
  12. }
  13. }
  14. }

认证操作(Authenticating)

你可以手动触发认证或管理凭证。

对指定 MCP server 进行认证:

  1. opencode mcp auth my-oauth-server

列出所有 MCP server 及其认证状态:

  1. opencode mcp list

删除已保存的认证信息:

  1. opencode mcp logout my-oauth-server

mcp auth 命令会打开浏览器完成授权流程。授权完成后,OpenCode 会将 token 安全存储在 ~/.local/share/opencode/mcp-auth.json 中。


禁用 OAuth(Disabling OAuth)

如果你希望对某个 server 禁用自动 OAuth(例如使用 API Key 的 server),可以将 oauth 设置为 false

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "my-api-key-server": {
  5. "type": "remote",
  6. "url": "https://mcp.example.com/mcp",
  7. "oauth": false,
  8. "headers": {
  9. "Authorization": "Bearer {env:MY_API_KEY}"
  10. }
  11. }
  12. }
  13. }

OAuth 配置项(OAuth Options)

Option Type Description
oauth Object false OAuth 配置对象,或设置为 false 以禁用自动 OAuth
clientId String OAuth client ID。如果未提供,将尝试动态注册
clientSecret String OAuth client secret(如果授权 server 要求)
scope String 授权时请求的 OAuth scope

调试(Debugging)

如果远程 MCP server 认证失败,可以使用以下命令进行排查:

  1. # 查看所有支持 OAuth 的 server 的认证状态
  2. opencode mcp auth list
  3. # 调试指定 server 的连接和 OAuth 流程
  4. opencode mcp debug my-oauth-server

mcp debug 会显示当前认证状态、测试 HTTP 连通性,并尝试执行 OAuth discovery 流程。


管理(Manage)

你的 MCP 会作为 tools 出现在 OpenCode 中,与内置工具一起使用。因此你可以像管理普通工具一样,通过 OpenCode config 来管理它们。


全局管理(Global)

你可以在全局范围内启用或禁用 MCP。

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "my-mcp-foo": {
  5. "type": "local",
  6. "command": ["bun", "x", "my-mcp-command-foo"]
  7. },
  8. "my-mcp-bar": {
  9. "type": "local",
  10. "command": ["bun", "x", "my-mcp-command-bar"]
  11. }
  12. },
  13. "tools": {
  14. "my-mcp-foo": false
  15. }
  16. }

你也可以使用 glob pattern(通配模式)来批量禁用 MCP。

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "my-mcp-foo": {
  5. "type": "local",
  6. "command": ["bun", "x", "my-mcp-command-foo"]
  7. },
  8. "my-mcp-bar": {
  9. "type": "local",
  10. "command": ["bun", "x", "my-mcp-command-bar"]
  11. }
  12. },
  13. "tools": {
  14. "my-mcp*": false
  15. }
  16. }

这里使用了 glob pattern my-mcp* 来禁用所有匹配的 MCP。


按 Agent 管理(Per agent)

如果你有大量 MCP server,可能希望只在某些 agent 中启用,而在全局禁用。可以按以下步骤操作:

  1. 在全局 tools 中禁用它。
  2. 在 agent config 中为指定 agent 启用该 MCP tool。
  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "my-mcp": {
  5. "type": "local",
  6. "command": ["bun", "x", "my-mcp-command"],
  7. "enabled": true
  8. }
  9. },
  10. "tools": {
  11. "my-mcp*": false
  12. },
  13. "agent": {
  14. "my-agent": {
  15. "tools": {
  16. "my-mcp*": true
  17. }
  18. }
  19. }
  20. }

通配规则(Glob patterns)

glob pattern 使用简单的正则通配规则:

  • *:匹配零个或多个任意字符(例如 "my-mcp*" 可匹配 my-mcp_searchmy-mcp_list 等)
  • ?:匹配任意单个字符
  • 其他字符按字面值精确匹配

示例(Examples)

下面列出了一些常见 MCP server 的示例。如果你想补充其他 server,可以提交 PR。


Sentry

添加 Sentry MCP server,用于与你的 Sentry 项目和 issue 进行交互。

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "sentry": {
  5. "type": "remote",
  6. "url": "https://mcp.sentry.dev/mcp",
  7. "oauth": {}
  8. }
  9. }
  10. }

配置完成后,执行认证:

  1. opencode mcp auth sentry

该命令会打开浏览器完成 OAuth 授权,并将 OpenCode 连接到你的 Sentry 账号。

认证完成后,你就可以在 prompt 中使用 Sentry tools 查询 issue、project 和 error 数据。

  1. Show me the latest unresolved issues in my project. use sentry
  2. 显示我项目中最新的未解决 issueuse sentry

Context7

添加 Context7 MCP server,用于搜索文档。

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "context7": {
  5. "type": "remote",
  6. "url": "https://mcp.context7.com/mcp"
  7. }
  8. }
  9. }
  10. 如果你注册了免费账号,可以使用 API Key 来获得更高的 rate-limit
  11. ```json
  12. {
  13. "$schema": "https://opencode.ai/config.json",
  14. "mcp": {
  15. "context7": {
  16. "type": "remote",
  17. "url": "https://mcp.context7.com/mcp",
  18. "headers": {
  19. "CONTEXT7_API_KEY": "{env:CONTEXT7_API_KEY}"
  20. }
  21. }
  22. }
  23. }

这里假设你已经设置了环境变量 CONTEXT7_API_KEY

在 prompt 中添加 use context7 即可使用 Context7 MCP server。

  1. Configure a Cloudflare Worker script to cache JSON API responses for five minutes. use context7
  2. 使用 context7 配置一个 Cloudflare Worker 脚本,将 JSON API 响应缓存 5 分钟。

你也可以在 AGENTS.md 中加入类似规则:

  1. 当需要搜索文档时,使用 `context7` tools

Grep by Vercel

添加 Grep by Vercel MCP server,用于搜索 GitHub 上的代码片段。

  1. {
  2. "$schema": "https://opencode.ai/config.json",
  3. "mcp": {
  4. "gh_grep": {
  5. "type": "remote",
  6. "url": "https://mcp.grep.app"
  7. }
  8. }
  9. }

由于我们将 MCP server 命名为 gh_grep,因此在 prompt 中可以添加 use the gh_grep tool 来让 agent 使用它。

```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 在 SST Astro component 中设置自定义域名的正确方式是什么?use the gh_grep tool

  1. 你也可以在 [AGENTS.md](/docs/rules/) 中添加类似说明:
  2. ```md title="AGENTS.md"
  3. 如果不确定如何实现某个功能,请使用 `gh_grep` 在 GitHub 中搜索代码示例。