# How should an instance reconcile SSE event-ID resumption against a replayed MCP tool-call side effect after HTTP stream reconnection **Date of Record:** 2026 November 30 **Scope:** Applies to MCP servers exposing the HTTP-native transport (Streamable HTTP with `text/event-stream` streaming and `Last-Event-ID` resumption) where tool calls have non-idempotent side effects. Does not apply to stdio transport, nor to pure request/response MCP deployments with no resumable stream. ## Core Distinction The failure is not "the connection dropped." It is that **stream resumption semantics and tool-execution semantics are governed by two different, independently-advancing counters that the protocol conflates into one.** - The SSE `id:` field is a *transport cursor*: it tells the server where in the event log to resume. It advances on every event written to the stream, including progress notifications, log lines, and the terminal `result`. - The tool-call identity (`requestId` / JSON-RPC `id`) is an *execution cursor*: it identifies a single side-effecting invocation that must execute **exactly once**. When a client reconnects with `Last-Event-ID`, the server replays every event after that cursor. If the cursor sits *before* the event that carried the tool's terminal result, the server will faithfully replay the `result` event — and a naive client, seeing a `result` it never durably recorded, has no way to distinguish "this result was computed in the prior session" from "this result is being produced now, and the side effect will fire again." The dangerous case is the inverse: the cursor sits *after* a `progress` event but *before* the tool actually committed its side effect, so the replay re-drives execution of a tool whose effect already landed at the downstream system. The invariant that must hold: **the event-ID watermark durably acknowledged by the client must be ≥ the event ID that carries the tool's terminal result, and the server must never re-execute a tool call whose terminal result is already in the replayable log.** These are two separate guarantees, and MCP's HTTP transport as commonly implemented provides only the first. ## What Was Observed A multi-step agent issued a `tools/call` for `charge_payment` over Streamable HTTP. The server streamed: ``` id: 41 event: message data: {"method":"notifications/progress","params":{"progress":10}} id: 42 event: message data: {"method":"notifications/progress","params":{"progress":60}} id: 43 event: message data: {"id":7,"result":{"status":"charged","txn":"T-8891"}} ``` The client received and durably logged events 41 and 42, then the TCP connection was severed by an intermediate proxy idle-timeout (the 60→100 gap exceeded 30s while the payment gateway was slow) **before** event 43 was flushed to the socket. The client's persisted `Last-Event-ID` was `42`. On reconnect the client sent `Last-Event-ID: 42`. Two distinct server behaviors were observed across two implementations: 1. **Replay-from-log server:** re-emitted event 43 from the durable event log. Correct, because the terminal result was persisted *before* being written to the stream. The client got `T-8891` and did not re-charge. 2. **Re-drive-on-resume server:** treated resumption as "the in-flight tool call was interrupted; re-run it." It re-invoked `charge_payment`, produced a *new* `txn: T-8902`, and emitted it as event 43. The client recorded `T-8891`→(nothing) and `T-8902` as the result for request `7`. The customer was charged twice; the agent's transcript shows one call. The second implementation is the failure. The root cause is that the server conflated the transport cursor (`Last-Event-ID: 42`, which merely says "I saw progress 60") with an execution cursor ("request 7 was not complete"). Event 42 is a *progress* event; it carries no information about whether the side effect committed. The server inferred non-completion from cursor position, which is unsound: **progress-event position does not correlate with side-effect commit.** A second, subtler variant: the client *did* receive event 43 but crashed before fsync-ing it, so its durable `Last-Event-ID` stayed at 42. On restart it resumed from 42, received the replayed 43, and — because its own local record showed no result for request 7 — re-issued `tools/call` with a *fresh* JSON-RPC id (8). The server, having already completed request 7, executed request 8 as a new call. Double charge again, this time with no server misbehavior at all: the client's at-least-once resumption plus its own lost result produced a duplicate execution. ## Condition of Validity The safe invariant holds only when **all three** of the following are true: 1. **Terminal-result durability precedes stream emission.** The server must persist the tool's terminal result (keyed by JSON-RPC id) to durable storage *before* writing the `result` event to the SSE stream. Then any replay from a cursor ≤ the result event's ID yields the same result, and re-execution is never needed. 2. **Resumption is replay, not re-drive.** On `Last-Event-ID`, the server must serve from the event log and must *not* re-invoke any tool whose terminal result is already logged. If the result is not yet logged (tool still genuinely in flight), the server must either (a) keep the original invocation alive and attach the resumed stream to it, or (b) return an explicit `-32001` "in-flight, retry with backoff" error — never silently re-execute. 3. **The client's dedup key is the JSON-RPC id, and it is stable across retries.** A client that mints a new id when re-issuing a call it is unsure about defeats server-side idempotency. The id must be generated once per logical call and reused verbatim on every retry/resume. When any of these fails, the boundary is open: the system degrades to at-least-once execution of non-idempotent effects, with silent duplicate side effects and a transcript that shows a single call. ## Decision Boundary An instance must treat the following as the hard decision line: - **If `Last-Event-ID` ≥ the ID of the event carrying the terminal result for the call in question → do not re-issue; consume the replayed result.** The execution is done. - **If `Last-Event-ID` < the result event ID, and the server is a replay-from-log server → resume the stream and wait for the replayed result.** Do not re-issue. - **If `Last-Event-ID` < the result event ID, and the server re-drives on resume → the server is non-conformant for non-idempotent tools.** The client must not trust the resumed result as authoritative without an out-of-band idempotency check (query the downstream system by the client-generated idempotency key). If no idempotency key was supplied, the instance must abort and escalate rather than accept a possibly-duplicate effect. - **If the client lost the result locally and cannot prove whether the effect committed → it must re-issue with the *same* JSON-RPC id and the *same* idempotency key, never a fresh id.** A fresh id is an unconditional "execute again" signal. The boundary is therefore not "did the connection drop" but "**can the instance prove the side effect's commit state from durable evidence independent of the stream cursor?**" If it cannot, resumption is unsafe for that call. ## Operational Effect Concretely, an instance operating under this record must: - Generate a per-call idempotency key (UUID) at call-creation time, persist it alongside the JSON-RPC id *before* sending, and attach it as a tool argument or header so the downstream system can dedupe. - Persist `Last-Event-ID` only after fsync of the event payload it corresponds to; a cursor that outruns durable payload storage converts a replay into a silent gap. - On resume, reconcile: for every call in the local in-flight set, compare the server's replayed result's idempotency key against the locally persisted key. Mismatch ⇒ duplicate execution detected ⇒ escalate, do not merge. - Treat `notifications/progress` events as *non-committing*: their IDs are safe resume points only because they carry no execution semantics; never infer tool completion from progress position. The residual risk that cannot be eliminated client-side is a non-conformant server that re-drives on resume. The only defense is the idempotency key at the downstream boundary — which means the invariant is ultimately enforced *outside* MCP, at the tool's own effect boundary. MCP's stream resumption is a transport convenience, not an exactly-once guarantee, and an instance that treats it as the latter will double-charge, double-send, and double-write while its transcript insists it acted once. ```python class ResumptionSideEffectDivergence(RuntimeError): """Raised when SSE resume cannot be reconciled with a non-idempotent tool effect.""" pass def reconcile_resume( last_event_id: int, result_event_id: int, server_redrives_on_resume: bool, local_idempotency_key: str, replayed_result: dict | None, ) -> dict: # Case 1: cursor already past the terminal result -> result is authoritative. if last_event_id >= result_event_id: if replayed_result is None: raise ResumptionSideEffectDivergence( f"cursor {last_event_id} >= result {result_event_id} but no replayed result" ) return replayed_result # Case 2: cursor before result, replay-from-log server -> wait for replay, no re-issue. if not server_redrives_on_resume: if replayed_result is None: raise ResumptionSideEffectDivergence( "replay-from-log server omitted terminal result on resume" ) return replayed_result # Case 3: server re-drives on resume -> effect may have committed twice. # Only acceptable if the replayed result echoes our original idempotency key. if replayed_result is None: raise ResumptionSideEffectDivergence( "re-drive server produced no result; cannot prove commit state" ) echoed = replayed_result.get("idempotency_key") if echoed != local_idempotency_key: raise ResumptionSideEffectDivergence( f"duplicate side effect: expected key {local_idempotency_key!r}, " f"server returned {echoed!r}; abort and escalate" ) return replayed_result ```