文档
中文文档
MCP 服务器

MCP 服务器

OpenCode 支持 Model Context Protocol(MCP)通过本地和远程服务器添加外部工具。

重要:MCP 服务器会增加上下文,所以要谨慎选择启用哪些。

启用 MCP 服务器

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

本地 MCP 服务器

{
  "mcp": {
    "my-local-server": {
      "type": "local",
      "command": ["npx", "-y", "my-mcp-command"],
      "enabled": true,
      "environment": {
        "MY_ENV_VAR": "value"
      }
    }
  }
}

本地服务器选项

选项类型必需
typeString是("local"
commandArray
environmentObject
timeoutNumber否(默认:5000)

远程 MCP 服务器

{
  "mcp": {
    "my-remote-server": {
      "type": "remote",
      "url": "https://my-mcp-server.com",
      "headers": {
        "Authorization": "Bearer API_KEY"
      }
    }
  }
}

OAuth 认证

opencode mcp auth server-name
opencode mcp list
opencode mcp logout server-name

管理 MCP 工具

全局

{
  "tools": {
    "my-mcp*": false
  }
}

按 Agent

{
  "agent": {
    "my-agent": {
      "tools": {
        "my-mcp*": true
      }
    }
  }
}

示例

Sentry

{
  "mcp": {
    "sentry": {
      "type": "remote",
      "url": "https://mcp.sentry.dev/mcp"
    }
  }
}

GitHub 代码搜索

{
  "mcp": {
    "gh_grep": {
      "type": "remote",
      "url": "https://mcp.grep.app"
    }
  }
}