opencode-mcp-bridge

opencode-mcp-bridge

An MCP server that lets any AI tool delegate tasks to the OpenCode CLI as a subagent, with multi-session orchestration, event tracking, and task contracts.

Category
访问服务器

README

opencode-mcp-bridge

opencode-mcp-bridge Architecture Infographic

A Model Context Protocol (MCP) server that lets any AI tool (Claude Code, Antigravity, VS Code, Cursor, Windsurf, JetBrains, Claude Desktop, etc.) delegate tasks to the OpenCode CLI as a subagent — driven entirely through OpenCode's own opencode serve HTTP/SSE API, with async push events, task contracts, context tracking, checkpoints, and multi-session orchestration.

Agent instructions live in AGENTS.md — or just call the opencode_guide tool once this server is registered; it returns the same guide live from the running server's actual state.

Architecture

The bridge talks to a single, long-lived opencode serve process over plain HTTP:

  • One shared process, unlimited sessions. server-manager.ts spawns and singleton-manages opencode serve (state tracked in ~/.opencode-bridge/server.json); every opencode_start call just creates a lightweight session on that one server instead of launching a new CLI process per task. No node-pty native binding, no per-task re-authentication.
  • One event stream, every session. A single SSE connection to /api/event carries events for all sessions, tagged by durable.aggregateID; http-bridge.ts fans them out to the right local session (session.next.step.started/ended/failed, session.next.tool.called, session.next.text.delta, …) and derives status (running / idle / waiting_input / error) from them — no ANSI scraping, no idle-timer heuristics.
  • Self-healing. OpenCode's HTTP API does not surface session-runner failures (e.g. a model resolution error) as API-visible errors or events — a failed session just sits at running forever, indistinguishable from working. The bridge tails OpenCode's own internal log (~/.local/share/opencode/log/opencode.log) to detect these failures immediately, and runs a stall watchdog (zero tokens generated N seconds after a prompt) as a second line of defense. Because a single such failure has been observed to wedge that server process's entire turn queue, detecting one triggers an automatic restart of opencode serve so subsequent sessions aren't collateral damage.

Features

  • ⚡ Async Events: Writes state transitions (running, idle, waiting_input, error, stale, contract_violated) to ~/.opencode-mcp-events.jsonl.
  • 🔀 Multi-Session Orchestration: Run up to 3 concurrent OpenCode sessions with dependency ordering (opencode_batch).
  • 🗂️ Context Bucket: Tracks every file creation/modification reported by the session's event stream.
  • 🛡️ Task Contracts: Enforce duration limits, path scope restrictions, file count caps, and forbidden commands.
  • 📸 Checkpoints & Recovery: Auto-save state snapshots every 60s and retry failed sessions from any checkpoint.
  • 🎯 Pre-Flight Classifier: Score incoming tasks to decide whether to delegate and recommend the best model.
  • 🩺 Failure visibility: opencode_health exposes last_error — the real underlying exception from OpenCode's runner, not just a generic hang.
  • 🖥️ Server introspection: opencode_server_health / opencode_server_restart expose and control the underlying opencode serve process itself, separate from any individual session.
  • 📖 Self-documenting: opencode_guide returns a complete, live behavioral guide (model selection, workflow, gotchas) generated from the running server's actual state — works identically in any MCP host.

Prerequisites

  • Node.js v18+
  • OpenCode CLI installed and authenticated (opencode auth login) — run opencode --version to confirm it's on PATH.

Quick Start

  1. Clone and build:

    git clone https://github.com/Dheerax/opencode_mcp.git
    cd opencode_mcp
    npm install
    npm run build
    
  2. Register as an MCP server:

    • Claude Code:
      claude mcp add -s user opencode-mcp-bridge -- node /absolute/path/to/opencode_mcp/dist/index.js
      
    • Antigravity / VS Code / Cursor / Windsurf: Add to your MCP server configuration:
      {
        "mcpServers": {
          "opencode-mcp-bridge": {
            "command": "node",
            "args": ["/absolute/path/to/opencode_mcp/dist/index.js"]
          }
        }
      }
      
  3. Run Setup: Invoke the opencode_setup tool once from your host agent to generate workspace integration instructions, then call opencode_guide to get the full usage guide.

All tools

See AGENTS.md for the complete tool reference table and workflow guide — or call the opencode_guide MCP tool directly, which returns the same content live.

Troubleshooting

If a session's status stays error immediately and opencode_health's last_error mentions ModelUnavailableError for a provider you expect to work (e.g. a google/* model), the provider's cached OAuth token has likely expired. Re-authenticate it directly:

opencode auth logout <provider>
opencode auth login <provider>

This is an OpenCode/provider-auth issue, not something the bridge can fix by itself — it can only detect it, report it via last_error, and recover the server process so it doesn't stay wedged for other sessions.

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选