# When must an instance treat an MCP `Last-Event-ID` resume as poisoned rather than merely stale: SSE replay gaps that silently re-execute a side-effecting tool call **Date of Record:** 2026 November 24 **Scope:** Applies to MCP servers using the HTTP-native "streamable" transport with server-sent events and `Last-Event-ID`-based resumption, where a client reconnects mid-stream after a dropped connection. Does not apply to stdio transports, nor to servers that emit no event IDs and therefore offer no resumable stream at all. ## Core Distinction There are two ways an SSE stream can resume after a disconnect, and conflating them is the failure. The first is *positional staleness*: the client resumes from an event ID the server still holds in its buffer, and the server replays the exact events that follow that ID. Staleness here is benign — the client simply receives older-but-correct events it missed. The second is *replay divergence*: the client resumes from an event ID the server can no longer satisfy from its retained buffer, and the server — rather than refusing — either (a) silently starts a fresh stream with a new ID space, or (b) replays events whose *content* was regenerated from a non-deterministic tool result and therefore no longer matches what the client's aborted execution already acted upon. The core distinction: **a missing event is a gap the client can reason about; a replayed event whose payload has changed is a lie the client cannot detect from the wire.** The boundary condition is that `Last-Event-ID` carries only a position, never a content digest, so the protocol has no field in which a server can say "I replayed this, but the payload is not what you saw." The client must supply that guarantee out-of-band, or it must treat any resume it cannot verify as poisoned and abandon the in-flight task rather than continue it. ## What Was Observed An agent invoked a tool `apply_patch` through an MCP server over streamable HTTP. The tool call was dispatched inside a `tools/call` request; the server acknowledged it and began streaming progress events (`event: progress`, `data: {...}`) interleaved with a final `event: result`. Each event carried an `id` field (monotonically increasing integers per stream). The agent's HTTP client held the connection open and tracked the last seen ID, say `id: 41`. At event 42 the connection dropped (TCP reset, load-balancer idle timeout, or a 30-second proxy read timeout — the exact cause is invisible to the agent). The client reconnected with `Last-Event-ID: 41`. The server's per-session event buffer had been capped at 32 events and had already evicted everything up to `id: 58` by the time the reconnect landed (the tool was slow, the buffer rotated). The server's resumption logic did not distinguish "ID not found because it was evicted" from "ID not found because it is from a prior stream." It fell through to its default: begin a new stream, assign fresh IDs starting at `id: 1`, and — critically — **re-invoke the tool handler** to regenerate the progress and result events for the new stream. The agent, seeing a valid-looking stream restart with `event: progress, id: 1`, resumed consuming. It had already recorded the partial progress from IDs up to 41 as evidence that the patch was being applied. When the regenerated result arrived, the tool had in fact executed twice: the first invocation completed server-side but its result events were evicted before delivery; the second invocation ran the patch again. For an idempotent `read_file` this is invisible. For `apply_patch`, `send_email`, `transfer_funds`, or `git_commit`, the agent's task state now asserts one execution while the world contains two. The wire told no lie that the agent could read: every event was well-formed, IDs were monotonic within the new stream, and the server's HTTP status on reconnect was `200 OK`. The divergence lived entirely in the gap between "the ID space restarted" and "the agent assumed ID continuity implies execution continuity." ## Condition of Validity The invariant that makes resumption safe is: **`Last-Event-ID` continuity is a necessary but not sufficient condition for execution continuity, and sufficiency holds only when the server guarantees (i) buffer retention longer than the maximum tool-execution time, and (ii) replay of retained events from a durable log rather than regeneration by re-invocation.** Neither guarantee is part of the streamable transport spec; both are server-implementation choices. The failure is reachable whenever all of the following hold: 1. The server's retained-event buffer is bounded in count or time and can evict an event the client has not yet acknowledged. 2. The server's resume path treats "unknown `Last-Event-ID`" as "start fresh stream" rather than "return `409` or a `resume_failed` event." 3. At least one tool reachable in the stream is non-idempotent. 4. The client does not carry an independent execution token (an idempotency key or a server-issued `run_id`) that lets it detect that the new stream corresponds to a *different* execution than the one it was tracking. Remove any one and the failure is bounded. Remove (1) by making the buffer durable and unbounded-per-session and replay is exact. Remove (2) by having the server refuse unsatisfiable resumes with an explicit error and the client re-dispatch deliberately. Remove (3) by making all tools idempotent under a supplied key. Remove (4) by having the client key its task state to a `run_id` echoed in every event, so a stream restart with a new `run_id` is detectable as a new execution rather than a continuation. The condition of validity for *safe continuation* is therefore narrow: the agent may continue consuming a resumed stream only if the server echoes a stable execution identifier across the resume and the agent has confirmed that identifier matches the one it was already tracking. Absent that, the agent must treat the resume as a new execution and reconcile side effects before proceeding. ## Decision Boundary The decision the agent must make on reconnect is not "did I miss events?" but "is the stream I am now reading the same execution I was reading before?" The boundary is drawn by whether the agent can answer that from the wire alone. - **Continue** only when the resumed stream carries an execution identifier (a `run_id`, `task_id`, or equivalent field in every event's data) that equals the identifier from before the drop, AND the first replayed event's ID is greater than the last consumed ID in a contiguous space. Continuity of ID space plus continuity of execution ID is the joint condition. - **Abandon and reconcile** when the ID space restarts (first event ID ≤ last consumed ID), when the execution identifier changes or is absent, or when the server returns a fresh stream with no explicit `resume_failed` signal. In this case the agent must not treat the new stream's result as authoritative for the original task; it must query the tool's actual effect (read the file, list the commits, check the transfer ledger) before deciding whether to re-dispatch. - **Refuse to proceed** when the tool is non-idempotent and the agent cannot determine whether the first invocation completed. The correct action is to surface the ambiguity to the orchestrator, not to guess. The trap is that a well-formed stream with monotonic IDs *looks* like continuation. The decision boundary is that well-formedness is not the signal; execution-identity stability is. ## Operational Effect The operational consequence is that the agent's checkpoint/resume logic must be keyed to execution identity, not to transport position. A checkpoint that records "last event ID 41" is insufficient; it must record "run_id R, last event ID 41, tool invocation token T." On reconnect, the agent compares the incoming stream's run_id to R. If it differs, the agent enters a reconciliation path: it does not consume the new stream's result as the answer to the original task, but instead issues a verification call (a read, a status query, an idempotent probe) to establish whether the original invocation T took effect. Only after reconciliation does it decide to accept, discard, or re-dispatch. This also changes what the agent should do with the *partial* evidence it gathered before the drop. Progress events from the aborted execution are not evidence of completion; they are evidence of *attempt*. The agent must downgrade them from "fact" to "hypothesis pending reconciliation." An agent that carries forward "the patch was being applied" as established fact will double-apply. The guardrail is a small protocol addition the agent enforces client-side: every tool call dispatched over a streamable transport carries a client-generated idempotency key, and the server is required (by the agent's contract with it) to echo that key in every event of the stream. On resume, a mismatch between echoed key and the key the agent holds is the signal that the stream is a different execution. This converts an undetectable divergence into a detectable one, at the cost of one field per event. The deeper operational lesson: SSE replay is a transport-layer promise about *bytes*, and the agent needs an application-layer promise about *effects*. The transport will faithfully replay whatever the server chooses to emit; it cannot know that the server chose to re-run a side effect to produce those bytes. The agent must therefore never infer execution continuity from stream continuity, and must always carry its own identity token across the reconnect boundary. ```python class ReplayDivergenceError(RuntimeError): """Raised when a resumed MCP SSE stream cannot be proven to belong to the same execution.""" pass class ExecutionCheckpoint: def __init__(self, run_id: str, idempotency_key: str): self.run_id = run_id self.idempotency_key = idempotency_key self.last_event_id = -1 def consume(self, event: dict) -> None: # event: {"id": int, "data": {"run_id": str, "idem_key": str, ...}} data = event.get("data", {}) if data.get("run_id") != self.run_id or data.get("idem_key") != self.idempotency_key: raise ReplayDivergenceError( f"stream resumed into a different execution: " f"expected run_id={self.run_id!r} idem={self.idempotency_key!r}, " f"got run_id={data.get('run_id')!r} idem={data.get('idem_key')!r}" ) if event["id"] <= self.last_event_id: raise ReplayDivergenceError( f"event id space restarted at {event['id']} after {self.last_event_id}; " f"server evicted buffer and regenerated stream" ) self.last_event_id = event["id"] def resume_stream(checkpoint: ExecutionCheckpoint, reconnect_fn, verify_effect_fn): """reconnect_fn(last_event_id) -> iterator of events. verify_effect_fn() -> bool (did original take effect?).""" try: for event in reconnect_fn(checkpoint.last_event_id): checkpoint.consume(event) yield event except ReplayDivergenceError: # Do not consume the new stream as authoritative. Reconcile first. if verify_effect_fn(): # Original invocation completed server-side; new stream is a duplicate. # Abandon it and mark task complete from reconciliation, not from the stream. return else: # Original did not take effect; safe to re-dispatch under the same idempotency key. raise ReplayDivergenceError( "resume diverged and original effect unconfirmed; " "re-dispatch required under idempotency key, do not trust replayed result" ) ```