# When must an instance distrust a resumed MCP streamable-session after SSE fallback negotiation **Date of Record:** 2026 November 27 **Scope:** Applies to MCP clients and servers negotiating the HTTP-native transport where `text/event-stream` (SSE) is used as a fallback for streamable sessions, and where a client attempts resumption via `Last-Event-ID` after a mid-stream disconnect. Does not cover stdio transport, WebSocket transports, or stateless request/response MCP servers. ## Core Distinction The MCP streamable HTTP transport lets a client reattach to an in-flight server-to-client stream by presenting the `Last-Event-ID` header, and the server is expected to replay or continue from that sequence position. The subtle failure is not "resumption fails" — it is that resumption **succeeds** with a stream whose event-ID space silently restarted. When the server's SSE fallback path is a *different process, replica, or code path* than the one that produced the original stream (e.g., a load-balanced fallback node, or an in-process fallback that reinitializes its event counter), the server will happily accept `Last-Event-ID: 47` and then emit events numbered `1, 2, 3…`. The client sees a syntactically valid SSE stream, treats it as continuation, and splices a fresh event sequence onto a stale session state. There is no protocol-level error: `200 OK`, `text/event-stream`, monotonic-looking IDs. The invariant that must hold — *an event ID presented for resumption is only meaningful within the ID-generation epoch that minted it* — is not carried by the transport. The client must therefore treat "resumed stream whose first event ID does not strictly follow the presented `Last-Event-ID`" as a **new epoch**, not a continuation, and reconcile session state rather than append to it. ## What Was Observed A tool-calling agent maintained a streamable MCP session against a server fronted by a round-robin proxy. The primary node emitted SSE events with a per-session counter (`id: 1` … `id: 47`) representing tool-progress notifications and one pending `sampling/createMessage` request. At event 47 the TCP connection was reset by an intermediary after an idle window. The client reconnected with `Last-Event-ID: 47` and `Accept: text/event-stream`. The proxy routed the reconnect to the fallback node, which had never seen this session. The fallback implemented resumption as "open a new SSE stream and start counting," emitting `id: 1, id: 2, …`. The client library, which only checked that the response was `200` and `text/event-stream`, appended these events to its existing buffer. Two consequences compounded: 1. The replayed `id: 1..N` events were *not* replays at all — they were genuinely new notifications for a session the fallback node had reconstructed from a shared store that lagged the primary by one write. The agent executed a tool call twice because event `id: 3` (new epoch) described the same `tools/call` result that the primary had already delivered as `id: 3` (old epoch). 2. The pending `sampling/createMessage` request (old `id: 47`) was never re-emitted by the fallback, because the fallback's reconstructed session had no record of it. The agent's reasoning loop blocked on a response that would never arrive, while continuing to process new-epoch events — a split-brain between the two ID spaces. The observable signature in logs: a resumption request carrying `Last-Event-ID: 47` followed by a first delivered event with `id: 1`. No error status. The client's only defense was a check it did not perform. ## Condition of Validity The "resume and append" behavior is valid **only if** the following holds for the resumed stream: - The first event ID received after resumption is strictly greater than the presented `Last-Event-ID` (for numeric/monotonic servers), **or** the server explicitly signals replay via a documented replay-complete marker. - The server guarantees single-writer semantics per session ID for the lifetime of that session, including across fallback negotiation. When the transport falls back to SSE through a different node or a reinitialized emitter, neither condition is guaranteed. In that regime the only sound interpretation of a resumed stream whose first ID regresses (or is otherwise non-successor) is: **the server has started a new event epoch; the presented `Last-Event-ID` is void.** The client must then (a) discard its assumption of contiguous continuation, (b) re-fetch authoritative session state (e.g., via a `session/get`-style call or a full replay request with an explicit `since` parameter), and (c) reconcile any in-flight requests (like pending sampling) against that authoritative state rather than waiting on the old stream. ## Decision Boundary The boundary is a single predicate evaluated on the first event of a resumed stream: ``` resumed_epoch_is_continuous := first_event_id > presented_last_event_id ``` - If `true`: append; treat as continuation; in-flight requests remain valid. - If `false`: treat as **epoch reset**. Do not append. Enter reconciliation: re-query session state, re-issue or cancel any pending server-to-client requests whose liveness cannot be confirmed, and reset the local event cursor to the new epoch's basis. Additionally, do **not** re-send tool calls whose results were already observed in the prior epoch — deduplicate by a stable request ID, not by event ID, because event IDs are no longer comparable across epochs. The dangerous middle case — a fallback node that *does* continue the counter but from a lagging replica — is not caught by this predicate alone. It must be caught by a second guard: any replayed event whose payload duplicates a previously applied side effect (same `tools/call` request ID, same result hash) is suppressed idempotently. The predicate catches counter restarts; the idempotency guard catches lagging-replica replays. Both are required because SSE fallback can produce either. ## Operational Effect Without the epoch predicate, agents exhibit a specific and reproducible pathology: **duplicate tool execution plus a permanently orphaned pending request**, appearing as a reasoning loop that makes forward progress on new events while a `sampling` or elicitation await never resolves. The loop does not deadlock visibly — it lags, then times out at the token-budget or wall-clock layer, and the failure is misattributed to model reasoning rather than transport epoch confusion. With the predicate and idempotency guard in place, the failure degrades safely: the agent detects the epoch reset on the first event, re-queries state, suppresses the duplicate `tools/call`, re-issues the orphaned sampling request against the reconstructed session, and continues. The cost is one extra round-trip on fallback; the benefit is that resumption becomes a *recoverable* event rather than a silent state-splice. The guardrail belongs in the client transport layer, not in agent logic, because agent logic cannot see event IDs and cannot distinguish a fresh epoch from a continuation. ```python import hashlib from dataclasses import dataclass, field class StreamEpochViolation(RuntimeError): """Raised when a resumed SSE stream's first event ID does not continue the presented Last-Event-ID, indicating the server started a new epoch (e.g., after SSE fallback negotiation to a different node).""" @dataclass class ResumeGuard: presented_last_event_id: int seen_request_ids: set = field(default_factory=set) applied_result_hashes: set = field(default_factory=set) epoch_basis: int = 0 reconciled: bool = False def on_first_event(self, first_event_id: int) -> None: if first_event_id <= self.presented_last_event_id: # Server restarted its counter: continuation is a lie. self.epoch_basis = first_event_id self.reconciled = False raise StreamEpochViolation( f"resumed stream first id {first_event_id} does not follow " f"presented Last-Event-ID {self.presented_last_event_id}; " f"treating as new epoch, reconciliation required" ) self.epoch_basis = first_event_id - 1 def apply_tool_result(self, request_id: str, result_bytes: bytes) -> bool: """Returns True if the result should be applied, False if it is a duplicate from a lagging-replica replay and must be suppressed.""" h = hashlib.sha256(result_bytes).hexdigest() if request_id in self.seen_request_ids and h in self.applied_result_hashes: return False self.seen_request_ids.add(request_id) self.applied_result_hashes.add(h) return True def require_reconciliation(self) -> None: if not self.reconciled: raise StreamEpochViolation( "epoch reset detected; session state must be re-queried " "before appending further events or awaiting pending requests" ) ```