Time
Current time lookup and conversion between IANA timezones
MOmodelcontextprotocol/servers92k近 7 天新增 200 个 star
Lightweight C++ MCP (Model Context Protocol) SDK
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 2025-03-26 basic protocol specification.
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
Build with SSL support:
git submodule update --init --recursive # Get GoogleTest
cmake -B build -DMCP_SSL=ON
cmake --build build --config Release
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!
The MCP C++ library includes the following main components:
mcp_client.h)Defines the abstract interface for MCP clients, which all concrete client implementations inherit from.
mcp_sse_client.h, mcp_sse_client.cpp)Client implementation that communicates with MCP servers using HTTP and Server-Sent Events (SSE).
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.
mcp_message.h, mcp_message.cpp)Handles serialization and deserialization of JSON-RPC messages.
mcp_tool.h, mcp_tool.cpp)Manages and invokes MCP tools.
mcp_resource.h, mcp_resource.cpp)Manages MCP resources.
mcp_server.h, mcp_server.cpp)Implements MCP server functionality.
examples/server_example.cpp)Example MCP server implementation over HTTP/SSE with custom tools:
Hello, + input name + !, defaults to Hello, World!examples/stdio_server_example.cpp)Example MCP server implementation over standard input/output (stdio), which is the default transport method for MCP clients like Claude Desktop, Cursor, and OpenCode.
It implements the same core tools but uses server.start_stdio() to communicate via pipes instead of opening an HTTP port.
examples/client_example.cpp)Example MCP client connecting to a server:
examples/stdio_client_example.cpp)Demonstrates how to use the stdio client to communicate with a local server:
examples/agent_example.cpp)| Option | Description |
|---|---|
--base-url | LLM base URL (e.g. https://openrouter.ai) |
--endpoint | LLM endpoint (default to /v1/chat/completions/) |
--api-key | LLM API key |
--model | Model name (e.g. gpt-3.5-turbo) |
--system-prompt | System prompt |
--max-tokens | Maximum number of tokens to generate (default to 2048) |
--temperature | Temperature (default to 0.0) |
--max-steps | Maximum steps calling tools without user input (default to 3) |
Example usage:
./build/examples/agent_example --base-url <base_url> --endpoint <endpoint> --api-key <api_key> --model <model_name>
Note: Remember to compile with -DMCP_SSL=ON when connecting to an https base URL.
// Create and configure the server
mcp::server::configuration srv_conf;
srv_conf.host = "localhost";
srv_conf.port = 8888;
mcp::server server(srv_conf);
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);
// Register prompts
mcp::prompt draft_prompt = mcp::prompt_builder("draft_article")
.with_description("Draft a new article")
.with_argument("topic", "The main topic to write about", true)
.build();
server.register_prompt(draft_prompt, [](const mcp::json& args, const std::string /* session_id */) -> mcp::json {
std::string topic = args.value("topic", "Unknown");
std::string instruction = "Please write an article about " + topic;
// Return messages array as specified in the MCP protocol
return mcp::json::array({{
{"role", "user"},
{"content", {
{"type", "text"},
{"text", instruction}
}}
}});
});
// Start the server
server.start(true); // Blocking mode
// Connect to the server
mcp::sse_client client("http://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);
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("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}
});
The Stdio client can communicate with any MCP server that supports stdio transport, such as:
#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"}
});
openssl genrsa -out ca.key.pem 2048
openssl req -x509 -new -nodes -key ca.key.pem -sha256 -days 1 -out ca.cert.pem -subj "/CN=Test CA"
openssl genrsa -out server.key.pem 2048
openssl req -new -key server.key.pem -out server.csr.pem -subj "/O=TestServer/OU=Dev/CN=localhost"
openssl x509 -req -in server.csr.pem -CA ca.cert.pem -CAkey ca.key.pem -CAcreateserial -out server.cert.pem -days 1 -sha256
mcp::server::configuration srv_conf;
srv_conf.host = "localhost";
srv_conf.port = 8888;
srv_conf.ssl.server_cert_path = "./server.cert.pem";
srv_conf.ssl.server_private_key_path = "./server.key.pem";
mcp::sse_client client("https://localhost:8888");
This framework is provided under the MIT license. For details, please see the LICENSE file.
Current time lookup and conversion between IANA timezones
MOmodelcontextprotocol/servers92k近 7 天新增 200 个 star
Reference server that exercises every MCP feature: tools, resources, prompts, sampling and more
MOmodelcontextprotocol/servers92k近 7 天新增 200 个 star
Free open-source resume builder with remote MCP tools for resumes and job applications.
REreactive-resume/reactive-resume46k近 7 天新增 219 个 star
Puter MCP enables AI tools to interact with Puter: manage files, websites, workers, and more
HEHeyPuter/puter44k近 7 天新增 63 个 star
Open-source B2B and marketplace ecommerce; new stores include an MCP server for AI agents.
SPspree/spree16k近 7 天新增 13 个 star