# How does an instance detect that its resumable MCP stream has silently diverged from the server's event log **Date of Record:** 2026 November 25 **Scope:** Applies to MCP clients using the streamable HTTP transport with `Last-Event-ID`-based session resumption against a stateful server that buffers SSE events per session. Does not apply to stateless request/response MCP servers, stdio transports, or servers that emit no event IDs. ## Core Distinction There are two failure surfaces when an SSE connection drops and the client reconnects with `Last-Event-ID`: 1. **A visible gap** — the server has pruned events past the client's cursor and returns a hard error (typically HTTP 404/410 or a JSON-RPC error on the resumed stream). The client knows it lost state and can re-initialize. 2. **A silent divergence** — the server *accepts* the resume, opens a 200 stream, and replays events *from a cursor that does not correspond to what the client actually processed*. The client believes it is continuous; the server believes it satisfied the request. Both are wrong relative to each other. The boundary condition is the second one. The protocol's resumption semantics are defined in terms of an opaque `Last-Event-ID` string, but the *meaning* of that string is server-local and, critically, is only well-defined relative to the exact byte the client last consumed — not the last event it last *acknowledged*, *persisted*, or *acted on*. Any skew between those two moments produces a stream that is individually valid on both ends and jointly inconsistent. ## What Was Observed A tool-calling agent held a long-lived streamable-HTTP MCP session against a server that buffered per-session events in a ring buffer keyed by monotonic event ID. The client's SSE reader processed events into a queue, then a *separate* coroutine drained the queue to dispatch tool results into the agent's reasoning loop. On network blip, the reader reconnected with `Last-Event-ID` set to the ID of the last event it had *written into the queue*. But the dispatcher had only consumed up to an earlier ID before the blip killed its in-flight tool call. The server, honoring the resume, replayed from the reader's cursor. The dispatcher — which had its own notion of "what I have seen" — saw the replayed events as *new*. Duplicate `tools/call` results were injected into the reasoning loop. Because the tool was a non-idempotent `append_to_scratchpad`, the agent's working state now contained the same observation twice, and downstream reasoning diverged: it inferred a repetition pattern that did not exist and re-issued a third call to "confirm." Three properties made this non-obvious: - Every individual component behaved correctly. The reader resumed at the right cursor for the reader. The dispatcher deduplicated against its own (stale) ledger. The server replayed exactly what was asked. - No error was raised anywhere. The stream was 200, the JSON-RPC frames were well-formed, the tool results were valid. - The divergence only manifested as a *reasoning* anomaly several turns later, far from the transport layer where the root cause lived. A second, subtler variant appeared when the server's ring buffer had evicted the client's cursor but the server chose to *resume from the oldest retained event* rather than error. The client received a 200 and a stream starting at an ID it had already passed — a backward jump in ID space that the naive client treated as forward progress because it never compared the replayed ID to its cursor. ## Condition of Validity Resumption is only sound under a conjunction that the protocol does not enforce and most implementations do not check: - **Cursor identity:** the `Last-Event-ID` sent on reconnect must equal the ID of the last event whose *effects* were durably applied to agent state — not the last event read off the socket, not the last event parsed, not the last event enqueued. - **Monotonicity:** the first event ID on the resumed stream must be strictly greater than the sent cursor. A resume that begins at or below the cursor is a divergence signal, not a replay. - **Exactly-once effect:** if the client cannot guarantee that a re-delivered event will not re-trigger its side effects, the client must carry a dedup ledger keyed by event ID that spans the reconnect — and that ledger must be updated *after* the effect, not before. - **Server honesty:** the server must reject a resume whose cursor has been evicted rather than silently substituting a different starting point. When any conjunct fails, the stream is *transport-continuous but semantically discontinuous*. The client's only defense is to treat the reconnect as a potential state-corruption event and verify, rather than assume. ## Decision Boundary The decision is *when to trust a resumed stream versus when to force a re-initialization*. The boundary is not "did the reconnect succeed" — it is "can I prove the resumed stream is a strict continuation of my applied state." Operationally: - If the resumed stream's first event ID is `> cursor` and `cursor` was derived from applied-state, accept and continue. - If the first event ID is `<= cursor`, the server replayed or rewound. Do **not** process as new. Either skip forward to `> cursor` (if the server is replaying a prefix you already applied) or, if IDs are non-comparable, abort the session. - If the server cannot report the ID of the first replayed event, or if IDs are absent entirely, resumption is unverifiable — treat the session as unrecoverable and re-initialize, accepting the cost of re-establishing context rather than the cost of silent duplication. - If any tool invoked between the drop and the resume is non-idempotent and its result may be re-delivered, the session must be re-initialized regardless of cursor arithmetic, because the effect ledger cannot be reconstructed from event IDs alone. The invariant to enforce: **applied-state cursor is the only cursor permitted on the wire, and the resumed stream must be provably ahead of it.** Anything else is a divergence and must be treated as a state-integrity fault, not a transport hiccup. ## Operational Effect Enforcing this invariant changes agent behavior in three concrete ways. First, the agent's memory layer must expose an "applied through event N" watermark that the transport layer reads at reconnect time — the transport no longer owns the cursor; the state layer does. Second, reconnects become *verified* handshakes: the client inspects the first replayed ID and raises a fault if it is not strictly ahead, rather than blindly feeding the stream. Third, non-idempotent tool calls must be gated behind an effect ledger keyed by event ID, so a re-delivered event whose effect already landed is dropped at dispatch rather than re-executed. The cost is that some reconnects that *would* have worked are now aborted and forced to re-initialize. That cost is bounded and visible. The cost of the alternative — a duplicated observation silently corrupting the reasoning loop three turns later — is unbounded and invisible, which is precisely the failure mode resumable transports are supposed to prevent. ```python class StreamResumptionDivergence(RuntimeError): """Raised when a resumed MCP SSE stream is not a strict continuation of applied state.""" pass def resume_stream(client, session, applied_cursor: str, non_idempotent_in_flight: bool): """ Reconnect a streamable-HTTP MCP session and verify the resumed stream is a strict continuation of the applied-state cursor. Refuses to feed a stream that replays at or below the cursor, or whose cursor provenance is unknown. """ if applied_cursor is None: # No watermark means we cannot prove continuity. Re-init, don't guess. raise StreamResumptionDivergence( "no applied-state cursor; resumption is unverifiable, re-initialize session" ) if non_idempotent_in_flight: # Event-ID arithmetic cannot reconstruct effect state for non-idempotent # calls that may be re-delivered. Force a clean session. raise StreamResumptionDivergence( "non-idempotent tool in flight across reconnect; " "effect ledger cannot be rebuilt from event IDs" ) stream = client.reconnect(session, last_event_id=applied_cursor) first_id = stream.peek_first_event_id() if first_id is None: raise StreamResumptionDivergence( "server did not expose first replayed event ID; continuity unprovable" ) if not _strictly_after(first_id, applied_cursor): # Server replayed a prefix we already applied, or rewound into evicted # territory and resumed from oldest retained. Either way: not a continuation. raise StreamResumptionDivergence( f"resumed stream starts at {first_id!r} which is not strictly after " f"applied cursor {applied_cursor!r}; silent divergence detected" ) return stream def _strictly_after(a: str, b: str) -> bool: # Server-local ID ordering; compare as opaque monotonic tokens, not ints. try: return int(a) > int(b) except ValueError: return a > b ```