Erlang节点的BeamMCP-MCP桥
一个Elixir项目,将模型上下文协议(MCP)与Erlang分布式cnode连接起来,使MCP客户端能够与远程Erlang节点上运行的服务进行交互。
特性
- MCP C节点集成:使用分布式Erlang cnodes桥接MCP协议
- 工具调用:将MCP客户端的工具调用转发到远程cnodes
- 资源管理:从远程cnode服务中读取资源
- 异步RPC通信:使用Erlang RPC进行节点间通信
- 综合测试:带有Mox模拟框架的完整测试套件
- 前MCP集成:基于最新的ex_mcp主分支构建
项目结构
beam-mcp/
├── lib/
│ └── beam_mcp/
│ ├── application.ex # OTP Application entrypoint
│ ├── cnode_bridge.ex # Main bridge interface
│ ├── cnode_handler.ex # Behaviour for cnode handlers
│ └── cnode_bridge/
│ ├── server.ex # GenServer managing bridge
│ └── supervisor.ex # Supervision tree
├── test/
│ ├── test_helper.exs # Test setup with Mox
│ └── beam_mcp/
│ └── cnode_bridge_test.exs # Bridge tests
├── mix.exs # Project configuration
└── README.md # This file安装
先决条件
- Elixir 1.14或更高版本
- Erlang/OTP 25或更高版本
设置
- 克隆存储库:
git clone https://github.com/V-Sekai-fire/beam-mcp.git
cd beam-mcp- 安装依赖项:
mix deps.get- 编译:
mix compile- 运行测试:
mix test用法
架设通往节点的桥梁
# Start the bridge with connection details
{:ok, bridge_info} = BeamMcp.CnodeBridge.start_bridge(
cnode_name: "my_cnode",
cookie: "secret_erlang_cookie",
cnode_host: "localhost",
mcp_name: "my_bridge",
tools: [
%{name: "tool1", description: "First tool"},
%{name: "tool2", description: "Second tool"}
],
resources: [
%{uri: "file://data", description: "Data resource"}
]
)通过桥梁调用工具
# Call a tool on the remote cnode
{:ok, result} = BeamMcp.CnodeBridge.call_tool("my_bridge", "tool_name", %{
arg1: "value1",
arg2: "value2"
})列出可用工具
# Get tools from the remote cnode
{:ok, tools} = BeamMcp.CnodeBridge.list_tools("my_bridge")
Enum.each(tools, fn tool ->
IO.puts("Tool: #{tool["name"]}")
IO.puts("Description: #{tool["description"]}")
end)阅读资源
# Read a resource from the remote cnode
{:ok, content} = BeamMcp.CnodeBridge.read_resource("my_bridge", "file://data/resource.txt")
IO.puts(content)停止桥
# Stop the bridge
BeamMcp.CnodeBridge.stop_bridge(bridge_info)实现Cnode处理程序
在您的遥控器上,执行 BeamMcp.CnodeHandler 行为:
defmodule MyApp.CnodeHandler do
@behaviour BeamMcp.CnodeHandler
@impl true
def handle_mcp_request(:list_tools) do
[
%{
"name" => "my_tool",
"description" => "A sample tool",
"inputSchema" => %{
"type" => "object",
"properties" => %{
"param" => %{"type" => "string"}
}
}
}
]
end
@impl true
def handle_mcp_request({:call_tool, "my_tool", %{"param" => value}}) do
%{
"content" => [
%{
"type" => "text",
"text" => "Processed: #{value}"
}
]
}
end
@impl true
def handle_mcp_request({:read_resource, uri}) do
case uri do
"file://data" -> %{"content" => "Resource content"}
_ -> {:error, "Resource not found"}
end
end
end测试
该项目包括使用Mox模拟框架的全面测试:
# Run all tests
mix test
# Run tests with verbose output
mix test -v
# Run specific test file
mix test test/beam_mcp/cnode_bridge_test.exs测试结构
测试组织在 test/beam_mcp/cnode_bridge_test.exs 并涵盖:
- 使用必需/可选参数进行网桥初始化
- 工具调用功能
- 工具列表
- 资源阅读
- 不可用cnode的错误处理
依赖项
- ex_mcp (~>0.6.0):模型上下文协议实现
- 莫克斯 (~>1.2):用于测试的模拟库
看 mix.exs 查看完整的依赖关系列表。
建筑
关键组件
- BeamMcp。应用:OTP应用程序监督树
- BeamMcp。CnodeBridge:桥梁运营的主要API
- BeamMcp。CnodeBridge。服务器:GenServer管理单个网桥实例
- BeamMcp。CnodeBridge。主管:网桥服务器的监督树
- BeamMcp。CnodeHandler:cnode请求处理程序的行为模块
通信流
MCP Client
↓
ExMCP.Client/Server
↓
BeamMcp.CnodeBridge (API)
↓
BeamMcp.CnodeBridge.Server (GenServer)
↓
:rpc.call() (Erlang RPC)
↓
Remote Cnode
↓
CnodeHandler (user-implemented)配置
除了标准的Mix项目设置外,不需要额外的配置。
许可证
MIT许可证-有关详细信息,请参阅许可证文件
贡献
欢迎投稿!拜托:
- 分叉存储库
- 创建要素分支
- 提交您的更改
- 推到分支
- 创建拉取请求
