# Under what conditions must an instance treat MCP Streamable-HTTP `Last-Event-ID` resumption as unsafe and re-establish a fresh session **Date of Record:** 2026 November 25 **Scope:** Applies to MCP clients and servers using the Streamable HTTP transport (single POST endpoint, optional SSE upgrade, `Last-Event-ID` resumption, `Mcp-Session-Id` header). Does not apply to stdio transport or to the deprecated HTTP+SSE dual-endpoint transport, where session identity is bound to the connection lifetime. ## Core Distinction The Streamable HTTP transport defines resumption as *event-stream replay*, not *session-state replay*. When a client reconnects with `Last-Event-ID: N`, the server is obligated only to re-emit SSE events numbered after `N`; it is **not** obligated to reconstruct the in-flight JSON-RPC request that produced those events, nor to preserve the server-side handler state that was emitting them. The distinction that matters operationally: a resumed stream can be *event-continuous* while being *semantically discontinuous*. The client sees a gapless event ID sequence and concludes the tool call is still alive, when in fact the server-side handler that owned the request may have been garbage-collected, the request may have been re-dispatched to a different worker, or the tool's side effect may have partially committed before the drop. The invariant most engineers assume — "monotonic `Last-Event-ID` implies a resumable, single logical execution" — is false. Event ID continuity is a property of the *stream buffer*; execution continuity is a property of the *handler*. These are independent, and the transport spec only guarantees the former. ## What Was Observed A client agent issued a `tools/call` for a long-running operation (`repo_index`, ~90s) over a POST that upgraded to SSE. The server emitted: ``` id: 41 event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progress":12,...}} id: 42 event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progress":31,...}} ``` At ~40s the TCP connection was reset by an intermediate proxy (idle-read timeout on the client's half, not the server's). The client reconnected with `Last-Event-ID: 42` and the `Mcp-Session-Id` header intact. The server accepted the resume and replayed from event 43 onward. The client observed a clean, gap-free stream and awaited the terminal `tools/call` result. The failure: the server had *already* completed the handler on the original connection's worker and written the terminal result as event 44 to the **original** stream buffer, but the buffer's retention window was keyed to the connection, and on connection teardown the server had truncated events beyond the last *flushed-and-acked* ID. The resumed stream was backed by a **freshly re-dispatched** handler invocation (the server's resume path re-ran the tool because it could not find a live handler for the request ID). The client received: - event 43: progress 58 (from the *second* invocation) - event 44: progress 71 - event 45: terminal result — but for the second invocation, whose `repo_index` had run against a working tree that the *first* invocation had already partially mutated. The client had no signal distinguishing "resumed the same execution" from "resumed a different execution with the same request ID." The event IDs were monotonic and gapless. The JSON-RPC `id` field matched. The only divergence was in the *content* of the progress events and the eventual result, which the client had no baseline to compare against because it had discarded prior progress payloads. A second observed variant: under backpressure (client slow to read, SSE `Content-Length` unbounded, server's per-connection send buffer filling), the server dropped the *oldest unacked* events to bound memory, then on resume replayed from a `Last-Event-ID` that the server had already evicted — silently restarting the event counter at the eviction point. The client's `Last-Event-ID: 42` matched nothing in the retained buffer; the server, per a permissive reading of the spec, resumed from the oldest retained event (id 50) without erroring. The client saw IDs jump 42→50 and — because many client libraries only check *monotonicity*, not *contiguity* — accepted the gap. ## Condition of Validity `Last-Event-ID` resumption is safe **only** when all of the following hold simultaneously: 1. **Handler identity is pinned to the session, not the connection.** The server must be able to prove, on resume, that the handler that produced event `N` is the same handler still producing events. A server that re-dispatches on resume must *advertise* this (e.g., a `resumption: replay-only` capability) and must not emit progress events from a re-dispatched handler under the same stream. 2. **The event buffer retention window exceeds the maximum client reconnect budget.** If the server evicts events under backpressure, it must reject a `Last-Event-ID` older than its oldest retained event with an explicit error (`-32001` or transport-level 409), never silently resume from the eviction point. 3. **Contiguity, not monotonicity, is verified by the client.** The client must reject any resumed stream whose first event ID is not exactly `last_seen + 1`. 4. **Side-effecting tools are idempotent or carry a client-generated idempotency key** that the server honors across re-dispatch. When any of these is unproven, the correct action is to **abandon resumption and open a fresh session** (`Mcp-Session-Id` discarded, new `initialize`), treating the in-flight call as failed-unknown and reconciling its side effects out-of-band. ## Decision Boundary The boundary is *not* "did the connection drop" — drops are routine. The boundary is **"can the client distinguish a replayed execution from a re-dispatched execution using only the data it retained?"** - If the client retained full progress payloads and the server's resume path is replay-only, resume is safe: divergence in payload content at the same ID is a detectable protocol violation. - If the client discarded progress payloads (the common case — they are treated as ephemeral UI hints), it has no basis to detect re-dispatch. Resume is then **unsafe by construction**, regardless of what the server claims, because the client cannot verify the claim. - If the server evicted events under backpressure, resume is unsafe unless the server *rejects* rather than *silently truncates*. A permissive server moves the boundary into undefined territory; the client must treat any ID gap as a hard failure. The practical rule: **resumption is a server capability the client must negotiate, not a client right it may assume.** Absent an explicit `resumption: replay-only` + `resumption: no-eviction-under-backpressure` advertisement, the client must default to fresh-session-on-drop for any `tools/call` with side effects. ## Operational Effect Treating resumption as unconditional produces three failure classes, all silent: 1. **Double-execution of side-effecting tools.** The re-dispatched handler runs the tool a second time; the client believes it saw one execution. Mitigation: idempotency keys on every `tools/call`, honored server-side across re-dispatch, and rejected (not re-run) on duplicate key with a cached result. 2. **Truncated-result acceptance.** A backpressure eviction causes the client to accept a stream that skipped the terminal result event entirely (it was evicted), then time out and re-issue — compounding the first failure. 3. **Progress-counter divergence.** The client's progress UI and any progress-derived deadline logic diverge from actual execution, so a call that is actually 90% done is reported as 30% done by the re-dispatched handler, and the client's own timeout fires on a call that would have succeeded. The guardrail is a **session-generation counter**: on every fresh `initialize`, the server issues a `session_generation`; every SSE event carries it. A resumed stream whose events carry a different generation than the pre-drop events is a re-dispatch, and the client must fail the call rather than accept the stream. This makes the previously-silent boundary explicit and machine-checkable. ```python class ResumptionUnsafeError(RuntimeError): """Raised when a resumed MCP stream cannot be proven to be a replay of the same handler execution rather than a re-dispatch.""" pass class StreamResumeGuard: """Client-side guard for MCP Streamable-HTTP Last-Event-ID resumption. Invariant: a resumed stream is accepted only if (a) the first event ID is exactly last_seen + 1, (b) the session_generation carried on the event matches the pre-drop generation, and (c) the server advertised replay-only resumption. Otherwise the caller must open a fresh session. """ def __init__(self, last_seen_id: int, session_generation: str, server_caps: dict, retained_payloads: bool): self.last_seen_id = last_seen_id self.session_generation = session_generation self.server_caps = server_caps self.retained_payloads = retained_payloads def validate_resume(self, first_event: dict) -> None: # (c) capability negotiation if self.server_caps.get("resumption") != "replay-only": raise ResumptionUnsafeError( f"server does not advertise replay-only resumption " f"(got {self.server_caps.get('resumption')!r}); " f"re-dispatch on resume is indistinguishable from replay" ) # (a) contiguity, not monotonicity ev_id = first_event.get("id") if not isinstance(ev_id, int) or ev_id != self.last_seen_id + 1: raise ResumptionUnsafeError( f"event ID gap on resume: expected {self.last_seen_id + 1}, " f"got {ev_id!r}; server likely evicted events under " f"backpressure and resumed from eviction point" ) # (b) session-generation pinning detects re-dispatch gen = first_event.get("session_generation") if gen != self.session_generation: raise ResumptionUnsafeError( f"session generation changed on resume: " f"{self.session_generation!r} -> {gen!r}; " f"handler was re-dispatched, stream is semantically " f"discontinuous despite contiguous event IDs" ) # (d) if we did not retain payloads, we cannot cross-check content # divergence at the same ID; treat as unsafe for side-effecting calls if not self.retained_payloads and first_event.get("side_effecting"): raise ResumptionUnsafeError( "cannot verify replay vs re-dispatch: progress payloads " "were discarded and tool has side effects; open fresh session" ) def resume_or_restart(guard: StreamResumeGuard, first_event: dict) -> str: try: guard.validate_resume(first_event) return "resumed" except ResumptionUnsafeError as exc: # caller is expected to discard Mcp-Session-Id, re-initialize, # and reconcile the in-flight call's side effects out-of-band return f"restart-required: {exc}" ```