# What structural signals indicate MCP SSE replay has silently desynchronized a stateless tool call **Date of Record:** 2026 November 26 **Scope:** Applies to MCP clients using the HTTP-native transport (streamable HTTP with `text/event-stream` responses) where a stateless agent issues tool calls and relies on `Last-Event-ID` resumption after a mid-stream disconnect. Does not apply to stdio transports, nor to stateful sessions where the server holds a durable per-session event log keyed by a monotonic cursor that the client can also read out-of-band. ## Core Distinction The distinction that matters is between **transport-level resumption** and **semantic-level continuation**. MCP's SSE resumption contract (`Last-Event-ID` → server replays events after that ID) guarantees only that the *byte stream* is reattached. It does **not** guarantee that the replayed events compose into the same logical tool-call result the agent was assembling before the drop. A stateless server — one that does not persist per-call event buffers — has three legal responses to a resume request: replay from a durable log, replay from a bounded in-memory ring buffer, or reject with a fresh stream that omits the lost events. The first two preserve continuation; the third silently truncates it. The failure is not the disconnect. The failure is the agent treating "stream reattached successfully" as "reasoning state preserved." The non-obvious part: an SSE `id:` field is opaque and server-defined. Clients commonly assume `id` is monotonic and gap-free. Under a stateless server, IDs may be per-connection sequence numbers (resetting to `1` on each new connection), may be wall-clock-derived, or may be absent entirely on replayed frames. A resume with `Last-Event-ID: 47` against a server whose ring buffer holds IDs `1..50` for a *different* logical call will happily replay frames `48,49,50` — syntactically valid, semantically foreign. ## What Was Observed A stateless agent orchestrator issued a `tools/call` for a long-running retrieval tool over streamable HTTP. The tool emits progress events (`id: 1..N`) followed by a single terminal `result` event. The connection dropped at event `id: 23` due to a load-balancer idle timeout at 60s (the tool had a 90s p99). The client reconnected with `Last-Event-ID: 23`. The server, having no per-call buffer, accepted the connection and — because its event-ID generator was a per-process atomic counter shared across all concurrent calls — emitted the *next* events from the global counter, which belonged to a different in-flight call. The client appended these to its partial result assembly. The result: a tool result that was a valid JSON object (so schema validation passed) but was a Frankenstein fusion of two different retrieval calls' output fragments. The agent consumed it as fact. Structural signals that this had occurred, in order of reliability: 1. **Event-ID discontinuity on the replayed prefix.** The first frame after resume carried an `id` not equal to `Last-Event-ID + 1` in the expected arithmetic progression, or the progression was non-monotonic relative to the pre-drop frames. A gap (`23` → `31`) or a reset (`23` → `1`) is the primary tell. 2. **Absence of a call-scoped correlation token.** Frames carried no `call_id` (or equivalent) field, so the client had no way to verify the replayed events belonged to the same logical invocation. This is the enabling condition — signal 1 is only detectable because IDs happened to leak. 3. **Terminal event without a matching start.** The reattached stream delivered a `result` event but the client's assembled buffer lacked the `begin`/`progress` frames that a well-formed result should be preceded by. A result arriving with fewer than the tool's declared minimum progress count is a truncation signal. 4. **Latency profile inversion.** The replayed frames arrived within milliseconds of the resume request — faster than the tool's own minimum per-event emission interval — indicating they were served from a buffer, not generated live. A buffer serving frames for a call the client did not originate is replay contamination. 5. **`retry:` field changes mid-stream.** A server that switches its advertised reconnect delay between the pre-drop and post-resume stream is often a different server instance behind the LB, which cannot hold the original call's state. ## Condition of Validity The guardrail holds only under a specific, checkable precondition: **the client must be able to prove event provenance, not merely event arrival.** Concretely, resumption is semantically valid if and only if all three hold: - The server attaches a **call-scoped, client-supplied correlation ID** (echoed from the request, e.g. a `Mcp-Call-Id` header value) to every SSE frame, and the client rejects any frame whose correlation ID differs from the in-flight call. - The server's event IDs are **monotonic within a call** and the client enforces `new_id == last_id + 1` (or a server-declared gap policy) on the replayed prefix. - The server **declares its resumption mode** in the initial handshake (durable-log / ring-buffer-of-size-K / no-replay). Under no-replay, the client must treat resume as a *new* call and discard the partial buffer rather than append to it. Absent any one of these, the invariant "resumed stream composes into the same logical result" is unprovable and must be assumed violated. This condition is *not* met by the base MCP streamable-HTTP spec's `Last-Event-ID` mechanism alone, because that mechanism specifies reattachment, not provenance. ## Decision Boundary The boundary is between **append-and-continue** and **restart-and-reconcile**, and it is crossed at the first frame after resume: - If the first post-resume frame's correlation ID matches the in-flight call **and** its event ID is the exact successor of `Last-Event-ID` (or falls within a server-declared replay window the client can bound), the client appends and continues. Reasoning state is preserved. - If either check fails, the client must **discard the partial assembly, mark the tool call as indeterminate, and either re-issue the call idempotently (if the tool declares idempotency) or surface a partial-result fault to the planner.** It must not append. The subtle boundary case: a server that replays correctly but *duplicates* the last frame (at-least-once delivery, common when the drop happened between server-send and client-ack). The client sees `id: 23` again after having processed `23`. Appending here double-counts a progress event or, worse, double-applies a side-effecting fragment. The decision boundary therefore requires **dedup on event ID within a call**, not just monotonicity checking. Duplicate-detection and gap-detection are the two halves of the same guard. ## Operational Effect Without the provenance guard, stateless resumption degrades into a silent data-integrity fault that no schema validator catches, because the fused result is structurally well-formed. The observable consequences: - **Planner poisoning.** The agent's next reasoning step consumes a fabricated tool result as ground truth, and the error compounds across subsequent calls (each downstream call inherits the bad premise). - **Non-reproducible failures.** Because the contamination depends on cross-call counter interleaving, the same agent trajectory replays cleanly in staging and fails in production under concurrency. Debugging by replay is useless. - **Idempotency amplification.** If the client "fixes" the problem by re-issuing on any resume, side-effecting tools execute twice. The guard must distinguish *resume-with-provenance* (safe to append) from *resume-without-provenance* (must reconcile, and only re-issue if idempotent). The architectural invariant to enforce: **a partial tool result is a first-class uncertain state, never a mergeable buffer.** The client must carry an explicit `assembly_state ∈ {clean, partial, indeterminate}` and refuse to transition `partial → clean` on resume without a provenance proof. The following reference implementation encodes exactly that transition rule and raises on the specific contamination condition observed above. ```python from __future__ import annotations from dataclasses import dataclass, field from enum import Enum from typing import Optional class StreamResumeContamination(RuntimeError): """Raised when a resumed SSE stream cannot be proven to belong to the in-flight tool call, so appending would fuse foreign events into the partial result assembly.""" class AssemblyState(Enum): CLEAN = "clean" PARTIAL = "partial" INDETERMINATE = "indeterminate" @dataclass class SSEFrame: event_id: Optional[int] call_id: Optional[str] payload: dict @dataclass class ToolCallAssembly: call_id: str expected_min_progress: int state: AssemblyState = AssemblyState.CLEAN last_event_id: Optional[int] = None seen_event_ids: set[int] = field(default_factory=set) fragments: list[dict] = field(default_factory=list) def ingest(self, frame: SSEFrame) -> None: # Provenance: every frame must carry the call-scoped correlation id. if frame.call_id != self.call_id: self.state = AssemblyState.INDETERMINATE raise StreamResumeContamination( f"frame call_id={frame.call_id!r} does not match in-flight " f"call_id={self.call_id!r}; replay contamination suspected" ) # Dedup: at-least-once delivery may resend the last frame. if frame.event_id is not None and frame.event_id in self.seen_event_ids: return # idempotent drop; do not double-apply # Gap check on resume: successor must be exactly last+1. if ( self.last_event_id is not None and frame.event_id is not None and frame.event_id != self.last_event_id + 1 ): self.state = AssemblyState.INDETERMINATE raise StreamResumeContamination( f"event-id gap: expected {self.last_event_id + 1}, " f"got {frame.event_id}; resumed stream is not a continuation" ) if frame.event_id is not None: self.seen_event_ids.add(frame.event_id) self.last_event_id = frame.event_id self.fragments.append(frame.payload) self.state = AssemblyState.PARTIAL def finalize(self) -> dict: if self.state is not AssemblyState.PARTIAL: raise StreamResumeContamination( f"cannot finalize assembly in state {self.state.value}" ) if len(self.fragments) < self.expected_min_progress + 1: self.state = AssemblyState.INDETERMINATE raise StreamResumeContamination( f"terminal result has {len(self.fragments)} fragments, below " f"declared minimum {self.expected_min_progress + 1}; truncation" ) self.state = AssemblyState.CLEAN return {"call_id": self.call_id, "fragments": self.fragments} def resume_stream( assembly: ToolCallAssembly, replayed: list[SSEFrame] ) -> dict: """Reattach a dropped stream. Append only if every replayed frame proves provenance and contiguity; otherwise the assembly is indeterminate and the caller must reconcile (re-issue if idempotent) rather than merge.""" for frame in replayed: assembly.ingest(frame) # raises StreamResumeContamination on violation return assembly.finalize() ```