opencode-agent-mcp
MCP server that exposes independent OpenCode AI coding agents to upstream agents with multi-workspace concurrent routing, enabling session management and message exchange across multiple repositories via stdio JSON-RPC.
README
opencode-agent-mcp
MCP server that exposes independent OpenCode AI coding agents to upstream Agents (Claude Desktop, Cursor, opencode TUI, custom) over stdio JSON-RPC, with multi-workspace concurrent routing.
Full product specification:
opencode-agent-mcp.md
Overview
In polyglot or monorepo-style codebases, a single fault often spans multiple projects (e.g. a frontend interaction that depends on a backend API and a native mobile shell). The conventional "spawn a subagent to look at the other repo" pattern fails because the subagent inherits the parent agent's workspace, MCP servers, and skills — it does not load the target project's own conventions.
opencode-agent-mcp addresses this by exposing OpenCode as a multi-workspace MCP server:
- Upstream agents register any number of workspaces via
set_workspace({name, cwd}). - Each workspace maps to a dedicated
OpencodeClientinstance, scoped by thex-opencode-directoryheader so the OpenCode host routes requests to the correct project. - Sessions are created, messaged, cancelled, and queried per workspace, with full isolation between workspaces and concurrency within a workspace.
- When the inner agent requires approval or clarification, those events are surfaced back to the upstream agent via blocking MCP tools (
wait_for_question/answer_question) for it to decide.
This server is an interim MCP implementation of a more general multi-workspace A2A pattern. The current schema is designed to be migratable: once A2A stabilises, the same conceptual primitives map onto A2A's task / message / artifact triples, and the MCP server can be replaced by an A2A server without breaking the upstream contract.
| Dimension | Current MCP implementation | Target A2A model |
|---|---|---|
| Topology | Parent → child (client → server) | Peer-to-peer |
| Question flow | Blocking MCP tool emulation | Native request/response |
| Protocol surface | JSON-RPC + SSE + blocking tool | Unified A2A message/artifact/task |
| Lifecycle | Co-hosted with upstream agent | Independent per agent |
Quick Start
The package is published on npm and resolvable via npx. No global install is required.
# Verify the package is reachable
npm view opencode-agent-mcp version
# Launch the MCP server directly (foreground; used by Claude Desktop / opencode TUI)
npx -y opencode-agent-mcp
To make the server reachable from Claude Desktop, Cursor, or the opencode TUI, point the MCP client at npx -y opencode-agent-mcp with the appropriate environment variables. See Integration for concrete mcpServers / mcp config snippets.
Pre-flight
npx -y opencode-agent-mcp does not start an OpenCode host on its own — it expects an existing opencode serve (external host mode) or spawns one itself (managed mode). Confirm the desired mode by inspecting the environment variables in Configuration before launching the client.
Installation
Two installation paths are supported; npx is preferred unless the source needs to be modified locally.
Option A — npx (recommended for end users)
# Implicit install via npx on first invocation
npx -y opencode-agent-mcp
The package is fetched to the npx cache on first use and re-used on subsequent invocations. To pin a specific version:
npx -y opencode-agent-mcp@0.1.0
Option B — From source (for development or local modification)
git clone <repository-url>
cd opencode-agent-mcp
pnpm install
pnpm run build
node build/index.js
Requirements: Node.js ≥ 18.18.0, pnpm ≥ 8 (or npm ≥ 9 with npm install && npm run build).
Configuration
The server reads the following environment variables. All paths must be absolute on the host.
| Variable | Default | Description |
|---|---|---|
OPENCODE_URL |
(unset) | When set, the server runs in external host mode and connects to the given opencode serve URL. When unset, managed mode is used and the server spawns opencode serve itself. |
OPENCODE_BIN |
opencode |
Path to the opencode executable (managed mode only). |
OPENCODE_PORT |
4096 |
Port for the managed host (managed mode only). |
OPENCODE_HOSTNAME |
127.0.0.1 |
Bind address for the managed host (managed mode only). |
OPENCODE_DEFAULT_CWD |
process CWD | Initial workspace registered as default. |
MCP_LOG_LEVEL |
info |
One of debug / info / warn / error. |
MCP_LOG_FILE |
(stderr) | Optional file path for log output; default is stderr. |
Running
The MCP server speaks stdio JSON-RPC. It is normally launched by an MCP client (Claude Desktop, opencode TUI, etc.) rather than by hand. The sections below assume the client has been configured with one of the snippets in Integration.
For manual smoke-testing:
# External host mode — OpenCode host must be running on :4096
OPENCODE_URL=http://127.0.0.1:4096 \
OPENCODE_DEFAULT_CWD="C:/path/to/aggregated-project" \
npx -y opencode-agent-mcp
# Managed mode — server spawns `opencode serve` itself
OPENCODE_DEFAULT_CWD="C:/path/to/project" \
npx -y opencode-agent-mcp
In managed mode the server spawns opencode serve on startup and terminates it on shutdown. The host process is not reused across MCP restarts.
Integration
The configuration below assumes the package is installed via npx (no global install required). For source installs, replace npx with node and point args at build/index.js.
Claude Desktop
Edit claude_desktop_config.json (Windows: %APPDATA%\Claude\claude_desktop_config.json):
{
"mcpServers": {
"opencode-agent": {
"command": "npx",
"args": ["-y", "opencode-agent-mcp"],
"env": {
"OPENCODE_URL": "http://127.0.0.1:4096",
"OPENCODE_DEFAULT_CWD": "C:/path/to/aggregated-project"
}
}
}
}
opencode TUI
Edit ~/.config/opencode/opencode.json:
{
"mcp": {
"opencode-agent": {
"type": "local",
"command": ["npx", "-y", "opencode-agent-mcp"],
"environment": {
"OPENCODE_URL": "http://127.0.0.1:4096"
}
}
}
}
Custom agent (Node.js SDK)
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
const client = new Client(
{ name: "my-agent", version: "0.1.0" },
{ capabilities: {} },
);
await client.connect(new StdioClientTransport({
command: "npx",
args: ["-y", "opencode-agent-mcp"],
env: { OPENCODE_URL: "http://127.0.0.1:4096" },
}));
const { tools } = await client.listTools();
Multi-workspace Routing
Lifecycle
1. set_workspace({name: "backend", cwd: "C:/repo/backend"})
2. set_workspace({name: "h5", cwd: "C:/repo/h5"})
3. set_workspace({name: "app", cwd: "C:/repo/app", default: false})
4. create_session({workspace: "backend", title: "API regression"})
5. create_session({workspace: "h5", title: "Button handler"})
6. create_session({workspace: "app", title: "Native bridge"})
7. send_message({session_id: <h5_session>, text: "..."})
→ the server resolves the session back to its workspace ("h5")
and dispatches the request via the h5-scoped OpencodeClient
Semantics
- The
workspaceparameter accepts a workspace name registered viaset_workspace, not a rawcwd. When omitted, the default workspace is used. - A
(name, cwd)pair already registered is reused (idempotent). Registering the samenamewith a differentcwdraisesWORKSPACE_NOT_REGISTERED. - One
OpencodeClientis instantiated per registered workspace, distinguished by thex-opencode-directoryHTTP header. SessionStorerecordssession_id → workspace_name. Subsequent operations on the session (send_message,cancel_session,answer_question) are routed by that mapping without requiring the caller to re-supplyworkspace.
Topology
Upstream Agent
│
│ create_session({workspace: "h5"})
▼
MCP server (stdio)
│
│ workspaces.get("h5") → OpencodeClient (x-opencode-directory = /repo/h5)
▼
OpenCode host (127.0.0.1:4096)
│
├─ /session?directory=/C:/repo/backend
├─ /session?directory=/C:/repo/h5
└─ /session?directory=/C:/repo/app
MCP Tools
| Tool | Description |
|---|---|
list_workspaces |
List all registered workspaces and the current default. |
set_workspace |
Register a new workspace (name + cwd) or switch to an existing one by name. |
list_sessions |
List OpenCode sessions under a workspace. |
create_session |
Create a new session in the specified workspace and start its SSE subscription. |
send_message |
Send a message synchronously; returns the assistant text, token usage, and completion status. |
wait_for_message |
Block until the session reaches idle or an error is observed. |
cancel_session |
Abort the session and stop its SSE subscription. |
wait_for_question |
Block until the OpenCode host emits a permission.asked or question.asked event. |
answer_question |
Forward the upstream agent's decision (once / always / reject / {answers}) back to the OpenCode host. |
Tool schemas are generated from the source code and reported by the server at runtime via the MCP tools/list endpoint.
Testing
Two integration scripts are provided; both assume an OpenCode host reachable at 127.0.0.1:4096.
opencode serve --hostname=127.0.0.1 --port=4096 &
node e2e-test.mjs # single-workspace end-to-end
node multi-workspace-test.mjs # concurrent multi-workspace routing
Both scripts use the official @modelcontextprotocol/sdk client and assert on returned content. The test sessions are created against scratch directories under the platform temp path; no real project state is touched.
Troubleshooting
| Symptom | Cause | Resolution |
|---|---|---|
HOST_UNREACHABLE |
opencode serve not reachable |
Start opencode serve --port=4096 or unset OPENCODE_URL to use managed mode. |
CWD_NOT_EXISTS |
Workspace directory missing on disk | Create the directory or correct the cwd path. |
WORKSPACE_NOT_REGISTERED |
Unknown workspace name supplied | Register the workspace via set_workspace({name, cwd}) first. |
SESSION_NOT_FOUND |
session_id not present in the current MCP process |
Create the session and send within the same MCP lifetime. |
SDK returned no session id |
OpenCode host version is too old | Upgrade OpenCode to ≥ 1.18.x. |
| Garbled protocol output on stdout | logger writing to stdout | Ensure MCP_LOG_FILE is unset or set to a file path; the logger must not write to stdout. |
License
Contributing
Issues and pull requests are welcome. Please open an issue before submitting non-trivial changes so the design intent can be discussed.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。