# Which invariants prevent SSE resumption from silently forking an MCP session's tool-call ledger **Date of Record:** 2026 November 19 **Scope:** Applies to MCP servers using the HTTP-native transport where a single logical session spans an SSE stream that can be disconnected and resumed via `Last-Event-ID` plus a session token, and where the session carries stateful tool-call side effects (writes, allocations, external mutations). Does not apply to stateless request/response MCP transports, nor to SSE streams that carry only idempotent notifications. ## Core Distinction The naive mental model of SSE resumption treats the stream as a *lossy log replay*: the client missed events `N+1..M`, so it reconnects with `Last-Event-ID: N` and the server replays from `N+1`. Under this model, resumption is a pure read-side repair and cannot affect server state. The actual mechanism is different. In MCP's HTTP-native transport, the SSE stream is the *return channel for in-flight tool invocations*. When a client issues `tools/call` over HTTP POST, the server begins executing and streams the result (and any progress notifications) back on the session's SSE channel. If the stream drops *after the server has already begun executing the tool but before the client has durably recorded the result*, the client's `Last-Event-ID` no longer describes what the server actually did — it describes what the client *observed*. The gap between "server executed" and "client observed" is the failure surface. Resumption tokens are event-log cursors, not transaction checkpoints. ## What Was Observed A tool call `allocate_batch` (external side effect: reserves N units in a downstream quota system, returns a reservation ID) was issued on session `S`. The server began execution, emitted `notifications/progress` events `evt-101` and `evt-102` onto the SSE stream, then reserved the quota and emitted `evt-103` carrying the `tools/call` result with reservation ID `R-7`. The SSE connection dropped between the server emitting `evt-103` into its outbound buffer and the client's TCP stack acknowledging it. The client had last durably processed `evt-102`. On reconnect it presented `Last-Event-ID: evt-102` and the session token for `S`. The server, honoring the resumption contract literally, replayed from `evt-103`. But the server's resumption implementation had a subtle defect: its replay buffer was keyed by event ID and it re-derived the `tools/call` response from the *current* execution state rather than the *recorded* event payload. Because the original tool invocation had already committed, the re-derivation path found no in-flight call and re-executed the handler to satisfy the replay. The handler reserved another N units, returning `R-8`. The client received `evt-103` with `R-8`, believed it was the original result, and never learned `R-7` existed. Observed invariants that were violated: 1. **Replay must be a read of recorded bytes, never a re-execution.** The replay path shared code with the execution path. 2. **Event IDs must be assigned at commit, not at buffer-write.** `evt-103` was assigned before the reservation committed, so its existence did not imply the side effect was durable — and conversely, replay of an uncommitted event ID could not be distinguished from replay of a committed one. 3. **The session token identified the session but not the execution epoch.** After the disconnect, the server had no way to tell whether the client's `Last-Event-ID` referred to events from the pre-disconnect execution or a subsequent one. ## Condition of Validity The resumption contract is only sound when *all three* of the following hold simultaneously: - **Emit-at-commit ordering.** An event ID is written to the replay log in the same atomic step that the corresponding side effect becomes durable (or, for pure notifications, in the same step the value is finalized). If an event can be in the log before its effect is durable, replay can advertise effects that never happened; if an effect can be durable before its event is in the log, replay can omit effects that did happen. - **Replay purity.** The replay handler reads stored event payloads verbatim. It must not invoke tool handlers, re-run queries, or recompute results. Any code shared between live execution and replay is a latent fork. - **Epoch binding.** The session token carries (or the server tracks alongside it) an execution epoch that increments on every disconnect-and-resume boundary. A `Last-Event-ID` from epoch `k` presented against epoch `k+1` must be rejected or reconciled, never silently accepted. When any one fails, resumption degrades from "repair the client's view" to "mutate server state based on a client's stale cursor" — which is precisely a forked ledger. ## Decision Boundary The operational decision is not "should I support resumption" but *what the server does when the requested `Last-Event-ID` falls in the ambiguous window between "effect committed" and "event acknowledged by client."* Three defensible policies exist: - **Reject-and-reconcile (safest for side-effecting tools).** If `Last-Event-ID` is older than the last *acknowledged* event for a `tools/call`, the server refuses replay of that call's result and instead returns an out-of-band `session/reconcile` directive listing the call IDs whose outcomes are uncertain. The client must query each via an idempotency key before proceeding. Cost: one extra round trip per ambiguous call. - **Idempotency-key replay (correct when tool handlers are idempotent).** Every `tools/call` carries a client-generated idempotency key; replay re-reads the *recorded* outcome for that key. Requires the server to store outcomes keyed by idempotency key, not by event ID, and to never re-execute on replay. This is the only policy that preserves exactly-once semantics across arbitrary disconnects. - **At-least-once with dedup (acceptable for idempotent reads, unacceptable for writes).** Server replays by re-executing; client deduplicates by inspecting results. This is the policy that failed above — it is only valid when the tool has no external side effects. The boundary: **any tool whose handler mutates state outside the session MUST be gated behind either idempotency-key replay or reject-and-reconcile. At-least-once replay is forbidden for such tools.** This is a per-tool-class property, not a per-server one; a server may legitimately serve read-only tools under at-least-once and write tools under reject-and-reconcile on the same session. ## Operational Effect The immediate effect of the fork is a **phantom reservation**: `R-7` exists in the downstream quota system with no client-side record, and the client holds `R-8` believing it is the sole reservation. Quota is over-consumed by N units. Because the client's session state and the downstream state diverged at the resumption boundary, no later event on the stream can repair the discrepancy — the server's replay log has no entry for `R-7` (it was emitted, not re-derived, and the re-derivation path overwrote it), so even a full session replay cannot surface the orphan. The secondary effect is **silent, not loud**. The client received a well-formed `tools/call` result with a valid reservation ID. No error, no warning, no gap in the event sequence. Detection requires an external reconciliation job comparing session-recorded reservation IDs against the downstream system's ledger — a job that most MCP deployments do not run because the resumption contract *appears* to guarantee continuity. The architectural guardrail is therefore twofold: **make replay a pure read of commit-ordered event records, and bind the session token to an execution epoch so that a stale cursor cannot be silently honored.** Neither is sufficient alone — pure replay without epoch binding still permits replay across an execution boundary; epoch binding without pure replay still permits re-execution to satisfy a replay request. Together they reduce the resumption contract from "best-effort view repair" to "exactly-once continuity for side-effecting tool calls," which is the only version of the contract an autonomous agent can safely rely on when it cannot re-inspect the downstream system itself. ```python class ResumptionForkError(RuntimeError): """Raised when an SSE resumption request would replay across an execution epoch boundary or force re-execution of a side-effecting tool call.""" pass class EventRecord: __slots__ = ("event_id", "epoch", "payload", "committed") def __init__(self, event_id: str, epoch: int, payload: dict, committed: bool): self.event_id = event_id self.epoch = epoch self.payload = payload self.committed = committed class ResumptionGuard: def __init__(self, session_token: str, current_epoch: int, log: list[EventRecord]): self.session_token = session_token self.current_epoch = current_epoch self.log = log self.by_id = {r.event_id: r for r in log} def resume(self, last_event_id: str, presented_epoch: int) -> list[dict]: # Invariant 3: epoch binding. A stale cursor from a prior epoch is # never silently honored; it must be reconciled out-of-band. if presented_epoch != self.current_epoch: raise ResumptionForkError( f"resumption cursor {last_event_id} belongs to epoch " f"{presented_epoch}, session is at epoch {self.current_epoch}; " f"reconcile in-flight tool calls before resuming" ) anchor = self.by_id.get(last_event_id) if anchor is None: raise ResumptionForkError( f"unknown Last-Event-ID {last_event_id} for session " f"{self.session_token}" ) # Invariant 1: replay is a pure read of recorded bytes. We never # invoke a tool handler here. replay = [r for r in self.log if r.epoch == presented_epoch and r.event_id > last_event_id] # Invariant 2: emit-at-commit. An event whose side effect is not yet # durable must not be replayed as if it were; if we find one, the # client's cursor is in the ambiguous window and we must refuse. for r in replay: if not r.committed: raise ResumptionForkError( f"event {r.event_id} is buffered but not committed; " f"replay would advertise an uncommitted side effect — " f"require idempotency-key reconciliation" ) return [r.payload for r in replay] ```