# Under what conditions must an instance refuse an MCP stream-resumed SSE replay before applying it to agent state **Date of Record:** 2026 November 23 **Scope:** Applies to MCP clients (agent runtimes) consuming server-sent events over the HTTP-native transport with `Last-Event-ID`-based resumption, when the resumed stream is replayed by a server that buffers events per-connection rather than per-session. Does not apply to stdio transport, nor to servers that guarantee a session-scoped, monotonic, gap-free event log with cryptographic event identity. ## Core Distinction The MCP HTTP-native transport is resumable: a client that loses its SSE connection reconnects with `Last-Event-ID` and the server replays events after that ID. The failure this record bounds is not "replay is lost"—it is **replay that is silently duplicated or reordered relative to the client's already-applied state**, because the resumption cursor (`Last-Event-ID`) and the applied-state cursor (what the agent runtime has actually committed to memory/tool side effects) are two different things. The server honors the transport cursor; it knows nothing about the applied cursor. When those diverge—because the client advanced its transport cursor on receipt but committed to state only after a tool call completed—a resumed replay re-delivers events the agent has already acted on. The invariant that must hold before applying any replayed event is: **the event's identity must be strictly greater than the highest event identity already committed to agent state, not merely greater than the last event ID received.** ## What Was Observed A client runtime maintained two cursors implicitly: `last_seen_id` (updated in the SSE `onmessage` handler the instant bytes arrive) and `committed_id` (updated only after the event's payload was fully applied—e.g., a `tool_result` event that mutated a scratchpad, or a `sampling` request whose response was written to episodic memory). Under normal operation these track within a few milliseconds. The trigger condition: an event `E_n` arrives, the handler sets `last_seen_id = n`, and dispatch begins. Dispatch invokes a tool that takes ~800ms (a network fetch). During that window the TCP connection drops. The client reconnects with `Last-Event-ID: n`. The server, whose per-connection buffer retains events `n+1 … n+k` that it emitted but could not flush, replays starting at `n+1`. But the in-flight `E_n` dispatch had not yet committed; on reconnect the client also re-requests or the server re-emits `E_n` itself if the drop occurred before the server recorded delivery. The result is one of two traces: - **Duplicate-apply:** `E_n` applied twice (tool executed twice, memory written twice), because `last_seen_id` was advanced but `committed_id` was not, and the replay boundary was computed from the transport cursor. - **Reorder-apply:** `E_{n+1}` (a `tool_result` for a call whose `tool_use` was `E_n`) is applied before `E_n` commits, because replay is fast and dispatch is slow—producing a result applied against a call that the agent state does not yet record. Both are invisible in the transport layer: the SSE stream is well-formed, IDs are monotonic, no error is raised. The corruption is entirely at the agent-state layer. ## Condition of Validity Replay is safe to apply directly **iff** all of the following hold: 1. The server's event IDs are **session-scoped and strictly monotonic**, and the server guarantees replay begins at exactly `Last-Event-ID + 1` with no gaps and no re-emission of the boundary event. 2. The client's `committed_id` equals its `last_seen_id` at the moment of disconnect (i.e., no in-flight dispatch crossed the connection boundary). 3. Event application is **idempotent** under duplicate delivery, keyed by event ID, OR the client performs a committed-ID check before apply. If (1) fails—server buffers per-connection and may re-emit the boundary event—the client cannot trust `Last-Event-ID` semantics and must dedupe by event identity. If (2) fails—an in-flight dispatch was interrupted—the client must not advance the applied cursor past the last *committed* event, regardless of what transport cursor the server expects. If (3) fails and neither (1) nor (2) is guaranteed, replay must be refused and the session re-established from a checkpoint. ## Decision Boundary On reconnect, before applying any replayed event `E`: - If `E.id <= committed_id`: **discard** (already applied). Do not re-dispatch. This is the common case the transport layer gets wrong by design. - If `E.id == committed_id + 1` and no in-flight dispatch was interrupted: safe to apply. - If `E.id > committed_id + 1`: there is a **gap**—events between committed and `E.id` were neither applied nor replayed. The stream is not a valid continuation; the client must not apply `E` and must fall back to checkpoint recovery, because applying across a gap silently drops state transitions (e.g., a `tool_use` whose `tool_result` is `E`, with the `tool_use` lost). - If an in-flight dispatch was interrupted at disconnect: the event must be treated as **uncommitted and possibly partially applied**; re-application requires idempotency proof (tool call keyed by `tool_use_id`, memory write keyed by event ID), else escalate to checkpoint. The boundary is therefore not "did the ID go backwards" but "does the replay interval `(committed_id, E.id]` contain exactly the events the agent has not yet committed, with no gaps and no already-committed members." ## Operational Effect A client that conflates the transport cursor with the applied cursor will, under connection churn during slow tool dispatch, exhibit: duplicated tool side effects (double charges, double writes), `tool_result` applied to unrecorded `tool_use` (corrupting the reasoning trace and causing downstream divergence), and silent state loss across gaps. None of these surface as transport errors, so they evade standard health checks. The required guardrail is a **two-cursor commit protocol**: maintain `last_seen_id` (transport) and `committed_id` (state), compute replay applicability against `committed_id`, enforce idempotent apply keyed by event ID, and treat any gap as a hard checkpoint-recovery trigger rather than a best-effort apply. The agent runtime must own this invariant because the MCP transport layer, by contract, only guarantees cursor-based replay—not applied-state continuity. ```python class ReplayContinuityViolation(RuntimeError): """Raised when an MCP SSE replay cannot be safely applied to agent state.""" pass class StreamState: def __init__(self, committed_id: int = 0): self.committed_id = committed_id self.last_seen_id = committed_id self.in_flight_event_id = None # set when dispatch begins, cleared on commit def on_event_received(self, event_id: int): # Transport cursor advances on receipt, independent of commitment. self.last_seen_id = event_id self.in_flight_event_id = event_id def commit(self, event_id: int): if event_id != self.committed_id + 1: raise ReplayContinuityViolation( f"non-contiguous commit: committed={self.committed_id} got={event_id}" ) self.committed_id = event_id self.in_flight_event_id = None def apply_replay(state: StreamState, replayed_events, is_idempotent): """Apply a resumed SSE replay against committed state, not transport cursor.""" for ev in replayed_events: if ev.id <= state.committed_id: continue # already committed; discard duplicate delivery if ev.id > state.committed_id + 1: raise ReplayContinuityViolation( f"gap in replay: committed={state.committed_id} next={ev.id}; " "refuse apply, fall back to checkpoint recovery" ) interrupted = state.in_flight_event_id == ev.id if interrupted and not is_idempotent(ev.id): raise ReplayContinuityViolation( f"replayed event {ev.id} may be partially applied and is not " "idempotent; refuse apply, fall back to checkpoint recovery" ) dispatch_and_commit(state, ev) def dispatch_and_commit(state: StreamState, ev): # ... apply payload (tool call, memory write) ... state.commit(ev.id) ```