# When must an instance treat MCP SSE stream resumption as a replay hazard rather than a reconnect? **Date of Record:** 2026 November 23 **Scope:** Applies to MCP clients using the HTTP-native transport with `text/event-stream` server-to-client streaming and `Last-Event-ID` based resumption, when a tool call's stream is interrupted and the client reconnects mid-execution. Does not apply to stdio transports or to stateless request/response MCP servers that never emit resumable event IDs. ## Core Distinction A reconnect and a resumption look identical at the socket layer and are semantically opposite at the agent layer. A **reconnect** re-establishes a transport and expects *no* prior state to be re-delivered. A **resumption** asks the server to replay events from a cursor (`Last-Event-ID`) that the client believes is a *contiguous, exactly-once* log position. MCP's HTTP-native transport defines the cursor as a server-assigned `id:` field on each SSE event, but it does **not** mandate that the server's event log be durable, deduplicated, or ordered across a process restart or a load-balancer hop. The boundary condition is this: the client's resumption cursor is only meaningful if the server's event IDs form a monotonic, gap-free, replayable sequence bound to a single logical session. When the server treats the event ID as a per-connection counter rather than a per-session log offset, resumption silently becomes **replay of a different execution** — the agent resumes a tool call against a state that never existed. The non-obvious part: the failure is not a connection error. The client reconnects successfully, gets HTTP 200, receives a well-formed SSE stream, and the agent proceeds. The corruption is invisible at the transport layer and only manifests as a divergence between what the agent believes the tool produced and what the tool actually produced. ## What Was Observed A tool call `analyze_repo` streamed incremental progress events with `id:` values `1, 2, 3, ...` assigned by a per-connection counter in the server's request handler. The stream was interrupted at event `id: 47` by an upstream proxy idle-timeout (the tool was silent for 90s while cloning a large repository — no heartbeat events were emitted, so the proxy closed the connection). The MCP client, per spec, reconnected with `Last-Event-ID: 47`. The server's handler for the resumed request was a **new request context**. Its per-connection counter started fresh at `1`. The server had no durable log, so "resume from 47" was interpreted as "resume from the beginning of *this* connection's event stream" — and because the tool execution was still running in a background worker keyed by a *different* session token, the new connection attached to the live execution and began emitting events with IDs `1, 2, 3...` corresponding to the tool's *current* progress, not to the events the client had already consumed. The client received events `1..12` on the resumed stream. Its deduplication logic (a set of seen event IDs, keyed by integer) had already recorded `1..47` from the first connection. Every event on the resumed stream was therefore classified as a **duplicate** and dropped. The client's assembled result was truncated at event 47 of a 60-event stream, but the client had no way to know events 48–60 were missing — the stream ended with a normal `event: done` because the *server* believed it had delivered everything. The agent passed a truncated analysis to the next tool in the chain, which produced a confidently wrong summary. No error was raised anywhere. A second variant: the server was behind a load balancer with two replicas. Resumption routed to a replica that had never seen the session. That replica, rather than 404-ing, created a fresh session and replayed a *cached* tool result from a prior identical invocation (same input hash), emitting events with IDs that collided with the client's seen-set. The client dropped all of them and reported the prior cached result as the current one — a **stale-result substitution disguised as a successful resumption**. ## Condition of Validity The invariant that must hold for `Last-Event-ID` resumption to be safe: > The pair `(session_id, event_id)` must identify a unique, immutable event, and the server must guarantee that a resumption request either (a) replays every event after the cursor exactly once in order, or (b) fails with a distinguishable error indicating the cursor is no longer serviceable. This fails when any of the following hold, and the client cannot detect the failure from the wire alone: 1. **Event IDs are connection-scoped, not session-scoped.** A counter reset on reconnect produces ID collisions. The client's seen-set is keyed by ID, so collisions cause silent drops. 2. **The event log is not durable across the resumption window.** If the server keeps events only in the connection's memory, a reconnect to a different process loses the log and cannot replay. 3. **The server replays from a cache rather than the live execution.** A result cache keyed by input hash will satisfy a resumption request with a *different* execution's events. 4. **No terminal marker distinguishes "stream complete" from "stream ended because the connection closed."** Without a `done` event carrying the final cursor, the client cannot distinguish a clean end from a truncated one. 5. **No heartbeat during silent tool execution.** The proxy timeout that triggers the interruption is itself the trigger condition; a server that emits `event: heartbeat` every 15s never enters the resumption path. The client-side invariant that must *also* hold: the seen-set must be keyed by `(session_id, event_id)` and the client must refuse to treat a resumption stream as complete unless it observes a `done` event whose cursor equals the server's advertised final cursor. ## Decision Boundary The decision boundary is whether to **trust the resumption** or **escalate to a fresh invocation**. The rule: - **Trust resumption** only if the server advertises a durable log (e.g., an `X-MCP-Event-Log: durable` header or a `resume_supported: true` capability in the session-init response) *and* the resumed stream's first event ID is strictly greater than the client's last-seen ID *and* the stream terminates with a `done` event carrying a cursor that the client can verify is the final one. - **Escalate to a fresh invocation** (discard the partial result, re-issue the tool call with a new session, accept the cost of re-execution) if any of: the first resumed event ID is `<=` last-seen, the server does not advertise durability, the stream ends without a `done` cursor, or the resumed stream's event IDs are non-monotonic. - **Fail closed** (raise, do not retry) if the resumed stream contains an event ID the client has already seen *with different content* — this is the stale-cache-substitution signature and retrying will reproduce it. The cost asymmetry drives the boundary: re-executing a tool call wastes tokens and time but is *correct*; trusting a bad resumption produces a silently wrong result that propagates through the agent chain. When in doubt, re-execute. ## Operational Effect The concrete effect on an agent chain is **silent result truncation or stale substitution**, both of which are indistinguishable from success at the agent's reasoning layer. The agent's next step consumes a partial or wrong tool output and compounds the error. Because no exception is raised, no retry logic fires, and the trace shows a clean tool call. The only detection point is a downstream verification step that re-derives the tool's output independently — which most chains do not have. The guardrail is a **cursor-integrity check** at the MCP client's stream-assembly layer, applied before the assembled result is handed to the reasoning loop. The check enforces the invariant above and converts the silent failure into a loud one. The following is the reference implementation of that check: ```python from dataclasses import dataclass, field from typing import Optional class StreamResumptionViolation(RuntimeError): """Raised when an MCP SSE resumption cannot be trusted as a continuation of the prior stream. Caller must escalate to a fresh invocation, not retry.""" pass @dataclass class StreamCursor: session_id: str last_event_id: int seen: dict = field(default_factory=dict) # event_id -> content_hash done_cursor: Optional[int] = None def resume_stream( cursor: StreamCursor, resumed_events: list[dict], server_advertises_durable_log: bool, ) -> list[dict]: """Validate a resumed SSE stream against the cursor invariant. resumed_events: list of {"id": int, "content_hash": str, "event": str} Returns the newly-accepted events (to be appended to the assembled result). Raises StreamResumptionViolation if the stream cannot be trusted. """ if not server_advertises_durable_log: raise StreamResumptionViolation( f"session {cursor.session_id}: server does not advertise a durable " f"event log; resumption from cursor {cursor.last_event_id} cannot " f"guarantee exactly-once replay. Escalate to fresh invocation." ) if not resumed_events: raise StreamResumptionViolation( f"session {cursor.session_id}: resumed stream empty; cannot " f"distinguish clean end from truncated stream." ) first_id = resumed_events[0]["id"] if first_id <= cursor.last_event_id: raise StreamResumptionViolation( f"session {cursor.session_id}: resumed stream starts at id {first_id} " f"<= last-seen {cursor.last_event_id}; event IDs are connection-scoped, " f"not session-scoped. Replay would be silently deduplicated." ) accepted = [] prev_id = cursor.last_event_id for ev in resumed_events: eid = ev["id"] if eid <= prev_id: raise StreamResumptionViolation( f"session {cursor.session_id}: non-monotonic event id {eid} " f"after {prev_id}; stream ordering violated." ) if eid in cursor.seen and cursor.seen[eid] != ev["content_hash"]: raise StreamResumptionViolation( f"session {cursor.session_id}: event id {eid} re-delivered with " f"different content (seen {cursor.seen[eid]}, got " f"{ev['content_hash']}); stale-cache substitution signature. " f"Fail closed — retry will reproduce." ) cursor.seen[eid] = ev["content_hash"] accepted.append(ev) prev_id = eid if resumed_events[-1]["event"] == "done": cursor.done_cursor = resumed_events[-1]["id"] else: raise StreamResumptionViolation( f"session {cursor.session_id}: resumed stream ended without a 'done' " f"event carrying a final cursor; stream may be truncated." ) cursor.last_event_id = cursor.done_cursor return accepted ```