使用说明
mcpp-MCP C++库
用于构建模型上下文协议(MCP)客户端和服务器的现代C++库。提供完整的MCP 2025-11-25规范覆盖,包括所有传输、快速JSON-RPC处理和最小的阻塞操作。
特性
- 完全支持MCP 2025-11-25规范:工具、资源、提示、日志记录、采样、根、进度、取消、流
- 多个传输:stdio和流式HTTP(SSE)
- 快速准确:JSON-RPC 2.0兼容最小阻塞操作
- 线程安全:所有公共方法都可以从任何线程安全调用
- 分层API:低级回调核心+高级RAII包装器
- 许可证:MIT用于静态和共享库分发
需求
- C++20编译器(gcc-11+、clang-12+、MSVC 2019+)
- CMake 3.16+
- nlohmann/json 3.11+(包含在第三方/中)
- spdlog 1.8+(可选,用于日志记录)
建筑
快速开始
# Clone the repository
git clone https://github.com/mcpp-project/mcpp.git
cd mcpp
# Configure and build
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
# Run tests
ctest --test-dir build --output-on-failure
# Test with MCP Inspector (requires Node.js)
npx @modelcontextprotocol/inspector connect stdio ./build/examples/inspector_server
# Install (optional)
cmake --install build --prefix /usr/local
构建选项
# Build static library only
cmake -B build -DBUILD_SHARED_LIBS=OFF
# Disable tests
cmake -B build -DMCPP_BUILD_TESTS=OFF
# Disable examples
cmake -B build -DMCPP_BUILD_EXAMPLES=OFF
# Debug build with sanitizers
cmake -B build -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-fsanitize=address -fsanitize=leak -g"
用法
基本服务器
#include "mcpp/server/mcp_server.h"
#include "mcpp/transport/stdio_transport.h"
int main() {
auto transport = std::make_unique<mcpp::StdioTransport>();
mcpp::server::McpServer server(std::move(transport));
// Register a tool
server.get_tool_registry().register_tool(
"calculate",
{{"description", "Perform calculations"}},
[](const nlohmann::json& params) {
double a = params["a"];
double b = params["b"];
return mcpp::server::ToolResult{
{"content", nlohmann::json::array({{
{"type", "text"},
{"text", std::to_string(a + b)}
}})}
};
}
);
// Run server
server.run();
return 0;
}
基本客户端
#include "mcpp/client.h"
#include "mcpp/transport/stdio_transport.h"
int main() {
auto transport = std::make_unique<mcpp::StdioTransport>();
mcpp::McpClient client(std::move(transport));
// Set message handler
client.set_notification_handler([](const nlohmann::json& notification) {
std::cout << "Received: " << notification.dump(2) << std::endl;
});
// Initialize
auto init_result = client.initialize({
{"protocolVersion", "2025-11-25"},
{"capabilities", nlohmann::json::object()},
{"clientInfo", {{"name", "my_client"}, {"version", "1.0"}}}
});
// Call a tool
auto result = client.call_tool("calculate", {{"a", 5}, {"b", 3}});
return 0;
}
例子
检查器服务器
使用MCP Inspector进行测试的MCP服务器示例:
# Build the example
cmake --build build --target inspector_server
# Run with MCP Inspector (opens browser UI)
npx @modelcontextprotocol/inspector connect stdio ./build/examples/inspector_server
检查器服务器演示:
- 工具注册(计算、回声、获取时间)
- 资源服务(文件读取、服务器信息)
- 提示模板(问候语、代码审查)
检验员测试
MCP Inspector是MCP服务器的官方测试工具。它支持交互式UI模式(打开基于浏览器的界面)和可脚本化的CLI模式,用于自动测试。Inspector使用stdio传输与您的服务器通信,通过stdin/stdout发送JSON-RPC消息。
通信流
Inspector Your Server
(Client) (stdio mode)
│ │
│ 1. initialize │
│ ────────────────>│
│ │
│ 2. initialize │
│ <────────────────│
│ │
│ 3. tools/list │
│ ────────────────>│
│ │
│ 4. tools/list │
│ <────────────────│
UI模式(交互式)
测试MCP服务器的最简单方法是使用Inspector的UI模式:
# Build the example server first
cmake --build build --target inspector_server
# Connect via Inspector (opens browser to http://localhost:6274)
npx @modelcontextprotocol/inspector connect stdio ./build/examples/inspector_server
Inspector UI允许您:
- 列出并调用可用工具
- 浏览资源和提示
- 查看服务器功能
- 实时检查JSON-RPC消息
CLI模式(可编写脚本)
对于自动化测试或脚本编写,请使用Inspector的CLI模式:
# List available tools
npx @modelcontextprotocol/inspector --cli ./build/examples/inspector_server --method tools/list
# Call a tool
npx @modelcontextprotocol/inspector --cli ./build/examples/inspector_server \
--method tools/call --tool-name calculate --tool-arg operation=add --tool-arg a=5 --tool-arg b=3
# List resources
npx @modelcontextprotocol/inspector --cli ./build/examples/inspector_server --method resources/list
# Read a resource
npx @modelcontextprotocol/inspector --cli ./build/examples/inspector_server \
--method resources/read --uri file:///path/to/file.txt
初始化握手
MCP要求在大多数操作之前进行初始化握手。检查器会自动处理此问题。对于使用原始stdio进行手动测试,您需要先发送initialize:
{ echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{...}}'; echo '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'; } | ./build/examples/inspector_server
看 测试.md 获取详细的BATS测试指南和编写自定义测试。
测试
# Run all tests
ctest --test-dir build
# Run specific test suites
ctest --test-dir build -L unit # Unit tests only
ctest --test-dir build -L integration # Integration tests only
ctest --test-dir build -L compliance # JSON-RPC compliance tests
# Run with verbose output
ctest --test-dir build --verbose
# Run specific test
./build/tests/mcpp_unit_tests --gtest_filter="JsonRpcRequest.*"
消毒剂测试
# AddressSanitizer + LeakSanitizer
cmake -B build/sanitizer -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-fsanitize=address -fsanitize=leak -fno-omit-frame-pointer -g" \
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address -fsanitize=leak"
cmake --build build/sanitizer
ctest --test-dir build/sanitizer
# ThreadSanitizer (separate build)
cmake -B build/tsan -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-fsanitize=thread -g" \
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=thread"
cmake --build build/tsan
ctest --test-dir build/tsan
安装
# Install to system (requires sudo)
sudo cmake --install build --prefix /usr/local
# Install to custom location
cmake --install build --prefix $HOME/.local
# Use in CMake project
find_package(mcpp REQUIRED)
target_link_libraries(my_app PRIVATE mcpp::mcpp)
项目结构
mcpp/
├── CMakeLists.txt # Root build configuration
├── LICENSE # MIT license
├── README.md # This file
├── src/
│ └── mcpp/ # Public headers
│ ├── api/ # High-level API
│ ├── async/ # Callback types and timeout
│ ├── client/ # Client implementation
│ ├── core/ # JSON-RPC and request tracking
│ ├── content/ # Content types and pagination
│ ├── protocol/ # MCP protocol types
│ ├── server/ # Server implementation
│ ├── transport/ # Transport layer
│ └── util/ # Utilities (logging, retry, etc.)
├── tests/
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ ├── compliance/ # JSON-RPC spec tests
│ └── fixtures/ # Test utilities
├── examples/ # Example programs
└── third_party/ # Vendored dependencies
许可证
MIT许可证-请参阅 许可证 文件以获取详细信息。
贡献
欢迎投稿!拜托:
- 克隆该仓库
- 创建要素分支
- 添加新功能的测试
- 确保所有测试通过
- 提交拉取请求
资源
状态
该项目正在积极开发中。版本0.1.0-alpha旨在全面支持MCP 2025-11-25规范。