跳转到内容

MCP 服务器

MCP 服务器

你可以使用 Model Context Protocol(或 MCP)向 OpenCode 添加外部工具。OpenCode 支持本地和远程服务器。

添加后,MCP 工具会自动与内置工具一起提供给 LLM。


注意事项

当你使用 MCP 服务器时,它会增加上下文。这可能会很快累积起来,特别是当你有很多工具时。因此,我们建议谨慎选择使用哪些 MCP 服务器。


启用

你可以在 OpenCode 配置 中的 mcp 下定义 MCP 服务器。用唯一名称添加每个 MCP。你可以在提示 LLM 时通过名称引用该 MCP。

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "name-of-mcp-server": {
      // ...
      "enabled": true,
    },
    "name-of-other-mcp-server": {
      // ...
    },
  },
}

你也可以通过将 enabled 设置为 false 来禁用服务器。如果你希望暂时禁用服务器而不从配置中删除它,这很有用。


覆盖远程默认值

组织可以通过其 .well-known/opencode 端点提供默认 MCP 服务器。这些服务器可能默认禁用,允许用户选择加入他们需要的服务器。

要从组织的远程配置中启用特定服务器,将其添加到你的本地配置中并设置 enabled: true

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "jira": {
      "type": "remote",
      "url": "https://jira.example.com/mcp",
      "enabled": true
    }
  }
}

你的本地配置值覆盖远程默认值。有关更多详细信息,请参阅配置优先级


本地

使用 type 设置为 "local" 添加本地 MCP 服务器。

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "my-local-mcp-server": {
      "type": "local",
      // 或 ["bun", "x", "my-mcp-command"]
      "command": ["npx", "-y", "my-mcp-command"],
      "enabled": true,
      "environment": {
        "MY_ENV_VAR": "my_env_var_value",
      },
    },
  },
}

command 是启动本地 MCP 服务器的方式。你也可以传入环境变量列表。

例如,以下是你可以添加测试 @modelcontextprotocol/server-everything MCP 服务器的方法。

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "mcp_everything": {
      "type": "local",
      "command": ["npx", "-y", "@modelcontextprotocol/server-everything"],
    },
  },
}

要使用它,我可以在我的提示词中添加 use the mcp_everything tool

use the mcp_everything tool to add the number 3 and 4

选项

以下是配置本地 MCP 服务器的所有选项。

选项类型必填描述
typeStringYMCP 服务器连接类型,必须为 "local"
commandArrayY运行 MCP 服务器的命令和参数。
environmentObject运行服务器时设置的环境变量。
enabledBoolean启动时启用或禁用 MCP 服务器。
timeoutNumber从 MCP 服务器获取工具的超时时间(毫秒)。默认为 5000(5 秒)。

远程

通过将 type 设置为 "remote" 添加远程 MCP 服务器。

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

url 是远程 MCP 服务器的 URL,你可以通过 headers 选项传入一个 header 列表。


选项

选项类型必填描述
typeStringYMCP 服务器连接类型,必须为 "remote"
urlStringY远程 MCP 服务器的 URL。
enabledBoolean启动时启用或禁用 MCP 服务器。
headersObject随请求发送的 header。
oauthObjectOAuth 身份验证配置。见下面的 OAuth 部分。
timeoutNumber从 MCP 服务器获取工具的超时时间(毫秒)。默认为 5000(5 秒)。

OAuth

OpenCode 自动处理远程 MCP 服务器的 OAuth 身份验证。当服务器需要身份验证时,OpenCode 将:

  1. 检测 401 响应并启动 OAuth 流程
  2. 如果服务器支持,使用 动态客户端注册(RFC 7591)
  3. 安全存储令牌以供将来请求

自动

对于大多数启用 OAuth 的 MCP 服务器,不需要特殊配置。只需配置远程服务器:

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "my-oauth-server": {
      "type": "remote",
      "url": "https://mcp.example.com/mcp"
    }
  }
}

如果服务器需要身份验证,OpenCode 会在你首次尝试使用它时提示你进行身份验证。如果没有,你可以手动触发流程 opencode mcp auth <server-name>


预注册

如果你有 MCP 服务器 provider 的客户端凭据,你可以配置它们:

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

身份验证

你可以手动触发身份验证或管理凭据。

对特定 MCP 服务器进行身份验证:

opencode mcp auth my-oauth-server

列出所有 MCP 服务器及其身份验证状态:

opencode mcp list

删除存储的凭据:

opencode mcp logout my-oauth-server

mcp auth 命令将打开你的浏览器进行授权。授权后,OpenCode 会将令牌安全存储在 ~/.local/share/opencode/mcp-auth.json 中。


禁用 OAuth

如果你想为服务器禁用自动 OAuth(例如,对于使用 API 密钥的服务器),将 oauth 设置为 false

{
  "$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 选项

选项类型描述
oauthObject | falseOAuth 配置对象,或 false 禁用 OAuth 自动检测。
clientIdStringOAuth 客户端 ID。如果未提供,将尝试动态客户端注册。
clientSecretStringOAuth 客户端密钥,如果授权服务器需要的话。
scopeString授权期间请求的 OAuth 范围。

调试

如果远程 MCP 服务器身份验证失败,你可以使用以下方法诊断问题:

# 查看所有支持 OAuth 的服务器的身份验证状态
opencode mcp auth list
# 调试特定服务器的连接和 OAuth 流程
opencode mcp debug my-oauth-server

mcp debug 命令显示当前身份验证状态,测试 HTTP 连接,并尝试 OAuth 发现流程。


管理

你的 MCP 作为工具在 OpenCode 中可用,与内置工具一起。因此,你可以通过 OpenCode 配置像管理任何其他工具一样管理它们。


全局

这意味着你可以全局启用或禁用它们。

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

我们也可以使用 glob 模式禁用所有匹配的 MCP。

{
  "$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 模式 my-mcp* 来禁用所有 MCP。


每个智能体

如果你有大量 MCP 服务器,你可能希望仅按智能体启用它们并全局禁用它们。要做到这一点:

  1. 全局禁用它作为工具。
  2. 在你的智能体配置中,将 MCP 服务器启用为工具。
{
  "$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 模式

glob 模式使用简单的正则 glob 匹配模式:

  • * 匹配零个或多个任意字符(例如,"my-mcp*" 匹配 my-mcp_searchmy-mcp_list 等)
  • ? 精确匹配一个字符
  • 所有其他字符按字面意思匹配

示例

以下是一些常见 MCP 服务器的示例。如果你想记录其他服务器,可以提交 PR。


Sentry

添加 Sentry MCP 服务器 以与你的 Sentry 项目和问题进行交互。

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "sentry": {
      "type": "remote",
      "url": "https://mcp.sentry.dev/mcp",
      "oauth": {}
    }
  }
}

添加配置后,使用 Sentry 进行身份验证:

opencode mcp auth sentry

这将打开浏览器窗口以完成 OAuth 流程并将 OpenCode 连接到你的 Sentry 账户。

身份验证后,你可以在提示词中使用 Sentry 工具来查询问题、项目和错误数据。

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

Context7

添加 Context7 MCP 服务器 以搜索文档。

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "context7": {
      "type": "remote",
      "url": "https://mcp.context7.com/mcp"
    }
  }
}

如果你已注册免费账户,你可以使用你的 API 密钥并获得更高的速率限制。

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "context7": {
      "type": "remote",
      "url": "https://mcp.context7.com/mcp",
      "headers": {
        "CONTEXT7_API_KEY": "{env:CONTEXT7_API_KEY}"
      }
    }
  }
}

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

在提示词中添加 use context7 以使用 Context7 MCP 服务器。

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

或者,你可以将类似这样的内容添加到你的 AGENTS.md

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

Grep by Vercel

添加 Grep by Vercel MCP 服务器以搜索 GitHub 上的代码片段。

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "gh_grep": {
      "type": "remote",
      "url": "https://mcp.grep.app"
    }
  }
}

由于我们将 MCP 服务器命名为 gh_grep,你可以在提示词中添加 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

或者,你可以将类似这样的内容添加到你的 AGENTS.md

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