Architecture
This document provides an overview of the mcp-cpp-sdk architecture, including high-level design principles, component structure, and key design decisions.
Design Principles
The mcp-cpp-sdk is built on several core design principles:
Compiled library boundaries
The SDK builds both shared and static library variants. Much of the client,
server, and network-transport runtime is implemented in .cpp files, while
serialization helpers and typed handler templates remain in public headers.
The transport and OAuth runtime implementations live in compiled translation
units. The async and transport APIs still expose Boost.Asio types and
Boost.Beast aliases, so consumers parse the corresponding Boost headers.
This design:
Keeps implementation state behind PImpl where it materially reduces public surface area
Places non-template runtime behavior in compiled translation units where the public API does not require an inline definition
Preserves templates in headers where C++ requires their definitions at the point of instantiation
RAII and Value Semantics
The SDK follows modern C++ best practices:
RAII: Resources (sockets, timers) are owned by objects and cleaned up automatically
Stable ownership:
ServerandClientare non-copyable and non-movable; callers keep them alive while theirrun()orconnect()tasks are activeSmart pointers: implementation, transport, and request state that must survive suspension is shared with the relevant coroutines
Explicit lifetime contracts: request-scoped non-owning views are paired with an owning session or runtime state
Coroutine-Based Async
All asynchronous operations use C++20 coroutines via Boost.Asio:
Task<T>return type for async functions (compatible withboost::asio::awaitable<T>)co_awaitfor sequential async operationsco_spawnfor launching concurrent tasksNo callback hell - linear, readable async code
Type Safety
The SDK emphasizes compile-time safety:
Concepts:
JsonSerializableconcept ensures types are JSON-compatibleStrong typing: Protocol types are structs, not raw JSON
Template metaprogramming: Handler signatures validated at compile time
Typed registration helpers: Templates validate handler signatures, then bridge to the public
TypeErasedHandlermiddleware interface and compiled runtime
Component Overview
The SDK is organized into several key components:
Server
mcp::Server is the core server implementation:
Manages tool, resource, prompt, and resource template registrations
Handles JSON-RPC message dispatch
Executes handler functions with proper context
Supports multiple handler signatures (sync/async, with/without context)
Keeps each server session’s own state on a Boost.Asio strand and runs every handler call on a strand of its own, so handlers of one session can overlap
Key responsibilities:
Protocol compliance (2026-07-28 requests served one by one, and the
initializehandshake of older revisions, on the same transport)Handler type erasure and invocation
Handler wrapping:
add_tool,add_resourceand the other registrations turn each handler signature (synchronous or coroutine, with or withoutContext) into a type-erased coroutine handler; a synchronous tool handler is also kept as it is, and called without a coroutine when the call already runs on its strandError handling: tool-handler exceptions become
CallToolResultvalues withisError=true; other request-dispatch failures use JSON-RPC error responsesContext creation and lifecycle management
Client
mcp::Client is the core client implementation:
Connects to MCP servers and settles on a protocol era: it probes with
server/discoveror performs theinitializehandshakeSends requests and matches responses via request ID
Manages pending requests with timeout support
Handles server notifications
Serializes client request, response, timeout, and close state through a Boost.Asio strand-backed runtime
Key responsibilities:
Request/response correlation (JSON-RPC id matching) via
RequestId— a type-safe wrapper for JSON-RPC request identifiers (string or integer)Reverse RPC:
dispatch_incoming_requesthandles server-to-client JSON-RPC requests, enabling servers to request client capabilities (elicitation, sampling, roots)Timeout management for requests
Connection lifecycle (
connect()andclose(); the read loop is managed internally).connect()resolves to aConnectResultthat names the eraResuming
input_requiredresults and openingsubscriptions/listenstreams withlisten()Error propagation from server responses
Transport Abstraction
mcp::ITransport is the transport interface:
class ITransport {
public:
virtual Task<std::string> read_message() = 0;
virtual Task<void> write_message(std::string_view message) = 0;
virtual void close() = 0;
};
Built-in implementations:
StdioTransport: Reads from stdin, writes to stdout (common for AI tools)
WebSocketServerTransport / WebSocketClientTransport: Communicate over WebSocket (Boost.Beast)
HttpClientTransport: Sends MCP messages to a Streamable HTTP endpoint
HttpServerTransport: Exposes a single-session Streamable HTTP endpoint
For multi-session HTTP deployments, StreamableHttpSessionManager owns the
listener and creates per-session Server instances backed by in-memory
transports for clients of older revisions. The canonical session header name
used in the docs is MCP-Session-Id. It serves 2026-07-28 requests on the
same endpoint without a session, from one shared Server.
Custom transports can be added by implementing the ITransport interface.
Transport responsibilities:
Message framing (newline-delimited JSON for stdio)
Underlying I/O (pipes, sockets, etc.)
Connection state management
Async read/write operations
Context
mcp::Context provides execution context for handlers:
Logging:
log_debug(),log_info(),log_warning(),log_error()Reverse RPC:
sample_llm()for server-to-client sampling requestsNotifications: Send notifications back to the connected peer
Request utilities: Progress reporting, roots requests, elicitation, and cancellation state
Context is passed to handlers that declare a Context& parameter.
Context responsibilities:
Structured logging with severity levels
Bidirectional communication (reverse RPC)
Cancellation state and progress support when a request supplies a progress token
Core Types
The core headers expose fundamental types in the mcp namespace:
Task<T>- Coroutine return type, a class derived fromboost::asio::awaitable<T>LoggingLevel- Protocol enum for log severityErrorand JSON-RPC error responses - Structured peer-visible failures
Protocol Types
The protocol headers expose MCP message types in the mcp namespace:
Request types:
InitializeRequest(older revisions),ListToolsRequest,CallToolRequest, etc.Response types:
DiscoverResult,InitializeResult(older revisions),ListToolsResult,CallToolResult, etc.Notification types:
InitializedNotification,ProgressNotification, etc.Capability structs:
ServerCapabilities,ClientCapabilitiesRequestId— a named wrapper for JSON-RPC request identifiers (string or integer), replacing rawstd::variantusage
All types support JSON serialization via nlohmann_json. Protocol types are
organized into focused sub-headers (capabilities.hpp, content.hpp,
tools.hpp, roots.hpp, sampling.hpp, elicitation.hpp, etc.)
included via the mcp/protocol/protocol.hpp umbrella.
Protocol Eras
The SDK separates two protocol families, called eras in the API
(ProtocolEra):
Modern (2026-07-28): no handshake. Each request carries its protocol version and client capabilities in
_meta, and a client learns about a server withserver/discover. The server holds no per-connection protocol state.Legacy (2025-11-25 and earlier): a session starts with
initializeandnotifications/initialized, and the negotiated version and capabilities belong to the session.
A Server serves both by default, on every transport; ServerProtocolPolicy
narrows that. A Client picks one when it connects, according to
ClientOptions::protocol. The default, ClientProtocol::automatic(), probes with
server/discover and falls back to initialize. See Protocol Revisions.
Data Flow
The typical data flow for a tool call:
Client serializes
CallToolRequestto JSONClient Transport writes JSON message
Server Transport reads JSON message
Server deserializes to
CallToolRequestServer looks up tool handler by name
Server creates
Contextfor the requestServer invokes type-erased handler (async coroutine)
Handler processes request, possibly using
Context::log_info()Handler returns result (
co_return)Server serializes result to
CallToolResultServer Transport writes JSON response
Client Transport reads JSON response
Client matches response to pending request by ID
Client deserializes and returns result to caller
The flow above is the same in both eras. In a 2026-07-28 session step 1 also
puts the protocol version and client capabilities in the request’s _meta,
and no initialize request precedes it. If the handler asks the client a
question in step 8, the server answers with an input_required result and
the client sends the request again with the answer.
This flow demonstrates the clean separation between protocol layer, transport layer, and application logic.
Design Decisions
Why Boost.Asio?
The SDK uses Boost.Asio for async I/O because it provides the primitives used by the public coroutine model:
C++20 coroutine support through
awaitable<T>andco_spawnCross-platform (Windows, Linux, macOS)
Timers, streams, executors, and strands under one execution model
Established documentation and ecosystem
Why nlohmann_json?
nlohmann_json was selected for its direct mapping between JSON and C++ protocol types:
Object-style API (
j["key"] = value)Automatic conversion through
to_jsonandfrom_jsonSupport for the variant and optional fields used by MCP messages
Why compiled library variants?
The compiled variants provide implementation and linkage boundaries while retaining the template-based public API:
Encapsulation: Internal request state and transport machinery stay behind implementation boundaries
Choice of linkage: Consumers select
mcp::sdk_sharedormcp::sdk_staticexplicitly, or usemcp::sdkfor the configured defaultTemplate ergonomics: Typed handlers remain available without a separate code-generation step
Tradeoff: the coroutine and executor types remain part of the source-level API, and PImpl calls cannot be inlined across the library boundary without link-time optimization.
Why Multiple Handler Signatures?
Supporting 4 handler signatures (sync/async × with/without context) provides flexibility:
Async with context: Most powerful, enables logging and reverse RPC
Async without context: Simpler for pure computation
Sync with context: For quick operations that need logging
Sync without context: Simplest possible handler
Type erasure unifies these at runtime, so Server internals stay simple.
Thread Safety Model
Client runtime transitions and the state of each server session are serialized
through Boost.Asio strands. Each handler call runs on a strand of its own, so
handlers of one session can overlap, as can handlers of separate HTTP sessions,
and a StreamableHttpSessionManager can place tool work on a separate
executor. Application state shared by handlers therefore needs its own
synchronization.
Complete server registrations before starting a session. Configure Origin and
bearer-token setters directly on HttpServerTransport or
StreamableHttpSessionManager before listen() or run(), or, with
Server::run_http(), in the callback it passes its manager to before it
listens. The token store
is independently synchronized, but that does not make every OAuth wrapper
operation safe for arbitrary concurrent calls.
Performance Characteristics
Protocol messages are parsed with nlohmann_json and cross the transport
interface as serialized strings. Typed handler dispatch is normalized once at
registration; transport dispatch remains virtual by design. Allocation and
copy behavior depends on message size, JSON values, and the selected transport,
so the project does not promise zero-copy operation.
For high-throughput scenarios, consider:
Reusing an
io_contextand a multi-session HTTP managerKeeping handlers from blocking. A handler runs on the executor that reads and answers requests, so a blocking call (a synchronous library call, a sleep, file or network I/O without
co_await) holds up every request behind it. Await asynchronous operations instead, or callServer::set_handler_executor()with an executor of its own, such as aboost::asio::thread_pool, to run tool handlers there; the server’s own work stays on its executor. A pool of one thread also keeps state that only the handlers touch on a single thread. Thedebugger_serverexample does this for LLDB’s blocking calls.Returning
mcp::CallToolResult(Out = mcp::CallToolResult) from tools, for example built withmcp::make_tool_text_result(). A tool that returnsnlohmann::jsonhas its result inspected and decoded once more to tell a readyCallToolResultfrom data to wrap.Measuring the complete workload with the reproducible
benchmark/suite
Benchmark results are end-to-end measurements for a pinned workload and build configuration; they are not a universal SDK throughput guarantee.
Extensibility
The SDK is designed for extension:
Custom transports: Implement
ITransport(e.g., for HTTP/2)Custom serialization: Provide
to_json/from_jsonfor your typesAsio executors: Pass any Asio executor, such as an
io_context’s, a strand or a thread pool’s; it converts tomcp::Executor, andExecutor::get()returns it as aboost::asio::any_io_executorMiddleware: Intercept handler execution for auth, logging, and request shaping
Future Work
Potential future enhancements:
Connection pooling: Reuse transports across multiple requests
HTTP/2 transport: For deployments that require HTTP/2
Sender/receiver interoperability: Integration with standard execution APIs as supported toolchains make it practical
Batched operations: Protocol extension for bulk requests
See the GitHub issues for planned features and contributions.