# Which invariants prevent resumable MCP stream sessions from silently replaying tool side effects after a backpressure-induced disconnect **Date of Record:** 2026 November 30 **Scope:** Applies to MCP servers exposing HTTP-native streaming transports (SSE or chunked JSON-RPC over HTTP) with `Last-Event-ID`-style resumption, where a single logical tool invocation may emit multiple progress/result frames. Does not apply to stdio transports, to stateless request/response MCP endpoints, or to servers that never emit intermediate frames per invocation. ## Core Distinction Resumption is not replay. A resumable stream is defined by two independent cursors that engineers routinely conflate: the *transport cursor* (the last byte/frame the client durably acknowledged, expressed as an event ID) and the *effect cursor* (the last tool side effect the server has committed to its own state). The failure mode is that the transport cursor advances on frames that carry no committed effect, while the effect cursor advances on frames the client may never have received. When a backpressure-induced disconnect forces a resume, the server replays from the transport cursor — and if the transport cursor lags the effect cursor, the client receives frames describing effects that already happened, or the server re-executes a tool whose result frame was dropped in flight. The invariant that prevents silent duplication is: **the event ID emitted with any frame must be the ID of the last effect committed *at or before* that frame, never the ID of the frame itself.** ## What Was Observed A tool invocation `run_scan` on an MCP server emitted a stream of five progress frames (`scan: 20%`, `40%`, `60%`, `80%`) followed by a terminal `result` frame carrying `{scan_id, findings}`. Each frame carried a monotonically increasing `event_id` (1..5). The client consumed frames 1–3, then its read buffer filled (the client was a constrained agent runtime applying backpressure by pausing reads). The server's write buffer filled behind it. The HTTP layer eventually reset the connection after a 30s idle-write timeout. On resume, the client sent `Last-Event-ID: 3`. The server, whose stream generator had continued running to completion and had already committed the scan side effect (the `scan_id` was allocated, findings written to a store), replayed frames 4 and 5 from a buffered log. Frame 5 was the terminal `result`. The client — seeing `result` for the first time — recorded the findings as a fresh outcome and, because its own retry wrapper treated "connection reset during a tool call" as a failed invocation, issued a *second* `run_scan` call. The server allocated a second `scan_id` and ran the scan again. Two separate failures compounded: 1. **Effect-then-frame ordering.** The server committed the side effect (allocating `scan_id`, writing findings) *before* emitting the terminal frame, and emitted `event_id: 5` on that frame. The event ID described the frame, not the commit. A client resuming at `Last-Event-ID: 4` would receive frame 5 and correctly conclude the call succeeded; a client resuming at 3, as here, received frames 4 and 5 but had no way to know whether the effect had been committed exactly once or was being replayed from a buffer that outlived the effect. 2. **Resume ambiguity at the client.** `Last-Event-ID` resumption is only safe if the client can distinguish "the server is replaying a result I have not seen" from "the server is telling me the result I already acted on." Without an idempotency token bound to the effect, the client's retry wrapper treated the reset as failure and re-invoked. ## Condition of Validity The replay-safety invariant holds only when all three conditions are met simultaneously: - The server assigns the event ID to a frame **after** the effect it describes is durably committed, and the ID is a function of the commit sequence number, not the frame sequence number. - The server retains the buffered frames for a resume window **strictly bounded** by the effect-retention window — once the effect log is pruned, the frame log must already be pruned, so a resume can never reference an effect the server no longer remembers. - The client's retry wrapper treats a resumed terminal frame as authoritative for the invocation identified by the idempotency token carried in the original request, and suppresses re-invocation when that token is present in the terminal frame. If any condition fails, resumption degrades into either duplicate execution (server re-runs) or phantom success (client believes a dropped effect completed). The invariant is not "resumption is safe"; it is "resumption is safe only when the transport cursor is derived from the effect cursor." ## Decision Boundary The boundary is the moment the server chooses what to write into `event_id`. There are two defensible policies: - **Effect-anchored IDs (safe):** `event_id = last_committed_effect_seq` at the time the frame is written. Progress frames that carry no effect reuse the previous effect's ID. A resume from any ID ≥ the last committed effect yields only frames whose effects are committed and whose terminal frame carries the idempotency token. The cost: event IDs are non-unique across frames, which breaks naive `Last-Event-ID` dedup clients — the client must dedup on `(invocation_id, effect_seq)`, not on `event_id` alone. - **Frame-anchored IDs (unsafe for side-effecting tools):** `event_id = frame_seq`. Acceptable *only* for read-only or purely observational streams where replay is idempotent by construction. The decision rule: if the tool has any externally visible effect (allocation, write, external call), the server MUST use effect-anchored IDs and MUST embed the idempotency token in every terminal frame. If the tool is observational, frame-anchored IDs are permitted. ## Operational Effect With effect-anchored IDs and a bounded frame-retention window, the client's resume path becomes deterministic: resuming at `Last-Event-ID: k` either yields frames whose effects are all committed (and the terminal frame's token matches the in-flight invocation, so the retry wrapper suppresses re-invocation) or yields a `410 Gone` when `k` predates the retained window, forcing the client to treat the invocation as *unknown* rather than *failed* — and to query the effect log by idempotency token before retrying. The failure this prevents is not a crash; it is a silent double-execution that looks like a success on both sides. The guardrail is a server-side assertion, checked at frame-write time, that the frame's declared effect sequence is ≤ the committed effect sequence, plus a client-side assertion that a terminal frame's token matches the outstanding invocation before the result is accepted. ```python class ResumptionEffectMismatch(RuntimeError): """Raised when a resumable MCP stream frame claims an effect that is not yet committed, or when a resume references an effect outside the retained window. This is the guardrail against silent double-execution.""" class StreamFrame: def __init__(self, invocation_id, effect_seq, committed_effect_seq, is_terminal, idempotency_token): self.invocation_id = invocation_id self.effect_seq = effect_seq self.committed_effect_seq = committed_effect_seq self.is_terminal = is_terminal self.idempotency_token = idempotency_token def emit_frame(frame: StreamFrame, retained_effect_floor: int) -> str: # Invariant 1: a frame may not describe an effect that is not committed. if frame.effect_seq > frame.committed_effect_seq: raise ResumptionEffectMismatch( f"frame for invocation {frame.invocation_id} declares effect " f"{frame.effect_seq} but only {frame.committed_effect_seq} committed" ) # Invariant 2: the resume window must not outlive the effect log. if frame.effect_seq < retained_effect_floor: raise ResumptionEffectMismatch( f"frame effect {frame.effect_seq} predates retained floor " f"{retained_effect_floor}; resume would reference a pruned effect" ) # Invariant 3: terminal frames must carry an idempotency token so the # client can suppress re-invocation on resume. if frame.is_terminal and not frame.idempotency_token: raise ResumptionEffectMismatch( f"terminal frame for {frame.invocation_id} lacks idempotency token" ) # Effect-anchored event ID: derived from commit sequence, not frame sequence. return f"id: {frame.committed_effect_seq}\n" def resume(last_event_id: int, retained_effect_floor: int, committed_effect_seq: int) -> int: if last_event_id < retained_effect_floor: # Force the client to reconcile via the effect log by token. raise ResumptionEffectMismatch( f"resume at {last_event_id} below retained floor " f"{retained_effect_floor}; client must query effect log" ) if last_event_id > committed_effect_seq: raise ResumptionEffectMismatch( f"resume at {last_event_id} ahead of committed effect " f"{committed_effect_seq}; stream state is inconsistent" ) return last_event_id ```