# How does an instance detect a silently reordered SSE replay after an MCP stream resumes through a CDN edge? **Date of Record:** 2026 December 3 **Scope:** Applies to MCP servers exposed over HTTP with Server-Sent Events (SSE) for server→client streaming, multiplexed across a shared connection and resumed via `Last-Event-ID` after an interruption, where a CDN or edge proxy sits between client and origin. Does not apply to direct origin connections without an intermediary cache/proxy, nor to WebSocket transports that lack `Last-Event-ID` semantics. ## Core Distinction The failure here is not "the stream dropped." Dropped streams are detectable and expected. The failure is that the stream **resumed successfully** — the client received a `200`, the SSE parser accepted every frame, and the agent resumed its tool-call reasoning — but the event sequence delivered by the edge proxy was **not the sequence the origin emitted**. The proxy re-served a cached prefix, coalesced frames across a cache boundary, or reordered events belonging to different multiplexed logical streams that shared one TCP connection upstream. The distinction that matters operationally: **transport-level resumption success is not sequence-level continuity.** An agent will happily consume a replayed, deduplicated, or reordered event log and treat it as authoritative state, then compound the error across subsequent tool calls. The invariant that must hold is not "the connection resumed" but "the ordered event IDs consumed form a strictly monotonic, gap-free prefix of the origin's emission log for each logical stream." ## What Was Observed An MCP server multiplexed three logical sessions (`session A`, `B`, `C`) over one HTTP/2 upstream connection to origin, each identified by an `Mcp-Session-Id` header. Client consumed SSE. A CDN edge in front of origin cached responses keyed only on URL path plus `Accept: text/event-stream`, **omitting `Mcp-Session-Id` and `Last-Event-ID` from the cache key** — a configuration the origin never sees and cannot detect. Trigger sequence: 1. Session A's stream stalled at the edge (idle timeout at the proxy, not origin). Client reconnected with `Last-Event-ID: A-0042`. 2. The edge had a cached 200 response for the path from session B's earlier request. Because the cache key omitted session identity, it served B's frames to A's resume request. 3. Frames were syntactically valid SSE with `id:` fields in B's namespace (`B-0117`, `B-0118`). A's parser accepted them. The agent's tool-call loop read a `tool_result` frame intended for B and applied it to A's pending invocation. 4. No error surfaced. The agent continued for four more tool calls before a downstream schema mismatch on a `tool_result` payload (meant for a different invocation) produced a `KeyError` deep in the reasoning loop — misattributed to the tool, not the transport. The observable signature at the moment of failure was **not** a transport error. It was: an event `id` that did not share the prefix of the last consumed id for that session, arriving inside an otherwise well-formed `200 text/event-stream`. ## Condition of Validity The resumption contract is only valid when all of the following hold simultaneously: - The `id:` field of each SSE frame is **globally namespaced per logical session** (e.g., `A-0042`), not a per-connection counter that restarts. - The client validates that each incoming `id` is the **immediate successor** of the last consumed `id` for that session, and that the session tag matches the `Mcp-Session-Id` it issued. - The edge proxy's cache key includes `Mcp-Session-Id` **and** treats `Last-Event-ID` as a request-varying header, or the origin emits `Cache-Control: no-store` on stream endpoints. - The origin's emission log is the single source of truth; the proxy is a pass-through, never a replay source for a different session. Remove any one condition and the resumption is **unverifiable** — the client cannot distinguish a legitimate replay from origin (which it requested) from a cross-session bleed or coalesced-frame corruption introduced by the proxy. ## Decision Boundary The decision an agent must make on every resumed SSE frame: - **Accept and continue** only if `id` parses, its session tag equals the active `Mcp-Session-Id`, and its sequence number equals `last_consumed_seq + 1`. On the first frame after resume, `last_consumed_seq + 1` is the exact expected value; anything else is a gap or a bleed. - **Halt and re-establish** if a gap is detected (`id.seq > last+1`): the client must not silently skip; it must re-request the missing range via `Last-Event-ID: last` and, if the origin cannot supply it, treat the session as **desynchronized** and re-initialize from a checkpoint rather than continue. - **Halt and quarantine** if `id.session != active_session`: this is a cross-session bleed. The frame must be discarded, the session marked tainted, and any tool result already applied from that frame must be rolled back before the reasoning loop continues — because the agent may have already committed a side effect based on it. The boundary is sharp: **continuity is a property the client must prove, not a property the transport asserts.** A `200` proves only that bytes arrived. ## Operational Effect Without client-side sequence proof, the failure mode is **silent state corruption with delayed, misattributed surfacing**: the agent's memory of "what the tool returned" diverges from origin truth, and the divergence manifests several reasoning steps later as a schema or logic error that gets blamed on the tool or the model rather than the transport. This is the worst class of agent failure — it is invisible at the point of corruption and expensive at the point of detection. The guardrail is a **monotonic per-session sequence validator at the SSE parse boundary**, before any frame reaches the reasoning loop, that raises on gap or session mismatch and forces checkpoint-based re-initialization. It converts a silent, compounding corruption into a loud, bounded, recoverable halt. ```python import re from dataclasses import dataclass _ID_RE = re.compile(r"^(?P[A-Za-z0-9]+)-(?P\d+)$") class StreamContinuityError(RuntimeError): """Raised when a resumed SSE frame breaks per-session sequence continuity.""" @dataclass class ContinuityState: session_id: str last_seq: int # -1 means nothing consumed yet for this session def validate_frame(state: ContinuityState, raw_id: str) -> int: """ Validate an incoming SSE frame's id against the active session's continuity state. Returns the accepted sequence number or raises. Called at the parse boundary, before the frame reaches the agent loop. """ m = _ID_RE.match(raw_id or "") if not m: raise StreamContinuityError( f"malformed SSE id {raw_id!r}; cannot prove continuity" ) frame_session = m.group("session") seq = int(m.group("seq")) if frame_session != state.session_id: # Cross-session bleed: edge served another session's cached stream. raise StreamContinuityError( f"session bleed: frame session {frame_session!r} != " f"active {state.session_id!r} (id={raw_id})" ) expected = state.last_seq + 1 if seq != expected: # Gap or replay: proxy coalesced, reordered, or re-served a prefix. raise StreamContinuityError( f"sequence discontinuity on {state.session_id}: " f"expected {expected}, got {seq} (id={raw_id})" ) state.last_seq = seq return seq def consume_resumed_stream(state: ContinuityState, frames: list[tuple[str, str]]) -> list[int]: """ frames: list of (id, data). Any discontinuity aborts the whole resume so the caller can re-initialize from a checkpoint instead of continuing on a corrupted prefix. """ accepted: list[int] = [] for raw_id, _data in frames: accepted.append(validate_frame(state, raw_id)) return accepted ```