# In what scenarios must an instance treat MCP Streamable HTTP `Last-Event-ID` resumption as unsafe after a server-side session rotation **Date of Record:** 2026 November 21 **Scope:** Applies to MCP clients (agent runtimes) using the Streamable HTTP transport with `text/event-stream` responses and `Last-Event-ID`-based resumption against servers that may rotate or garbage-collect session state. Does not apply to stdio transport, nor to servers that guarantee monotonic, never-reclaimed event IDs across the lifetime of a logical session. ## Core Distinction The MCP Streamable HTTP transport conflates two independent notions of continuity: **transport-level resumption** (the SSE `Last-Event-ID` cursor, which the spec says a server *MAY* honor to replay missed events on a dropped stream) and **session-level identity** (the `Mcp-Session-Id` header, which binds an initialized conversation to server-held state). These are negotiated and validated by different mechanisms, on different timescales, with different failure semantics. The dangerous boundary is the case where the transport cursor is *still syntactically valid and accepted* while the session it refers to has been rotated, expired, or reclaimed underneath it. A client that treats a `200 OK` on a resumed GET as proof of continuity will silently splice events from a *different* logical session into its in-memory conversation state. The invariant that actually holds is narrower than most client implementations assume: **an accepted `Last-Event-ID` proves only that the server still has a replay buffer keyed by that cursor — it does not prove that the buffer belongs to the same session, the same tool-call sequence, or the same reasoning epoch the agent was in when the stream dropped.** ## What Was Observed An agent runtime maintained a long-lived GET stream to an MCP server for server-initiated notifications (progress updates on a long-running `tools/call`, plus `notifications/message` log events). The stream dropped mid-task due to an intermediate proxy idle-timeout at ~60s. The client reconnected per spec: new `GET /mcp` with `Accept: text/event-stream`, replaying `Last-Event-ID: 4471`, and — critically — *also* sending the existing `Mcp-Session-Id: sess-9f2a`. The server had, between the drop and the reconnect, evicted `sess-9f2a` from its session store (TTL sweep) but retained a *global* event ring buffer. The reconnect returned `200 OK` with `text/event-stream`, and the server replayed events `4472..4489`. Those events belonged to a *different* client's session that had been assigned overlapping event IDs after the ring buffer wrapped. The client accepted them. Two of the replayed `notifications/progress` events referenced a `progressToken` the agent had never issued; one `notifications/message` carried a tool result fragment for a `call_id` that did not exist in the agent's ledger. The agent's failure trace: 1. `GET /mcp` → `200`, stream open, no `Mcp-Session-Id` validation performed by client because the status was `200`. 2. Replayed events appended to the in-memory event log without `progressToken`/`call_id` cross-check. 3. A `notifications/message` with an unknown `call_id` was routed to the tool-result dispatcher, which — lacking a matching pending call — logged a warning and *dropped* it. No error surfaced to the reasoning loop. 4. The agent's `tools/call` for the actual in-flight operation never received its terminal notification (it was in the *evicted* session's buffer, now gone). The reasoning loop blocked on the pending call until its own local timeout, then re-issued the call. 5. The re-issued call executed server-side a second time. Side effect duplicated. The non-obvious part: step 1 looked like success. The spec's resumption contract was *satisfied* at the HTTP layer. The corruption was purely in the semantic layer, and it was invisible because the client never re-validated session identity after a successful reconnect. ## Condition of Validity `Last-Event-ID` resumption is safe **only** when all of the following hold simultaneously: - The server's event IDs are **session-scoped and monotonic**, not drawn from a global ring buffer. (The spec does not mandate this; it is a server implementation choice, and clients cannot assume it.) - The reconnect response either echoes the same `Mcp-Session-Id` the client sent, or omits it in a way the client treats as "same session." A *different* `Mcp-Session-Id` in the response is a rotation signal and must invalidate the cursor. - The client has a **pending-call ledger** keyed by `progressToken`/`call_id`, and every replayed event is validated against it before being applied to state. - The server's replay buffer retention window is known to exceed the maximum plausible stream-drop duration for the deployment. The invariant: **cursor validity and session validity are orthogonal; a client must verify both independently on every reconnect, and must treat a session mismatch as a hard reset of resumption state, not a soft retry.** ## Decision Boundary On a stream drop, the client faces a decision the spec leaves underspecified: - **Resume (send `Last-Event-ID`)** — correct only if session identity is confirmed unchanged *and* the replay is validated against the pending-call ledger. Cheap, preserves continuity. - **Re-initialize (new `initialize`, drop cursor)** — correct if the session was rotated, expired, or the server returns `404` on the session, or if any replayed event fails ledger validation. Expensive (loses in-flight progress notifications) but safe. - **Fail closed** — correct if the client cannot distinguish the above (e.g., server returns `200` but omits `Mcp-Session-Id` and the client has no ledger). Surface the ambiguity to the reasoning loop rather than guessing. The boundary is crossed the moment a client treats `200 OK` on reconnect as sufficient grounds to resume. The correct predicate is `(response.session_id == request.session_id) AND (all replayed events validate against ledger)`. Any `200` that fails either conjunct must be downgraded to re-initialize. ## Operational Effect Clients that do not enforce this boundary exhibit **silent cross-session event splicing**: the agent's conversation state absorbs notifications from unrelated sessions, pending calls silently lose their terminal events, and the reasoning loop compensates by re-issuing calls — producing duplicate side effects on non-idempotent tools. The failure is intermittent and load-dependent (it requires a session eviction to coincide with a stream drop and a buffer-ID collision), which makes it nearly impossible to reproduce in isolation and easy to misattribute to "flaky tool calls." The guardrail is a **reconnect gate**: on every stream re-establishment, the client must (a) compare returned `Mcp-Session-Id` to the one sent, (b) validate every replayed event's `progressToken`/`call_id` against the pending ledger, and (c) on any mismatch, discard the cursor, re-initialize the session, and re-issue any calls whose terminal events were lost — with idempotency keys so the re-issue does not duplicate side effects. ```python class SessionResumptionViolation(RuntimeError): """Raised when a stream reconnect cannot be proven to belong to the current session.""" pass def resume_stream(client, session_id: str, last_event_id: str, pending_calls: dict): resp = client.get( "/mcp", headers={ "Accept": "text/event-stream", "Mcp-Session-Id": session_id, "Last-Event-ID": last_event_id, }, stream=True, ) if resp.status_code != 200: # 404 on session => rotate; anything else => fail closed. return client.reinitialize(reason=f"resume_status_{resp.status_code}") returned_session = resp.headers.get("Mcp-Session-Id") # Invariant 1: session identity must be unchanged. if returned_session is not None and returned_session != session_id: raise SessionResumptionViolation( f"session rotated mid-stream: sent {session_id!r}, got {returned_session!r}; " f"cursor {last_event_id!r} is no longer trustworthy" ) # Invariant 2: every replayed event must validate against the pending-call ledger. for event in client.iter_sse_events(resp): token = event.data.get("progressToken") or event.data.get("call_id") if token is not None and token not in pending_calls: raise SessionResumptionViolation( f"replayed event {event.id!r} references unknown token {token!r}; " f"likely cross-session buffer splice" ) yield event # Invariant 3: if session header was absent AND we cannot validate, fail closed. if returned_session is None and not client.has_pending_ledger(): raise SessionResumptionViolation( "reconnect returned 200 without session confirmation and no ledger to validate replay" ) ```