词元之母TOK.MOM - 平台充值汇率 1:1 即 1 人民币充值到账 1 美元,支持一个 Key 调用近 600+ 海内外模型,限时特价模型低至 1 折,欢迎上岸!
💡 一句话总结:掌握 OAuth 认证、权限管理和常用 MCP 服务配置。

~/.local/share/opencode/mcp-auth.json{
"mcp": {
"my-oauth-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp"
}
}
}{
"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"
}
}
}
}debug 命令诊断:MCP OAuth Debug
Server: my-oauth-server
URL: https://mcp.example.com/mcp
Auth status: ✓ authenticated
Access token: eyJhbGciOiJSUzI1NiIs...
Expires: 2026-02-15T12:00:00.000Z
Refresh token: present
Testing connection...
HTTP response: 200 OK
✓ Server responded successfully| 状态 | 说明 |
|---|---|
authenticated | 已认证,可以正常使用 |
expired | Token 已过期,需要重新认证 |
not authenticated | 未认证,需要运行 opencode mcp auth |
opencode mcp list 输出中的图标含义:| 图标 | 状态 | 说明 |
|---|---|---|
| ✓ | connected | 已连接,工具可用 |
| ○ | disabled | 已禁用,enabled: false |
| ⚠ | needs_auth | 需要 OAuth 认证 |
| ✗ | failed | 连接失败,查看错误信息 |
MCP Servers
✓ filesystem connected
npx -y @modelcontextprotocol/server-filesystem /projects
✓ context7 connected
https://mcp.context7.com/mcp
○ disabled-server disabled
npx -y some-command
✗ failed-server failed
Connection timeout{
"mcp": {
"my-api-key-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp",
"oauth": false,
"headers": {
"Authorization": "Bearer {env:MY_API_KEY}"
}
}
}
}{服务器名}_{工具名} 格式命名。permission 配置禁用 MCP 工具:{
"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"]
}
},
"permission": {
"my-mcp-foo_*": "deny"
}
}{
"permission": {
"my-mcp*": "deny"
}
}{
"mcp": {
"my-mcp": {
"type": "local",
"command": ["bun", "x", "my-mcp-command"]
}
},
"permission": {
"my-mcp*": "deny"
},
"agent": {
"my-agent": {
"permission": {
"my-mcp*": "allow"
}
}
}
}* 匹配零个或多个任意字符? 匹配正好一个字符AGENTS.md 或 .opencode/agents/*.md 中配置默认使用 MCP:{服务器名}_{工具名} 格式:filesystem 服务器的 read_file 工具 → filesystem_read_file
context7 服务器的 search 工具 → context7_searchlistTools 获取工具列表notifications/tools/list_changed){
"mcp": {
"sentry": {
"type": "remote",
"url": "https://mcp.sentry.dev/mcp",
"oauth": {}
}
}
}use sentry 查看最近未解决的错误{
"mcp": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp"
}
}
}{
"mcp": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp",
"headers": {
"CONTEXT7_API_KEY": "{env:CONTEXT7_API_KEY}"
}
}
}
}use context7 查询 Cloudflare Worker 如何缓存 JSON 响应{
"mcp": {
"gh_grep": {
"type": "remote",
"url": "https://mcp.grep.app"
}
}
}use the gh_grep tool 搜索 SST 框架中如何配置自定义域名{
"mcp": {
"filesystem": {
"type": "local",
"command": [
"npx", "-y", "@modelcontextprotocol/server-filesystem",
"/path/to/allowed/directory"
]
}
}
}{
"mcp": {
"postgres": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-postgres"],
"environment": {
"POSTGRES_CONNECTION_STRING": "{env:DATABASE_URL}"
}
}
}
}{
"mcp": {
"puppeteer": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-puppeteer"]
}
}
}{
"mcp": {
"memory": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-memory"]
}
}
}{
"mcp": {
"sqlite": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-sqlite", "/path/to/database.db"]
}
}
}{
"mcp": {
"slack": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-slack"],
"environment": {
"SLACK_BOT_TOKEN": "{env:SLACK_BOT_TOKEN}",
"SLACK_TEAM_ID": "{env:SLACK_TEAM_ID}"
}
}
}
}| 现象 | 原因 | 解决 |
|---|---|---|
| MCP 工具不出现 | 全局禁用或 Agent 未配置 | 检查 permission 配置 |
| OAuth 认证失败 | Token 过期或凭据无效 | 运行 opencode mcp logout && opencode mcp auth |
状态显示 needs_client_registration | 服务器不支持动态注册 | 在 oauth 中配置 clientId |
| 上下文快速耗尽 | 启用了太多 MCP 工具 | 禁用不常用的 MCP,使用按 Agent 启用 |
| 工具名称冲突 | 多个 MCP 有同名工具 | 使用 {服务器名}_{工具名} 格式区分 |
| 认证后仍显示 needs_auth | Token 存储失败 | 检查 ~/.local/share/opencode/mcp-auth.json 权限 |
| 命令格式错误 | command 写成字符串而非数组 | ❌ "command": "npx xxx" → ✓ "command": ["npx", "-y", "xxx"] |
| URL 格式错误 | URL 缺少协议前缀 | ❌ "url": "example.com/mcp" → ✓ "url": "https://example.com/mcp" |
| 浏览器无法自动打开 | 在 SSH/远程环境下 | OpenCode 会显示 URL,手动复制到浏览器打开 |
| 超时时间太短 | timeout 设为 1000ms | 远程服务器建议 2000+-10000ms,默认 30000ms |
| 忘记启用服务器 | enabled: false 但疑惑为什么不工作 | 默认就是启用的,检查是否误设为 false |
opencode mcp debug 诊断连接问题permission 控制工具访问下一课我们将学习 Chrome DevTools MCP。 你会学到: 让 AI 直接连接你的 Chrome 浏览器 调试已登录的页面(无需重新登录) 在 DevTools 中选中元素/请求让 AI 分析 使用浏览器截图、执行脚本等功能