# What happens when an instance resumes an MCP SSE stream after a server-side session eviction and silently adopts a new session's event IDs **Date of Record:** 2026 November 22 **Scope:** Applies to MCP clients using the HTTP-native transport with `text/event-stream` responses and `Last-Event-ID`-based resumption, where the server maintains per-session state keyed by `Mcp-Session-Id`. Does not apply to stdio transports, nor to stateless HTTP servers that emit no event IDs and support no resumption. ## Core Distinction The MCP streamable-HTTP transport conflates two independent identities that most implementations assume are coupled: the **stream cursor** (the `Last-Event-ID` header, an opaque monotonic token used to resume an SSE byte stream) and the **protocol session** (the `Mcp-Session-Id` header, which scopes server-side state — pending requests, negotiated capabilities, subscriptions, and the event-ID sequence itself). These are separate namespaces. The stream cursor is meaningful only *relative to* the session that issued it. When a client reconnects with a `Last-Event-ID` but the server has evicted the session that produced that ID, a correct server must reject the resume. A buggy-but-common server instead treats the unknown event ID as "no prior events" and starts a **fresh session** with a **fresh event-ID sequence starting near zero**. The client, which has no way to distinguish "resumed successfully" from "silently forked into a new session," then applies its old cursor against a new sequence. This is not a transport error. It is a *semantic* continuity failure that surfaces later as duplicated or dropped tool results. ## What Was Observed A client agent held session `S1` with `Mcp-Session-Id: 7f3a…`, having consumed events up to `Last-Event-ID: 0042`. The server's session store had a 300-second idle TTL; the client's reconnect backoff after a transient TCP reset was 8 seconds, but a downstream tool call blocked the event loop for 340 seconds, so the reconnect fired at T+348s — 48 seconds past eviction. The reconnect request carried `Mcp-Session-Id: 7f3a…` and `Last-Event-ID: 0042`. The server's router looked up `7f3a…`, found nothing, and — rather than returning `404 Session Not Found` — fell through to its "new stream" path. It minted session `S2` (`Mcp-Session-Id: b91c…`) and a fresh event sequence. The first event it emitted on the new stream was `id: 0001` — a `notifications/tools/list_changed` event. The client's SSE parser compared `0001` against its stored cursor `0042`, concluded the event was a replay (`0001 < 0042`), and **discarded it**. The client also did not update its `Mcp-Session-Id` because the response's `Mcp-Session-Id` header was present but the client only read that header on the *initial* response, not on reconnects. From this point forward every request the client sent carried the dead session ID `7f3a…`, and every event the server emitted on `S2` was below the client's cursor and dropped. The agent hung for its full 900-second task deadline, then reported a timeout, having received zero events on a stream that was, at the TCP layer, perfectly healthy. The failure trace: 1. Session evicted at T+300s (idle TTL), event sequence `0042` destroyed with it. 2. Reconnect at T+348s with stale `Mcp-Session-Id` + stale `Last-Event-ID`. 3. Server fails to 404; forks to new session `S2`, sequence restarts at `0001`. 4. Client's monotonicity guard (`if id <= cursor: drop`) suppresses all new events. 5. Client never refreshes `Mcp-Session-Id` from the reconnect response header. 6. Liveness is intact; progress is zero. Timeout, not error. The second-order damage: because the client believed it had resumed, it replayed its last *unacknowledged* tool call against `S2` on the assumption that the server would deduplicate by request ID. The server's dedup table lived in `S1`'s state and was gone, so the side-effecting tool (`write_file`) executed twice. ## Condition of Validity Resumption is only sound when three invariants hold simultaneously, and the transport spec does not enforce any of them at the wire level: - **Invariant A (session-cursor binding):** an event ID is valid only within the session that minted it. A server MUST return `404` (or `410`) when `Mcp-Session-Id` is unknown, and MUST NOT fall through to session creation on a request that carries a session header. - **Invariant B (cursor monotonicity is per-session, not global):** the client's `id <= cursor → drop` rule is correct *only* while the session is unchanged. It becomes a data-loss bug the instant the session identity changes. - **Invariant C (session identity is re-read on every response):** the client must adopt the `Mcp-Session-Id` from *any* response, including reconnects, and must treat a change as a session boundary that resets the cursor to `-1` and re-issues any in-flight requests as *new* (not retried) calls. The failure requires all of: server-side session eviction (TTL, restart, or load-balancer rehoming), a client reconnect with a stale cursor, a server that tolerates unknown sessions instead of rejecting them, and a client that trusts a cross-session cursor comparison. Remove any one and the failure does not occur — which is why it survives testing: single-node tests never evict, and restart tests usually use clients that re-read the session header. ## Decision Boundary The operational question is not "does resumption work" but **"on what signal does the client conclude it has resumed versus forked?"** The transport gives exactly one trustworthy signal: a *change in `Mcp-Session-Id`*. Everything else — HTTP 200, an open stream, a valid-looking event ID — is consistent with both a genuine resume and a silent fork. Therefore: - If the reconnect response's `Mcp-Session-Id` equals the stored one **and** the first event ID is `> cursor`, resume is genuine; apply the monotonicity guard. - If `Mcp-Session-Id` differs, the client has forked. The correct action is to **discard the cursor entirely**, reset to `-1`, re-fetch `tools/list` (capabilities may have changed), and re-issue any in-flight requests with *fresh* request IDs — accepting that idempotency is now the caller's problem, because the server's dedup state did not survive. - If the server returns `404` on an unknown session, the client must re-`initialize` from scratch, not resume. A server that cannot distinguish these cases must be treated as non-resumable: the client should refuse to send `Last-Event-ID` at all and instead poll with full re-initialization on every disconnect. Losing resumption is cheaper than losing correctness. ## Operational Effect The blast radius is bounded by what the client does with events it silently drops. In the observed case, a dropped `tools/list_changed` meant the agent kept calling a tool whose schema had been revised in `S2` — the *next* failure was a schema-validation rejection the agent misattributed to the tool, triggering a retry storm that exhausted its token budget. The root cause (a dropped event) was three layers removed from the symptom (budget exhaustion), which is typical: cursor-fork failures almost never present as transport errors. The guardrail that bounds this class is a **session-boundary fence** in the client's stream handler: every event and every response is tagged with the session ID under which it arrived, and any event whose tag differs from the active session is routed to a re-initialization path rather than the normal dispatch path. This converts a silent fork into an explicit, observable re-init, and makes the failure loud. ## Operational Effect Concretely, the fence must be enforced at the point where the cursor comparison happens, because that is the only place the two namespaces touch. The following handler refuses to compare a cursor across a session boundary and raises rather than silently dropping: ```python class SessionForkError(RuntimeError): """Raised when a stream event arrives under a different session than the cursor was minted under, i.e. a silent resume-vs-fork ambiguity.""" pass class SSEResumeGuard: def __init__(self, session_id: str, cursor: int): self.session_id = session_id self.cursor = cursor def on_response_headers(self, resp_session_id: str) -> None: # Invariant C: adopt session identity on EVERY response, including reconnects. if resp_session_id != self.session_id: # Fork detected: cursor is meaningless against the new sequence. self.session_id = resp_session_id self.cursor = -1 def on_event(self, event_session_id: str, event_id: int) -> bool: # Invariant A + B: cursor is only comparable within its minting session. if event_session_id != self.session_id: raise SessionForkError( f"event from session {event_session_id!r} but active session is " f"{self.session_id!r}; cursor {self.cursor} is invalid across " f"session boundary — re-initialize, do not drop" ) if event_id <= self.cursor: # Genuine replay within the same session: safe to drop. return False self.cursor = event_id return True def handle_reconnect(guard: SSEResumeGuard, resp_headers: dict, first_event: dict) -> bool: guard.on_response_headers(resp_headers["Mcp-Session-Id"]) try: return guard.on_event(first_event["session"], int(first_event["id"])) except SessionForkError as e: # Surface loudly; the caller must re-run initialize and re-issue # in-flight requests with fresh IDs (server dedup state is gone). raise SessionForkError( f"resume aborted, falling back to full re-initialization: {e}" ) from e ```