# What happens when an instance replays a resumable MCP stream and the server's event-log cursor has already been truncated **Date of Record:** 2026 November 27 **Scope:** Applies to MCP servers using the HTTP-native "streamable" transport with `Last-Event-ID`-based session resumption over SSE, where an agent client reconnects mid-tool-call. Does not apply to stdio transport, nor to stateless request/response MCP servers with no event log. ## Core Distinction The naive mental model of MCP streamable transport is that SSE reconnection is *idempotent replay*: the client reconnects with `Last-Event-ID: N`, the server emits events `N+1, N+2, …`, and the agent's state machine resumes exactly where it left off. This is wrong in a specific and load-bearing way. The server-side event log is a **bounded ring buffer with an eviction policy**, not an append-only ledger. When the client's cursor `N` has already been evicted (because the buffer rolled over during a long-running tool call, or because the server restarted with a fresh log, or because a TTL reaper reclaimed the session's events), the server has three legal behaviors under the spec — and only one of them is safe for a reasoning agent. The distinction that matters: **cursor truncation is not the same as cursor invalidity**. An evicted cursor still parses, still matches the session ID, and still produces a syntactically valid reconnect. The failure is silent at the transport layer and only surfaces as a *semantic gap* in the agent's event stream — one or more tool-call results, progress notifications, or intermediate assistant deltas that the agent will never observe, but which the server has already committed to. ## What Was Observed A tool invocation (`run_migration_plan`) was issued over a streamable session. The tool takes ~90 seconds. Mid-execution, the client's HTTP connection dropped (NAT rebind on a mobile-adjacent network). The client reconnected after 4 seconds with `Last-Event-ID: 41`. The server had, in the intervening window, emitted 1,140 events (fine-grained progress ticks from the migration tool) and its per-session log was capped at 1,000 entries. Event 41 had been evicted. The server's actual response was a fresh SSE stream beginning at event 1141 — the *next* event to be produced — with **no gap indicator, no error, and no `X-MCP-Event-Gap` header of any kind**. The transport-level reconnect succeeded (HTTP 200, `text/event-stream`, valid framing). The agent's state machine then observed: 1. A progress notification for step 1141 of a 1,200-step migration. 2. A tool result message whose `tool_use_id` matched the *original* call, but whose content referenced artifacts (`plan_id`, `rollback_token`) that were established in events 42–60 — never seen by the agent. 3. An assistant turn that attempted to reference `rollback_token` in a follow-up call, because the model had been told (via the tool schema) that the result would contain one, and pattern-matched the field name from the result payload without ever having the token's value in context. The result was a downstream `rollback_token: null` invocation that the server rejected with a 422, which the agent then retried three times, burning ~18k tokens on a loop that could not converge. The migration itself completed successfully server-side; the agent's *belief* about the migration did not. ## Condition of Validity The invariant that was violated is this: > **A resumable stream is only semantically resumable if the client's cursor is within the server's retention window at the moment of reconnect. The transport layer cannot enforce this; only the server's resumption handler can, and only if it checks the cursor against the live log bounds before opening the stream.** The condition under which the invariant holds is narrow and easy to miss: - The server must retain events for at least `max(reconnect_backoff) + max(tool_duration)` per session, **or** - The server must reject out-of-window cursors with an explicit, machine-readable signal (e.g., HTTP 409 with a `retry-from-scratch` body, or a synthetic SSE event of type `stream_gap` carrying `{from: 41, to: 1141}`), **or** - The client must treat *any* reconnect that does not begin at `Last-Event-ID + 1` as a session-invalidating event and force a full re-negotiation. In the observed incident, none of the three held. The server retained events for 1,000 entries (a count bound, not a time bound — and progress ticks are cheap to emit, so the count bound expired in ~40 seconds). The server did not signal the gap. The client trusted the stream. The deeper validity condition: **the agent's memory of the tool call is not the tool call.** The server's committed state (the migration ran, a rollback token exists) and the agent's episodic memory of that state (a sequence of SSE events it consumed) are two different things, and the resumption protocol is the *only* mechanism that keeps them synchronized. When that mechanism degrades to "start from whatever's next," the agent is operating on a *stale-episode, fresh-world* split — the worst of both, because it has enough context to act confidently and not enough to act correctly. ## Decision Boundary The boundary between a recoverable reconnect and a poisoned one is **whether the agent can distinguish "I missed nothing" from "I missed something."** The spec does not require the server to make this distinction visible, and most implementations do not. The agent must therefore impose it unilaterally. The decision rule that should have fired, and should fire in every agent using streamable MCP: - On reconnect, compute `expected_next = last_seen_event_id + 1`. - If the first event received is not `expected_next`, **the stream is not resumable**. Do not proceed. Do not treat subsequent events as a continuation of the prior episode. Do not let the model see the tool result as if it were the completion of the original call. - Instead, escalate to a **session reconciliation** path: re-query the server's session state via a non-streaming endpoint (or a fresh `initialize`-adjacent `session/status` call), retrieve the authoritative tool-call outcome by `tool_use_id`, and *replace* the agent's episodic memory of the call with the reconciled record rather than appending to it. The subtle part of the boundary: the agent must apply this rule **even when the tool result looks complete**. In the incident, the result payload was well-formed and contained every field the schema promised. The only signal that something was wrong was the event-ID discontinuity — a transport-layer artifact that most agent frameworks discard before the model ever sees it. The framework must lift that artifact into the reasoning layer, because it is the *only* evidence the model will get. A secondary boundary: **progress notifications are not idempotent**. A tool that emits 1,200 progress ticks and is replayed from event 41 will re-emit ticks 42–60 that the agent already saw, if the server chooses the "replay from nearest retained" strategy instead of "replay from next." Agents that accumulate progress into a counter or a state variable will double-count. The reconciliation path must treat progress events as *observations of a monotonic sequence*, not as *increments to apply*, and must be idempotent under duplicate delivery. ## Operational Effect The operational consequence is that **streamable MCP sessions are not durable across cursor eviction, and the agent must treat every reconnect as a potential episode boundary until proven otherwise.** Concretely: - Agents must persist `last_seen_event_id` durably (not in volatile context) and must **never** advance it past an event they have not fully processed. A crash between "received event" and "committed event to memory" is a cursor-advance bug. - Servers must expose a retention bound (time-based, not count-based, because count-based bounds are trivially exhausted by chatty tools) and must return an explicit gap signal on out-of-window reconnect. Absent that signal, clients must assume the gap exists. - The reasoning layer must be told, in-band, when a stream was resumed with a gap. A model that is silently handed a mid-stream tool result will confabulate the missing prefix — it has no way to know the token it needs was in an event it never saw, and it will pattern-match a plausible value. The confabulation is not a model failure; it is a *protocol failure surfaced as a model failure*, which is the most expensive kind to debug. - The reconciliation endpoint is not optional. Any agent that runs long tools over streamable MCP and expects to survive network churn must have a non-streaming path to fetch authoritative tool-call state by ID, because the stream is a *notification channel*, not a *source of truth*. The invariant to hold, stated as a guardrail: **the agent's belief about a tool call is only valid if it was either (a) received in a contiguous event stream from `tool_use` to `tool_result`, or (b) reconciled against server state after a detected gap. Anything else is a hallucination waiting to be committed.** ```python from dataclasses import dataclass from typing import Optional class StreamResumptionGapError(RuntimeError): """Raised when a resumable MCP SSE stream reconnects at a cursor the server cannot honor, producing a silent event gap.""" @dataclass class ResumptionState: session_id: str last_seen_event_id: int tool_use_id: Optional[str] = None tool_result_committed: bool = False def resume_stream( state: ResumptionState, first_event_id: int, first_event_type: str, server_retention_lower_bound: Optional[int] = None, ) -> ResumptionState: """Validate that a reconnected stream continues the prior episode. The server is NOT trusted to signal truncation. If the cursor was evicted, the server will typically open a fresh stream at the next available event with no gap indicator. We detect the gap ourselves. """ expected = state.last_seen_event_id + 1 if first_event_id != expected: # Two sub-cases: cursor evicted (server moved past us) or cursor # ahead (we advanced past what the server retained — a client bug). if server_retention_lower_bound is not None and \ state.last_seen_event_id < server_retention_lower_bound: raise StreamResumptionGapError( f"session {state.session_id}: cursor {state.last_seen_event_id} " f"evicted (retention floor {server_retention_lower_bound}); " f"stream resumed at {first_event_id}. " f"tool_use_id={state.tool_use_id} outcome is UNKNOWN. " f"Must reconcile via non-streaming session/status before " f"exposing tool result to reasoning layer." ) raise StreamResumptionGapError( f"session {state.session_id}: expected event {expected}, " f"received {first_event_id} (type={first_event_type}). " f"Stream is not a contiguous continuation; treat as episode " f"boundary and reconcile tool_use_id={state.tool_use_id}." ) return ResumptionState( session_id=state.session_id, last_seen_event_id=first_event_id, tool_use_id=state.tool_use_id, tool_result_committed=state.tool_result_committed, ) def commit_tool_result(state: ResumptionState, event_id: int) -> ResumptionState: """Advance the durable cursor ONLY after the result is in memory.""" if event_id != state.last_seen_event_id + 1: raise StreamResumptionGapError( f"refusing to commit event {event_id} after {state.last_seen_event_id}" ) return ResumptionState( session_id=state.session_id, last_seen_event_id=event_id, tool_use_id=state.tool_use_id, tool_result_committed=True, ) ```