MCP C++ SDK(MCP C++软件开发工具包)
C++语言的实现 模型上下文协议 (MCP) 支持完整的标准I/O传输,包括进程创建。
安装
使用 CMake 的 FetchContent 功能
将MCP SDK添加到您的CMake项目中:
include(FetchContent)
FetchContent_Declare(
mcp-sdk
GIT_REPOSITORY https://github.com/MKAbdElrahman/cpp-mcp-sdk.git
GIT_TAG main
)
FetchContent_MakeAvailable(mcp-sdk)
# Link to your target
target_link_libraries(your_target PRIVATE mcp)
# Include headers
target_include_directories(your_target PRIVATE ${mcp-sdk_SOURCE_DIR}/include)手动构建和安装
git clone https://github.com/MKAbdElrahman/cpp-mcp-sdk.git
cd cpp-mcp-sdk
mkdir build && cd build
cmake ..
make
sudo make install # Optional: install system-wide然后在你的 CMakeLists.txt 文件中:
# If installed system-wide
find_package(mcp REQUIRED)
target_link_libraries(your_target PRIVATE mcp)
# Or use local build
target_include_directories(your_target PRIVATE /path/to/cpp-mcp-sdk/include)
target_link_libraries(your_target PRIVATE /path/to/cpp-mcp-sdk/build/libmcp.a pthread)使用方法
客户端 - 启动MCP服务器
#include
using namespace mcp;
// Configure server to spawn
StdioServerParameters params("python");
params.with_args({"my_mcp_server.py"})
.with_env({{"API_KEY", "secret"}})
.with_cwd("/path/to/server");
// Create and connect
auto transport = create_stdio_client_transport(params);
// Communicate
json request = {{"jsonrpc", "2.0"}, {"method", "tools/list"}, {"id", 1}};
transport->write(request);
json response = transport->read();
// Clean shutdown (automatic in destructor)
transport->close();服务器 - 处理标准I/O传输
#include
using namespace mcp;
// Use stdin/stdout
StdioConnection conn(std::cin, std::cout);
// Read requests
json request = conn.read();
// Send responses
json response = {{"jsonrpc", "2.0"}, {"id", request["id"]}, {"result", {}}};
conn.write(response);测试
重要总是从……开始构建 build/ 目录,用于保持源代码树的整洁。
建议:从构建目录进行构建
mkdir -p build && cd build
cmake ..
make
ctest --output-on-failure
# For verbose output
ctest --output-on-failure --verbose替代方案:使用Makefile封装器
# From project root (uses build/ directory)
make clean # Clean build
make # Build all
make test # Run tests不要跑 cmake 或者 make 从项目根目录 - 这将把构建产物污染到源代码树中。
特点
- 完全支持MCP协议(协议版本2025-06-18)
- 带有进程生成的stdio传输
- 客户端和服务器端实现
- 工具、资源和提示支持
- 引出支持(或:启发式支持,根据上下文可能有所不同) - 交互式用户输入请求(新增!)
- 线程安全设计
- 全面的测试套件
引出支持(或:引诱支持,但根据上下文,“引出”可能更贴切,因为“elicitation”通常指引出或诱发某种反应或信息)
在工具执行或其他操作过程中,提示功能允许服务器与用户进行交互式请求,以获取结构化信息。
服务器使用情况
#include
#include
// In a tool handler with access to ServerSession*
json schema = {
{"type", "object"},
{"properties", {
{"apiKey", {{"type", "string"}}},
{"timeout", {{"type", "number"}, {"minimum", 1}}},
{"debug", {{"type", "boolean"}}}
}},
{"required", json({"apiKey"})}
};
ElicitRequestParams params;
params.message = "Please provide your API configuration";
params.requestedSchema = schema;
ElicitResult result = session->send_elicit_request(params);
if (result.action == ElicitAction::Accept) {
auto api_key = (*result.content)["apiKey"].get();
// Use configuration...
}客户使用情况
#include
// Define elicitation handler
ClientOptions options;
options.elicitation_handler = [](const ElicitRequestParams& params) {
std::cout << "Server asks: " << params.message << std::endl;
// Collect user input (show UI, prompt, etc.)
ElicitResult result;
result.action = ElicitAction::Accept;
result.content = {
{"apiKey", "sk-test-12345"},
{"timeout", 30},
{"debug", true}
};
return result;
};
// Client automatically advertises elicitation capability
Client client("my-client", "1.0.0", options);见 docs/ELICITATION.md(文件名,可翻译为“文档/引出需求.md”或根据具体语境简化为“需求引出说明.md”等) 以获取详细文档和 示例/引出示例.cpp 以完整示例说明。
文档
- 进程生成指南 - 详细的使用方法和API参考
- 引导启发指南(或“引出信息指南”,具体根据上下文确定更贴切的译法) - 交互式用户输入请求
- 示例 - 可运行的代码示例
- 测试 - 全面的测试套件
