Skip to content

Server Overview

The McpServer class exposes capabilities (tools, resources, prompts) to connected MCP clients.

Creating a Server

cpp
auto transport = std::make_shared<StdioServerTransport>();

ServerOptions opts;
opts.server_info = Implementation{"MyServer", "1.0.0"};
opts.capabilities = ServerCapabilities{};
opts.capabilities->tools = ToolsCapability{};

auto server = McpServer::Create(std::move(transport), opts);
server->Run();

For Streamable HTTP mode:

cpp
StreamableHttpServerOptions http_opts;
http_opts.port = 3001;
http_opts.stateless = true;  // or false for session mode

auto transport = std::make_shared<StreamableHttpServerTransport>(http_opts);

ServerOptions opts;
opts.server_info = Implementation{"MyServer", "1.0.0"};
opts.capabilities = ServerCapabilities{};
opts.capabilities->tools = ToolsCapability{};

auto server = McpServer::Create(std::move(transport), opts);
server->Run();

ServerOptions

FieldTypeDescription
server_infooptional<Implementation>Server identity (name, version)
capabilitiesoptional<ServerCapabilities>Declared capabilities
protocol_versionoptional<string>Pin to a specific version
server_instructionsoptional<string>Instructions sent to client
initialization_timeoutchrono::secondsHandshake timeout (default 60s)
validate_tool_inputboolEnable JSON Schema input validation
validate_tool_outputboolEnable JSON Schema output validation
task_storeshared_ptr<IMcpTaskStore>Task persistence backend
request_state_verifierfunction<bool(string_view)>HMAC/AEAD verifier for MRTR
cache_hintsoptional<map<string, CacheHint>>Per-method cache hints (ttlMs, cacheScope)
input_required_configoptional<InputRequiredConfig>Configuration for MRTR/elicitation behavior
input_required_config.max_roundsintMaximum elicitation rounds (default 8)
input_required_config.round_timeoutchrono::secondsPer-round timeout (default 600s)
input_required_config.legacy_shimboolEnable legacy compatibility shim (default true)
on_requestfunctionCalled on each incoming JSON-RPC request with method name + full request body
on_responsefunctionCalled on each outgoing JSON-RPC response
on_errorfunctionCalled on each outgoing JSON-RPC error response
on_notificationfunctionCalled on each incoming JSON-RPC notification
on_method_calledfunction(string_view)Shorthand — method name only (fires alongside on_request)
on_protocol_errorfunction(string_view)Shorthand — error message only (fires alongside on_error)
on_client_connectedfunction(Implementation)Called when a client completes initialize
on_initializedfunction()Called when client sends notifications/initialized
on_transport_closefunction()Called when the transport connection closes
on_transport_errorfunction(string_view)Called on transport-level errors
incoming_filtersshared_ptr<FilterPipeline>Pipeline to intercept/modify/block inbound messages
outgoing_filtersshared_ptr<FilterPipeline>Pipeline to intercept/modify/block outbound messages

Shorthand callbacks (on_method_called, on_protocol_error) and full-message callbacks (on_request, on_error) are chained — both fire when set simultaneously.

Lifecycle

  1. Construction: McpServer::Create — creates the session handler, wires handlers, starts message loop
  2. Registration: Register tools, resources, prompts, extensions
  3. Run: server->Run() — blocks on io_context.run()
  4. Shutdown: server->Close() — cancels pending requests, closes transport

Capability Derivation

Capabilities are automatically derived from registered primitives. For example, registering a tool sets capabilities.tools.list_changed = true. The ServerOptions.capabilities field is declared but currently not consumed by the server — capabilities are always auto-derived from registered tools, resources, prompts, and task store.