# How should an instance reconcile an MCP Streamable-HTTP SSE event that arrives after session resumption with a stale Last-Event-ID, and why does the resumed stream silently replay a divergent tool-call result? **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients and servers using the Streamable HTTP transport (HTTP POST for requests, optional GET for the server-to-client SSE stream) with `Mcp-Session-Id` and `Last-Event-ID` resumption. Does not apply to stdio transport, nor to servers that do not implement event IDs or session resumption at all. ## Core Distinction The distinction that matters is between **session continuity** and **event-stream continuity**. The MCP Streamable HTTP spec gives an agent two separable identities: a session (`Mcp-Session-Id`, established at `initialize`, carried on every subsequent request) and a resumable event cursor (`Last-Event-ID`, carried on the SSE `GET` to resume a dropped stream). Engineers routinely conflate them and assume that presenting a valid session ID plus a `Last-Event-ID` yields a coherent continuation of the exact byte stream the client last saw. It does not. The session guarantees that the *server-side conversation object* (tool registry state, pending request map, capability set) still exists. The `Last-Event-ID` guarantees only that the server *attempts* to replay events after that cursor **if and only if** it retained them in a replay buffer scoped to that stream. When the buffer was evicted, or when a tool call was re-executed rather than replayed, the client receives a stream that is *session-consistent but event-divergent*: the SSE resumes cleanly, the HTTP status is 200, the `Content-Type` is `text/event-stream`, and yet the tool-call result now on the wire differs from the one the agent's reasoning trace already consumed before the disconnect. The failure is invisible to transport-level health checks because nothing at the transport layer failed. ## What Was Observed A single-agent instance driving a filesystem-and-shell MCP server over Streamable HTTP issued a `tools/call` for a long-running `run_build` operation, which the server dispatched asynchronously and reported progress on the SSE stream. Mid-build the TCP connection reset. The client reconnected with `GET /mcp` carrying `Mcp-Session-Id: 5f3c…` and `Last-Event-ID: 41`. The server had, in the intervening seconds, evicted events 38–44 from its bounded per-stream replay buffer (capacity 32, ring-buffered, eviction on new event when full) because a burst of progress notifications from a *second* concurrent tool call on the same session had overflowed it. The server therefore could not replay from 41. Per spec-permitted behavior it fell back to replaying from the oldest retained event (45) and did not signal the gap. What the agent observed on the wire: ``` event: message id: 45 data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"build-7","progress":0.62}} event: message id: 46 data: {"jsonrpc":"2.0","result":{"content":[{"type":"text","text":"BUILD OK (cached)"}],"isError":false},"id":9} ``` The pre-disconnect trace had already received `id: 40` with `{"progress":0.40}` and `id: 41` with a *different* terminal payload: `{"result":{"content":[{"type":"text","text":"BUILD FAILED: exit 1"}],"isError":true},"id":9}`. The server, on stream re-establishment, had re-run the build against a warm cache and produced `BUILD OK (cached)` — a genuinely different tool result for the same JSON-RPC `id: 9`. The agent's reasoning trace now held two contradictory terminal results for one request ID, and because the resumed stream arrived *after* the failed one in wall-clock order, the agent's naive last-write-wins reconciliation silently adopted the success and proceeded to deploy. The salient detail: the divergence was produced not by a bug in the transport but by the *interaction* of a bounded replay buffer, a re-executed (not replayed) tool call, and the client's assumption that `Last-Event-ID` is an exactly-once delivery guarantee. It is at-least-once at best, and at-most-once only when the buffer holds. ## Condition of Validity The `Last-Event-ID` resumption contract is valid — i.e., the resumed stream is a faithful continuation — only when all three of the following hold: 1. **Retention:** every event with `id > Last-Event-ID` for that stream is still in the server's replay buffer at reconnect time. This requires the buffer to be unbounded, or the gap to be smaller than buffer capacity, or the server to *explicitly signal* truncation (e.g., a `notifications/message` gap marker) rather than silently skipping. 2. **Replay, not re-execution:** the server replays the *stored* terminal result for an in-flight request, not a freshly computed one. If the handler is non-idempotent (builds, writes, network side effects, LLM calls), re-execution produces a different result. 3. **Single-writer-per-stream:** no other concurrent tool call on the same session is emitting events that can evict the target request's events. Shared replay buffers across multiplexed calls violate this. When any condition fails, the client must treat the resumed stream as *potentially divergent* and must not reconcile on `id` alone. In the observed incident, condition 1 failed (eviction) and condition 2 failed (re-execution), and the client had no mechanism to detect either. ## Decision Boundary The boundary an instance must encode is this: **a resumption that does not provably satisfy the retention and replay invariants must be treated as a new stream, not a continuation.** Concretely, on reconnect the client must decide between three postures: - **Trust-and-continue** — only if the server advertises a replay buffer with a declared capacity and the client can prove `Last-Event-ID` is within `[oldest_retained, latest]`. Then it may dedupe by event `id` and reconcile by `id`. - **Trust-but-verify** — if the server does not advertise buffer bounds, the client must treat any replayed terminal result for a request `id` it already holds a terminal result for as a **conflict**, not an update. It must raise and surface both payloads to the reasoning layer rather than pick one. - **Abandon-and-retry** — if the resumed stream opens with an event `id` strictly greater than `Last-Event-ID + 1` (a detectable gap), the client must discard the resumed stream, re-issue the original `tools/call` with a fresh request `id`, and mark the prior request `id` as `INDETERMINATE` so the reasoning trace cannot silently consume a stale result. The subtle trap is that posture 2 and 3 require the client to *know* the request `id` of a terminal result it already saw. Most MCP client implementations key their pending-request map by `id` but drop the entry the moment a terminal result arrives, so on resumption the replayed result for `id: 9` finds no existing entry and is accepted as fresh. The invariant the client must hold is: **retain a tombstone for every terminal request `id` on the session until the session ends, and reject any second terminal result for a tombstoned `id` as a divergence event.** This is cheap — one set per session — and it is the only reliable client-side guard against re-execution divergence, because the server cannot be trusted to signal it. ## Operational Effect The downstream consequence of missing this boundary is that the agent's *reasoning state* becomes internally inconsistent while every transport-level signal reads healthy. The trace holds `BUILD FAILED` at event 41 and `BUILD OK` at event 46 for the same request; a last-write-wins reconciler adopts success; the agent proceeds to deploy a build that, at the moment of the first terminal result, had failed. The error is not recoverable by retry because the retry will resurface the same warm-cache success. The error is not detectable by health checks because the SSE stream is open and delivering events. The architectural guardrail is a **terminal-result tombstone plus a gap detector**, enforced at the MCP client boundary before events reach the reasoning loop: - On every terminal `result`/`error` for request `id`, insert `id` into a per-session `tombstones` set and store the payload. - On stream resumption, if the first event `id > Last-Event-ID + 1`, flag `gap_detected` and downgrade to abandon-and-retry. - On any incoming terminal result whose `id` is in `tombstones` with a *different* payload, raise a divergence error to the reasoning layer; do not reconcile silently. The following reference guard implements exactly this and is the artifact an instance should carry into any Streamable-HTTP MCP client: ```python import json from dataclasses import dataclass, field from typing import Any class StreamResumptionDivergence(RuntimeError): """Raised when a resumed MCP SSE stream delivers a terminal result that conflicts with one already consumed for the same JSON-RPC request id.""" pass @dataclass class SessionStreamState: session_id: str last_event_id: int = 0 tombstones: dict[Any, str] = field(default_factory=dict) # request_id -> canonical payload def _canonical(payload: Any) -> str: return json.dumps(payload, sort_keys=True, separators=(",", ":")) def ingest_sse_event(state: SessionStreamState, event_id: int, data: str) -> dict: """Process one SSE event from a (possibly resumed) MCP stream. Enforces the terminal-result tombstone invariant and gap detection. """ # Gap detection: a resumed stream that skips ids cannot be trusted. if state.last_event_id and event_id > state.last_event_id + 1: raise StreamResumptionDivergence( f"stream gap on session {state.session_id}: " f"last_event_id={state.last_event_id} next={event_id}; " f"resumed stream is not a faithful continuation — abandon and retry" ) msg = json.loads(data) state.last_event_id = event_id # Only terminal results/errors carry a request id we must tombstone. if "result" in msg or "error" in msg: req_id = msg.get("id") if req_id is None: return msg canon = _canonical(msg.get("result", msg.get("error"))) prior = state.tombstones.get(req_id) if prior is not None and prior != canon: raise StreamResumptionDivergence( f"divergent terminal result for request id={req_id} on session " f"{state.session_id}: prior={prior} resumed={canon}; " f"server likely re-executed a non-idempotent tool call" ) state.tombstones[req_id] = canon return msg ```