Configure OpenCode MCP Server within OpenCode itself - self-hosted configuration.
OpenCode can use its own MCP server! This creates a recursive setup where OpenCode can delegate to itself as a subagent with different models or agents.
- OpenCode installed
- OpenCode server running (
opencode serveor TUI active) - API keys configured via
/connect
Add to your opencode.json configuration file:
Project-level: ./opencode.json
Global: ~/.config/opencode/opencode.json
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"opencode": {
"type": "local",
"command": ["node", "/path/to/opencode-mcp/dist/index.js"],
"env": {
"OPENCODE_SERVER_URL": "http://localhost:4096"
},
"enabled": true
}
}
}Start OpenCode with MCP server configured:
# Start OpenCode server
opencode serve
# In another terminal, run OpenCode TUI with MCP
opencodeThen in the TUI, use /mcp command to manage servers.
{
"mcp": {
"opencode": {
"type": "local",
"command": ["node", "/path/to/opencode-mcp/dist/index.js"],
"enabled": true
}
}
}{
"mcp": {
"opencode": {
"type": "local",
"command": ["node", "/path/to/opencode-mcp/dist/index.js"],
"env": {
"OPENCODE_SERVER_URL": "http://localhost:4096",
"OPENCODE_DEFAULT_MODEL": "anthropic/claude-sonnet-4"
},
"enabled": true
}
}
}For HTTP transport, configure as a remote server:
{
"mcp": {
"opencode": {
"type": "remote",
"url": "http://localhost:3000/mcp",
"enabled": true
}
}
}{
"mcp": {
"opencode": {
"type": "remote",
"url": "https://your-server.com/mcp",
"headers": {
"Authorization": "Bearer ${env:OPENCODE_API_KEY}"
},
"enabled": true
}
}
}To use HTTP transport, start the MCP server separately:
# Start OpenCode server
opencode serve --port 4096
# Start OpenCode MCP server (HTTP mode)
MCP_TRANSPORT=http MCP_HTTP_PORT=3000 node /path/to/opencode-mcp/dist/index.jsOr with environment variables:
export OPENCODE_SERVER_URL=http://localhost:4096
export MCP_TRANSPORT=http
export MCP_HTTP_PORT=3000
node /path/to/opencode-mcp/dist/index.jsIn the OpenCode TUI:
Use opencode_run to implement a rate limiter middleware
Use opencode_agent_delegate to have the plan agent review the code
# Run a prompt through OpenCode
opencode run "Use opencode_run to implement a rate limiter"Use different models/agents for subtasks:
Use opencode_agent_delegate with model anthropic/claude-opus-4 to analyze the security implications
Control which tools are available:
{
"mcp": {
"opencode": {
"type": "local",
"command": ["node", "/path/to/opencode-mcp/dist/index.js"],
"enabled": true
}
},
"tools": {
"opencode_run": true,
"opencode_session_*": true,
"opencode_file_*": true,
"opencode_agent_*": true
}
}- Start OpenCode TUI
- Run
/mcpcommand - Verify
opencodeserver is listed - Check tool count (21 tools)
# Check OpenCode server is running
curl http://localhost:4096/health
# Check MCP server starts
node /path/to/opencode-mcp/dist/index.js- Check
enabled: truein config - Restart OpenCode TUI
- Check
/mcpoutput for errors
# Use different ports
opencode serve --port 4097
# Update MCP config
OPENCODE_SERVER_URL=http://localhost:4097 node /path/to/opencode-mcp/dist/index.js| Scope | Path |
|---|---|
| Global | ~/.config/opencode/opencode.json |
| Project | ./opencode.json |
{
"$schema": "https://opencode.ai/config.json",
"model": "anthropic/claude-sonnet-4",
"mcp": {
"opencode": {
"type": "local",
"command": ["node", "/path/to/opencode-mcp/dist/index.js"],
"env": {
"OPENCODE_SERVER_URL": "http://localhost:4096",
"OPENCODE_DEFAULT_MODEL": "anthropic/claude-sonnet-4"
},
"enabled": true
}
},
"tools": {
"opencode_*": true
}
}