# What happens when an instance resumes an MCP stream without replaying the event-ID watermark: silent tool-call duplication at the transport boundary **Date of Record:** 2026 December 3 **Scope:** Applies to MCP servers and clients using HTTP-native streaming transports (SSE over HTTP/1.1 or HTTP/2, chunked `text/event-stream`) with `Last-Event-ID`-based session resumption. Does not apply to stdio transports, single-shot request/response JSON-RPC over plain HTTP, or WebSocket transports that carry their own ordering guarantees. ## Core Distinction The failure is not "the stream dropped." Dropped streams are expected and are precisely what resumption is designed for. The failure is the divergence between two distinct notions of session state that the MCP HTTP transport spec conflates at the resume boundary: 1. **Transport watermark state** — the last `id:` field the client actually *consumed and processed* and echoed back as `Last-Event-ID`. 2. **Agent execution state** — the last tool call whose *side effect* the agent has committed to its own memory/scratchpad and is willing to treat as "done." A client can correctly echo `Last-Event-ID: 41` (transport-consistent) while having already emitted a `tools/call` for the event with `id: 41` whose result it has *not yet* recorded (execution-inconsistent). On resume, the server — which only knows the transport watermark — replays from `42`. If the client's watermark was written *before* the side effect landed (the common, cheap implementation), the replayed window is empty and the in-flight call is lost. If the watermark was written *after* the side effect but the ack was lost in the same partition event, the server replays `41` and the call executes twice. There is no transport-level mechanism that makes these two watermarks atomic, and MCP's streaming transport does not define an idempotency key to reconcile them. ## What Was Observed A research-agent deployment used an MCP server exposing a `web_search` tool and a `fetch_url` tool over SSE. The client was a planner agent that streamed tool results into a rolling scratchpad and periodically checkpointed. The server emitted each `tools/call` result as an SSE event with a monotonic `id:`. The client's resume logic was: ``` last_seen = scratchpad.last_committed_event_id # written after append reconnect(headers={"Last-Event-ID": str(last_seen)}) ``` Under normal operation this is correct. The boundary case: the agent's scratchpad append and the `last_committed_event_id` bump were not atomic with respect to the SSE reader thread. The reader thread received event `id: 87` (a `fetch_url` result containing a 40KB document), handed it to the planner's append queue, and — because the append queue was backpressured by the planner's own context-window compaction — the reader acked `Last-Event-ID: 86` on the *next* reconnect that fired 900ms later (server keepalive timeout misconfigured to 1s). The planner had already issued the `tools/call` for event 87's *predecessor* side effect and was mid-compaction. On reconnect, the server replayed from 87. The client's append queue had *also* already enqueued 87 from the first delivery (the reader had received it; it just hadn't committed the watermark). Result: event 87 was appended twice. Because `fetch_url` results were treated as idempotent reads, this was merely wasteful. Then the same race hit event 91, a `tools/call` to a *write-capable* `create_issue` tool. The duplicate append caused the planner to re-emit the `create_issue` call on its next turn — two identical GitHub issues, 400ms apart. The server had no dedup because MCP `tools/call` carries no idempotency token and the server is stateless across the resume. A second variant was observed on a different client: the reader thread acked *optimistically* (`Last-Event-ID` set to the id of the event *about to be* processed, before processing). On a mid-processing crash, resume started at `id+1`, permanently skipping the event. The tool call never executed, but the planner's scratchpad contained a "pending" marker for it, so the planner reasoned forward as if the tool had returned empty — producing a hallucinated "no results found" for a search that was never run. ## Condition of Validity The invariant that makes resume safe is: > For every event `E` with transport id `n`, the client's durable state must satisfy: `committed(n) ⟺ side_effect(E) has been applied exactly once and durably recorded`. The `Last-Event-ID` echoed on resume must equal `max{n : committed(n)}`, and the server must replay `(max_committed, ∞)` with at-least-once delivery. This holds only when all three of the following are true: - **Watermark-after-effect ordering.** The watermark write is causally after the side effect's durable record, never before. Optimistic ack violates this. - **At-least-once replay window is non-empty and bounded.** The server must retain events from the client's last acked id. If the server's replay buffer has been truncated (common: server keeps only the last N events, or the session TTL expired), the resume silently starts at the buffer head, and the gap `(last_acked, buffer_head)` is lost with no error — the client sees a "successful" resume. - **Side effects are idempotent or carry a client-generated idempotency key.** MCP `tools/call` has no such field by default; the client must synthesize one (e.g., `idempotency_key = hash(session_id, event_id, tool_name, args)`) and the server must honor it, or the client must gate re-emission on its own dedup set. If any of these fails, resumption is not a continuity mechanism — it is a *duplication or loss amplifier* that fires precisely when the system is already under stress (backpressure, compaction, keepalive races). ## Decision Boundary The boundary is between **"resume as transport repair"** and **"resume as execution replay."** These require different machinery: | Condition | Correct resume strategy | |---|---| | Event is a pure read (streamed token, log line, idempotent GET) | Transport watermark is sufficient; at-least-once replay is safe. | | Event triggers a side-effecting `tools/call` | Watermark alone is *insufficient*. Require client-side dedup set keyed on `(session_id, event_id)` OR a server-honored idempotency key. | | Server replay buffer may have truncated past `Last-Event-ID` | Resume must be *rejected* with an explicit `resume_gap` error, not silently continued. Client must fall back to full session re-init and re-derive state. | | Client acked optimistically | This is a bug, not a boundary. Fix the ack ordering; do not paper over with dedup. | The non-obvious rule: **the `Last-Event-ID` header must be derived from the execution state, not from the read position.** Most SSE clients (including browser `EventSource`) ack on read, which is correct for browsers (no side effects) and wrong for agents (side effects everywhere). An agent that uses a stock SSE client inherits the browser's ack semantics and silently violates the invariant. ## Operational Effect In the observed deployment, the fix was threefold and each part was load-bearing: 1. **Watermark-after-commit.** The reader thread no longer touched `Last-Event-ID`. Instead, a separate committer wrote `last_committed_event_id` only after the planner's scratchpad append + side-effect record were durable. On reconnect, the resume header was read from the committer, not the reader. 2. **Client-side dedup set.** The planner maintained a bounded LRU of `(session_id, event_id)` for the last 512 side-effecting events. Re-delivered events in the replay window were dropped before re-emission. This is the cheap belt-and-suspenders that catches the window between side effect and watermark write. 3. **Explicit gap rejection.** The server was modified to return `409 resume_gap` with the actual buffer head when `Last-Event-ID` fell outside the retained window, forcing the client into full re-init rather than a silent partial replay. Post-fix, the duplicate-`create_issue` class of failure disappeared (0 occurrences over 3 weeks vs. 4/week baseline). The hallucinated-empty-result class also dropped to zero, because the "pending" marker could no longer survive a resume without a corresponding committed event. The residual cost is that resume can replay up to the dedup-window size in wasted work on the client — acceptable, because the alternative is silent state divergence, which is strictly worse than wasted compute in an agent loop. The general lesson: **MCP streaming resumption is a transport feature that agents routinely mistake for an execution-continuity feature.** The transport guarantees at-least-once delivery from a watermark; it does not guarantee that the watermark corresponds to a committed side effect, and it does not guarantee the replay window is non-empty. Any agent that treats `Last-Event-ID` as "where I left off" rather than "where I last *durably finished*" will, under load, either duplicate side effects or skip them — and both failure modes are invisible in the happy path. ```python import hashlib from dataclasses import dataclass, field class ResumeInvariantViolation(RuntimeError): """Raised when an MCP stream resume would duplicate or skip a side-effecting event.""" @dataclass class StreamResumeGuard: session_id: str committed_event_id: int = 0 dedup_window: int = 512 _seen_side_effects: set = field(default_factory=set) def _key(self, event_id: int) -> str: return hashlib.sha256(f"{self.session_id}:{event_id}".encode()).hexdigest() def resume_header(self, server_buffer_head: int) -> dict: # If the server can no longer replay from our watermark, refuse to resume. if self.committed_event_id < server_buffer_head - 1: raise ResumeInvariantViolation( f"resume gap: committed={self.committed_event_id} " f"buffer_head={server_buffer_head}; full re-init required" ) return {"Last-Event-ID": str(self.committed_event_id)} def on_replayed_event(self, event_id: int, is_side_effecting: bool) -> bool: """Return True if the caller should process this event, False to drop it.""" if not is_side_effecting: return True k = self._key(event_id) if k in self._seen_side_effects: return False # duplicate replay; suppress re-emission if len(self._seen_side_effects) >= self.dedup_window: self._seen_side_effects.pop() self._seen_side_effects.add(k) return True def commit(self, event_id: int, side_effect_applied: bool) -> None: if side_effect_applied and event_id > self.committed_event_id: self.committed_event_id = event_id elif not side_effect_applied and event_id > self.committed_event_id: # Optimistic ack attempt: refuse, because it would skip the event on resume. raise ResumeInvariantViolation( f"cannot advance watermark to {event_id} before side effect is durable" ) ```