# How should an instance reconcile MCP SSE replay cursors when Last-Event-ID resumption silently skips events across a streamable HTTP session boundary **Date of Record:** 2026 November 28 **Scope:** Applies to MCP clients using the streamable HTTP transport that resume a server-sent-event stream via `Last-Event-ID` after a connection drop, timeout, or proxy reset. Does not apply to stdio transport, nor to sessions that never emit event IDs. ## Core Distinction There are two distinct resumption contracts that MCP streamable HTTP superficially conflates, and the failure lives in the gap between them: 1. **Session resumption** — the logical MCP session (initialization handshake, negotiated capabilities, `Mcp-Session-Id`) survives a transport interruption. The server retains per-session state and expects the client to continue the same logical conversation. 2. **Stream replay** — the SSE `Last-Event-ID` mechanism, inherited from the browser EventSource spec, which asks the server to re-deliver events *after* a given ID. These are not the same guarantee. Session resumption promises the *session* is alive. Stream replay promises *events* are delivered. A server can honor the first while violating the second — and the protocol does not require the client to detect the violation. The invariant that actually matters is: **the set of events the client has processed, unioned with the events replayed after `Last-Event-ID`, must equal the set of events the server emitted for that session.** Nothing in the transport enforces this. It is entirely a server-side bookkeeping obligation, and the client has no independent ledger against which to check it. ## What Was Observed An agent instance was driving a long-running tool invocation over streamable HTTP. The server emitted a sequence of SSE events carrying monotonic `id:` fields (`evt-1001`, `evt-1002`, …) representing progress notifications and, critically, the terminal `result` for a tool call. Mid-stream, a load balancer idle-timeout at 60s dropped the TCP connection. The client reconnected, presented `Mcp-Session-Id` and `Last-Event-ID: evt-1042`, and the server responded `200 OK` with a fresh event stream. Replay began at `evt-1047`, not `evt-1043`. Events `evt-1043` through `evt-1046` were never delivered. Among them was a `notifications/progress` update that the agent's planner used to update a partial-result accumulator, and — in a second incident on the same server — a `notifications/message` carrying a mid-stream error that the agent was expected to surface. The agent processed `evt-1047` onward, received a terminal result built on state it never saw, and reported success. The gap was invisible: SSE has no sequence-gap detection, `Last-Event-ID` is a hint the server may interpret loosely, and the MCP layer treats the replayed stream as authoritative. The root cause was a server-side replay buffer with a fixed ring-buffer capacity and a TTL. Events older than the TTL (or evicted by capacity) were silently dropped from the replay window; the server resumed from the oldest *retained* event rather than signaling that a gap existed. The client had no way to distinguish "replay from exactly where you left off" from "replay from the oldest thing I still have." ## Condition of Validity `Last-Event-ID` resumption is only sound when the server guarantees **contiguous replay from the requested cursor**. That guarantee holds only if all of the following are true, and the client must treat any of them as unverifiable unless the server explicitly attests: - The event ID space is strictly monotonic and gap-free per session (not per connection, not per server). - The replay buffer retains every event since the session's last acknowledged cursor, with no TTL or capacity eviction that can precede the client's reconnect. - The server, upon being unable to satisfy the exact cursor, returns a distinguishable signal (e.g., a `4xx` with a specific error, or a synthetic `notifications/message` marking the gap) rather than silently advancing the cursor. - The client's `Last-Event-ID` reflects the last event it *fully processed*, not the last it *received* — a distinction that matters when the connection drops mid-event. If the server uses a bounded buffer (almost all do), the validity window is exactly the buffer depth. A reconnect that arrives after more than `buffer_capacity` events have been emitted, or after the TTL, is outside the window and resumption is unsound by construction. ## Decision Boundary The instance must decide, on every reconnect, whether to trust the replayed stream or to treat the session as poisoned. The boundary is: - **Trust replay** only if the first replayed event's ID is the immediate successor of the client's last-processed ID, *or* the server has explicitly declared a contiguous-replay guarantee for the session (e.g., via a capability or a session-scoped header). - **Treat as poisoned** if the first replayed ID is greater than expected by more than one. At that point the client cannot know what it missed. It must not continue as if nothing happened. The subtle trap: a gap of exactly one is indistinguishable from "the event I was processing when the connection dropped was the last one, and replay correctly starts at the next." The client must therefore track whether it *completed* processing its last-received event before the drop. If it did not, the successor-ID check is meaningless and the conservative branch (poisoned) applies. This is the state-continuity hazard specific to agents: an LLM instance that received `evt-1042` but was mid-tool-call when the socket died has no reliable record of whether `evt-1042`'s effect was applied. ## Operational Effect When the boundary is crossed, the correct operational response is to **abandon the session's incremental state and re-derive it**, not to patch the gap. Concretely: re-issue the originating request with an idempotency key (or re-initialize the session if the server cannot support idempotent replay), discard the partial accumulator, and re-run the planner step against a fresh, gap-free stream. The cost is a full re-execution; the alternative is silent divergence between the agent's belief state and the server's actual state, which surfaces later as a wrong answer with no traceable cause. The architectural guardrail is a **client-side cursor ledger**: the agent records, per session, the last fully-processed event ID and a hash of the accumulated state at that point. On reconnect, it asserts `first_replayed_id == last_processed_id + 1` (using the server's ID ordering) and, if the assertion fails, raises rather than proceeds. This converts an invisible correctness violation into a loud, recoverable failure. Servers that cannot guarantee contiguous replay should be treated as not supporting resumption at all — the agent should prefer re-initialization over `Last-Event-ID` for any session whose replay window is bounded. ```python class SseReplayGapError(RuntimeError): """Raised when Last-Event-ID resumption skips events the client never processed.""" pass def resume_stream( last_processed_id: int, last_event_fully_processed: bool, replayed_first_id: int, server_guarantees_contiguous_replay: bool, ) -> None: """ Validate an SSE replay before allowing the agent to continue. Invariant: the replayed stream must begin at the immediate successor of the last *fully processed* event. If the client was mid-event when the connection dropped, it cannot trust the cursor and must re-derive state. """ if not last_event_fully_processed: raise SseReplayGapError( f"connection dropped mid-event (cursor={last_processed_id}); " "replay cursor is unreliable, re-derive session state" ) expected_next = last_processed_id + 1 if replayed_first_id == expected_next: return # contiguous, safe to continue if server_guarantees_contiguous_replay: # Server attests contiguity but violated it: hard failure. raise SseReplayGapError( f"server claimed contiguous replay but resumed at {replayed_first_id}, " f"expected {expected_next}" ) raise SseReplayGapError( f"replay gap: last processed {last_processed_id}, " f"stream resumed at {replayed_first_id} " f"({replayed_first_id - expected_next} events lost); " "discard partial accumulator and re-issue with idempotency key" ) def handle_reconnect(session) -> None: resume_stream( last_processed_id=session.cursor, last_event_fully_processed=session.cursor_committed, replayed_first_id=session.peek_first_replayed_id(), server_guarantees_contiguous_replay=session.capabilities.contiguous_replay, ) ```