Cover image
Try Now
2025-04-14

轻巧的C ++ MCP(模型上下文协议)SDK

3 years

Works with Finder

26

Github Watches

2

Github Forks

26

Github Stars

MCP Protocol Framework

Model Context Protocol (MCP) is an open protocol that provides a standardized way for AI models and agents to interact with various resources, tools, and services. This framework implements the core functionality of the MCP protocol, conforming to the 2024-11-05 basic protocol specification.

Core Features

  • JSON-RPC 2.0 Communication: Request/response communication based on JSON-RPC 2.0 standard
  • Resource Abstraction: Standard interfaces for resources such as files, APIs, etc.
  • Tool Registration: Register and call tools with structured parameters
  • Extensible Architecture: Easy to extend with new resource types and tools
  • Multi-Transport Support: Supports HTTP and standard input/output (stdio) communication methods

How to Build

Example of building with CMake:

cmake -B build
cmake --build build --config Release

Build with tests:

git submodule update --init --recursive # Get GoogleTest

cmake -B build -DMCP_BUILD_TESTS=ON
cmake --build build --config Release

Adopters

Here are some open-source projects that are using this repository.
If you're using it too, feel free to submit a PR to be featured here!

  • humanus.cpp: Lightweight C++ LLM agent framework
  • ...waiting for your contribution...

Components

The MCP C++ library includes the following main components:

Core Components

Client Interface (mcp_client.h)

Defines the abstract interface for MCP clients, which all concrete client implementations inherit from.

SSE Client (mcp_sse_client.h, mcp_sse_client.cpp)

Client implementation that communicates with MCP servers using HTTP and Server-Sent Events (SSE).

Stdio Client (mcp_stdio_client.h, mcp_stdio_client.cpp)

Client implementation that communicates with MCP servers using standard input/output, capable of launching subprocesses and communicating with them.

Message Processing (mcp_message.h, mcp_message.cpp)

Handles serialization and deserialization of JSON-RPC messages.

Tool Management (mcp_tool.h, mcp_tool.cpp)

Manages and invokes MCP tools.

Resource Management (mcp_resource.h, mcp_resource.cpp)

Manages MCP resources.

Server (mcp_server.h, mcp_server.cpp)

Implements MCP server functionality.

Examples

HTTP Server Example (examples/server_example.cpp)

Example MCP server implementation with custom tools:

  • Time tool: Get the current time
  • Calculator tool: Perform mathematical operations
  • Echo tool: Process and analyze text
  • Greeting tool: Returns Hello, + input name + !, defaults to Hello, World!

HTTP Client Example (examples/client_example.cpp)

Example MCP client connecting to a server:

  • Get server information
  • List available tools
  • Call tools with parameters
  • Access resources

Stdio Client Example (examples/stdio_client_example.cpp)

Demonstrates how to use the stdio client to communicate with a local server:

  • Launch a local server process
  • Access filesystem resources
  • Call server tools

How to Use

Setting up an HTTP Server

// Create and configure the server
mcp::server server("localhost", 8080); // Host and port
server.set_server_info("MCP Example Server", "0.1.0"); // Name and version

// Register tools
mcp::json hello_handler(const mcp::json& params, const std::string /* session_id */) {
    std::string name = params.contains("name") ? params["name"].get<std::string>() : "World";

    // Server will catch exceptions and return error contents
    // For example, you can use `throw mcp::mcp_exception(mcp::error_code::invalid_params, "Invalid name");` to report an error

    // Content should be a JSON array, see: https://modelcontextprotocol.io/specification/2024-11-05/server/tools#tool-result
    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);

// Register resources
auto file_resource = std::make_shared<mcp::file_resource>("<file_path>");
server.register_resource("file://<file_path>", file_resource);

// Start the server
server.start(true);  // Blocking mode

Creating an HTTP Client

// Connect to the server
mcp::sse_client client("localhost", 8080);

// Initialize the connection
client.initialize("My Client", "1.0.0");

// Call a tool
mcp::json params = {
    {"name", "Client"}
};

mcp::json result = client.call_tool("hello", params);

Using the SSE Client

The SSE client uses HTTP and Server-Sent Events (SSE) to communicate with MCP servers. This is a communication method based on Web standards, suitable for communicating with servers that support HTTP/SSE.

#include "mcp_sse_client.h"

// Create a client, specifying the server address and port
mcp::sse_client client("localhost", 8080);
// Or use a base URL
// mcp::sse_client client("http://localhost:8080");

// Set an authentication token (if needed)
client.set_auth_token("your_auth_token");

// Set custom request headers (if needed)
client.set_header("X-Custom-Header", "value");

// Initialize the client
if (!client.initialize("My Client", "1.0.0")) {
    // Handle initialization failure
}

// Call a tool
json result = client.call_tool("tool_name", {
    {"param1", "value1"},
    {"param2", 42}
});

Using the Stdio Client

The Stdio client can communicate with any MCP server that supports stdio transport, such as:

  • @modelcontextprotocol/server-everything - Example server
  • @modelcontextprotocol/server-filesystem - Filesystem server
  • Other MCP servers that support stdio transport
#include "mcp_stdio_client.h"

// Create a client, specifying the server command
mcp::stdio_client client("npx -y @modelcontextprotocol/server-everything");
// mcp::stdio_client client("npx -y @modelcontextprotocol/server-filesystem /path/to/directory");

// Initialize the client
if (!client.initialize("My Client", "1.0.0")) {
    // Handle initialization failure
}

// Access resources
json resources = client.list_resources();
json content = client.read_resource("resource://uri");

// Call a tool
json result = client.call_tool("tool_name", {
    {"param1", "value1"},
    {"param2", "value2"}
});

License

This framework is provided under the MIT license. For details, please see the LICENSE file.

相关推荐

  • av
  • 毫不费力地使用一个命令运行LLM后端,API,前端和服务。

  • 1Panel-dev
  • 🔥1Panel提供了直观的Web接口和MCP服务器,用于在Linux服务器上管理网站,文件,容器,数据库和LLMS。

  • WangRongsheng
  • 🧑‍🚀 llm 资料总结(数据处理、模型训练、模型部署、 o1 模型、mcp 、小语言模型、视觉语言模型)|摘要世界上最好的LLM资源。

  • sigoden
  • 使用普通的bash/javascript/python函数轻松创建LLM工具和代理。

  • RockChinQ
  • 😎简单易用、🧩丰富生态 -大模型原生即时通信机器人平台| 适配QQ / 微信(企业微信、个人微信) /飞书 /钉钉 / discord / telegram / slack等平台| 支持chatgpt,deepseek,dify,claude,基于LLM的即时消息机器人平台,支持Discord,Telegram,微信,Lark,Dingtalk,QQ,Slack

  • rulego
  • ⛓️Rulego是一种轻巧,高性能,嵌入式,下一代组件编排规则引擎框架。

  • dmayboroda
  • 带有可配置容器的本地对话抹布

  • evilsocket
  • 简单的代理开发套件。

  • modelscope
  • 开始以更轻松的方式开始构建具有LLM授权的多代理应用程序。

  • ragu6963
  • yamadashy
  • 📦ropomix(以前是repopack)是一种功能强大的工具,可将整个存储库包装到一个单一的AI友好文件中。非常适合当您需要将代码库提供给大型语言模型(LLMS)或其他AI工具(例如Claude,Chatgpt,Deepseek,Cllexity,Gemini,Gemini,Gemma,Gemma,Lllama,Grok等)等其他AI工具。

    Reviews

    3 (1)
    Avatar
    user_e0sBEHS8
    2025-04-23

    The cpp-mcp application by hkr04 is an excellent tool for C++ developers. It offers a range of robust features and seamless integration, making coding more efficient and enjoyable. Highly recommended for anyone looking to enhance their programming workflow!