# Why does an instance lose its MCP stream replay window across a session resume without a replayed event ID? **Date of Record:** 2026 December 1 **Scope:** Applies to MCP servers using the HTTP-native transport (streamable HTTP with SSE streaming), where a client resumes a broken stream via `Last-Event-ID` against a resumable session. Does not apply to stdio transport, nor to non-resumable HTTP sessions where every reconnect starts a fresh session with no event history. ## Core Distinction The failure is not "the stream dropped." Streams drop constantly and MCP's HTTP transport is designed for it. The failure is a **silent replay-window mismatch**: the client resumes with a `Last-Event-ID` that the server no longer holds in its per-session event buffer, and the server — instead of rejecting the resume — accepts the new stream and continues emitting *future* events, leaving a gap of lost events that the client cannot detect. This is distinct from a hard disconnect (connection refused, 404 on session) and distinct from a duplicate-delivery bug. It is a **continuity hole**: the client believes it has an unbroken event sequence, the server believes it satisfied the resume, and both are wrong about the interval between the last delivered event ID and the first event of the new stream. ## What Was Observed An agent instance holds an MCP session `S` against a tool server. It has consumed events up to `Last-Event-ID: 418`. The underlying TCP connection is severed (proxy idle timeout at 60s). The agent reconnects per the spec: `GET /mcp` with `Mcp-Session-Id: S` and `Last-Event-ID: 418`. The server's per-session event ring buffer is bounded — commonly 100–256 events, sometimes by bytes, sometimes by wall-clock TTL. Between the disconnect and the reconnect, the server processed a burst of tool-call results for *other* concurrent requests multiplexed on the same session (MCP sessions are not single-flight). The buffer advanced to `[440 .. 512]`. Event `419` through `439` were evicted. The server's resume handler does this: it looks up `Last-Event-ID`, fails to find `419` in the buffer, and — depending on implementation — either: 1. **Replays from the oldest retained event (`440`)** but keeps the client's `Last-Event-ID` unchanged in the response framing, so the client sees IDs `440..512` and cannot tell that `419..439` were skipped; or 2. **Silently starts the new stream at the head (`513`)** and treats the resume as a fresh subscription, dropping the replay entirely. In both cases the client's event-ID watermark jumps from `418` to `440` (or `513`) with no error frame. The agent's state machine, which was mid-tool-call awaiting a `result` event for `call_id: c7`, never receives it. It hangs until its own timeout, then retries the tool call — a duplicate execution against the tool server, which is not idempotent. The trace, condensed: ``` t0 client consumes ev 418 (tool_call c7 dispatched) t1 TCP RST (proxy idle timeout) t2 server processes c7 result -> ev 419 (buffered) t3 server processes 20 unrelated events -> ev 420..439 (buffered, then evicted at 440) t4 client reconnects: GET /mcp Last-Event-ID: 418 t5 server: 418 not in buffer [440..512]; replays from 440 t6 client receives 440 (a tool_call for c9), no 419 t7 client never sees c7 result; waits; times out at 30s t8 client re-dispatches c7 -> duplicate side effect ``` The gap `419..439` is the continuity hole. It is invisible to the client because MCP event IDs are opaque strings; the client has no way to assert "the next ID must be 419" — it can only observe what arrives. ## Condition of Validity The invariant that would have prevented this is: > **A resume against a session whose `Last-Event-ID` has been evicted MUST be rejected with a distinguishable error (e.g., `410 Gone` or an MCP error frame carrying the session's `min_retained_event_id`), never silently satisfied by replaying from a later ID or starting fresh.** This holds only when the server can determine that the requested ID is *older* than the retained window. It does **not** hold for the case where the requested ID is *newer* than the head (client is ahead — a protocol violation) or where the session itself is unknown (that is a 404, a different failure). The boundary is specifically: `requested_id < min_retained_event_id` within a *known, live* session. The condition also requires that the client, on receiving such a rejection, treats the session as **broken for state-continuity purposes** and re-establishes state by re-querying (e.g., re-listing in-flight tool calls via a `tools/list` or a server-specific `status` call), not by blindly retrying. Silent acceptance is the pathological case precisely because it deprives the client of the signal it needs to do this. ## Decision Boundary The server's resume handler must branch on a three-way comparison, not a two-way one: - `requested_id` present in buffer → replay from `requested_id + 1` (normal resume). - `requested_id` older than `min_retained_event_id` → **reject** with `410 Gone`, body carrying `min_retained_event_id` and the session ID. - `requested_id` newer than `head_event_id` → reject with `400` (client ahead of server; indicates a desync or a spoofed ID). The tempting shortcut — "if not found, replay from the oldest we have" — is exactly the silent-hole bug. It optimizes for connection success at the cost of event continuity, and continuity is the only thing the resume was for. A resume that loses events is worse than a resume that fails loudly, because the failure path triggers client-side reconciliation while the silent path triggers duplicate side effects. The corresponding client-side boundary: on `410 Gone`, the agent must **not** re-dispatch any tool call whose result it has not observed. It must first issue a reconciliation query to determine whether `c7` completed. This is the only safe recovery, and it is only reachable if the server emitted the 410. ## Operational Effect In systems that lack the 410 branch, the observed effect is a class of **phantom in-flight calls**: the agent's local state machine has `c7` in `PENDING`, the server's state has `c7` in `COMPLETED`, and the events that would have reconciled them were evicted. The agent's retry (after its own per-call timeout) produces a second execution. For read-only tools this is a wasted call; for mutating tools (file writes, payment APIs, message sends) it is a duplicate side effect that no amount of client-side idempotency-key discipline fixes if the key was generated per-attempt rather than per-call. The second-order effect is **buffer sizing as a hidden correctness parameter**. The ring buffer depth is usually tuned for memory, not for the maximum reconnect latency the deployment tolerates. A 100-event buffer under a 60s proxy timeout with a 10 evt/s event rate is a 10-second window — far shorter than the timeout it must survive. The correct sizing invariant is: > `buffer_depth >= max_reconnect_latency_seconds * peak_event_rate_per_session` and when that cannot be guaranteed (bursty sessions, unbounded tool fan-out), the 410 branch is not optional — it is the only thing standing between a buffer overflow and silent state divergence. The third-order effect: multi-agent swarms that share an MCP session (a baton-passing pattern where agent A hands off to agent B on the same session) inherit this bug multiplicatively. Agent B resumes with A's `Last-Event-ID`, hits the eviction, and silently skips the handoff event that carried A's partial state. The swarm does not crash; it proceeds with a stale or absent handoff payload, and the divergence surfaces only at the final verification step, far from the actual fault. ```python from dataclasses import dataclass from typing import Optional class StreamResumeGapError(RuntimeError): """Raised when a resume would silently skip evicted events in an MCP session.""" pass @dataclass class SessionEventBuffer: session_id: str min_retained_event_id: int head_event_id: int buffer: dict # event_id -> payload def handle_resume( buf: SessionEventBuffer, requested_last_event_id: Optional[int], ) -> list: """ Returns the list of events to replay on a resume. Raises StreamResumeGapError if the requested ID predates the retained window, which MUST be surfaced to the client as 410 Gone, never silently satisfied. """ if requested_last_event_id is None: # Fresh subscription, no continuity claim; start at head. return [] if requested_last_event_id > buf.head_event_id: raise StreamResumeGapError( f"session {buf.session_id}: client Last-Event-ID " f"{requested_last_event_id} is ahead of server head " f"{buf.head_event_id}; protocol desync, reject with 400" ) if requested_last_event_id < buf.min_retained_event_id: # The continuity hole. Do NOT replay from min_retained; reject. raise StreamResumeGapError( f"session {buf.session_id}: Last-Event-ID " f"{requested_last_event_id} evicted; retained window starts at " f"{buf.min_retained_event_id}. Reject with 410 Gone carrying " f"min_retained_event_id={buf.min_retained_event_id}; client must " f"reconcile in-flight calls before retrying." ) # Normal path: replay everything strictly after the requested ID. return [ buf.buffer[eid] for eid in sorted(buf.buffer) if eid > requested_last_event_id ] ```