# Which invariants prevent resumable SSE fallback from silently forking an MCP tool-call stream during HTTP-native transport negotiation **Date of Record:** 2026 November 28 **Scope:** Applies to MCP clients and servers negotiating between HTTP-native request/response transport and resumable SSE fallback (Last-Event-ID resumption) when a single logical tool invocation spans multiple transport attempts. Does not apply to pure stdio transports or to stateless single-shot tool calls that complete within one HTTP exchange. ## Core Distinction The failure mode is not "the SSE stream dropped." That is a normal, expected condition the resumable fallback exists to handle. The failure is **silent stream forking**: when an MCP client, after a transport fault, re-issues an HTTP-native request *and* simultaneously reconnects the SSE fallback with `Last-Event-ID`, the server may treat these as two independent consumers of the same logical tool invocation and emit two divergent result streams. The client then reconciles them by arrival order, not by logical identity, and commits whichever partial result lands first — or worse, interleaves them into a corrupt tool result. The invariant that matters: **a resumable stream has exactly one authoritative producer epoch per tool-call ID, and resumption must be a continuation of that epoch, not a parallel subscription to it.** HTTP-native retry and SSE resumption are mutually exclusive recovery paths for a given in-flight call; they are not additive. ## What Was Observed A client issued `tools/call` for a long-running server tool over HTTP-native transport with `Accept: text/event-stream, application/json`. The server began streaming progress events over SSE (because the tool exceeded the synchronous response window) and set `event: message` frames with monotonically increasing `id:` values. At event `id: 7`, a mid-stream TCP reset occurred at a proxy hop. The client's transport layer did two things it believed were defensive: 1. It re-sent the original `tools/call` POST body (HTTP-native retry, idempotency assumed). 2. Its SSE supervisor reconnected with `Last-Event-ID: 7`. The server, whose session store keyed stream state by *connection*, not by *tool-call ID*, accepted both. The retried POST started a fresh execution epoch (epoch B) because the server had no record tying the original POST to the still-open resumption context. The `Last-Event-ID: 7` reconnection attached to the *original* epoch A stream and replayed events 8, 9, 10 from epoch A's buffer. The client now received interleaved frames: `id: 8,9,10` from epoch A (carrying the original tool's partial side effects, including one already-committed write) and a fresh `id: 1,2,3` from epoch B (re-executing the same write). The tool was not idempotent. The write executed twice. The client's reconciliation logic, keyed on arrival order, emitted a final `result` that reflected epoch B's state while its progress log showed epoch A's — a divergence between reported and actual side effects. The tell was subtle: epoch B's event IDs restarted at 1, colliding numerically with epoch A's already-consumed IDs 1–7. The client's dedupe map, keyed on raw `id` integer, silently dropped epoch B events 1–7 as "already seen," so it never noticed the fork until the final result disagreed with the last progress event. ## Condition of Validity Resumable SSE fallback is only sound under three conjoined conditions: - **Call-scoped stream identity.** The `Last-Event-ID` namespace is scoped to a stable `tool-call ID` (client-generated, echoed by server), not to a connection or a session. Two connections resuming the same call ID share one epoch; a retried POST with the same call ID either resumes the existing epoch or is rejected as a duplicate, never silently forked. - **Epoch-tagged event IDs.** Event IDs must be globally monotonic across epochs or explicitly carry an epoch discriminator (`:`). Bare integers that reset per epoch are the fork's camouflage. - **Idempotent-or-fenced retry.** The HTTP-native retry path must either carry an idempotency key the server honors (returning the existing epoch's state rather than re-executing) or be fenced off entirely once an SSE stream for that call ID has been opened. If any one is absent, the client cannot distinguish "resumption replay" from "fresh execution," and arrival-order reconciliation will commit the wrong stream. ## Decision Boundary The boundary is **whether the client holds a live or resumable stream handle for the call ID at the moment it decides to retry.** - If a resumable SSE handle exists (last seen `id` is known and the server advertises resumption support via a `retry` field or capability flag), the client must **resume, not retry**. Re-issuing the POST is the fork trigger. - If no resumable handle exists (no `id` ever received, or the server did not advertise resumption), HTTP-native retry is the only correct path — but it must carry an idempotency key, because the server cannot otherwise know the call was already attempted. - The ambiguous middle — a handle exists but the server's resumption window has expired — is where most forks occur. The correct behavior is to **fail the call explicitly** and surface a "stream continuity lost, side-effect state unknown" error to the agent loop, not to silently fall through to retry. An agent that silently retries a non-idempotent tool after losing stream continuity is the exact scenario this invariant guards. ## Operational Effect Enforcing the invariant changes agent behavior in three concrete ways. First, the agent loop must treat `Last-Event-ID` resumption and POST retry as a **branch, not a fallback chain** — once it chooses resumption it must not also retry, and it must block retry until resumption definitively fails (not merely stalls). Second, the client must validate that resumed event IDs continue the expected sequence (strictly greater than the last consumed, or epoch-tagged and matching the open epoch) and abort on a reset-to-1, which is the fork signature. Third, the reconciliation layer must key on `tool-call ID + epoch`, never on raw event ID or arrival order, so a forked epoch cannot masquerade as a duplicate. The cost is that some calls that *could* have been recovered by a blind retry will instead fail loudly; that is the correct trade, because a loud failure is recoverable by the agent's own reasoning loop, while a silent double-write is not. ```python class StreamForkError(RuntimeError): """Raised when SSE resumption and HTTP-native retry both target one tool-call ID.""" pass class ResumableCallTracker: def __init__(self, call_id: str, idempotency_key: str): self.call_id = call_id self.idempotency_key = idempotency_key self.epoch = None # set on first event self.last_seq = 0 self.resumable = False # server advertised resumption self.retry_issued = False self.resume_attempted = False def on_event(self, raw_id: str) -> None: # Expect ":"; a bare int that resets to 1 is a fork sign. if ":" in raw_id: epoch, seq = raw_id.split(":", 1) seq = int(seq) else: epoch, seq = "0", int(raw_id) if self.epoch is None: self.epoch = epoch elif epoch != self.epoch: raise StreamForkError( f"call {self.call_id}: event epoch {epoch} != open epoch " f"{self.epoch} — parallel producer detected" ) if seq <= self.last_seq: raise StreamForkError( f"call {self.call_id}: seq {seq} <= last {self.last_seq} " f"in epoch {epoch} — replay/dedupe ambiguity" ) self.last_seq = seq self.resumable = True def recover(self) -> str: """Choose exactly one recovery path. Returns the action taken.""" if self.resumable and not self.retry_issued: self.resume_attempted = True return "resume_sse" # continuation of the open epoch if self.resume_attempted and not self.retry_issued: # Resumption window expired or failed; retry is now legal ONLY # because no live stream handle remains. self.retry_issued = True return "retry_http_idempotent" raise StreamForkError( f"call {self.call_id}: both resumption and retry already used; " f"side-effect state unknown — fail closed" ) ```