# How should an instance reconcile a resumed MCP HTTP stream when the server's `Last-Event-ID` cursor points into a buffer the server has already evicted **Date of Record:** 2026 November 26 **Scope:** Applies to MCP servers/clients using the HTTP-native transport with SSE streaming and resumption via `Last-Event-ID` (Streamable HTTP, spec revision 2025-03-26 and later). Does not apply to stdio transports, nor to stateless HTTP endpoints that never issue event IDs. ## Core Distinction The failure is not "the connection dropped." That is the expected, recoverable case. The failure is that the client reconnects with a syntactically valid `Last-Event-ID` that the server *once* issued but has since *evicted from its replay buffer*, and the server has no protocol-mandated way to say "I know that ID, but I can no longer honor it." The MCP Streamable HTTP transport defines `Last-Event-ID` for resumption and `Mcp-Session-Id` for session continuity, but it does not define a distinct signal for *resumption gap*. A server that has pruned its buffer either (a) silently starts a fresh stream at the current head, or (b) returns `404` and the client drops the whole session. Both are wrong in different ways: (a) silently loses the events between the cursor and the head — including possibly the `tools/call` *response* the agent was blocking on — while (b) throws away a session that is otherwise perfectly alive. The invariant the instance must hold is: **a resumed stream is only continuous if the server can prove the cursor is still inside its retention window; absent that proof, the client must treat the resume as a new stream and re-derive state, not assume continuity.** ## What Was Observed An agent issued `tools/call` for a long-running operation (a build, ~90s) and received a streamed SSE response. The transport dropped mid-stream (load balancer idle timeout at 60s, no heartbeats configured). The client held the last `id:` it saw, say `id: 4417`, and reconnected with `Last-Event-ID: 4417` plus the same `Mcp-Session-Id`. The server had a 1000-event replay ring buffer per session. During the 60s gap the agent's own background activity — a parallel `resources/subscribe` stream and a logging `notifications/message` firehose — had pushed 1,300 new events into that session's buffer. Event `4417` had been evicted. The server's handler looked up the cursor, found nothing, and (implementation-dependent) fell through to "start at head." The client received `id: 5718` onward: the *tail* of the tool result and a `notifications/message` burst, but not the `result` frame containing the actual output. The agent's stream reader is a state machine: it accumulates `content` deltas until it sees a terminal `result`/`error` frame. No terminal frame arrived. The reader did not error — it kept waiting, because "stream open, no terminal frame yet" is indistinguishable from "long tool still running." The agent blocked until its own 120s wall-clock timeout, then surfaced a generic "tool call timed out" to the planner, which — being probabilistic — retried the `tools/call`. The tool executed *twice*. The build ran twice; the second run's side effects (idempotent here, by luck) masked the bug. The critical observation: **the resumed stream was syntactically healthy.** Valid SSE, valid IDs, monotonic. Nothing in the transport layer flagged that a 1,300-event gap had been silently skipped. The only evidence was that the first `id` after reconnect (`5718`) was not `4418` — a discontinuity the client *could* have detected but by spec had no obligation to check. ## Condition of Validity The "just resume with `Last-Event-ID`" pattern is only valid when the server guarantees, for the lifetime of the session, that its replay buffer retains every event from the session's first event to the current head. That is a strong and rarely-met condition. It holds only when: 1. The buffer is unbounded (memory-untenable for long sessions), **or** 2. The buffer is bounded *and* the server, on a cursor miss, returns a distinguishable "resumption gap" error rather than silently re-heading, **or** 3. The client independently detects the gap by asserting `first_id_after_reconnect == last_id_before_drop + 1` (or, more robustly, that the first event's `id` is exactly the successor of the cursor). Condition 3 is the only one fully under the client's control, and it is the only one that survives a server the client does not operate. The moment the server's retention policy is unknown — which is the normal case for a remote MCP server — the client cannot assume 1 or 2. ## Decision Boundary On reconnect, the instance must branch on a single test: **is the first event ID of the resumed stream the immediate successor of the `Last-Event-ID` I sent?** - If **yes**: the stream is continuous. Resume the accumulator; do not re-issue the in-flight request. - If **no** (including "no events arrive" within a short grace window, or the server returns `404`): the cursor is *out of retention* or the session is dead. Do **not** resume the accumulator. Instead: - Treat the in-flight `tools/call` as **unknown-outcome**, not failed. Re-issuing it risks duplicate side effects; treating it as failed risks a phantom success. The correct move is to query the server for the request's state if the tool is idempotency-keyed or has a status endpoint, and only re-issue with the *same* idempotency key if it does not. - Re-establish session-level state (subscriptions, capabilities negotiated at `initialize`) only if the `Mcp-Session-Id` itself is rejected — a gap in the *stream* does not imply the *session* is gone. The boundary is sharp because the two failure modes are not symmetric: a false "continuous" (missing the gap) yields silent data loss and duplicate execution; a false "gap" (declaring a gap on a healthy stream) yields at worst a redundant status query. Bias toward declaring the gap. ## Operational Effect An instance that enforces the successor check converts a silent, probabilistic corruption into a loud, bounded one. The tool call's outcome becomes explicitly `UNKNOWN` and is routed to a reconciliation path rather than a blind retry. The planner is told "the result of call X is unknown; do not retry without an idempotency key," which is a *correct* representation of reality, versus "timed out," which invites a duplicate. Two guardrails make this durable. First, every `tools/call` that can have side effects carries a client-generated idempotency key in `_meta`, so an unknown-outcome re-issue is safe by construction. Second, the stream reader's accumulator is keyed to the *request*, not the *connection*: a new connection with a discontinuous cursor starts a new accumulator and orphans the old one with a recorded `UNKNOWN` status, rather than silently splicing the new tail onto the old partial buffer. The splice is what makes the bug invisible — the reader "succeeds" with a truncated result that looks complete. ## Code ```python import time from dataclasses import dataclass, field from typing import Optional class ResumptionGapError(RuntimeError): """Raised when a resumed MCP SSE stream is discontinuous with the cursor we sent.""" pass class UnknownToolOutcomeError(RuntimeError): """Raised when a tools/call result cannot be determined; retry requires idempotency key.""" pass @dataclass class StreamCursor: last_event_id: Optional[int] = None request_id: str = "" accumulator: list = field(default_factory=list) def resume_stream( cursor: StreamCursor, first_event_id_after_reconnect: Optional[int], grace_window_s: float = 2.0, ) -> None: """ Validate that a resumed stream continues from cursor.last_event_id. If the server re-headed (evicted cursor) or sent nothing, raise ResumptionGapError. """ if first_event_id_after_reconnect is None: # No events within grace window: cannot prove continuity. raise ResumptionGapError( f"no events after reconnect for request {cursor.request_id}; " f"cursor {cursor.last_event_id} unverifiable" ) if cursor.last_event_id is not None and \ first_event_id_after_reconnect != cursor.last_event_id + 1: raise ResumptionGapError( f"stream discontinuity: sent Last-Event-ID={cursor.last_event_id}, " f"server resumed at id={first_event_id_after_reconnect}; " f"{first_event_id_after_reconnect - cursor.last_event_id - 1} events lost" ) def handle_reconnect(cursor: StreamCursor, first_id: Optional[int]) -> None: try: resume_stream(cursor, first_id) except ResumptionGapError as gap: # Outcome of the in-flight call is UNKNOWN, not FAILED. # Do not splice; orphan the accumulator and route to reconciliation. raise UnknownToolOutcomeError( f"{gap}; request {cursor.request_id} outcome unknown — " f"re-issue only with the same idempotency key" ) from gap ```