2025-03-13 21:46:49 +08:00
|
|
|
|
# MCP Protocol Framework
|
|
|
|
|
|
|
|
|
|
[Model Context Protocol (MCP)](https://spec.modelcontextprotocol.io/specification/2024-11-05/architecture/) 是一个开放协议,为AI模型和代理提供与各种资源、工具和服务交互的标准化方式。本框架实现了MCP协议的核心功能,符合2024-11-05基本协议规范。
|
|
|
|
|
|
|
|
|
|
## 核心特性
|
|
|
|
|
|
|
|
|
|
- **JSON-RPC 2.0通信**: 基于JSON-RPC 2.0标准的请求/响应通信
|
|
|
|
|
- **资源抽象**: 文件、API等资源的标准接口
|
|
|
|
|
- **工具注册**: 注册和调用带有结构化参数的工具
|
|
|
|
|
- **可扩展架构**: 易于扩展新的资源类型和工具
|
|
|
|
|
- **多传输支持**: 支持HTTP和标准输入/输出(stdio)通信方式
|
|
|
|
|
|
|
|
|
|
## 组件
|
|
|
|
|
|
|
|
|
|
### 核心协议 (`mcp_message.h`, `mcp_message.cpp`)
|
|
|
|
|
|
|
|
|
|
定义MCP的基本结构和类型:
|
|
|
|
|
- 请求/响应处理
|
|
|
|
|
- 错误代码
|
|
|
|
|
- 工具定义
|
|
|
|
|
|
|
|
|
|
### HTTP服务器 (`mcp_server.h`, `mcp_server.cpp`)
|
|
|
|
|
|
|
|
|
|
实现一个HTTP服务器,暴露MCP资源和工具:
|
|
|
|
|
- 在特定路径注册资源
|
|
|
|
|
- 注册带有处理程序的工具
|
|
|
|
|
- 处理传入的HTTP请求
|
|
|
|
|
- 将请求路由到适当的资源
|
|
|
|
|
|
|
|
|
|
### 客户端
|
|
|
|
|
|
|
|
|
|
#### HTTP客户端 (`mcp_client.h`, `mcp_client.cpp`)
|
|
|
|
|
|
|
|
|
|
实现连接到HTTP MCP服务器的HTTP客户端,符合SSE通信规范。
|
|
|
|
|
|
2025-03-13 21:50:35 +08:00
|
|
|
|
可使用[MCP Inspector](https://github.com/modelcontextprotocol/inspector)连接并测试:
|
|
|
|
|

|
|
|
|
|
|
2025-03-13 21:46:49 +08:00
|
|
|
|
#### Stdio客户端 (`mcp_stdio_client.h`, `mcp_stdio_client.cpp`)
|
|
|
|
|
|
|
|
|
|
通过标准输入/输出与MCP服务器通信的客户端:
|
|
|
|
|
- 启动本地服务器进程
|
|
|
|
|
- 通过管道进行通信
|
|
|
|
|
- 支持资源访问和工具调用
|
|
|
|
|
- 适合与本地进程集成
|
|
|
|
|
|
|
|
|
|
### 资源 (`mcp_resource.h`, `mcp_resource.cpp`)
|
|
|
|
|
|
|
|
|
|
提供常见资源类型的基本实现:
|
|
|
|
|
- 文件资源
|
|
|
|
|
- API资源
|
|
|
|
|
|
|
|
|
|
### 工具 (`mcp_tool.h`, `mcp_tool.cpp`)
|
|
|
|
|
|
|
|
|
|
提供工具相关定义与功能:
|
|
|
|
|
- 工具构建器
|
|
|
|
|
|
|
|
|
|
## 示例
|
|
|
|
|
|
|
|
|
|
### HTTP服务器示例 (`examples/server_example.cpp`)
|
|
|
|
|
|
|
|
|
|
MCP服务器实现示例,带有自定义工具:
|
|
|
|
|
- 时间工具: 获取当前时间
|
|
|
|
|
- 计算器工具: 执行数学运算
|
|
|
|
|
- 回显工具: 处理和分析文本
|
|
|
|
|
- 招呼工具:返回`Hello, `+传入名字+`!`,默认返回`Hello, World!`
|
|
|
|
|
|
|
|
|
|
### HTTP客户端示例 (`examples/client_example.cpp`)
|
|
|
|
|
|
|
|
|
|
连接到服务器的MCP客户端示例:
|
|
|
|
|
- 获取服务器信息
|
|
|
|
|
- 列出可用工具
|
|
|
|
|
- 使用参数调用工具
|
|
|
|
|
- 访问资源
|
|
|
|
|
|
|
|
|
|
### Stdio客户端示例 (`examples/stdio_client_example.cpp`)
|
|
|
|
|
|
|
|
|
|
展示如何使用stdio客户端与本地服务器通信:
|
|
|
|
|
- 启动本地服务器进程
|
|
|
|
|
- 访问文件系统资源
|
|
|
|
|
- 调用服务器工具
|
|
|
|
|
|
|
|
|
|
## 如何使用
|
|
|
|
|
|
|
|
|
|
### 设置HTTP服务器
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
// 创建并配置服务器
|
|
|
|
|
mcp::server server("localhost", 8080);
|
|
|
|
|
server.set_server_info("MCP Example Server", "2024-11-05");
|
|
|
|
|
|
|
|
|
|
// 注册工具
|
|
|
|
|
mcp::json hello_handler(const mcp::json& params) {
|
|
|
|
|
std::string name = params.contains("name") ? params["name"].get<std::string>() : "World";
|
|
|
|
|
return {
|
|
|
|
|
{
|
|
|
|
|
{"type", "text"},
|
|
|
|
|
{"text", "Hello, " + name + "!"}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mcp::tool hello_tool = mcp::tool_builder("hello")
|
|
|
|
|
.with_description("Say hello")
|
|
|
|
|
.with_string_param("name", "Name to say hello to", "World")
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
server.register_tool(hello_tool, hello_handler);
|
|
|
|
|
|
|
|
|
|
// 注册资源
|
|
|
|
|
auto file_resource = std::make_shared<mcp::file_resource>("<file_path>");
|
|
|
|
|
server.register_resource("file://<file_path>", file_resource);
|
|
|
|
|
|
|
|
|
|
// 启动服务器
|
|
|
|
|
server.start(true); // 阻塞模式
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 创建HTTP客户端
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
// 连接到服务器
|
|
|
|
|
mcp::client client("localhost", 8080);
|
|
|
|
|
|
|
|
|
|
// 初始化连接
|
|
|
|
|
client.initialize("My Client", "1.0.0");
|
|
|
|
|
|
|
|
|
|
// 调用工具
|
|
|
|
|
mcp::json params = {
|
|
|
|
|
{"name", "Client"}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mcp::json result = client.call_tool("hello", params);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 使用Stdio客户端
|
|
|
|
|
|
|
|
|
|
Stdio客户端可以与任何支持stdio传输的MCP服务器进行通信,例如:
|
|
|
|
|
|
|
|
|
|
- @modelcontextprotocol/server-everything - 示例服务器
|
|
|
|
|
- @modelcontextprotocol/server-filesystem - 文件系统服务器
|
|
|
|
|
- 其他支持stdio传输的MCP服务器
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#include "mcp_stdio_client.h"
|
|
|
|
|
|
|
|
|
|
// 创建客户端,指定服务器命令
|
|
|
|
|
mcp::stdio_client client("npx -y @modelcontextprotocol/server-everything");
|
|
|
|
|
// mcp::stdio_client client("npx -y @modelcontextprotocol/server-filesystem /path/to/directory");
|
|
|
|
|
|
|
|
|
|
// 初始化客户端
|
|
|
|
|
if (!client.initialize("My Client", "1.0.0")) {
|
|
|
|
|
// 初始化失败处理
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 访问资源
|
|
|
|
|
json resources = client.list_resources();
|
|
|
|
|
json content = client.read_resource("resource://uri");
|
|
|
|
|
|
|
|
|
|
// 调用工具
|
|
|
|
|
json result = client.call_tool("tool_name", {
|
|
|
|
|
{"param1", "value1"},
|
|
|
|
|
{"param2", "value2"}
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 构建框架
|
|
|
|
|
|
|
|
|
|
框架依赖以下库:
|
|
|
|
|
- httplib.h - HTTP服务器和客户端
|
|
|
|
|
- json.hpp - JSON解析和生成
|
|
|
|
|
- gtest - 测试
|
|
|
|
|
|
|
|
|
|
所有依赖项都包含在仓库中。
|
|
|
|
|
|
|
|
|
|
使用CMake构建示例:
|
|
|
|
|
```bash
|
|
|
|
|
cmake -B build
|
|
|
|
|
cmake --build build --config Release
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 许可证
|
|
|
|
|
|
|
|
|
|
本框架根据MIT许可证提供。有关详细信息,请参阅LICENSE文件。
|