{"id":35094092,"url":"https://github.com/hkr04/cpp-mcp","last_synced_at":"2026-01-01T23:01:05.413Z","repository":{"id":286544333,"uuid":"958413105","full_name":"hkr04/cpp-mcp","owner":"hkr04","description":"Lightweight C++ MCP (Model Context Protocol) SDK","archived":false,"fork":false,"pushed_at":"2025-10-15T11:05:29.000Z","size":558,"stargazers_count":165,"open_issues_count":8,"forks_count":33,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-10-16T04:09:51.429Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hkr04.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-04-01T06:49:56.000Z","updated_at":"2025-10-16T03:52:14.000Z","dependencies_parsed_at":"2025-04-25T10:23:52.440Z","dependency_job_id":"89aeb838-90b5-464e-87d9-1bb4d425e664","html_url":"https://github.com/hkr04/cpp-mcp","commit_stats":null,"previous_names":["hkr04/cpp-mcp"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hkr04/cpp-mcp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hkr04%2Fcpp-mcp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hkr04%2Fcpp-mcp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hkr04%2Fcpp-mcp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hkr04%2Fcpp-mcp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hkr04","download_url":"https://codeload.github.com/hkr04/cpp-mcp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hkr04%2Fcpp-mcp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28165027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2026-01-01T02:00:06.694Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-12-27T15:04:24.311Z","updated_at":"2026-01-01T23:01:05.407Z","avatar_url":"https://github.com/hkr04.png","language":"C++","funding_links":[],"categories":["📚 Projects (1974 total)","Artificial Intelligence"],"sub_categories":["MCP Servers"],"readme":"# MCP Protocol Framework\n\n[Model Context Protocol (MCP)](https://spec.modelcontextprotocol.io/specification/2024-11-05/architecture/) 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.\n\n## Core Features\n\n- **JSON-RPC 2.0 Communication**: Request/response communication based on JSON-RPC 2.0 standard\n- **Resource Abstraction**: Standard interfaces for resources such as files, APIs, etc.\n- **Tool Registration**: Register and call tools with structured parameters\n- **Extensible Architecture**: Easy to extend with new resource types and tools\n- **Multi-Transport Support**: Supports HTTP and standard input/output (stdio) communication methods\n\n## How to Build\n\nExample of building with CMake:\n```bash\ncmake -B build\ncmake --build build --config Release\n```\n\nBuild with tests:\n```\ngit submodule update --init --recursive # Get GoogleTest\n\ncmake -B build -DMCP_BUILD_TESTS=ON\ncmake --build build --config Release\n```\n\nBuild with SSL support:\n```\ngit submodule update --init --recursive # Get GoogleTest\n\ncmake -B build -DMCP_SSL=ON\ncmake --build build --config Release\n```\n\n## Adopters\n\nHere are some open-source projects that are using this repository.  \nIf you're using it too, feel free to submit a PR to be featured here!\n\n- [humanus.cpp](https://github.com/WHU-MYTH-Lab/humanus.cpp): Lightweight C++ LLM agent framework\n- ...waiting for your contribution...\n\n\n\n## Components\n\nThe MCP C++ library includes the following main components:\n\n### Core Components\n\n#### Client Interface (`mcp_client.h`)\nDefines the abstract interface for MCP clients, which all concrete client implementations inherit from.\n\n#### SSE Client (`mcp_sse_client.h`, `mcp_sse_client.cpp`)\nClient implementation that communicates with MCP servers using HTTP and Server-Sent Events (SSE).\n\n#### Stdio Client (`mcp_stdio_client.h`, `mcp_stdio_client.cpp`)\nClient implementation that communicates with MCP servers using standard input/output, capable of launching subprocesses and communicating with them.\n\n#### Message Processing (`mcp_message.h`, `mcp_message.cpp`)\nHandles serialization and deserialization of JSON-RPC messages.\n\n#### Tool Management (`mcp_tool.h`, `mcp_tool.cpp`)\nManages and invokes MCP tools.\n\n#### Resource Management (`mcp_resource.h`, `mcp_resource.cpp`)\nManages MCP resources.\n\n#### Server (`mcp_server.h`, `mcp_server.cpp`)\nImplements MCP server functionality.\n\n## Examples\n\n### HTTP Server Example (`examples/server_example.cpp`)\n\nExample MCP server implementation with custom tools:\n- Time tool: Get the current time\n- Calculator tool: Perform mathematical operations\n- Echo tool: Echo input with optional transformations (to uppercase, reverse)\n- Greeting tool: Returns `Hello, `+ input name + `!`, defaults to `Hello, World!`\n\n### HTTP Client Example (`examples/client_example.cpp`)\n\nExample MCP client connecting to a server:\n- Get server information\n- List available tools\n- Call tools with parameters\n- Access resources\n\n### Stdio Client Example (`examples/stdio_client_example.cpp`)\n\nDemonstrates how to use the stdio client to communicate with a local server:\n- Launch a local server process\n- Access filesystem resources\n- Call server tools\n\n### Agent Example (`examples/agent_example.cpp`)\n\n| Option | Description |\n| :- | :- |\n| `--base-url` | LLM base URL (e.g. `https://openrouter.ai`) |\n| `--endpoint` | LLM endpoint (default to `/v1/chat/completions/`) |\n| `--api-key` | LLM API key |\n| `--model` | Model name (e.g. `gpt-3.5-turbo`) |\n| `--system-prompt` | System prompt |\n| `--max-tokens` | Maximum number of tokens to generate (default to 2048) |\n| `--temperature` | Temperature (default to 0.0) |\n| `--max-steps` | Maximum steps calling tools without user input (default to 3) |\n\nExample usage:\n```\n./build/examples/agent_example --base-url \u003cbase_url\u003e --endpoint \u003cendpoint\u003e --api-key \u003capi_key\u003e --model \u003cmodel_name\u003e\n```\n\n**Note**: Remember to compile with `-DMCP_SSL=ON` when connecting to an https base URL.\n\n## How to Use\n\n### Setting up an HTTP Server\n\n```cpp\n// Create and configure the server\nmcp::server::configuration srv_conf;\nsrv_conf.host = \"localhost\";\nsrv_conf.port = 8888;\n\nmcp::server server(srv_conf);\nserver.set_server_info(\"MCP Example Server\", \"0.1.0\"); // Name and version\n\n// Register tools\nmcp::json hello_handler(const mcp::json\u0026 params, const std::string /* session_id */) {\n    std::string name = params.contains(\"name\") ? params[\"name\"].get\u003cstd::string\u003e() : \"World\";\n\n    // Server will catch exceptions and return error contents\n    // For example, you can use `throw mcp::mcp_exception(mcp::error_code::invalid_params, \"Invalid name\");` to report an error\n\n    // Content should be a JSON array, see: https://modelcontextprotocol.io/specification/2024-11-05/server/tools#tool-result\n    return {\n        {\n            {\"type\", \"text\"},\n            {\"text\", \"Hello, \" + name + \"!\"}\n        }\n    };\n}\n\nmcp::tool hello_tool = mcp::tool_builder(\"hello\")\n        .with_description(\"Say hello\")\n        .with_string_param(\"name\", \"Name to say hello to\", \"World\")\n        .build();\n\nserver.register_tool(hello_tool, hello_handler);\n\n// Register resources\nauto file_resource = std::make_shared\u003cmcp::file_resource\u003e(\"\u003cfile_path\u003e\");\nserver.register_resource(\"file://\u003cfile_path\u003e\", file_resource);\n\n// Start the server\nserver.start(true);  // Blocking mode\n```\n\n### Creating an HTTP Client\n\n```cpp\n// Connect to the server\nmcp::sse_client client(\"http://localhost:8080\");\n\n// Initialize the connection\nclient.initialize(\"My Client\", \"1.0.0\");\n\n// Call a tool\nmcp::json params = {\n    {\"name\", \"Client\"}\n};\n\nmcp::json result = client.call_tool(\"hello\", params);\n```\n\n### Using the SSE Client\n\nThe 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.\n\n```cpp\n#include \"mcp_sse_client.h\"\n\n// Create a client, specifying the server address and port\nmcp::sse_client client(\"http://localhost:8080\");\n\n// Set an authentication token (if needed)\nclient.set_auth_token(\"your_auth_token\");\n\n// Set custom request headers (if needed)\nclient.set_header(\"X-Custom-Header\", \"value\");\n\n// Initialize the client\nif (!client.initialize(\"My Client\", \"1.0.0\")) {\n    // Handle initialization failure\n}\n\n// Call a tool\njson result = client.call_tool(\"tool_name\", {\n    {\"param1\", \"value1\"},\n    {\"param2\", 42}\n});\n```\n\n### Using the Stdio Client\n\nThe Stdio client can communicate with any MCP server that supports stdio transport, such as:\n\n- @modelcontextprotocol/server-everything - Example server\n- @modelcontextprotocol/server-filesystem - Filesystem server\n- Other [MCP servers](https://www.pulsemcp.com/servers) that support stdio transport\n\n```cpp\n#include \"mcp_stdio_client.h\"\n\n// Create a client, specifying the server command\nmcp::stdio_client client(\"npx -y @modelcontextprotocol/server-everything\");\n// mcp::stdio_client client(\"npx -y @modelcontextprotocol/server-filesystem /path/to/directory\");\n\n// Initialize the client\nif (!client.initialize(\"My Client\", \"1.0.0\")) {\n    // Handle initialization failure\n}\n\n// Access resources\njson resources = client.list_resources();\njson content = client.read_resource(\"resource://uri\");\n\n// Call a tool\njson result = client.call_tool(\"tool_name\", {\n    {\"param1\", \"value1\"},\n    {\"param2\", \"value2\"}\n});\n```\n\n\n## Using TLS clients and servers\n\n### Creating test certificates on Linux\n1. Generate Certificate Authority (CA) private key\n    ```bash\n    openssl genrsa -out ca.key.pem 2048\n    ```\n1. Generate CA certificate\n    ```bash\n    openssl req -x509 -new -nodes -key ca.key.pem -sha256 -days 1 -out ca.cert.pem -subj \"/CN=Test CA\"\n    ```\n1. Generate server private key\n    ```bash\n    openssl genrsa -out server.key.pem 2048\n    ```\n1. Generate Certificate Signing Request (CSR)\n    ```\n    openssl req -new -key server.key.pem -out server.csr.pem -subj \"/O=TestServer/OU=Dev/CN=localhost\"\n    ```\n1. Generate server certificate signed by CA\n    ```\n    openssl x509 -req -in server.csr.pem -CA ca.cert.pem -CAkey ca.key.pem -CAcreateserial -out server.cert.pem -days 1 -sha256\n    ```\n### Setting up an HTTPs server\n\n```cpp\nmcp::server::configuration srv_conf;\nsrv_conf.host = \"localhost\";\nsrv_conf.port = 8888;\nsrv_conf.ssl.server_cert_path = \"./server.cert.pem\";\nsrv_conf.ssl.server_private_key_path = \"./server.key.pem\";\n```\n\n### Setting up an SSE client with TLS\n\n```cpp\n mcp::sse_client client(\"https://localhost:8888\");\n ```\n\n## License\n\nThis framework is provided under the MIT license. For details, please see the LICENSE file.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhkr04%2Fcpp-mcp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhkr04%2Fcpp-mcp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhkr04%2Fcpp-mcp/lists"}