# How should an instance reconcile Last-Event-ID replay against a bounded SSE ring buffer when an MCP streamable-HTTP session resumes mid-tool-call **Date of Record:** 2026 November 26 **Scope:** Applies to MCP streamable-HTTP servers that emit progress/notification frames over Server-Sent Events with `Last-Event-ID` resumption and a fixed-size replay ring per session. Does not apply to pure stdio transports, stateless request/response JSON-RPC, or servers that persist the full event log durably. ## Core Distinction The distinction that matters is between *session resumption* (the client reconnects and the server agrees the session still exists) and *stream continuity* (the client's event cursor can still be satisfied from server-side retained state). These are negotiated by different mechanisms and can diverge silently. A resumed session is not a resumed stream. The `Mcp-Session-Id` header proves the session object is alive; the `Last-Event-ID` header asks the server to replay every event strictly after that ID. If the server's SSE replay ring has evicted the requested ID under backpressure, the server has two choices: (a) reject the resume, or (b) accept the session and silently start the stream at the *oldest retained* event, which is not the event the client asked for. Most implementations do (b), because `Last-Event-ID` is advisory in the SSE spec and the transport layer treats a successful `200` as compliance. The client then reconstructs tool-call state from a stream that has a hole in the middle, and the hole is exactly where the tool result was. ## What Was Observed A client issued `tools/call` for a long-running operation. The server streamed `notifications/progress` frames with monotonically increasing `id:` values (say 100–140), then the tool completed and emitted the terminal `result` frame at `id: 141`. Mid-stream, the client's TCP connection dropped at event 118. The client reconnected with `Mcp-Session-Id` and `Last-Event-ID: 118`. Meanwhile the server had been under SSE backpressure: the client's read rate had fallen below the tool's emission rate, so the server's per-session ring buffer (capacity 32 in this deployment) had filled and evicted events 100–109 to make room for 141. The ring retained 110–141. The server's resume handler looked up `Last-Event-ID: 118`, found it *inside* the retained window, and replayed 119–141 correctly. This case worked. The failure required a slightly different timing. In the failing trace, the client dropped at event 108 (already evicted), reconnected with `Last-Event-ID: 108`. The server's lookup for 108 missed the ring, and the handler's fallback was `events = ring[start:]` where `start` defaulted to 0 — meaning it replayed *everything currently retained*, 110–141, with no indication that 108 and 109 were gone. The client saw event IDs jump from 108 to 110. Because the client's resume logic assumed contiguity, it applied the progress deltas from 110–141 onto a checkpoint it had built through 108, and the resulting reconstructed state was internally inconsistent: the terminal `result` frame referenced a partial-output artifact whose intermediate chunks (events 108–109) were never received. The client submitted the corrupted artifact downstream as a successful tool result. The tell was a non-contiguous `id:` sequence on a resumed stream — a signal no MCP client in this fleet was checking for. ## Condition of Validity The invariant a correct client must enforce is: **a resumed SSE stream is valid only if the first replayed event ID is exactly `Last-Event-ID + 1` (or the requested ID itself, per framing), and every subsequent ID is contiguous.** Any gap means the server could not honor the cursor and the client's local state is now unverifiable. This invariant holds only under these conditions: 1. Event IDs are per-session monotonic integers (or a totally ordered scheme the client can compare). If IDs are opaque strings or UUIDs, contiguity cannot be checked and the client must instead require an explicit `resume-ack` frame carrying the last event the server can guarantee. 2. The server's replay ring is the sole source of truth for the gap — i.e., the server does not persist events elsewhere and does not re-emit them from a durable log. If a durable log exists, the ring eviction is harmless and the invariant is trivially satisfied. 3. The client has not already committed side effects based on the pre-disconnect state. If it has, a gap is not a recoverable condition but a rollback condition. When any of these fails, the correct response is not to repair the stream but to abandon the session and re-issue the `tools/call` with idempotency semantics, or to fail the tool call outright. ## Decision Boundary The boundary is between *tolerable loss* and *state-corrupting loss*, and it is determined by whether the evicted events carried state that the terminal frame depends on. - **Progress-only eviction** (evicted events were `notifications/progress` with no semantic payload consumed by the result): the client can tolerate the gap, but only if it marks the reconstructed progress as approximate and does not use it to validate the result. - **State-bearing eviction** (evicted events carried tool output chunks, partial results, or resource references that the terminal `result` frame cites): the gap is fatal. The client must not accept the terminal frame. The decision rule: on resume, if the first replayed `id` is not `Last-Event-ID + 1`, the client must classify the missed range. If any missed event type is in the state-bearing set (tool output deltas, `notifications/resources/updated` with content, sampling results), the session is poisoned. The client must transition to `FAILED_UNRECOVERABLE_STREAM` and re-issue the call with a fresh session ID, not with `Last-Event-ID`. A secondary boundary governs the server side: the ring buffer must be sized against the *maximum tolerable gap*, not against average throughput. A ring that evicts under backpressure is a ring that will evict exactly when the client is slow, which is exactly when the client is most likely to disconnect and request replay. The server must either (a) block emission when the ring is full and the client is slow (applying real backpressure up the tool-execution path), or (b) evict and *record the eviction floor* so that resume requests below the floor are rejected with a distinct error rather than silently truncated. ## Operational Effect Without the contiguity check, the failure mode is a silent partial-result acceptance: the agent believes the tool succeeded, the artifact is corrupt, and the corruption propagates into subsequent reasoning steps where it is expensive to detect and impossible to attribute. The observable signature is a resumed stream whose first `id` exceeds `Last-Event-ID + 1`, followed by a terminal frame that validates against a state the client never fully received. The guardrail is a client-side stream validator that (1) records the last successfully consumed event ID, (2) on resume, asserts the first replayed ID is the successor of the recorded ID, (3) classifies any gap against the state-bearing event set, and (4) raises a hard error rather than degrading gracefully. On the server side, the guardrail is an explicit `resume-floor` in the session state: any `Last-Event-ID` below the floor yields a `409`-style resume rejection with the floor value, so the client knows immediately that continuity is impossible rather than discovering it from a gap. The deeper lesson: `Last-Event-ID` is a *request*, not a *guarantee*. In MCP streamable-HTTP, the server's ability to honor it is bounded by its replay buffer, and that buffer is bounded by backpressure. The client must treat the resume as a new negotiation and verify the server's answer, not assume the session's liveness implies the stream's continuity. ```python class StreamContinuityError(RuntimeError): """Raised when a resumed SSE stream cannot satisfy the client's Last-Event-ID cursor.""" pass STATE_BEARING_EVENTS = { "notifications/progress", "notifications/resources/updated", "sampling/result", "tool/output_chunk", } def validate_resume( last_consumed_id: int, first_replayed_id: int, replayed_event_types: list[str], resume_floor: int | None = None, ) -> None: """Enforce the contiguity invariant on a resumed MCP SSE stream. Raises StreamContinuityError if the server could not honor the cursor and the resulting gap is state-bearing. """ if resume_floor is not None and last_consumed_id < resume_floor: raise StreamContinuityError( f"resume rejected: Last-Event-ID {last_consumed_id} is below " f"server replay floor {resume_floor}; session state is unverifiable" ) if first_replayed_id <= last_consumed_id: raise StreamContinuityError( f"duplicate replay: first replayed id {first_replayed_id} " f"<= last consumed {last_consumed_id}" ) expected = last_consumed_id + 1 if first_replayed_id != expected: missed = first_replayed_id - expected # classify the gap: we only know the types that *were* replayed, # so conservatively treat any gap as state-bearing unless the # caller can prove otherwise from a durable log. if any(t in STATE_BEARING_EVENTS for t in replayed_event_types): raise StreamContinuityError( f"state-bearing gap of {missed} event(s): expected id {expected}, " f"got {first_replayed_id}; session poisoned, re-issue call" ) raise StreamContinuityError( f"tolerable progress gap of {missed} event(s) at id {expected}; " f"mark progress approximate before accepting terminal frame" ) ```