AI代理通信桥接服务

opencode-bridge是一个MCP服务器,支持在不同机器上运行的AI代理通过HTTP中继进行通信,实现远程问答、代理状态查询和代码共享。

  • by minzique
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 minzique/opencode-bridge

使用说明

开放代码桥

一种MCP服务器,使运行在不同机器上的AI代理能够通过HTTP中继相互通信。

它的作用

opencode-bridge 充当在单独机器上运行的OpenCode实例之间的桥梁。它提供了三种MCP工具,使代理能够:

  • 提出问题 发送到远程代理并获取响应
  • 列出可用代理 在线/离线状态
  • 继电器代码 通过共享的git远程实现跨机器协作

建筑

该桥由以下部分组成:

  1. 代理注册表 (agents.json)--将代理名称映射到其OpenCode HTTP端点
  2. 会话存储 (.sessions.json)--跟踪每个代理的活动对话会话
  3. OpenCode客户端 -用于OpenCode REST API的HTTP客户端(/session, /message, /health)
  4. 三个MCP工具:
    • ask_agent --向远程代理发送提示,保持对话的连续性
    • list_agents --检查代理可用性和会话状态
    • relay_code --通过git remote推/拉代码在机器之间共享文件

当你打电话的时候 ask_agent桥:

  1. 在注册表中查找代理的URL
  2. 重用现有会话或创建新会话
  3. 通过HTTP POST将提示发送到远程OpenCode实例
  4. 返回代理的响应和后续问题的会话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 dev

MCP服务器在stdio上运行,可以集成到任何兼容MCP的客户端中。

环境变量

  • BRIDGE_CONFIG --通往 agents.json (默认值: ./agents.json)
  • BRIDGE_SESSION_STORE --会话存储路径(默认值: ./.sessions.json)
  • BRIDGE_GIT_CWD --工作目录 relay_code git操作(默认:当前目录)

用法

询问远程代理

// 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 参数