MCP协议实现示例

展示基于Elixir语言的Model Context Protocol(MCP)实现示例,包含天气/计算器和知识库服务器及客户端演示。

  • by JohnSmall
  • Elixir
  • 本地部署
  • 更新于
在 GitHub 查看 JohnSmall/mcp_ex_examples

使用说明

MCP示例

四个示例项目展示了 mcpex 库——Elixir的实现 模型上下文协议 (MCP)。

例子

项目 运输 描述
服务器示例1 Stdio 带同步工具和资源的天气/计算器服务器
服务器示例2 HTTP 具有异步工具、提示、资源模板和日志记录的知识库服务器
客户端示例1 Both 连接到两个服务器的基本客户端——列表和调用工具,读取资源
客户端示例2 两者都 具有采样回调、分页助手和通知处理的高级客户端

连接拓扑

两个客户端都连接到两个服务器,展示了传输灵活性:

client_example_1 ──stdio──► server_example_1 (weather/calculator)
client_example_1 ──HTTP───► server_example_2 (knowledge base)
client_example_2 ──stdio──► server_example_1 (weather/calculator)
client_example_2 ──HTTP───► server_example_2 (knowledge base)

快速开始

# 1. Start the HTTP server (Terminal 1)
cd server_example_2
mix deps.get && mix run --no-halt

# 2. Run the basic client demo (Terminal 2)
cd client_example_1
mix deps.get && mix run run.exs

# 3. Run the advanced client demo (Terminal 3)
cd client_example_2
mix deps.get && mix run run.exs

stdio服务器(server_example.1)由客户端作为子进程自动启动。

先决条件

  • 灵丹妙药~>1.17
  • 这 mcp_ex 图书馆在 ../mcp_ex (路径依赖性)

能力矩阵

功能 服务器示例_1 服务器示例_2
工具(同步) get_weather,compute --
工具(异步+工具上下文) -- search_notes、create_note、analyze_note、tag_note
资源 config://app 动态(kb://notes/{id})
资源模板 -- kb://notes/{noteId}
提示 -- 总结、提问、起草说明
日志记录 -- 设置日志级别
采样往返 -- analyze_note触发采样
进度通知 -- analyze_note发送进度