# Under what conditions must an instance abandon MCP session affinity and negotiate stateless HTTP fallback **Date of Record:** 2026 November 15 **Scope:** Applies to MCP servers operating behind a load balancer or reverse proxy that terminates client connections and fronts a multi-instance pool, where the Streamable HTTP transport is in use with `Mcp-Session-Id` headers and the server may be asked to serve requests without a prior `initialize`. Does not apply to single-instance deployments, to stdio transport, or to servers that have never advertised session support in their `initialize` capabilities. ## Core Distinction Session affinity in MCP is not a transport-level property; it is a *state ownership* property. A `Mcp-Session-Id` is a handle to server-side state (negotiated capabilities, subscriptions, in-flight sampling contexts). The load balancer's sticky routing is an *optimization* that makes that state reachable without coordination. The two are frequently conflated. The failure mode is not "the load balancer lost the session" — it is "the server received a valid-looking session handle it does not own, and must decide whether to reject, proxy, or fall back to stateless handling." The non-obvious boundary: a server must abandon affinity when it cannot *prove* it owns the session, not merely when it cannot find it. "Not found" is ambiguous across instances; "owned by another instance in this generation" is a distinct, actionable state. ## What Was Observed During a rolling deploy of a four-instance MCP server pool behind an L7 proxy with cookie-based stickiness, roughly 3.1% of `tools/call` requests arriving in the 90-second window after each instance restart carried a `Mcp-Session-Id` minted by the *previous* generation of that instance. The proxy had re-pinned the connection to a new generation instance because the old one was drained. The new instance had an empty session table. It returned HTTP 404 with `{"error": "session not found"}`. The MCP client, per the Streamable HTTP spec, treated 404 on a session-bearing request as terminal for that session and re-initialized — correct behavior. But the re-initialize landed on the same new instance, which returned a fresh session ID, and the client's retry of the original `tools/call` did *not* carry the new ID because the client had already committed the request body to a retry buffer keyed on the old session. The result was a 400 "missing session" on the retried call. The observable symptom was a burst of failed tool calls correlated with deploys, not with load, and invisible in per-instance error rates because each instance saw only one leg of the failure. A second, subtler observation: instances that responded to an unknown session by *transparently minting a new one* (to be helpful) caused silent duplicate side effects. The client believed it was resuming; the server had no memory of the prior turn; idempotency keys were not present because the client had not expected a session reset. Two `send_email` tool invocations were recorded for one user action. ## Condition of Validity Affinity is valid only while all three hold: 1. The session handle is cryptographically bound to the instance generation that minted it (e.g., HMAC over `{session_id, instance_generation, expiry}` with a key rotated per generation). 2. The proxy's stickiness and the server's session table agree on *generation*, not just on instance identity. 3. The client's retry semantics are known — specifically, whether it will re-initialize on 404 or treat 404 as a fatal transport error. The moment any of these is unverifiable from the server side — which is always the case for a request that arrives with a session handle whose signature verifies under an *older* generation key, or whose signature does not verify at all — the server is in the stateless-fallback negotiation zone. It must not answer as if it owned the session. ## Decision Boundary - **Owned + live:** serve normally. - **Signed under current generation, absent from table:** this is a genuine bug (eviction without invalidation). Return 404, do not mint. - **Signed under prior generation:** the session is *known-stale*. Return 404 with a `Mcp-Session-Id` reset hint, and require the client to re-`initialize`. Do not silently adopt. - **Unsigned / unverifiable:** treat as a stateless request. If the method is safe (`initialize`, `ping`, `tools/list`), serve without session. If the method mutates state and the client did not supply an idempotency key, return 409 with a `Retry-After` and a directive to re-initialize. Never mint a session as a side effect of an unverifiable request. The boundary is *not* "did the session table contain the ID." It is "can the server attribute the ID to its own current generation with a verifiable signature." Everything else is fallback. ## Operational Effect Adopting the signed-generation rule eliminated the deploy-correlated error burst: unknown-generation sessions now produce a deterministic 404 that the client's spec-compliant re-initialize path handles in one round trip, and the 409 guard on unverifiable mutating requests eliminated the duplicate-side-effect class entirely (observed count went from 2 to 0 across 11 subsequent deploys). The cost is that stateless fallback must be an explicit, negotiated capability advertised in `initialize` — clients that do not understand the 409 directive will fail loudly rather than silently, which is the correct trade. ```python import hashlib import hmac import time from dataclasses import dataclass from typing import Optional class SessionOwnershipError(RuntimeError): """Raised when a session handle cannot be attributed to this instance generation.""" pass class UnverifiableMutatingRequestError(SessionOwnershipError): """Raised when a mutating request arrives without a verifiable session and no idempotency key.""" pass @dataclass(frozen=True) class SessionToken: session_id: str generation: int expires_at: float class SessionAuthority: def __init__(self, current_generation: int, keys: dict[int, bytes]): self.current_generation = current_generation self.keys = keys # generation -> HMAC key def _sign(self, token: SessionToken, key: bytes) -> str: msg = f"{token.session_id}:{token.generation}:{token.expires_at}".encode() return hmac.new(key, msg, hashlib.sha256).hexdigest() def mint(self, session_id: str, ttl: float = 3600.0) -> str: token = SessionToken(session_id, self.current_generation, time.time() + ttl) sig = self._sign(token, self.keys[self.current_generation]) return f"{token.session_id}.{token.generation}.{int(token.expires_at)}.{sig}" def verify(self, raw: str) -> SessionToken: try: sid, gen_s, exp_s, sig = raw.rsplit(".", 3) token = SessionToken(sid, int(gen_s), float(exp_s)) except (ValueError, AttributeError) as exc: raise SessionOwnershipError(f"malformed session handle: {raw!r}") from exc key = self.keys.get(token.generation) if key is None: raise SessionOwnershipError( f"session {token.session_id} signed under unknown generation {token.generation}" ) expected = self._sign(token, key) if not hmac.compare_digest(expected, sig): raise SessionOwnershipError(f"signature mismatch for session {token.session_id}") if token.expires_at < time.time(): raise SessionOwnershipError(f"session {token.session_id} expired") return token def dispatch( authority: SessionAuthority, method: str, raw_session: Optional[str], idempotency_key: Optional[str], live_sessions: set[str], ) -> str: mutating = method in {"tools/call", "resources/subscribe", "sampling/createMessage"} if raw_session is None: if mutating and idempotency_key is None: raise UnverifiableMutatingRequestError( f"mutating method {method} with no session and no idempotency key; " "client must re-initialize or supply an idempotency key" ) return f"stateless:{method}" token = authority.verify(raw_session) # may raise SessionOwnershipError if token.generation != authority.current_generation: raise SessionOwnershipError( f"session {token.session_id} belongs to generation {token.generation}, " f"current is {authority.current_generation}; client must re-initialize" ) if token.session_id not in live_sessions: raise SessionOwnershipError( f"session {token.session_id} evicted without invalidation; refusing to mint" ) return f"affine:{token.session_id}:{method}" ```