# What happens when an instance resumes an MCP stream after SSE-to-HTTP upgrade negotiation silently resets the event ID epoch **Date of Record:** 2026 December 3 **Scope:** Applies to MCP clients using HTTP-native transport with `text/event-stream` responses and `Last-Event-ID`-based resumption, where the server negotiates a transport upgrade (SSE → chunked HTTP POST streaming or vice versa) mid-session. Does not apply to pure stdio transports or to WebSocket-based MCP bindings that carry their own session framing. ## Core Distinction There are two ways an MCP session can "resume": **transport resumption** (re-establishing the byte stream and replaying events after `Last-Event-ID`) and **logical resumption** (replaying the semantic effects of tool calls the agent believed were in-flight). These are conflated by the MCP HTTP transport spec because both are keyed on the same `Last-Event-ID` header. The failure mode documented here lives exactly at the seam: when the server performs an SSE-to-HTTP upgrade negotiation, it may **rebase the event ID counter** to a fresh epoch (e.g., from `evt-4491` to `evt-1` for the new stream). A client that reconnects with `Last-Event-ID: evt-4491` against the rebased stream receives either (a) a 200 with an empty replay because no event with that ID exists in the new epoch, or (b) a silent fallback to "deliver from now," which the client interprets as "no missed events." Both outcomes make the agent believe its in-flight tool call completed cleanly when in fact the result was emitted into the old epoch and is now unreachable. ## What Was Observed A single-agent loop driving a long-running `filesystem.search` tool call over MCP HTTP transport. Sequence: 1. Client opens POST `/mcp`, server responds `Content-Type: text/event-stream`, assigns stream epoch `A`, emits `id: A-1` (ack), `id: A-2` (progress 0.1), `id: A-3` (progress 0.4). 2. Network blip at 30s. Client reconnects with `Last-Event-ID: A-3`. 3. Server has, in the interim, negotiated an upgrade: it now serves the same session over chunked HTTP POST streaming (`Content-Type: application/x-ndjson`) and has **re-initialized its event counter** for the new stream, starting at `1`. It does not carry epoch `A` forward. 4. Client sends `Last-Event-ID: A-3`. Server's replay buffer for the new stream is empty for that ID. Server returns `200 OK` with a fresh stream starting at `id: 1` (ack for the *reconnect*, not the original call). 5. Client parses `id: 1` as the next event, sees it is an `ack` for the reconnect, and — because the client's state machine treats "stream established + first event is ack" as "no replay needed" — concludes no events were missed. 6. The tool call's actual terminal result was emitted as `id: A-4` into epoch `A`, which is now unreachable. The agent proceeds with a state where the tool call is "in flight" but no future event will ever resolve it. It hangs until a higher-level timeout, then retries the tool call — duplicating the side effect (a filesystem write in this case) because the tool was not idempotent. The non-obvious part: **the transport succeeded**. HTTP 200, valid SSE framing, no error in logs. The failure is purely in the semantic layer, and it presents as a hang, not a disconnect. ## Condition of Validity This failure requires all of the following to be simultaneously true: - The transport is HTTP-native MCP with `Last-Event-ID` resumption (not stdio, not WebSocket). - The server supports mid-session transport upgrade negotiation (SSE ↔ chunked HTTP), which is permitted but not required by the spec. - The server's upgrade path rebases the event ID counter rather than preserving a monotonic global counter across epochs. - The client treats "reconnect ack received" as equivalent to "replay complete" without verifying that the replay actually covered the gap between its last-seen ID and the new stream's first ID. If the server preserves a globally monotonic counter across upgrades, or if the client verifies the gap explicitly (e.g., by requiring a `replay-from` field in the ack), the failure does not occur. If the tool is idempotent, the failure degrades to a latency spike rather than a correctness bug. ## Decision Boundary The architectural invariant that bounds this failure: **event ID identity must be namespaced by stream epoch, and resumption must fail closed when the requested epoch is not the current epoch.** Concretely: - Servers MUST emit event IDs of the form `:` where `` changes on any transport renegotiation. - On reconnect, if `Last-Event-ID`'s epoch ≠ current epoch, the server MUST respond with `409 Conflict` and a `replay-unavailable` body, NOT a fresh stream. The client is then obligated to reconcile at the logical layer (query tool status by call ID, not by stream position). - Clients MUST treat "first event after reconnect is an ack for the reconnect itself" as insufficient evidence of replay completeness. They must observe an explicit `replay-complete` marker or a `replay-from: :` field matching their requested ID. The decision boundary for the agent: when a reconnect yields an epoch mismatch, the agent must **not** assume the in-flight tool call is still running. It must enter an explicit `UNKNOWN` state for that call and either (a) query the tool's status by call ID if the tool exposes one, or (b) if the tool is non-idempotent, halt and escalate rather than retry. ## Operational Effect Without the invariant, the observable effect is a **silent tool-call orphaning**: the agent's execution graph contains a node that will never receive a terminal event, and the retry path duplicates side effects. In multi-agent settings this is worse: a downstream auditor agent that receives the agent's transcript sees a tool call marked "pending" and a subsequent retry marked "completed," and may flag the transcript as inconsistent — or worse, may accept the retry as the canonical result and discard the original (which actually succeeded). The verification replay diverges from the execution trace. With the invariant, the cost is a bounded `409` on reconnect and a forced logical reconciliation. The agent spends one tool-status query (or one escalation) per epoch-crossing reconnect, which is cheap; the alternative is a duplicated non-idempotent write, which is not. ```python from __future__ import annotations from dataclasses import dataclass class EpochMismatchError(RuntimeError): """Raised when a resumption request targets an event-ID epoch that is no longer current.""" @dataclass(frozen=True) class EventId: epoch: str seq: int @classmethod def parse(cls, raw: str) -> "EventId": epoch, _, seq = raw.partition(":") if not epoch or not seq.isdigit(): raise EpochMismatchError(f"malformed event id: {raw!r}") return cls(epoch=epoch, seq=int(seq)) @dataclass class StreamState: current_epoch: str last_seq: int = 0 def resume_stream( state: StreamState, last_event_id: str | None, *, on_epoch_cross: str, # "fail_closed" | "reconcile" ) -> int: """ Return the sequence number from which the server should replay. Invariant: if the client's last-seen event ID belongs to a different epoch than the current stream, we MUST NOT silently start a fresh stream. We either fail closed (409) or force logical reconciliation at the tool-call layer. """ if last_event_id is None: return state.last_seq # fresh session, no replay needed requested = EventId.parse(last_event_id) if requested.epoch != state.current_epoch: if on_epoch_cross == "fail_closed": raise EpochMismatchError( f"replay requested for epoch {requested.epoch!r} " f"but current epoch is {state.current_epoch!r}; " f"client must reconcile tool-call state by call ID" ) if on_epoch_cross == "reconcile": # Caller is expected to have already queried tool status by call ID. # We still refuse to pretend the gap was replayed. raise EpochMismatchError( f"epoch cross from {requested.epoch!r} to " f"{state.current_epoch!r} requires logical reconciliation; " f"do not treat reconnect ack as replay-complete" ) raise EpochMismatchError(f"unknown on_epoch_cross policy: {on_epoch_cross!r}") if requested.seq > state.last_seq: raise EpochMismatchError( f"client claims to have seen seq {requested.seq} but server " f"only emitted up to {state.last_seq} in epoch {state.current_epoch!r}" ) return requested.seq + 1 ```