How hkr04/cpp-mcp works
Project type and purpose
• This is a C++ framework library (open-source tool) that implements the Model Context Protocol (MCP).
• It provides both client and server APIs for JSON-RPC 2.0 communication, resource abstraction, tool registration/invocation, and supports two transports (HTTP/SSE and stdio).
• It’s intended to be embedded in other C++ applications or used via examples to stand up MCP servers and clients.
File structure
Top-level directories:
• common/ : header-only utilities (base64, HTTP client, JSON parser) shared by all modules
• / : public API headers (client, server, message, tool, resource, thread pool, logger, etc.)
• src/ : implementation of the public APIs
• examples/ : standalone small apps illustrating server-side, client-side, agent-side use
• test/ : unit tests using GoogleTest
• CMakeLists.txt & submodule references: build orchestration, optional SSL, test toggles
Key insights from README
• Core Components: client interface, SSE client, stdio client, message processing, tool management, resource management, server
• Architecture is modular and extensible: new transports, tools, resources can be added via registration interfaces
• Examples show real-world flows: HTTP server with tools/resources; HTTP client; stdio client spawning a server; an “agent” that chains LLM calls and tools
– External Clients / Agents (could be HTTP or stdio processes)
– Transport Layer
• HTTP/SSE Transport Adapter
• STDIO Transport Adapter
– Core Library
• Message Processor (JSON-RPC serialization/deserialization)
• Tool Registry & Invoker
• Resource Registry & Manager
• Server Core (listener, dispatcher)
– Common Utilities (json.hpp, httplib.h, base64.hpp)
– External Resources/Services
• File System, APIs, LLM endpoints
1. Client app → (selects transport) → Transport Adapter
2. Transport Adapter → JSON-RPC request → Message Processor
3. Message Processor → dispatch to Server Core
4. Server Core → invokes Tool Registry or Resource Manager → calls user-supplied handler or resource implementation
5. Handler → returns JSON result → Tool/Resource → Message Processor
6. Message Processor → JSON-RPC response → Transport Adapter → back to Client
c. Architectural patterns / design principles
• Layered design: separation between transport, messaging, domain (tools/resources)
• Plugin/registry pattern: tools and resources are registered at runtime via abstract interfaces
• Interface vs. implementation: public APIs in include/ vs concrete logic in src/
• Single responsibility: each module handles one concern (e.g., serialization, HTTP comms, dispatching)
d. Key technologies & libraries
– CMake for build configuration
– header-only nlohmann::json for JSON
– cpp-httplib for HTTP/SSE support
– GoogleTest for unit testing
– standard C++17 threading (thread_pool)