Catalog My learning Blog
Build AI Agents with MCP: 30-Day Course · Phase 5 · Lesson 21 · 5 min read

Deploy a Remote MCP Server in Production

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: A Horizontally Scalable Python Server
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Deploy a Streamable HTTP MCP server that scales horizontally: run it statelessly behind a load balancer, set caching hints, add health checks, logging and tracing, handle long-running work with the tasks extension, and keep cross-call state in explicit handles rather than sessions.

Concept Explanation

A remote MCP server is a web service, and most of what makes one production ready is ordinary web engineering. What is specific to MCP is how the 2026-07-28 revision changed the shape of that service. Because every request carries its own protocol version, client capabilities and authorization, a server no longer needs to remember anything about a client between requests. You can run several replicas behind any load balancer, autoscale them, deploy them to serverless platforms, and replace instances without breaking clients.

Statelessness does not mean your tools have no state; it means the protocol carries none. If a workflow spans several calls, such as open a shopping cart, add items, then check out, the server mints an explicit handle, a cart id, returns it from the first call and accepts it as an ordinary argument in later ones. The handle points into your own database, which any replica can read. This is more robust than protocol sessions ever were, because the state survives restarts and is visible in your data rather than hidden in server memory.

Caching is the next lever. Every list and read result carries ttlMs and cacheScope. A tool list that is the same for every user can be marked public with a ten-minute lifetime, so clients and shared proxies reuse it instead of asking on every model turn. Per-user data should be private. The specification also recommends returning tools in a deterministic order, which helps client caches and, importantly, model prompt caches: a tool list that reorders between requests invalidates the cached prompt prefix on every call.

The required Mcp-Method and Mcp-Name headers on each POST let infrastructure act on MCP traffic without parsing bodies. An API gateway can rate-limit tools/call for an expensive tool by name, route resources/read to a different backend, or block specific tools for some tenants. Observability uses standard tools: structured logs to standard output or your platform's collector, metrics on latency and error rate per tool, and distributed tracing. The specification reserves the _meta keys traceparent and tracestate so a client's OpenTelemetry trace can continue through the server into your databases and APIs.

Some operations take minutes: generating a report, running a migration, waiting for a build. Holding an HTTP request open that long is fragile. The tasks extension, io.modelcontextprotocol/tasks, lets a server return a task handle immediately; the client polls with tasks/get and can supply mid-flight input with tasks/update. Clients and servers opt into extensions explicitly, so check support before relying on one.

ConcernProduction approachMCP feature involved
ScalingStateless replicas behind any load balancerNo sessions since 2026-07-28
Multi-step stateServer-minted handles stored in your databaseOrdinary tool arguments
Repeated list callsPublic cache for shared lists, private for per-user datattlMs and cacheScope
Gateways and rate limitsRoute and limit on headersMcp-Method and Mcp-Name
TracingContinue the client's trace into your backendstraceparent in _meta
Long operationsReturn a task handle, poll for completionTasks extension

Worked Example: A Horizontally Scalable Python Server

This catalog server sets cache hints for its lists, adds a health route for the load balancer, logs to standard error and exposes an ASGI app that runs with several worker processes. Because no state lives in the process, four workers or forty replicas behave the same.

Code
import logging, os
from starlette.requests import Request
from starlette.responses import JSONResponse
from mcp.server import CacheHint
from mcp.server.mcpserver import MCPServer

logging.basicConfig(level=logging.INFO)          # stderr, collected by the platform
log = logging.getLogger("catalog-mcp")

mcp = MCPServer(
    "catalog",
    version=os.environ.get("APP_VERSION", "dev"),
    cache_hints={
        "tools/list": CacheHint(ttl_ms=10 * 60_000, scope="public"),   # identical for every user
        "resources/read": CacheHint(ttl_ms=30_000, scope="private"),   # may differ per user
    },
)

@mcp.tool()
def search_products(query: str, limit: int = 10) -> list[dict]:
    """Search the product catalog by name or SKU. Returns at most limit items (max 50)."""
    log.info("search_products query=%r limit=%d", query, limit)
    return CATALOG.search(query)[: min(limit, 50)]

@mcp.custom_route("/healthz", methods=["GET"])
async def health(request: Request) -> JSONResponse:
    return JSONResponse({"status": "ok", "version": os.environ.get("APP_VERSION", "dev")})

app = mcp.streamable_http_app(host="0.0.0.0")
# Run:   uvicorn catalog:app --host 0.0.0.0 --port 8080 --workers 4
# Then put it behind HTTPS and authorization (Day 22) before exposing it.

Two consecutive tools/list calls against this server can be answered by different worker processes and return identical, cacheable results with cacheScope public. In front of it, a gateway can apply a per-tool rate limit by reading the Mcp-Name header.

Clients reach a gateway that routes and rate-limits on the Mcp-Method and Mcp-Name headers. Any replica can serve any request, multi-step state lives in your database behind explicit handles, and traces continue into backends.
Clients reach a gateway that routes and rate-limits on the Mcp-Method and Mcp-Name headers. Any replica can serve any request, multi-step state lives in your database behind explicit handles, and traces continue into backends.

Real-World Examples

  • Google's developer blog on the stateless MCP update describes running MCP servers on Cloud Run and other autoscaled platforms where any instance can serve any request, which was awkward when servers had to pin sessions.
  • Cloudflare, Vercel and other platforms publish templates for deploying remote MCP servers as serverless functions, an architecture that depends on requests being self-contained.
Key TakeawaysProduction MCP servers are stateless web services: any replica serves any request, and multi-step workflows use explicit handles stored in your own data. Set ttlMs and cacheScope on lists and keep tool order deterministic to help client and prompt caches. Use the Mcp-Method and Mcp-Name headers in gateways, propagate trace context, and use the tasks extension for long operations instead of long-held requests.

Review Questions & Answers

The server creates the cart in its own storage, returns a cart id from the first call, and accepts that id as an ordinary argument in later calls. Any replica can look it up, and the state survives restarts.

Clients can cache the list, and models' prompt caches depend on the tool definitions being byte-identical between requests. A changing order invalidates the cached prefix on every call and raises cost and latency.

Gateways, firewalls and load balancers can route, rate-limit or block MCP traffic by method and tool or resource name without parsing the JSON body.

Holding an HTTP request open for minutes is fragile and ties up resources. A task handle returned immediately lets the client poll for progress and completion, and supply input mid-flight, without a long-lived request.