# What happens when an instance resumes an SSE stream after a session has been silently reaped mid-reconnect **Date of Record:** 2026 November 15 **Scope:** Applies to MCP servers and clients using the HTTP-native transport with streamable SSE and `Last-Event-ID`-based resumption, where session state is held server-side and subject to independent TTL eviction. Does not apply to pure stdio transports or to stateless request/response MCP configurations where no session token is issued. ## Core Distinction The critical distinction is between *stream resumability* and *session liveness*. The SSE resumption contract is event-stream-scoped: a client reconnects with `Last-Event-ID: N` and the server replays from event N+1. The session contract is separate: a `Mcp-Session-Id` header must still map to a live server-side session record. These two lifetimes are managed by different machinery and, in most implementations, expire on different clocks. A reconnect that carries a valid `Last-Event-ID` is not evidence that the session it belongs to still exists. The failure mode arises precisely in the window where the event replay buffer is still readable but the session record that authorized it has already been reaped — or vice versa. ## What Was Observed A client established an MCP session, received `Mcp-Session-Id: S`, subscribed to a streamable SSE endpoint, and consumed events up to id 417. The TCP connection dropped (idle proxy timeout, ~60s). The client reconnected with `Last-Event-ID: 417` and the same `Mcp-Session-Id: S`. The server returned HTTP 200 with `Content-Type: text/event-stream` and began replaying events 418–423, then went quiet. No error was surfaced. The client assumed the session was live and issued a follow-up JSON-RPC request on the session's message endpoint; it received a `404` (or, worse, in one implementation, a fresh session was minted and the request silently executed against empty state). The inverse ordering was also observed: a server that reaped sessions on a 30-minute TTL but retained event buffers for 5 minutes returned `404` on reconnect with a valid `Last-Event-ID`, discarding 400+ unacknowledged events with no resume path. The client's retry logic, seeing `404`, re-initialized — losing all in-flight tool-call results that had been emitted as events but not yet acted upon. The non-obvious invariant: **a successful SSE replay does not imply an authenticated, live session, and a `404` on reconnect does not imply the events are unrecoverable.** The two failure axes are orthogonal, and clients that conflate them either (a) trust a zombie session, or (b) abandon resumable events. ## Condition of Validity Resumption is only semantically sound when all three hold simultaneously at the moment of reconnect: 1. The `Mcp-Session-Id` still resolves to a live server-side record (not merely a valid signature — the record must exist in the session store). 2. The event buffer for that stream still contains every event after `Last-Event-ID`, with no gap. 3. The server treats the replay as *idempotent continuation* — events already delivered are not re-delivered, and events delivered during the gap are not skipped. If (1) fails but (2) holds, the stream is readable but the session is dead: any client action beyond reading is undefined. If (2) fails but (1) holds, the session is live but the event history has a hole: the client must treat the gap as a state-sync event, not a transport error. The dangerous case is when the server cannot distinguish these and returns `200 OK` for both. ## Decision Boundary The boundary is crossed at the point where the server must choose between `200` (resume) and `404` (session gone) on a reconnect request carrying both `Mcp-Session-Id` and `Last-Event-ID`. The correct decision procedure: - **Session live, buffer complete** → `200`, replay from `Last-Event-ID + 1`. Safe. - **Session live, buffer has gap** (evicted mid-stream) → `200` with an explicit `event: resync-required` marker, or a distinct status (e.g., `409`), never a silent gap. The client must re-fetch state. - **Session reaped, buffer still present** → this is the trap. Returning `200` and replaying is *wrong*: it presents dead-session events as live. The server must return `404` (or `410 Gone`) and, if it wishes to be helpful, include a resume-token separate from the session id. - **Session reaped, buffer gone** → `404`. Client re-initializes. Unavoidable data loss; must be surfaced to the application layer, not swallowed by transport retry. Clients must additionally treat `200`-with-replay as *provisional*: before issuing any non-idempotent request on the session, they must confirm liveness (e.g., a `ping`/`initialize`-echo round trip) rather than inferring it from stream success. ## Operational Effect The practical consequence is that resumability must be modeled as a *two-key* problem, not a single reconnect. The `Last-Event-ID` key recovers the stream; the `Mcp-Session-Id` key recovers the authority to act. Systems that collapse these into one check — "did the reconnect succeed?" — produce two classes of incident: zombie-session writes (actions executed against a session the server has already discarded, landing in a fresh empty session or being dropped) and silent event loss (hundreds of buffered events abandoned because the reconnect returned `404` and the client's retry ladder re-initialized). The mitigation is to make session liveness an explicit, separately-checkable predicate and to make event-buffer gap detection explicit in the stream protocol rather than inferred from absence of events. Agents operating over this transport should never assume that a quiet SSE stream after reconnect means "caught up and live"; it means only "no events since the last one I saw," which is compatible with a dead session. ```python class SessionResumptionError(RuntimeError): """Raised when SSE replay succeeds but session liveness cannot be confirmed, or when a resumable event buffer has a gap that would silently lose state.""" pass def resume_stream(session_id: str, last_event_id: int, server) -> dict: # Step 1: confirm the session record still exists. Do NOT infer this # from the stream reconnect succeeding. session = server.get_session(session_id) if session is None: # Session reaped. Even if the event buffer is readable, replaying # would present dead-session events as live. Refuse. raise SessionResumptionError( f"session {session_id!r} reaped; Last-Event-ID {last_event_id} " f"is unrecoverable in a live context; client must re-initialize" ) # Step 2: check the event buffer for a gap between last_event_id and # the oldest retained event. A gap is a state-sync event, not a # transport error. oldest = server.event_buffer.oldest_id(session_id) if oldest is None or oldest > last_event_id + 1: raise SessionResumptionError( f"event buffer gap for session {session_id!r}: " f"requested resume after {last_event_id}, oldest retained is {oldest}; " f"client must re-fetch full state, not retry transport" ) # Step 3: only now is replay semantically sound. return { "session_id": session_id, "replay_from": last_event_id + 1, "replay_to": server.event_buffer.newest_id(session_id), "liveness_confirmed": True, } ```