Opencode Bridge事件转发器
该项目提供了从OpencodeBridgeneneneba API捕获事件并将其转发到后台代理进行处理的脚本。它还包括一个MCP(模型上下文协议)服务器,为AI助手提供定制工具。
文件
event-forwarder.js-事件转发器的基本实现(ES模块版本)opencode-event-forwarder.js-具有事件缓冲和重试逻辑的更完整的实现opencode-api-integration.js-显示如何与OpencodeBridge API集成的示例test-event-forwarder.js-验证事件转发功能的测试脚本mcp_server.py-具有事件转发功能的Opencode Bridge MCP服务器opencode.json-定义了事件转发器代理的Opencode配置文件.env-网桥和事件转发器的环境变量start-bridge-with-agent.sh-启动MCP服务器和模拟代理的脚本MCP_TOOLS.md-Opencode Bridge MCP工具的综合文档
如何使用
先决条件
- Node.js(建议使用v14或更高版本)
- npm或纱线
- 访问开放代码桥API
安装
- 克隆此存储库
- 安装依赖项:
npm install axios配置
设置以下环境变量以配置事件转发器:
AGENT_ENDPOINT-代理API终结点的URL(默认值:http://localhost:3000/agent)
运行事件转发器
# Run the basic event forwarder
node event-forwarder.js
# Run the more complete event forwarder
node opencode-event-forwarder.js
# Run the Opencode Bridge with event forwarding
./start-bridge-with-agent.sh测试
# Run the test script
node test-event-forwarder.js开放代码集成
该项目已配置为通过以下方式使用Opencode opencode.json 配置文件:
{
"mcp": {
"opencode-bridge": {
"type": "local",
"command": ["bash", "-c", "cd /root/code/project-brainsurgeon && . venv/bin/activate && python mcp_server.py"],
"enabled": true,
"description": "Provides custom MCP tools for TUI interaction and enhanced file operations. Tools use prefix 'opencode-bridge_'. See MCP_TOOLS.md for complete documentation."
},
"event-forwarder": {
"type": "local",
"command": ["node", "/root/code/project-brainsurgeon/opencode-event-forwarder.js"],
"enabled": true,
"env": {
"AGENT_ENDPOINT": "http://localhost:3000/agent"
}
}
}
}此配置:
- 运行Opencode Bridge MCP服务器
- 运行事件转发器代理,该代理侦听事件并将其转发到指定的端点
与Opencode Bridge API集成
与OpencodeBridge API的实际集成将取决于API的公开方式。在当前实现中,我们使用的占位符函数需要替换为实际的API调用。
在实际实现中,您将:
- 导入或初始化Opencode Bridge客户端
- 设置事件侦听器或轮询机制
- 将事件转发给您的代理
看 opencode-api-integration.js 有关如何实现这一点的更多详细信息。
开放代码桥MCP工具
这 opencode-bridge MCP服务器提供定制工具,人工智能助手可以使用这些工具与TUI交互并执行各种操作。这些工具使用前缀 opencode-bridge_.
有关综合文档,请参阅 MCP_TOOLS.md.
AI助手中的示例用法
以下是如何在AI助手中使用开源桥接工具的示例:
获得TUI课程
// Example: Get all TUI sessions
const sessions = await opencode-bridge_tui_get_sessions();
// Returns a list of all TUI sessions读取和写入文件
// Example: Read a file
const fileContent = await opencode-bridge_read_file({
filePath: "/path/to/file.txt"
});
// Example: Write to a file
await opencode-bridge_write_file({
filePath: "/path/to/new-file.txt",
content: "This is the file content"
});搜索文件
// Example: Find all Python files
const pythonFiles = await opencode-bridge_glob_files({
pattern: "**/*.py"
});
// Example: Search for text in files
const searchResults = await opencode-bridge_grep_search({
pattern: "function getUser",
include: "*.js"
});管理待办事项列表
// Example: Create a todo list
await opencode-bridge_todowrite({
todos: [
{
content: "Task 1",
status: "pending",
priority: "high",
id: "1"
},
{
content: "Task 2",
status: "completed",
priority: "medium",
id: "2"
}
]
});
// Example: Read the todo list
const todos = await opencode-bridge_todoread();后台代理
事件转发器将事件转发到后台代理API。代理可以是任何接受带有JSON有效载荷的HTTP POST请求的服务。有效载荷格式为:
{
"event": {
"type": "event.type",
"properties": {
"timestamp": 1758551400574,
"data": { ... }
}
}
}或者对于批量事件:
{
"events": [
{
"type": "event.type",
"properties": { ... }
},
...
]
}发展
添加新功能
要向事件转发器添加新功能,请执行以下操作:
- 分叉此存储库
- 实施您的更改
- 添加测试
- 提交拉取请求
建筑
事件转发器使用简单的架构:
- 事件捕获-从Opencode Bridge API捕获事件
- 事件缓冲-缓冲事件以优化转发
- 事件转发-将事件转发到后台代理
- 错误处理-处理错误和重试失败的请求
添加新的MCP工具
要向Opencode Bridge MCP服务器添加新工具,请执行以下操作:
- 打开
mcp_server.py - 使用添加新功能
@app.tool()装饰器 - 实现工具的功能
- 使用以下命令重新启动MCP服务器
./start_mcp_server.sh - 更新
MCP_TOOLS.md带有新工具的文档
