开放代码桥
一种MCP服务器,使运行在不同机器上的AI代理能够通过HTTP中继相互通信。
它的作用
opencode-bridge 充当在单独机器上运行的OpenCode实例之间的桥梁。它提供了三种MCP工具,使代理能够:
- 提出问题 发送到远程代理并获取响应
- 列出可用代理 在线/离线状态
- 继电器代码 通过共享的git远程实现跨机器协作
建筑
该桥由以下部分组成:
- 代理注册表 (
agents.json)--将代理名称映射到其OpenCode HTTP端点 - 会话存储 (
.sessions.json)--跟踪每个代理的活动对话会话 - OpenCode客户端 -用于OpenCode REST API的HTTP客户端(
/session,/message,/health) - 三个MCP工具:
- ask_agent --向远程代理发送提示,保持对话的连续性 - list_agents --检查代理可用性和会话状态 - relay_code --通过git remote推/拉代码在机器之间共享文件
当你打电话的时候 ask_agent桥:
- 在注册表中查找代理的URL
- 重用现有会话或创建新会话
- 通过HTTP POST将提示发送到远程OpenCode实例
- 返回代理的响应和后续问题的会话ID
设置
先决条件
- Node.js 18+或Bun
- 在要连接的每台计算机上运行的OpenCode实例
- 机器之间的网络访问(HTTP)
安装
cd opencode-bridge
bun install # or npm install配置
创建 agents.json 在项目根目录中:
{
"agents": {
"mac-mini": {
"url": "http://minzis-mac-mini.local:4096",
"description": "Remote Mac Mini agent — Google Gemini, general purpose",
"username": "opencode",
"password": "optional-password"
},
"workstation": {
"url": "http://192.168.1.100:4096",
"description": "Linux workstation with GPU"
}
}
}必填字段:
url--OpenCode实例的完整HTTP URL(包括端口)
可选字段:
description--人类可读的描述username--HTTP基本身份验证用户名(默认为“opencode”)password--HTTP基本身份验证密码
运行服务器
bun run devMCP服务器在stdio上运行,可以集成到任何兼容MCP的客户端中。
环境变量
BRIDGE_CONFIG--通往agents.json(默认值:./agents.json)BRIDGE_SESSION_STORE--会话存储路径(默认值:./.sessions.json)BRIDGE_GIT_CWD--工作目录relay_codegit操作(默认:当前目录)
用法
询问远程代理
// First question (creates new session)
ask_agent({
agent: "mac-mini",
prompt: "What's the weather in Tokyo?"
})
// Follow-up question (reuses session)
ask_agent({
agent: "mac-mini",
prompt: "What about tomorrow?"
})
// Force new session
ask_agent({
agent: "mac-mini",
prompt: "New topic: explain quantum computing",
new_session: true
})退货:
{
"agent": "mac-mini",
"session_id": "ses_abc123...",
"response": "The weather in Tokyo is..."
}列出可用代理
list_agents()退货:
mac-mini: ONLINE (v1.2.3) @ http://minzis-mac-mini.local:4096 — Remote Mac Mini agent
session: ses_abc123...
workstation: OFFLINE @ http://192.168.1.100:4096 — Linux workstation with GPU
no active session机器之间的中继码
// Push local changes to relay remote
relay_code({
direction: "push",
message: "Add new feature",
remote: "relay",
branch: "main"
})
// Pull changes from relay remote
relay_code({
direction: "pull",
remote: "relay",
branch: "main"
})使用案例: 机器1上的代理A编写代码,推送到共享的git remote。机器2上的代理B提取代码并继续工作。
会话如何工作
- 每个代理一次维护一个活动会话(存储在
.sessions.json) - 会话在网桥重新启动后仍然存在
- 如果会话无效(404),则会自动清除
- 使用
new_session: true开始新的对话
发展
# Run tests
bun test
# Type checking
bun run tsc --noEmit项目结构
opencode-bridge/
├── src/
│ ├── index.ts # MCP server entry point
│ ├── registry.ts # Agent registry loader
│ ├── session-store.ts # Session persistence
│ ├── opencode-client.ts # HTTP client for OpenCode API
│ └── tools/
│ ├── ask-agent.ts # ask_agent tool
│ ├── list-agents.ts # list_agents tool
│ └── relay-code.ts # relay_code tool
├── agents.json # Agent registry (user-created)
├── .sessions.json # Session store (auto-generated)
└── package.json故障排除
代理显示离线:
- 检查OpenCode实例是否在目标计算机上运行
- 验证网络连接:
curl http://target-url:4096/global/health - 检查防火墙规则
会话错误(404):
- 远程实例上的会话可能已过期
- 使用
new_session: true创建新会话
relay_code失败:
- 确保已配置git remote:
git remote -v - 检查您是否具有推/拉权限
- 使用验证工作目录
cwd参数
