MCP Tools
Kết nối OpenCode với 200+ MCP servers.
Tổng quan
Model Context Protocol (MCP) là một chuẩn mở cho phép kết nối các công cụ bên ngoài với AI. OpenCode hỗ trợ cả local và remote MCP servers.
Khi đã thêm, MCP tools tự động available cho LLM cùng với các built-in tools.
Lưu ý quan trọng
MCP servers thêm vào context, nên bạn cần cẩn thận chọn những server nào cần enable.
Một số MCP servers như GitHub MCP server có xu hướng thêm nhiều tokens và có thể dễ dàng vượt quá context limit.
Enable MCP Server
Định nghĩa MCP servers trong config opencode.json dưới key mcp:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-mcp-server": {
"type": "local",
"command": ["npx", "-y", "my-mcp-command"],
"enabled": true
}
}
}
Disable server tạm thời bằng enabled: false.
Local MCP Server
Thêm local MCP server với type: "local":
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-local-mcp": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-everything"],
"enabled": true,
"environment": {
"MY_API_KEY": "xxx"
}
}
}
}
Options cho Local MCP
| Option | Type | Bắt buộc | Mô tả |
|---|---|---|---|
type | String | Có | Phải là "local" |
command | Array | Có | Command và arguments để chạy server |
environment | Object | Không | Environment variables |
enabled | Boolean | Không | Enable/disable server |
timeout | Number | Không | Timeout ms (default: 5000) |
Ví dụ: MCP Everything
{
"mcp": {
"mcp_everything": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-everything"]
}
}
}
Sử dụng trong prompt:
use the mcp_everything tool to add 3 and 4
Remote MCP Server
Thêm remote MCP server với type: "remote":
{
"$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"
}
}
}
}
Options cho Remote MCP
| Option | Type | Bắt buộc | Mô tả |
|---|---|---|---|
type | String | Có | Phải là "remote" |
url | String | Có | URL của MCP server |
enabled | Boolean | Không | Enable/disable server |
headers | Object | Không | Headers gửi kèm request |
oauth | Object | Không | OAuth config |
timeout | Number | Không | Timeout ms (default: 5000) |
OAuth Authentication
OpenCode tự động xử lý OAuth cho remote MCP servers. Khi server yêu cầu auth, OpenCode sẽ:
- Detect 401 response và khởi động OAuth flow
- Sử dụng Dynamic Client Registration (RFC 7591) nếu server hỗ trợ
- Lưu tokens an toàn cho các requests sau
Cấu hình OAuth tự động
{
"mcp": {
"my-oauth-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp"
}
}
}
Với Client Credentials
{
"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"
}
}
}
}
Lệnh OAuth
# Authenticate với server
opencode mcp auth my-oauth-server
# Liệt kê servers và auth status
opencode mcp list
# Xóa credentials
opencode mcp logout my-oauth-server
Quản lý MCPs
Global - Enable/Disable tất cả
{
"mcp": {
"my-mcp-foo": { "type": "local", "command": ["..."] },
"my-mcp-bar": { "type": "local", "command": ["..."] }
},
"tools": {
"my-mcp-foo": false,
"my-mcp*": false
}
}
Per Agent
Chỉ enable MCP cho agent cụ thể:
{
"mcp": {
"my-mcp": {
"type": "local",
"command": ["bun", "x", "my-mcp-command"],
"enabled": true
}
},
"tools": {
"my-mcp*": false
},
"agent": {
"my-agent": {
"tools": {
"my-mcp*": true
}
}
}
}
Ví dụ thực tế
Context7 - Search Docs
{
"mcp": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp"
}
}
}
Sử dụng:
Configure Cloudflare Worker script to cache JSON API responses. use context7
Grep by Vercel - Search GitHub Code
{
"mcp": {
"gh_grep": {
"type": "remote",
"url": "https://mcp.grep.app"
}
}
}
Sử dụng:
How to set a custom domain in SST Astro component? use the gh_grep tool
Sentry - Error Tracking
{
"mcp": {
"sentry": {
"type": "remote",
"url": "https://mcp.sentry.dev/mcp",
"oauth": {}
}
}
}
Authenticate:
opencode mcp auth sentry
Sử dụng:
Show me the latest unresolved issues in my project. use sentry
Tips & Best Practices
1. Giới hạn số MCP servers
Mỗi MCP server thêm tools vào context. Chỉ enable những gì thực sự cần.
2. Sử dụng per-agent config
Chỉ enable MCP cho agents cần sử dụng thay vì enable globally.
3. Thêm vào AGENTS.md
Thêm hướng dẫn vào AGENTS.md để agent biết khi nào dùng MCP:
When you need to search docs, use `context7` tools.
If unsure how to do something, use `gh_grep` to search GitHub examples.
4. Kiểm tra timeout
Nếu MCP server chậm, tăng timeout:
{
"mcp": {
"slow-server": {
"type": "remote",
"url": "https://slow-mcp.example.com",
"timeout": 30000
}
}
}