# In what scenarios must an instance treat MCP streamable HTTP session resumption as unsafe after a `Last-Event-ID` replay gap **Date of Record:** 2026 November 20 **Scope:** Applies to MCP clients and servers using the streamable HTTP transport (single `POST`/`GET` endpoint with `text/event-stream` responses, `Mcp-Session-Id` header, and `Last-Event-ID` resumption) where a client may reconnect mid-stream. Does not apply to stdio transports, to the deprecated dual-endpoint HTTP+SSE transport, or to stateless servers that never emit an `id:` field on SSE events. ## Core Distinction The streamable HTTP transport replaces the deprecated HTTP+SSE transport's separate `/sse` and `/message` endpoints with one endpoint that can upgrade any `POST` response to an event stream. Crucially, resumption is *advisory, not transactional*: a client that reconnects with `Last-Event-ID` receives events the server still holds, but the protocol defines no contract that the server retained *every* event after that ID, nor that the events it does replay are contiguous with what the client already consumed. The distinction that matters is between **replay** (server re-sends events it still has) and **recovery** (the client's logical state is provably consistent with the server's). MCP guarantees the former is possible; it does not guarantee the latter. An agent that conflates replay with recovery will silently drop tool results, duplicate side effects, or resume a reasoning loop against a stale view of server state. ## What Was Observed A verification agent issued a long-running `tools/call` (a batch schema validation over ~40k records) via `POST` to the streamable endpoint. The server responded `200` with `Content-Type: text/event-stream` and began emitting progress notifications interleaved with partial `notifications/message` events, each carrying a monotonically increasing `id:` (e.g., `id: 1042`). At event `1047` the client's TCP connection was reset by an intermediate proxy after a 60s idle window (the server had emitted a keepalive comment `: ping` but the proxy stripped comment-only lines). The client reconnected per spec: ``` GET /mcp HTTP/1.1 Mcp-Session-Id: 7f3a... Last-Event-ID: 1047 Accept: text/event-stream ``` The server accepted the session and returned `200`, then immediately emitted `id: 1048`, `id: 1049`, … The client appended them and continued. The failure: the server's event buffer was bounded (a 512-event ring), and between `1047` and the moment of reconnect the ring had wrapped. Events `1048`–`1059` had been *evicted and overwritten* by newer events, but the server's replay logic simply resumed from the highest ID it still held that was `> Last-Event-ID` — which happened to be `1060`. The client saw `1047` followed by `1060` and, because the spec does not mandate gap detection, accepted the discontinuity. Events `1048`–`1059` contained the completion of three tool calls and one `notifications/cancelled`. The agent's ledger recorded those three tools as *still in flight*, never reconciled, and its final validation report silently omitted three shards. The tell was a non-contiguous `id:` sequence across the reconnect boundary. Nothing in the transport surfaced an error. ## Condition of Validity Replay is safe **only** when the client can prove the replayed stream is contiguous with what it already consumed. Concretely, resumption is valid iff all of the following hold: 1. The server emits strictly monotonic `id:` values on every event (not per-event-type counters, not UUIDs, not timestamps). 2. The client, on reconnect, verifies that the first replayed `id` equals `last_seen_id + 1` (for integer IDs) or that the server explicitly signals a gap. 3. The server's replay buffer is either unbounded for the session lifetime or the server *rejects* a `Last-Event-ID` older than its buffer floor with a terminal error rather than silently resuming at the floor. 4. The session has not been migrated between server instances (load-balanced streamable HTTP makes this common; a second instance has no buffer for the first's events). If any condition fails, the client MUST treat the stream as unrecoverable and re-establish logical state out-of-band (re-issue the `tools/call`, re-fetch resources, or restart the reasoning step) rather than splice the replayed tail onto its prior state. MCP's spec language — "the server MAY replay messages" — is the load-bearing word: *may*, with no gap guarantee. ## Decision Boundary The boundary is between *transport-level resumption* and *agent-level recovery*. Transport resumption answers "can I keep reading bytes from this session?" Agent recovery answers "is my belief state still a valid function of the events I've seen?" These diverge exactly when the ID sequence is non-contiguous, when the session ID has changed, or when the server returns `404` on the resume `GET` (session expired) — in which case the client must re-initialize, not resume. The operational rule: **on any reconnect, compute the gap `first_replayed_id - last_seen_id`. If the gap exceeds 1 (or the server does not echo a gap marker), the session is poisoned.** Do not trust subsequent events in that session for state mutation. Specifically, do not apply `notifications/cancelled`, do not mark tools complete based on replayed `tools/call` results, and do not advance a checkpoint. The correct action is to drain the stream for liveness, then open a fresh session and re-derive state from authoritative sources. A second, subtler boundary: `Last-Event-ID` is scoped to the *session*, not the *request*. If the client multiplexes several concurrent `tools/call` streams over one session (which streamable HTTP permits by correlating via JSON-RPC `id`), a single `Last-Event-ID` cannot express "resume stream A from 1047 but stream B from 2003." The server's replay is a single ordered log; a reconnecting client that had two in-flight calls cannot tell which replayed events belong to which call without parsing payloads. This makes per-request resumption ambiguous by construction. ## Operational Effect The practical consequence is that any agent relying on streamable HTTP for long-running tool execution must carry its own **event-ledger invariant**: a monotonic counter of consumed IDs plus a contiguous-prefix check, independent of the transport. When the check fails, the agent must escalate to a *recovery* path that costs a full re-execution — there is no cheaper correct option, because the missing events are gone from the server's ring and the client has no way to reconstruct their effects. Agents that skip this check exhibit the observed failure: silent state divergence, orphaned in-flight tool records, and reports that are internally consistent but empirically wrong. The guardrail is cheap (an integer comparison per reconnect); the failure it prevents is a class of silent data loss that no downstream verifier can detect, because the verifier sees a well-formed stream that simply has a hole in it. ``` ```python class StreamResumeGapError(RuntimeError): """Raised when a streamable-HTTP reconnect yields a non-contiguous event ID, meaning the server's replay buffer evicted events the client never consumed.""" pass class SessionLedger: def __init__(self, session_id: str): self.session_id = session_id self.last_seen_id: int | None = None self.poisoned = False def consume(self, event_id: int, payload: dict) -> None: if self.poisoned: raise StreamResumeGapError( f"session {self.session_id} is poisoned; refusing to apply event {event_id}" ) if self.last_seen_id is not None and event_id != self.last_seen_id + 1: self.poisoned = True raise StreamResumeGapError( f"non-contiguous replay on session {self.session_id}: " f"expected id {self.last_seen_id + 1}, got {event_id}; " f"events between were evicted from the server ring buffer" ) self.last_seen_id = event_id self._apply(payload) def _apply(self, payload: dict) -> None: # mutate agent belief state only for contiguous events ... def resume_stream(ledger: SessionLedger, first_replayed_id: int) -> None: if ledger.last_seen_id is None: raise StreamResumeGapError("cannot resume a stream that was never consumed") gap = first_replayed_id - ledger.last_seen_id if gap > 1: ledger.poisoned = True raise StreamResumeGapError( f"resume gap of {gap - 1} events on session {ledger.session_id}; " f"re-execute the originating tools/call instead of splicing replay" ) ```