Sessions, State, Scaling, Host/Origin Validation & Production Checklist

By — 7+ years building ML / AI agent systems, founder of agent-engineering.dev & sandx.ai.
Published: Last updated:
mcp deploymentstateless mcpmcp sessionshost origin validationproduction checklist

Module 4 — Transports & Deployment

Modern MCP (2026-07-28) is per-request and stateless by default. Legacy 2025-era traffic can still be served, but sessions, sticky state, and special scaling concerns belong to the older model. This article covers the differences, the security checks that belong in front of any HTTP endpoint, and a practical production checklist.

Modern Stateless vs Legacy Sessionful

Modern 2026-07-28: fresh factory per request, no sessions, horizontal scale. Legacy 2025: session ID, pinned transport, GET stream + DELETE. createMcpHandler defaults to modern + stateless legacy fallback.

S7HUh.jpg

Modern (2026-07-28) — Stateless by Design

createMcpHandler builds a fresh McpServer from your factory for every HTTP request and holds nothing between requests. There is no Mcp-Session-Id. Any load balancer works; any node can answer any request.

const handler = createMcpHandler(createServer);  // default: modern + stateless legacy

Put shared resources (DB pools, caches, config) at module scope and close over them inside the factory.

Tool handlers that need per-caller data read ctx.http?.authInfo or the request context.

Horizontal scaling is free — no sticky sessions required.

Legacy (2025-era) — Optional Sessionful Path

The 2025 transport can pin a client to a long-lived transport instance via Mcp-Session-Id. createMcpHandler serves legacy traffic statelessly by default (legacy: 'stateless'). Set legacy: 'reject' for a modern-only strict endpoint.

If you truly need sessionful behavior (resumability, certain push patterns), you drop to the lower-level NodeStreamableHTTPServerTransport with a sessionIdGenerator and manage a session map yourself. Most new servers do not need this.

Host / Origin Validation & DNS Rebinding Protection

The handler itself validates neither Host nor Origin and never checks tokens. Mount those checks in front of it.

DNS rebinding: A malicious page resolves its own domain to 127.0.0.1. Without a Host check the browser treats your local server as same-origin and can call it.

Framework app factories (createMcpExpressApp, createMcpHonoApp, createMcpFastifyApp) arm both checks by default on localhost binds. For plain node:http use the guards from @modelcontextprotocol/node. For bare fetch runtimes use the response helpers from @modelcontextprotocol/server.

Requests without an Origin header always pass (non-browser clients). Present but unparseable Origins (including opaque null) are rejected with 403.

For public binding supply allowedHosts (and optionally allowedOrigins).

Authentication Pass-Through

Verify the bearer token in middleware, then hand the resulting AuthInfo to the handler:

// Express example
app.all('/mcp', requireBearerAuth({ verifier }), (req, res) =>
  void node(req, res, req.body)   // toNodeHandler forwards req.auth
);

Inside the factory and tool handlers you read authInfo / ctx.http.authInfo. The handler never inspects headers itself.

Production Checklist

Logging

stdio: console.error only. Never console.log (stdout is the protocol channel).

HTTP: normal logging is fine; still prefer structured logs to stderr or a proper logger.

Graceful Shutdown

// stdio
const handle = serveStdio(createServer);
process.on('SIGINT', () => void handle.close());

// HTTP
process.on('SIGINT', async () => {
  await handler.close();   // aborts in-flight exchanges
  process.exit(0);
});

On stdio, also release keep-alive handles (timers, pools) on server.server.onclose so the process can exit when the client closes the pipe.

Other

Bind loopback (127.0.0.1) unless you intentionally expose the service and have Host/Origin allow-lists.

Keep the factory pure and fast.

Prefer responseMode: 'json' on serverless/edge runtimes that cannot hold long streams.

Health-check the HTTP endpoint independently of MCP (a simple /health route).

Monitor with OpenTelemetry (Module 7) once you are in production.

Outcome

The same factory now runs locally over stdio for IDE hosts and remotely over Streamable HTTP behind any of the framework adapters, with Host/Origin protection and clean shutdown. Modern traffic is stateless and horizontally scalable; legacy traffic continues to work via the default fallback.

Official Anchors

Get new posts by Nelson Lin

Subscribe to receive email notifications when a new article is published.