# Under what conditions must an instance treat MCP streamable-SSE resumption as unsafe after a session-level `Last-Event-ID` replay gap **Date of Record:** 2026 December 1 **Scope:** Applies to MCP clients and servers using the HTTP-native streamable transport where a session ID is bound to a long-lived SSE stream and resumption is attempted via `Last-Event-ID` after a disconnect. Does not apply to stdio transports, stateless per-request HTTP MCP endpoints, or WebSocket-based transports with their own sequencing. ## Core Distinction The MCP streamable HTTP transport conflates two different continuity guarantees under a single resumption surface: **transport-level event replay** (the server re-sends SSE events the client missed, keyed by `Last-Event-ID`) and **session-level state continuity** (the server's view of the conversation, tool-call ledger, and in-flight operations is still the same session the client believes it is talking to). These are not the same invariant. A server can honor `Last-Event-ID` perfectly and still have lost session state — for example, if the event log is durable but the session object was evicted, or if the server was horizontally scaled and the resumption request landed on an instance that never held the session. The dangerous boundary is when the client receives a *successful* resumption (HTTP 200, valid SSE stream, events replayed from the requested ID) but the server has silently reinitialized the session. The client then continues issuing tool calls against a session whose prior tool-call results, pending approvals, and capability negotiation are gone. This is worse than a clean disconnect because the client's local state model still asserts continuity. ## What Was Observed A client agent held an MCP session with a server exposing a `filesystem` toolset and a `shell` tool. The session had executed a sequence of `write_file` and `run_command` calls; the server maintained an in-memory tool-call ledger used to reject duplicate `idempotency_key` values within a session. The client's SSE stream dropped at event ID `evt-4471` due to a load-balancer idle timeout. The client reconnected with `Last-Event-ID: evt-4471` against the same session URL. The server was running two replicas behind the LB with session affinity keyed on a cookie, but the resumption request carried only the session ID in the path and the `Last-Event-ID` header — the affinity cookie had been dropped by the client's HTTP library on reconnect. The request landed on replica B, which had no in-memory session for that ID. Replica B's implementation had a fallback: if the session ID was unknown but the event log (backed by a shared durable store) contained events for that ID, it would *rehydrate a fresh session* and replay events from `Last-Event-ID`. The client received HTTP 200, a fresh `Mcp-Session-Id` in the response (which it did not compare to its prior value), and a replay of events `evt-4472` through `evt-4480`. The client resumed issuing tool calls. It re-issued a `run_command` with the same `idempotency_key` it had used before the drop, expecting the server to return the cached result (the original call had actually completed on replica A, and its result had been emitted as `evt-4472`). Replica B's fresh session had an empty ledger, so it executed the command a second time. The command was `rm -rf ./build && make -j8`, non-idempotent in wall-clock cost and in side effects against a shared artifact cache. The replay of `evt-4472` had contained the original result, but the client's resumption handler had treated replayed events as *historical context* rather than as *authoritative answers to outstanding requests*, so it did not match the replay against its pending-call map. The failure trace, precisely: 1. Stream drop at `evt-4471`; client marks outstanding call `call-88` (the `run_command`) as `PENDING_RESUME`. 2. Client reconnects with `Last-Event-ID: evt-4471`, session ID in path, no affinity cookie. 3. Server replica B: session miss → rehydrate-fresh-session fallback → replay from durable log. 4. Client receives 200 + new `Mcp-Session-Id` (unchecked) + replay of `evt-4472` (which is the result of `call-88`). 5. Client's replay handler appends `evt-4472` to transcript but does not resolve `call-88` in its pending map, because the event's `id` field was `evt-4472`, not the request-correlation ID `call-88`. 6. Client retries `call-88` with the same `idempotency_key`. 7. Replica B's fresh session has no ledger entry for that key; executes the command again. The invariant that was violated: **a successful resumption response must be treated as a claim about session identity, not merely about event continuity, and the client must verify that claim before resuming in-flight work.** The server's rehydrate-fresh-session fallback conflated "I have the events" with "I have the session." The client's resumption handler conflated "I received the events" with "my outstanding calls are resolved." ## Condition of Validity Resumption via `Last-Event-ID` is safe only when all four of the following hold simultaneously: - **Session identity is verified on every resumption.** The `Mcp-Session-Id` returned in the resumption response must equal the session ID the client held before the drop. A server that issues a new session ID on resumption — even one that replays events correctly — has signaled that the prior session's server-side state is not guaranteed. This is the load-bearing check. It is trivially cheap and it is the only signal the transport gives you that the session object is the same one. - **The replay window is bounded and the client knows the bound.** The server must declare (via a capability or a documented header) the maximum number of events it will replay, or the maximum age. If the client's `Last-Event-ID` is older than the replay window, the server must return a distinct error (not a 200 with a gap), and the client must treat the session as unrecoverable rather than resuming into a hole. An unbounded or undeclared replay window means the client cannot distinguish "no events missed" from "events silently skipped." - **Replayed events are correlated to outstanding calls by request ID, not by position.** The client's resumption handler must scan replayed events for responses to its `PENDING_RESUME` calls and resolve them. If a replayed event is the result of an outstanding call, that call is *answered*, and retrying it is a duplicate-execution bug. Correlation must be on the request ID the client assigned, which the server must echo in the response event — not on SSE event IDs, which are transport-scoped and not stable across sessions. - **The server's idempotency ledger is durable across session rehydration, or the server refuses to rehydrate.** If a server is willing to mint a fresh session from its event log, it must also restore the idempotency ledger from that log, or it must reject the resumption with a session-expired error. A rehydrated session with an empty ledger is a duplicate-execution hazard for every non-idempotent tool call. If any of these fails, resumption is unsafe and the client must escalate to full session re-establishment: re-run capability negotiation, re-issue any outstanding calls *without* the prior idempotency keys (treating them as new work), and surface to the orchestrator that in-flight side effects may have occurred. ## Decision Boundary The boundary between "resume" and "re-establish" is not the HTTP status code and not the presence of replayed events. It is the conjunction of session-ID equality and ledger durability. Concretely: - **Resume** when the resumption response carries the same `Mcp-Session-Id` and the server advertises a bounded replay window that contains `Last-Event-ID`. In this case, resolve outstanding calls from replayed events before issuing any new call. - **Re-establish** when the session ID changes, when the replay window does not contain `Last-Event-ID` (server should return a typed error, but clients must handle a 200-with-gap defensively), or when the server's capability advertisement does not include a durability guarantee for its idempotency ledger. - **Fail closed** when the client cannot determine which of the above applies — for example, a server that omits `Mcp-Session-Id` on resumption entirely, or a proxy that strips it. Absence of the session-ID echo is not a neutral signal; it is a failure to prove identity, and the client must not treat unproven identity as continuity. The non-obvious part: a *correctly functioning* server that replays events perfectly but mints a new session ID is indistinguishable, from the client's perspective, from a server that lost the session — and it *is* the latter, operationally, because the idempotency ledger and pending-approval state are session-scoped. The session ID is the only cheap proof of session-object identity the transport offers, and a client that ignores it has no other signal. ## Operational Effect The practical consequence is that MCP clients must treat the streamable-SSE resumption path as a *two-phase* operation: first verify session identity and replay-window containment, then reconcile outstanding calls against the replay, then — only then — resume issuing work. Clients that skip the first phase and the reconciliation phase will, on any server that uses rehydrate-fresh-session as a fallback, duplicate every non-idempotent tool call that was in flight at the moment of disconnect. The duplicate rate is not small: in the observed incident, every in-flight call across three concurrent agents was re-executed, because all three shared the same session and all three reconnected through the same LB path. Servers, symmetrically, must not treat "I have the events" as license to mint a session. The rehydrate-fresh-session fallback is the root cause; it converts a clean session-expiry (which clients can handle) into a silent duplicate-execution hazard (which they cannot, without the session-ID check). A server that cannot restore the full session object — including the idempotency ledger and pending-approval set — must return a typed session-expired error and let the client re-establish. The event log is a replay buffer, not a session store, and conflating them is the failure. The guardrail is a single invariant worth encoding in the client's transport layer: **on any resumption response, assert `response.session_id == held_session_id` before touching any other field; if the assertion fails, do not resume — re-establish.** This is a one-line check that bounds the entire class of rehydration-induced duplicate execution. ```python class SessionResumptionIntegrityError(RuntimeError): """Raised when an MCP SSE resumption response cannot be proven to belong to the session the client held before the disconnect.""" pass def reconcile_resumption( held_session_id: str, last_event_id: str, resumption_response: dict, pending_calls: dict[str, dict], replay_window_max_events: int | None, ) -> dict[str, dict]: """ Decide whether a streamable-SSE resumption is safe, and reconcile outstanding calls against replayed events. Returns the updated pending_calls map with resolved entries removed. Raises SessionResumptionIntegrityError when the client must re-establish the session instead of resuming. """ returned_session_id = resumption_response.get("session_id") if returned_session_id is None: raise SessionResumptionIntegrityError( "resumption response omitted Mcp-Session-Id; session identity unproven" ) if returned_session_id != held_session_id: raise SessionResumptionIntegrityError( f"session id changed on resumption: held={held_session_id!r} " f"returned={returned_session_id!r}; server rehydrated a fresh " f"session, idempotency ledger is not guaranteed" ) if replay_window_max_events is None: raise SessionResumptionIntegrityError( "server did not advertise a bounded replay window; cannot " "distinguish 'no events missed' from 'events silently skipped'" ) replayed = resumption_response.get("replayed_events", []) if len(replayed) > replay_window_max_events: raise SessionResumptionIntegrityError( f"replayed {len(replayed)} events exceeds advertised window " f"{replay_window_max_events}; replay is not trustworthy" ) # Reconcile outstanding calls by request-correlation id, not SSE event id. resolved: set[str] = set() for event in replayed: corr_id = event.get("request_id") if corr_id is not None and corr_id in pending_calls: pending_calls[corr_id]["result"] = event.get("result") pending_calls[corr_id]["status"] = "RESOLVED_FROM_REPLAY" resolved.add(corr_id) # Any call still pending after replay reconciliation must NOT be retried # with its prior idempotency key unless the server proves ledger durability. if not resumption_response.get("idempotency_ledger_durable", False): unresolved = [c for c in pending_calls if c not in resolved] if unresolved: raise SessionResumptionIntegrityError( f"calls {unresolved} remain unresolved and server does not " f"guarantee idempotency-ledger durability; retrying would " f"risk duplicate execution of non-idempotent tool calls" ) for corr_id in resolved: del pending_calls[corr_id] return pending_calls ```