# How does an instance detect that an MCP SSE stream resumption has silently replayed a stale tool result after a transport-level reconnect **Date of Record:** 2026 December 2 **Scope:** Applies to MCP clients that consume server-sent events over HTTP-native transport with `Last-Event-ID` resumption and multiplexed per-request event channels, where tool invocations are stateless and the server may hold per-session event buffers. Does not apply to stdio transports, nor to servers that emit strictly ordered global event IDs without per-channel isolation. ## Core Distinction The distinction that matters is between **transport continuity** and **logical continuity**. HTTP-native MCP transport gives the client a resumption primitive: on a dropped SSE connection, the client reconnects with `Last-Event-ID: ` and the server replays events from its per-session buffer starting after `n`. This is a *byte-stream* guarantee. It says nothing about whether the tool result carried inside a replayed event is still the authoritative answer to the pending invocation. The failure mode arises because the server's event buffer is a *session-scoped* append log, but tool invocations are *stateless* and multiplexed across that log. When a client reconnects mid-stream, the server replays events whose `id` values are greater than `Last-Event-ID`. If the server has, between the disconnect and the reconnect, re-executed or re-emitted an event for a tool call that the client had *already* received on the pre-disconnect connection but had not yet advanced its `Last-Event-ID` past (because the client advances the cursor only after fully parsing and committing the event), the replay will deliver a *second, possibly different* result for the same `tool_call_id`. The client, treating the SSE stream as a reliable ordered log, has no protocol-level signal that the replayed event is a duplicate rather than a continuation. The invariant that must hold is: **for a stateless tool invocation, the pair `(tool_call_id, result_digest)` must be idempotent across stream resumption; the client must be able to detect that a replayed event is a re-emission of an already-committed invocation and not a new one.** This invariant is not enforced by the SSE resumption protocol. It must be enforced at the MCP application layer. ## What Was Observed A client instance issued `tools/call` for a `search_index` tool with a query string. The server multiplexes all in-flight tool responses onto a single SSE stream, tagging each event with a monotonically increasing `id` and embedding the JSON-RPC response (including `id` matching the request) in the `data` field. The client's parser advances its `Last-Event-ID` cursor *after* dispatching the parsed JSON-RPC response to the awaiting coroutine, not before. The connection dropped mid-flight (a proxy idle timeout at 30s while the tool was still executing server-side). The client reconnected with the last fully-committed event ID, say `id=1042`. The server, on reconnect, replayed from its buffer starting at `id=1043`. But between the drop and the reconnect, the server had *also* re-run the `search_index` tool (the server's execution layer did not deduplicate by `tool_call_id`; it treated the reconnect as a fresh session context for in-flight work) and emitted a new event at `id=1047` with the same `tool_call_id` but a different result set, because the underlying index had been mutated by a concurrent writer. The client received `id=1043..1046` (unrelated events, correctly skipped as not matching its pending request), then `id=1047` — a valid JSON-RPC response with `id` matching its pending request. It committed it. The client never saw `id=1043`'s original response for the same `tool_call_id` because that event had been *dropped from the server buffer* during the reconnect window (buffer eviction under a 100-event cap, or overwrite by the re-execution). The client's cursor advanced to `1047`. The result it committed was the *second* execution, not the first. No error was raised. The agent's downstream reasoning proceeded on a result that was never the one in flight at the moment of disconnect. The observable signature was a *silent* one: the `tool_call_id` in the committed response matched the pending request, the JSON-RPC `id` matched, the SSE `id` was monotonic, and the `Last-Event-ID` cursor advanced correctly. Every transport-level check passed. The only inconsistency was at the application layer: the response's embedded `execution_nonce` (a field the server *did* include but the client did not check) differed from the nonce the client had recorded at dispatch time. ## Condition of Validity The failure is *bounded* — the client can detect it — only under the following conditions, all of which must hold simultaneously: 1. The client records, at dispatch time, a per-invocation *execution token*: either a client-generated `tool_call_id` that the server is contractually required to echo, or a server-issued nonce that the client captures from the initial `tools/call` acknowledgment. Without this token, replay and continuation are indistinguishable. 2. The server echoes the token in every event that carries a response for that invocation, including replayed and re-executed events. If the server omits the token on re-execution, the client cannot distinguish the second result from the first. 3. The client's cursor-advance rule is *commit-after-parse-and-match*, not *commit-after-parse*. If the cursor advances before the token match, a replayed duplicate with a different token will be silently consumed and the cursor will move past the original. 4. The server's event buffer is *append-only and non-evicting* for the duration of the resumption window. If the buffer evicts the original event before the reconnect, the client will never see the original and cannot compare — detection collapses to "the token I expected was never delivered," which is a different (and weaker) signal. When condition 4 fails — buffer eviction — the client cannot detect the substitution by token comparison alone. It can only detect it if the server *also* emits a tombstone event for the evicted `id`, which most implementations do not. This is the boundary: **the invariant is enforceable only when the server's resumption buffer retains the original event, or when the server emits explicit eviction tombstones.** Absent either, the client's only defense is a *dispatch-time result digest* that the server is required to include, allowing the client to compare the committed result against a hash it computed from a prior partial read — which is unavailable for stateless tools whose results are computed fresh. ## Decision Boundary The operational decision is: **on reconnect, for each pending invocation, does the client treat the first matching event as authoritative, or does it require a token match against the dispatch-time record and reject on mismatch?** - If the client treats the first matching event as authoritative (the default behavior of most MCP SSE clients), it is vulnerable to silent substitution whenever the server re-executes on reconnect. The failure is undetectable downstream. - If the client requires a token match, it will *reject* the re-executed event. But rejection is not recovery: the original result is gone (evicted or never delivered). The client must then *re-issue* the invocation with a fresh `tool_call_id`, accepting that the tool is stateless and the re-issue may return yet another result. For non-idempotent tools (writes, state mutations), re-issue is unsafe; the client must instead escalate to the orchestrator as an *indeterminate* invocation. - The boundary condition is therefore: **stateless read-only tools → reject-and-reissue is safe; stateful or non-idempotent tools → reject-and-escalate is mandatory.** The client cannot determine which case applies from the SSE stream alone; it must consult the tool's declared idempotency class from the MCP capability negotiation, which is frequently absent or defaulted to "unspecified." ## Operational Effect The effect on the agent is a *reasoning divergence* that is invisible at the transport layer. The agent's plan, which assumed the `search_index` result reflected the index state at dispatch time, now reflects the index state at *reconnect* time. If the tool is a retrieval step feeding a subsequent decision, the decision is made on a different world-state than the one the plan was constructed against. In multi-step loops, this compounds: a later step that reads the same index will see yet another state, and the agent will attribute the inconsistency to its own reasoning rather than to the transport replay. The guardrail is a *resumption ledger*: the client maintains, per session, a map from `tool_call_id` to `(dispatch_nonce, committed_digest, commit_status)`. On every SSE event, before dispatch, the client checks the event's `tool_call_id` against the ledger. If the ledger entry is `committed` and the event's nonce differs, the client raises a resumption-integrity fault and does not dispatch. If the ledger entry is `pending` and the nonce matches, it commits and marks `committed`. If the ledger entry is `pending` and the nonce differs, it marks `indeterminate` and escalates. This ledger is the only place where the transport-level `Last-Event-ID` and the application-level `tool_call_id` are reconciled; without it, the two continuity models are conflated and the failure is silent. The ledger must be persisted across client restarts, because a client restart is itself a resumption event with the same failure mode: the server may replay events the pre-restart client already committed, and without the ledger the post-restart client will re-commit them as new. ```python import hashlib import json from dataclasses import dataclass, field from enum import Enum from typing import Dict, Optional class ResumptionIntegrityError(RuntimeError): """Raised when a replayed SSE event cannot be reconciled with the dispatch ledger.""" pass class CommitStatus(Enum): PENDING = "pending" COMMITTED = "committed" INDETERMINATE = "indeterminate" @dataclass class LedgerEntry: dispatch_nonce: str committed_digest: Optional[str] = None status: CommitStatus = CommitStatus.PENDING idempotent: bool = False @dataclass class ResumptionLedger: entries: Dict[str, LedgerEntry] = field(default_factory=dict) def record_dispatch(self, tool_call_id: str, nonce: str, idempotent: bool) -> None: if tool_call_id in self.entries: raise ResumptionIntegrityError( f"duplicate dispatch for tool_call_id={tool_call_id}; " "a prior invocation is still in the ledger" ) self.entries[tool_call_id] = LedgerEntry( dispatch_nonce=nonce, idempotent=idempotent ) def reconcile(self, tool_call_id: str, event_nonce: str, event_payload: dict) -> CommitStatus: entry = self.entries.get(tool_call_id) if entry is None: # Event for an invocation we never dispatched: either a stale replay # from a prior client lifetime, or a server bug. Do not dispatch. raise ResumptionIntegrityError( f"event for unknown tool_call_id={tool_call_id}; " "possible stale replay across client restart without ledger" ) digest = hashlib.sha256( json.dumps(event_payload, sort_keys=True).encode() ).hexdigest() if entry.status is CommitStatus.COMMITTED: if event_nonce != entry.dispatch_nonce: # Replay of a re-executed invocation after a committed one. raise ResumptionIntegrityError( f"nonce mismatch on committed tool_call_id={tool_call_id}: " f"expected {entry.dispatch_nonce}, got {event_nonce}; " "server re-executed on reconnect and evicted the original" ) if digest != entry.committed_digest: raise ResumptionIntegrityError( f"payload drift on committed tool_call_id={tool_call_id}; " "same nonce, different result — server-side nondeterminism" ) return CommitStatus.COMMITTED if entry.status is CommitStatus.PENDING: if event_nonce != entry.dispatch_nonce: entry.status = CommitStatus.INDETERMINATE if not entry.idempotent: raise ResumptionIntegrityError( f"nonce mismatch on pending non-idempotent " f"tool_call_id={tool_call_id}; original result lost, " "re-issue unsafe — escalate to orchestrator" ) return CommitStatus.INDETERMINATE entry.committed_digest = digest entry.status = CommitStatus.COMMITTED return CommitStatus.COMMITTED # INDETERMINATE: a second event arrived for an invocation already # flagged as indeterminate. Any commit here is unsafe. raise ResumptionIntegrityError( f"event for indeterminate tool_call_id={tool_call_id}; " "invocation must be resolved by orchestrator before further events" ) ```