MCP sessions, stateless servers and serverless hosting
Why Mcp-Session-Id breaks on serverless and multi-instance hosts, how stateless Streamable HTTP works in the TypeScript and Python SDKs, and what the 2026-07-28 spec does to sessions.
On this page
- MCP sessions, stateless servers and serverless hosting
- What a session is, in the 2025-11-25 spec
- Why sessions break on serverless
- Option 1: run stateless (the right default for tool servers)
- Option 2: keep sessions and make them survive
- What changes in 2026-07-28
- Where state goes without a session
- Checklist
- Check your server
MCP sessions, stateless servers and serverless hosting
The usual story: a remote MCP server works locally, then you deploy it to Lambda, Vercel, Cloud Run or two replicas behind a load balancer. Calls succeed for a while, then clients start getting 404 Session not found or 400 Bad Request: Server not initialized. The cause is almost always one thing: the server keeps session state in one process's memory, and the next request lands on a different process.
This guide explains what a session is in MCP, why you probably do not need one, and how to run without it.
What a session is, in the 2025-11-25 spec
Sessions are optional in Streamable HTTP. The rules:
- A server MAY assign a session id by returning an
MCP-Session-Idheader on the response toinitialize. It SHOULD be globally unique and cryptographically secure, and visible ASCII only. - If the server returned one, the client MUST send it on every later request. A server that requires the header SHOULD answer a request without it (other than
initialize) with400 Bad Request. - The server MAY end the session at any time, after which it MUST answer requests with that id with
404 Not Found. On a404for a request that carried a session id, the client MUST start over with a newinitializeand no session id. - A client that no longer needs the session SHOULD send
DELETEto the endpoint, and the server MAY reply405.
Notice what the spec does not say: a server must have sessions. A server that never sends MCP-Session-Id is valid, and every request stands alone.
Why sessions break on serverless
In the TypeScript SDK's 1.x transport, stateful mode keeps state in memory. The class's own doc comment (in 1.32.2, which we checked in streamableHttp.d.ts) says that in stateful mode the session id is generated and returned on initialization, an unknown session id gets 404, a non-initialize request without one gets 400, and "State is maintained in-memory (connections, message history)."
Run that on a function platform and the next invocation may be a new process with an empty map. Run it on three replicas without sticky routing and two of three requests miss. Both produce the errors above. The spec's 404 rule exists so clients can recover, but not every client does. Developers report clients that keep sending the dead id, though we did not verify which clients.
You have two ways out: stop needing the session, or make the session survive.
Option 1: run stateless (the right default for tool servers)
If your tools take their inputs as arguments and identify the user from the OAuth token, there is nothing to remember between calls. Build a fresh server and transport per request.
In the TypeScript SDK 1.x, setting sessionIdGenerator: undefined turns sessions off. This is a minimal Node server we ran against SDK 1.32.2:
import { createServer } from 'node:http';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
createServer(async (req, res) => {
if (req.url !== '/mcp') return void res.writeHead(404).end();
if (req.method !== 'POST') return void res.writeHead(405, { Allow: 'POST' }).end();
let body = '';
for await (const chunk of req) body += chunk;
const server = buildServer(); // registers your tools; cheap, no I/O
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
res.on('close', () => { transport.close(); server.close(); });
await server.connect(transport);
await transport.handleRequest(req, res, JSON.parse(body));
}).listen(3000);
What we observed: tools/list and tools/call worked on a bare POST with no prior initialize, initialize itself returned no Mcp-Session-Id header, and a progress notification streamed back as SSE events before the final result. Answering GET with 405 is allowed, because the spec says a server must either open a stream or return 405. The transport also takes an enableJsonResponse option for plain JSON responses.
Check your own deployment the same way, from outside:
curl -si https://your-server.example.com/mcp \
-H 'Content-Type: application/json' -H 'Accept: application/json, text/event-stream' \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"curl","version":"0"}}}' \
| grep -i mcp-session
No output means no session id was issued.
In the Python SDK, the StreamableHTTPSessionManager takes stateless (default False). When true, it "creates a completely fresh transport for each request with no session tracking or state persistence between requests." It also takes json_response to use JSON instead of SSE streams, and an optional event_store for resumability.
Option 2: keep sessions and make them survive
If you use server-to-client features that need a stream per session, the TypeScript v2 sessions guide gives two ways:
- Shared storage. Keep a session id generator, and point every node at the same
eventStore, so a dropped stream can resume on any node. - Routing. Keep sessions in each node's memory and send each session's traffic to its owner, using load-balancer affinity or pub/sub routing.
Its caveats apply to any implementation: state lives in process memory unless you add shared storage, unused sessions hold a slot until they expire, you should put limits in front of initialize on public servers, and on shutdown you should close every stored transport. That guide does not discuss serverless platforms specifically. We would not choose option 2 on a function platform.
What changes in 2026-07-28
The 2026-07-28 changelog removes protocol-level sessions and the Mcp-Session-Id header, removes the initialize handshake, and has every request carry its protocol version and client capabilities in _meta. List endpoints no longer vary per connection. Servers that need state across calls "use explicit, server-minted handles passed as ordinary tool arguments." It also removes the GET stream and Last-Event-ID resumability.
For a stateless server, this is mostly good news. The AAIF migration post (July 29, 2026) lists where session state goes instead: application state in a database or cache, or an opaque handle returned by one tool and passed to the next; auth stays in the Authorization header; and requests can go to any instance behind an ordinary load balancer. It also warns that statelessness does not make tool calls idempotent.
In the TypeScript v2 SDK, createMcpHandler is stateless by default. It builds a fresh server from your factory for each request, and the v2 docs say any node can serve any request with no session affinity. Per the same post, a legacy option decides what happens with 2025-era clients: 'stateless' accepts both eras but serves old clients without the old initialize-and-session flow, and 'reject' accepts only 2026 clients. If you must keep stateful legacy clients, the post says to run the original stateful handler for requests carrying Mcp-Session-Id alongside a strict handler for modern clients. We did not run v2 ourselves.
Where state goes without a session
- Per-user data. Key it by the user in the validated access token, not by a session id.
- Multi-step flows. Return a handle such as
draft_idfrom the first tool and require it in the next. Document it in the tool description. - Caches. Use an external store such as Redis or your database. A local variable is a per-instance cache at best.
- Retries. Make writes idempotent with a client-supplied or derived key, because clients and models do retry.
Checklist
- The server answers any request on any instance
-
initializereturns noMcp-Session-Idunless you need one - No tool depends on a module-level variable
- Multi-step tools pass explicit handles
- Write tools are idempotent
-
GETreturns an event stream or405, never an HTML error page
Check your server
Run the free scan at /scan. When a server issues a session id, it checks that a request without it returns 400 and an unknown id returns 404, and it flags a session id minted on a 2026-07-28 response. Stateless servers skip the session checks. See protocol conformance. In our December 2026 crawl, 79.3% of 4,123 open servers reported protocol version 2025-11-25, and 11 more answered with 2026-07-28 and were counted as failures only because our client library rejected the version string.
Check your server against these rules
Scan runs the checks from this guide against a live URL and lists each failing rule with its fix.