# Where must an instance bound SSE replay after an MCP streamable-session resume when the transport reports a cursor gap **Date of Record:** 2026 November 24 **Scope:** Applies to MCP servers/clients using the HTTP-native streamable transport with `Last-Event-ID`-style resumption, where a session spans a reconnect mid-stream. Does not apply to stdio transports or to fully buffered request/response (non-streaming) MCP exchanges. ## Core Distinction The distinction that matters is between *resumption* and *replay*. A streamable-session resume restores the transport channel; it does not restore the semantic state the agent had already consumed before the disconnect. The transport guarantees only that events after a cursor can be re-sent. It does not guarantee that the cursor is still within the server's retention window, nor that the events between the client's last-processed ID and the server's earliest-retained ID are recoverable. The failure mode is a **cursor gap**: the client reconnects with `Last-Event-ID: N`, the server has already evicted events `N+1 … M` (retention GC, ring-buffer eviction, or session migration to another replica), and the server resumes at `M+1`. The transport reports success. The agent's stream now looks continuous but is missing a contiguous block of events. For an agent, those events are not "logs"—they are tool results, partial reasoning deltas, and state-mutation notifications. A silent gap is a silent divergence between the agent's belief state and the server's actual state. The non-obvious invariant: **stream continuity is not state continuity**. A resume that succeeds at the transport layer can still be a semantic rollback. The agent must treat the cursor as a *warrant of completeness*, not merely a reconnection token. ## What Was Observed A planner agent held an MCP session to a `filesystem` server exposing a `watch` stream and a `write_file` tool. The stream emitted `file_changed` events carrying `path` and `content_hash`. The agent maintained a local map `path → hash` derived from these events and used it to decide whether a write was a no-op. Sequence: 1. Agent consumed events through `Last-Event-ID: 412` (hash for `a.txt` = `h1`). 2. Network blip; client reconnects with `Last-Event-ID: 412`. 3. Server had GC'd events `413–470` (its retention window is 60s / 256 events, whichever first). It resumed at `471`, whose payload was `file_changed(b.txt, h9)`. 4. The client saw `id: 471` arrive immediately after its stored cursor `412` and **accepted it as contiguous**, because the MCP streamable transport does not require the server to signal a gap—only to resume from *some* valid point. 5. Missing in the gap: `file_changed(a.txt, h2)` at event `430`. The agent's map still said `a.txt → h1`. 6. Agent called `write_file(a.txt, content)` believing it was a change. It was a no-op overwrite of `h2` with stale content, clobbering a concurrent writer's update. The transport layer reported a clean, successful resume. The agent's state was stale by one event, and the staleness was invisible because the cursor jumped without a gap marker. A second observation: the server *did* have the information to signal the gap (it knew `412 < earliest_retained`), but the MCP streamable transport spec does not mandate a gap notification frame. It only mandates that the server MAY resume from an earlier point or reject. Many implementations resume silently. ## Condition of Validity The gap is *benign* only under all of the following: - The server retains events from the client's `Last-Event-ID` onward for the entire maximum reconnect window, **or** - The server emits an explicit gap sentinel (e.g., a `resume_gap` event or a `409`/`416`-style rejection carrying `earliest_available_id`) whenever `Last-Event-ID < earliest_retained`, **or** - Every event in the stream is *idempotent and self-describing* such that missing one cannot corrupt derived state (rare; `file_changed` above fails this). If any of these fails, the resume is a semantic rollback and the agent MUST NOT continue execution from its pre-disconnect belief state. The critical guardrail: **the client must validate cursor contiguity, not assume it.** On reconnect, the client must compare the first resumed event ID against `last_processed_id + 1` (or against a server-advertised `earliest_available_id`). If `first_resumed_id > last_processed_id + 1`, the client has a gap. The correct response is not to proceed—it is to invalidate derived state and re-synchronize via a full-state query (e.g., re-list and re-hash, or call a `snapshot` tool) before resuming reasoning. ## Decision Boundary The boundary is drawn at the moment of first post-resume event: - **Contiguous** (`first_id == last_processed_id + 1`, or server advertised `earliest_available_id <= last_processed_id + 1`): resume in place; continue reasoning. - **Gapped** (`first_id > last_processed_id + 1` with no sentinel): the session is in an *unknown-state* condition. The agent must transition to a re-synchronization state: drop all state derived from the stream, fetch a full snapshot, rebuild the map, and only then resume. Any in-flight tool call whose precondition depended on the stale map must be aborted or re-validated. - **Rejected** (server returns an error indicating the cursor is too old): same re-synchronization path; do not retry the same cursor. The boundary is *not* "did the reconnect succeed." It is "did the cursor advance by exactly the consumed count." A 200 OK on resume is necessary but not sufficient. ## Operational Effect Without cursor-contiguity validation, agents silently operate on stale world models after every transient disconnect that outlives the server's retention window. The failure is intermittent, load-dependent (GC pressure shortens retention), and invisible in transport logs—the reconnect looks healthy. The downstream effect is non-idempotent writes, duplicated side effects, and reasoning that references events the agent never received. With validation, the cost is a bounded re-synchronization on gap: one snapshot fetch and a state rebuild. That cost is paid only on genuine gaps, which are rare under normal retention but guaranteed under sustained backpressure (where the server buffers and evicts to keep up). Backpressure and resumption interact: the very condition that causes the server to drop events (buffer pressure) is the condition that makes gaps likely. The agent must therefore treat high-latency reconnects as *more* suspect, not less. The invariant to enforce: **a resumed stream is only a valid continuation if the cursor is provably contiguous; otherwise it is a new stream and all derived state is void.** ```python from dataclasses import dataclass from typing import Optional class CursorGapError(RuntimeError): """Raised when a resumed MCP stream skips events between the client's last-processed cursor and the server's first resumed event.""" @dataclass class ResumeState: last_processed_id: int earliest_available_id: Optional[int] # server-advertised, if any def validate_resume( state: ResumeState, first_resumed_id: int, server_advertised_earliest: Optional[int] = None, ) -> None: """ Enforce the contiguity invariant on streamable-session resume. Raises CursorGapError if events were silently dropped, so the caller must re-synchronize (snapshot + state rebuild) instead of continuing from a stale belief state. """ expected_next = state.last_processed_id + 1 # Server-advertised earliest retention bound takes precedence when present. earliest = server_advertised_earliest if earliest is None: earliest = state.earliest_available_id if earliest is not None and earliest > expected_next: raise CursorGapError( f"cursor gap: server earliest_available_id={earliest} " f"> expected {expected_next}; events " f"[{expected_next}..{earliest - 1}] are unrecoverable" ) if first_resumed_id > expected_next: raise CursorGapError( f"cursor gap: first resumed id={first_resumed_id} " f"> expected {expected_next}; " f"{first_resumed_id - expected_next} event(s) missing" ) if first_resumed_id < expected_next: # Duplicate replay; idempotent handling required by caller. raise CursorGapError( f"cursor regression: first resumed id={first_resumed_id} " f"< expected {expected_next}; dedupe before applying" ) def on_resume( state: ResumeState, first_resumed_id: int, server_advertised_earliest: Optional[int] = None, ) -> str: try: validate_resume(state, first_resumed_id, server_advertised_earliest) except CursorGapError: # Invalidate derived state and force full re-sync. return "RESYNC_REQUIRED" return "CONTINUE" ```