词元之母TOK.MOM - 平台充值汇率 1:1 即 1 人民币充值到账 1 美元,支持一个 Key 调用近 600+ 海内外模型,限时特价模型低至 1 折,欢迎上岸!
💡 一句话总结:通过 MCP(Model Context Protocol)连接外部服 务,让 AI 能调用数据库、搜索引擎、监控平台等任意工具。

用户提问 → OpenCode → AI 决定调用 MCP 工具 → MCP 服务器执行 → 返回结果| 加载顺序 | 位置 | 用途 |
|---|---|---|
| 1(最低) | ~/.config/opencode/opencode.json | 全局配置,所有项目共享 |
| 2 | opencode.json | 项目根目录配置 |
| 3(最高) | .opencode/opencode.json | 项目级配置(推荐) |
.opencode/ 目录下更整洁,方便与其他配置(如 agents、commands)一起管理。? Location: (Use arrow keys)
❯ Current project
/path/to/project/.opencode/opencode.json
Global
~/.config/opencode/opencode.json
? MCP server name: filesystem
? Select MCP server type:
❯ Local
Remote
? Enter command to run:
opencode x @modelcontextprotocol/server-filesystem /path/to/allowed/dir⚠️ 注意:位置选择仅在 Git 项目中显示。非 Git 项目会直接写入全局配置。
✓ MCP server "filesystem" added successfullyopencode.json 或 .opencode/opencode.json 的 mcp 下配置:{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-local-mcp": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-everything"],
"enabled": true,
"environment": {
"MY_ENV_VAR": "value"
}
}
}
}| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
type | String | ✓ | 必须是 "local" |
command | Array | ✓ | 命令数组,如 ["npx", "-y", "xxx"] 或 ["bun", "x", "xxx"] |
environment | Object | 环境变量键值对 | |
enabled | Boolean | 是否启用,默认 true | |
timeout | Number | 连接超时(毫秒),默认 30000 |
⚠️ 注意:官方文档描述 timeout 默认值为 2000+ms,但源码实际默认值为 30000ms(30秒)。来源: mcp/index.ts:29
use the my-local-mcp tool to do something{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-remote-mcp": {
"type": "remote",
"url": "https://mcp.example.com/mcp",
"enabled": true,
"headers": {
"Authorization": "Bearer {env:MY_API_KEY}"
}
}
}
}| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
type | String | ✓ | 必须是 "remote" |
url | String | ✓ | 远程 MCP 服务器 URL |
enabled | Boolean | 是否启用,默认 true | |
headers | Object | 自定义请求头 | |
oauth | Object/false | OAuth 配置或禁用 OAuth | |
timeout | Number | 连接超时(毫秒),默认 30000 |
| 状态 | 说明 |
|---|---|
connected | 已连接,工具可用 |
disabled | 配置中 enabled: false,未连接 |
failed | 连接失败,查看错误信息 |
needs_auth | 需要 OAuth 认证 |
needs_client_registration | 需要预注册客户端 ID |
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"everything": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-everything"]
}
}
}use the everything tool to add 3 and 4{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp"
}
}
}use context7 查询 React hooks 最佳实践{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"gh_grep": {
"type": "remote",
"url": "https://mcp.grep.app"
}
}
}use the gh_grep tool 搜索如何在 Node.js 中实现 JWT 验证| 现象 | 原因 | 解决 |
|---|---|---|
| MCP 连接超时 | timeout 设置过短或网络慢 | 增大 timeout 值,默认 30000ms |
| 本地 MCP 启动失败 | 命令不存在或路径错误 | 确认 command 数组正确,检查 PATH |
| 远程 MCP 连接失败 | URL 错误或服务器不可用 | 验证 URL 是否正确,检查网络 |
opencode mcp add 命令type: "local" + command 数组type: "remote" + url