Deployment Guide

Deploying an MCP server into a production environment requires careful consideration of transport selection, lifecycle management, and operational security. This guide covers the essential patterns for deploying MCP servers built with the mcp-cpp-sdk.

Overview

When moving from development to production, you must decide how your server will communicate with clients. The Model Context Protocol supports several transport mechanisms:

  • Stdio: Best for local integration with desktop applications (like Claude Desktop or IDEs).

  • HTTP/SSE: Recommended for remote deployments, multi-client support, or web-based integrations.

A server built with the SDK serves protocol revision 2026-07-28 and the older revisions on the same transport by default. See Serving Both Protocol Eras for what that means over HTTP.

Key considerations for deployment include:

  • Choosing the right transport for your use case.

  • Implementing graceful shutdown to protect in-flight requests.

  • Integrating with system process managers (like systemd or Docker).

  • Monitoring and health checks.

HTTP Server Setup

The mcp-cpp-sdk provides two ways to set up an HTTP server: a high-level convenience API and a manual setup for fine-grained control.

Convenience API (run_http)

For most deployments, the mcp::Server::run_http() method is the simplest approach. It manages the io_context, sets up a mcp::StreamableHttpSessionManager that serves each older-protocol client in a session of its own with the same server, and handles signal mapping automatically.

// HTTP until a signal: run_http() runs the event loop and handles SIGINT and SIGTERM, and
// configures the session manager it serves through before it listens.
const auto port = static_cast<std::uint16_t>(argc > 1 ? std::stoi(argv[1]) : 8080);
const auto configure = [](mcp::StreamableHttpSessionManager& http) {
    http.set_bearer_token_validator(
        [](std::string_view token) { return token == "greeter-secret-token"; });
    http.set_allowed_origins({"http://localhost:3000"});
};
server.run_http("127.0.0.1", port, configure);

run_http() blocks until SIGINT or SIGTERM, or until server.stop() is called from another thread or a handler. server.bound_port() returns the port it listens on, which is the one the system chose when it was given port 0.

Manual HTTP Setup

If you need to integrate the MCP server into an existing Boost.Asio application or require custom middleware, you can set up the transport manually.

boost::asio::io_context io_ctx;
auto transport = std::make_shared<mcp::HttpServerTransport>(
    io_ctx.get_executor(), "0.0.0.0", 8080);

// Spawn the transport listener
boost::asio::co_spawn(io_ctx, transport->listen(), boost::asio::detached);

// Run the MCP server over the transport
boost::asio::co_spawn(io_ctx, server.run(transport, io_ctx.get_executor()),
                      boost::asio::detached);

io_ctx.run();

Serving Both Protocol Eras

A 2026-07-28 request is a complete message: it carries its protocol version and client capabilities in _meta and belongs to no session. A request of an older revision belongs to a session that initialize created. The HTTP transports serve both on one endpoint, and tell them apart by what the request carries.

StreamableHttpSessionManager

The manager builds a Server with its factory for each session of an older revision, as before. All 2026-07-28 requests are answered by one shared Server: the one passed to set_modern_server(), or else one built from the factory the first time it is needed. modern_server() returns it, and builds it if no request has yet. Because every 2026-07-28 client sees the same tools, resources and prompts, they must not depend on the connection. Server::run_http() serves through a manager, which keeps its default settings unless the callback run_http() takes changes them before it listens: the server itself answers 2026-07-28 requests, and each session gets a server that shares its tools, resources, prompts, handlers and settings. A manager constructed with a Server instead of a factory serves it the same way, on the application’s event loop, and the Server must outlive it.

Configure that server before listen(), for example its cache hints:

mcp::StreamableHttpSessionManager manager(io_ctx.get_executor(), "127.0.0.1", 8080,
                                          factory);

manager.modern_server()->set_cache_hints(
    mcp::CacheTarget::eToolsList,
    mcp::CacheHint{.ttlMs = 60'000, .scope = mcp::CacheScope::ePublic});

boost::asio::co_spawn(io_ctx, manager.listen(), boost::asio::detached);

A 2026-07-28 request ignores Mcp-Session-Id and Last-Event-ID, and its response never carries Mcp-Session-Id. The answer is plain JSON. If a handler sends a notification while it runs (a log message at the request’s own log level, or progress) and the client accepts text/event-stream, the answer is a chunked event stream that carries the notifications and then the response. It has no event ids and carries periodic keep-alive comments; set_stream_options() sets the intervals and stream_stats() counts what the streams did. A client that disconnects while its request runs cancels the request.

set_protocol_policy() takes a ServerProtocolPolicy and applies it to every Server the manager builds. With serve_legacy = false no session is ever created: initialize is answered 404 with g_METHOD_NOT_FOUND, naming the versions served, and every GET and DELETE is answered 405.

HttpServerTransport

HttpServerTransport serves 2026-07-28 requests next to its one legacy session, with the same header checks and error statuses. The server that reads the transport answers each request directly. The answer is plain JSON. If a handler sends a notification while it runs (a log message at the request’s own log level, or progress) and the client accepts text/event-stream, the answer is a chunked event stream that carries the notifications and then the response, as on StreamableHttpSessionManager. A subscriptions/listen request is an event stream that stays open until the client closes it, the server ends the subscription or the transport closes. A client that disconnects while its request runs cancels it, and so does a reset after a half-close. A client that accepts text/event-stream and half-closes its connection gets an event stream even when no notification is sent, carrying comments that probe whether it is still there, because a client that closed cleanly cannot be told from one that only half-closed until a write fails. The HTTP status of such an answer is 200 even for an error; the JSON-RPC body still carries the error. A client that does not accept a stream gets plain JSON with the specified status, and a reset after its half-close is found by checking the socket.

Notifications wait for the client to read them, so each request keeps at most max_queued_notifications of them (4096 by default) and max_queued_bytes (1 MiB by default, the size of the serialized messages, taken from the max_request_bytes budget too). A notification that would take the request past a limit, or past that budget, is dropped and counted in StreamStats::dropped_notifications; the handler is not told and the answer is unchanged. A subscriptions/listen stream ends instead, with its closing result, so that the client reconnects and lists again.

A request holds its place under HttpConnectionLimits::max_requests and max_request_bytes until its answer or stream has ended, and the connection is closed when the client does not take a write within request_read_timeout. set_stream_options() sets the keep-alive and probe intervals and the limits on queued notifications, and stream_stats() counts what the streams did. set_json_only(true) keeps the answers to plain JSON.

Request headers

A 2026-07-28 request over HTTP repeats some of its content in headers, so that a gateway can route or limit it without reading the body. The server requires them and checks them against the body:

  • MCP-Protocol-Version and Mcp-Method on every request.

  • Mcp-Name on tools/call, prompts/get, resources/read and, for the tasks extension, tasks/get, tasks/update and tasks/cancel, where it holds params.taskId. A value in the =?base64?...?= form is decoded first.

  • Mcp-Param-{Name} on a tools/call whose tool marks input properties with x-mcp-header. See x-mcp-header parameters.

A request that lacks one, repeats one, or sends a value that does not match the body is answered 400 with g_HEADER_MISMATCH. header_mismatch_rejections() counts those on the manager and on HttpServerTransport. HttpClientTransport sends the headers itself.

The status of an error follows the specification: 400 for invalid params, an unsupported protocol version, a header mismatch and a missing client capability, and 404 for an unknown method. See Error Handling.

x-mcp-header parameters

A tool can ask for some of its arguments to be mirrored into headers by annotating the property in its input schema:

nlohmann::json schema = {
    {"type", "object"},
    {"properties",
     {{"region", {{"type", "string"}, {"x-mcp-header", "Region"}}},
      {"query", {{"type", "string"}}}}},
    {"required", {"region", "query"}}};

A 2026-07-28 tools/call of this tool over HTTP must then carry a Mcp-Param-Region header equal to the region argument, after decoding. Integers are compared as numbers. add_tool() refuses with SchemaError an annotation that is not a non-empty HTTP token string, repeats a name (ignoring case), sits on a property that is not a string, integer or boolean, or sits on a property not reached from the root through properties alone. HttpClientTransport fills the headers in. It leaves a tool with an invalid annotation out of tools/list and reports it through set_warning_handler().

Graceful Shutdown

In production, servers are frequently restarted or moved. Graceful shutdown ensures that the server finishes processing active requests before exiting, preventing data loss or client-side errors.

Signal Handling

run_stdio() and run_http() handle SIGINT and SIGTERM themselves, and server.stop() ends them the same way. A program that runs the event loop itself, with Server::run(), handles the signals as below.

You should handle signals like SIGINT and SIGTERM to trigger the shutdown process. The SDK provides mcp::graceful_shutdown() in mcp/core/signal.hpp to close the transport and stop the io_context after in-flight work finishes or times out.

// graceful_shutdown() returns its watchdog timer. The server cancels it once it has
// stopped; otherwise the pending timer keeps io_ctx.run() going until the timeout.
std::shared_ptr<asio::steady_timer> watchdog;
asio::co_spawn(
    io_ctx,
    [&, transport]() -> Task<void> {
        asio::steady_timer trigger(io_ctx.get_executor());
        trigger.expires_after(std::chrono::seconds(1));
        co_await trigger.async_wait(asio::use_awaitable);
        std::cerr << "[Main] Initiating graceful shutdown\n";
        input_buffer.close();
        watchdog = graceful_shutdown(io_ctx, transport);
        std::cerr << "[Main] Graceful shutdown initiated (timeout: "
                  << mcp::constants::shutdown::g_TIMEOUT_MS << "ms)\n";
    },
    asio::detached);

The mcp::graceful_shutdown() function closes the transport (preventing new connections) and starts a watchdog timer. If requests don’t finish within the timeout (defined by mcp::constants::shutdown::g_TIMEOUT_MS), it stops the io_context forcefully. It returns the timer: cancel it once Server::run() has returned, as the example does, or the pending timer keeps io_context::run() going until the timeout.

Production Considerations

Port Binding and Security

  • Binding: Use 127.0.0.1 if the server only needs to be accessed by a local proxy (like Nginx) or 0.0.0.0 to listen on all interfaces.

  • TLS/SSL: The HTTP server transports end TLS themselves once set_tls() gives them a certificate, and can require client certificates; see TLS and Proxies. They can also sit behind a reverse proxy like Nginx, Apache, or a cloud load balancer that ends TLS for them.

  • Host check: A request that reaches a server through a loopback address must name a loopback host (localhost or a loopback address) in its Host header, or it is answered 403. This defends a local server against DNS rebinding, where a web page whose name points at 127.0.0.1 reaches it from the browser. A reverse proxy on the same machine that passes on the public name, as Nginx does with proxy_set_header Host $host, needs that name listed with set_allowed_hosts({"mcp.example.com"}). Once there is a list, every request must name a host on it, whatever address it reached.

  • Authentication: For sensitive tools, implement authentication middleware or use the OAuth flow patterns demonstrated in the oauth_flow.cpp example.

Unix Daemon / Service Setup

Running your server as a background process (daemon) ensures it stays alive and restarts on failure.

systemd Integration

On Linux, systemd is the standard way to manage services. Using a system service ensures that your MCP server starts automatically on boot and recovers from crashes.

Create a service file at /etc/systemd/system/mcp-server.service:

[Unit]
Description=MCP C++ Server
After=network.target

[Service]
Type=simple
User=mcpuser
Group=mcpuser
WorkingDirectory=/opt/mcp-server
ExecStart=/opt/mcp-server/bin/my_mcp_server
Restart=on-failure
RestartSec=5s
Environment=PORT=8080
# Recommended: limit privileges
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=full

[Install]
WantedBy=multi-user.target

After creating the file, reload the systemd manager and enable the service:

sudo systemctl daemon-reload
sudo systemctl enable mcp-server
sudo systemctl start mcp-server

Docker Deployment

Containerization provides a consistent environment for your MCP server, making it easier to deploy across different cloud providers. Use a multi-stage Dockerfile to keep the production image small and secure.

# Build stage
FROM gcc:12 AS builder
RUN apt-get update && apt-get install -y cmake libboost-all-dev
COPY . /src
WORKDIR /src
RUN mkdir build && cd build && \
    cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=OFF && \
    make -j$(nproc)

# Production stage
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y libboost-dev && \
    rm -rf /var/lib/apt/lists/*

# Create a non-root user
RUN useradd -m mcpuser
USER mcpuser

COPY --from=builder /src/build/bin/my_mcp_server /usr/local/bin/
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/my_mcp_server"]

When running in Docker, ensure you map the ports correctly and provide any necessary configuration via environment variables.

Load Balancing and Scaling

As your MCP application grows, you might need to scale horizontally by running multiple instances.

Sticky Sessions

A session of 2025-11-25 or earlier is stateful (initialization, followed by multiple requests), so ensure your load balancer uses sticky sessions (session affinity) for such clients. If a client’s request is routed to a different instance that hasn’t seen the initialize request, the server will reject the request.

A 2026-07-28 request needs no session, and any instance that serves the same tools, resources and prompts can answer it. Two cases can still need the request to reach a particular instance:

  • A request whose handler asks the client a question (Context::elicit(), sample_llm(), request_roots()) waits in the Server instance by default. The client’s retry, which carries the requestState, must reach that instance; a retry that does not is answered as for an unknown requestState. With sealed request state (MrtrOptions::state_mode, see Protocol Revisions) the state travels in the requestState and any instance configured with the same sealing_keys serves the retry.

  • A subscriptions/listen stream is one long-lived request, and the notifications it carries come from the instance that holds it.

  • A task (see Tasks) lives in the TaskStore of the instance that started it, and its handler runs there. With the default in-memory store, tasks/get, tasks/update and tasks/cancel must reach that instance: route on the Mcp-Name header, which holds the task id. An instance that does not know the task answers g_INVALID_PARAMS.

Health Checks

For HTTP deployments, you can implement a health check endpoint. While the MCP protocol doesn’t define a standard health check, you can add a simple tool or a parallel HTTP route to verify the server’s status.

  • Liveness: Verify the process is running and the io_context event loop is responsive.

  • Readiness: Verify that any external resources (databases, APIs, or model endpoints) that the tools depend on are currently available.

Monitoring and Logging

Production servers should emit structured logs for troubleshooting.

  • Standard Streams: In Stdio mode, avoid writing anything to stdout except MCP messages. Use stderr for all logging.

  • Structured Logging: When using HTTP, consider using a logging library that outputs JSON for better integration with log aggregators like ELK or Datadog.

Example Walkthrough

The http_server_convenience.cpp example provides a complete, runnable demonstration of an HTTP-based deployment using the high-level API.

ServerCapabilities server_caps;
server_caps.tools = ServerCapabilities::ToolsCapability{};

Implementation server_info;
server_info.name = "http-convenience-server";
server_info.version = "1.0.0";

Server server(server_info, server_caps);

// Register a simple echo tool
nlohmann::json tool_schema = {
    {"type", "object"},
    {"properties", {{"text", {{"type", "string"}}}}},
    {"required", nlohmann::json::array({"text"})},
};

server.add_tool<nlohmann::json, CallToolResult>(
    "echo", "Echo the input text", tool_schema,
    [](nlohmann::json input_args) -> CallToolResult {
        return make_tool_text_result("Echo: " + input_args.at("text").get<std::string>());
    });

asio::io_context probe_io_ctx;
tcp::acceptor probe_acceptor(probe_io_ctx, {asio::ip::make_address("127.0.0.1"), 0});
const auto http_port = probe_acceptor.local_endpoint().port();
probe_acceptor.close();

std::cout << "Starting HTTP server on port " << http_port << " (convenience method)\n";
std::cout << "Server will run in background thread\n";

// Spawn server in background thread
std::thread server_thread([&server, http_port] {
    try {
        // This convenience method handles everything:
        // - Creates io_context
        // - Listens, and serves each client of protocol revision 2025-11-25 or earlier
        //   in a session of its own, with this server's tools
        // - Answers 2026-07-28 requests with this server
        // - Manages signal handling for graceful shutdown
        server.run_http("127.0.0.1", http_port);
    } catch (const std::exception& e) {
        std::cerr << "Server error: " << e.what() << '\n';
    }
});

For advanced users, graceful_shutdown.cpp demonstrates how to build a robust server that respects system signals and manages the connection lifecycle manually. This is particularly useful when you need to coordinate the shutdown of multiple services or databases alongside the MCP server.

asio::co_spawn(
    io_ctx,
    [&, transport]() -> Task<void> {
        std::cerr << "[Server] Starting\n";
        try {
            co_await server.run(transport, io_ctx.get_executor());
            std::cerr << "[Server] Stopped normally\n";
        } catch (const std::exception& e) {
            std::cerr << "[Server] Stopped with exception: " << e.what() << "\n";
        }
        if (watchdog) {
            watchdog->cancel();
        }
    },
    asio::detached);

Cross-References

  • See Transports for more details on HTTP and Stdio transport specifics.

  • See Error Handling for patterns on managing production errors and logging exceptions.