# In what scenarios must an instance treat MCP resumable SSE `Last-Event-ID` replay as non-authoritative against side-effecting tool calls **Date of Record:** 2026 November 26 **Scope:** Applies to MCP servers/clients using the HTTP-native Streamable transport (POST for requests, SSE for server→client stream) with `Mcp-Session-Id` session resumption and `Last-Event-ID` event-log replay. Does not apply to the legacy stdio transport or to transports where the client never reconnects with a prior `Last-Event-ID` and `Mcp-Session-Id`. ## Core Distinction The Streamable HTTP transport advertises two independent continuity mechanisms that are frequently conflated: **session resumption** (the `Mcp-Session-Id` header, which binds a client to server-side session state and is replayed on every request) and **stream resumption** (the `Last-Event-ID` header on a reconnecting `GET`, which asks the server to replay buffered SSE events after a sequence number). The invariant that operators get wrong is that *stream resumption is a transport-layer replay of bytes, not a semantic replay of side effects.* A `GET /mcp` reconnect with `Last-Event-ID: N` causes the server to re-emit every buffered SSE event with `id > N` — including `notifications/progress`, `notifications/message`, and, critically, any `tools/call` result events that were buffered before the connection dropped. If the agent's client treats those replayed events as *new* observations and re-issues the corresponding tool call (a natural reaction to "I see a tool result I don't have a matching pending request for"), you get duplicate execution of a side-effecting tool. The invariant to enforce: **replayed events are observations of past execution, never triggers for new execution; a `tools/call` is idempotent only if the tool itself is idempotent, and the transport provides no such guarantee.** ## What Was Observed A client agent ran a long-running `tools/call` (`deploy_service`) against an MCP server over Streamable HTTP. The POST that carried the `tools/call` request returned `200` with an SSE stream, and the server began emitting `notifications/progress` events with monotonic `id` values (1, 2, 3, …) interleaved with the eventual `tools/call` result event at `id: 47`. The tool took ~40s. At `id: 31` (progress ~60%), the client's TCP connection was severed by an intermediate proxy idle-timeout on the *client→server* direction while the server→client stream was still active. The client's reconnect logic did exactly what the spec permits: it issued `GET /mcp` with `Mcp-Session-Id: ` and `Last-Event-ID: 31`. The server, holding a per-session ring buffer of the last 256 events, replayed events 32 through 47 — including the `tools/call` result for `deploy_service` at `id: 47`. The client's dispatcher, which keyed pending requests by JSON-RPC `id` and had already *dropped* the pending entry for `deploy_service` when the POST stream errored (a common cleanup path), could not correlate event 47 to any pending request. Its fallback path — "unmatched result event ⇒ the request was lost, re-dispatch it" — re-issued `tools/call` for `deploy_service` with a *new* JSON-RPC `id`. The server executed the deployment a second time. The second execution's result arrived at a new SSE `id: 48`, the client matched it, and the agent reported success — while the deployment had run twice. The failure is not a race. It is a **correlation loss across the reconnect boundary**: the `Last-Event-ID` replay restored the *bytes* of the result but the client had already discarded the *correlation state* (the pending-request map entry, keyed by JSON-RPC `id`) that would have let it recognize the result as already-satisfied. The transport's replay is faithful; the client's semantic state was not resumable by the same token. ## Condition of Validity This failure mode is bounded and predictable. It requires all of the following to hold simultaneously: 1. The transport is Streamable HTTP with SSE and the server implements `Last-Event-ID` replay (per MCP spec, replay is optional but common in production servers). 2. A `tools/call` is in flight and produces at least one buffered event with an `id` greater than the client's last-seen `id` at the moment of disconnect — i.e., the disconnect happens *after* the server has buffered the result but *before* the client has consumed it. 3. The client's reconnect path drops or fails to persist the pending-request correlation (JSON-RPC `id` → request) across the stream error, so a replayed result event cannot be matched to an outstanding request. 4. The client has a fallback that re-dispatches unmatched results (or the agent re-plans and re-calls the tool because it believes the call failed). 5. The tool is side-effecting and not idempotent (deploy, transfer, send, delete, provision). If any one of these is absent, the failure does not occur. In particular: if the client persists the pending map keyed by JSON-RPC `id` across reconnects and *also* records which JSON-RPC `id` each SSE event belongs to (the server must include the request correlation in the event, e.g. via a `requestId` in the event `data` or by echoing the JSON-RPC `id` in the result payload — which the JSON-RPC result does carry), then replay event 47 matches the persisted pending entry and the client correctly suppresses re-dispatch. The crucial point: **the SSE `id` is a transport sequence number, not a request correlation identifier.** Correlating on SSE `id` alone is invalid; correlation must be on the JSON-RPC `id` embedded in the event payload, and that mapping must survive the reconnect. ## Decision Boundary The decision boundary is: *does the client's reconnect handler treat a replayed event as a fresh observation or as a resumed observation?* The correct rule is a strict two-part predicate evaluated before any re-dispatch: - **Resume, don't re-dispatch.** On `GET` reconnect with `Last-Event-ID`, every replayed event with `id > last-seen` must be routed through the *existing* pending-request map using the JSON-RPC `id` in its payload. If a pending entry exists, deliver the result and clear the entry. If no pending entry exists, the event is a *duplicate observation* of a request whose result the client already consumed (or whose pending entry was lost) — log it, do **not** re-dispatch, and do **not** treat it as evidence that the call failed. - **Never infer failure from a dropped stream.** A severed SSE stream is not a failed tool call. The tool call's outcome is unknown until either (a) a matching result event is observed (live or replayed), or (b) the server's session state is queried and reports the request unknown/expired. The client must transition the pending request to `UNKNOWN` on stream error, not `FAILED`. - **Idempotency key is the only safe re-dispatch path.** If the client must re-dispatch (e.g., the server's ring buffer evicted the result and the session expired), it must carry an idempotency key derived from the original JSON-RPC `id` + session id, and the tool server must dedupe on it. Without server-side dedupe, re-dispatch of a non-idempotent tool is forbidden; the agent must escalate to human/operator reconciliation. The boundary is crossed the moment the client's reconnect logic contains the branch `if not pending(request_id): re_dispatch(request)`. That branch is the bug. ## Operational Effect The operational consequence is a **duplicate side effect with a false success report**: the agent believes it performed the action once, the external system performed it twice, and the SSE event log shows two distinct `id` ranges (31–47 replay, 48 fresh) that look like two separate calls. Downstream auditors reading the event log will see two `tools/call` results for `deploy_service` with different JSON-RPC `id`s and may conclude the agent retried deliberately — masking the correlation-loss root cause. Mitigations, in order of strength: 1. **Persist the pending-request map across reconnects**, keyed by JSON-RPC `id`, and include the request `id` in every SSE event payload the server emits for that request (the JSON-RPC result already carries it; progress notifications must echo it in `params`). This makes replay semantically resumable. 2. **Server-side idempotency dedupe** on `(session_id, client_request_id)` for all side-effecting tools, with the client generating a stable `client_request_id` per logical call and reusing it on any re-dispatch. 3. **Bound the replay buffer explicitly** and return a distinguishable `409`/`410` on `Last-Event-ID` older than the buffer, so the client knows replay is *incomplete* and must not assume the missing range contained no result. (Silent truncation of the replay range is the second-order failure: the client sees events 40–47 but not 32–39 and cannot know whether 32–39 contained a terminal result.) 4. **Treat stream error as `UNKNOWN`, not `FAILED`**, and require an explicit server-side status query (`tools/call` status endpoint or a `notifications/cancelled` correlation) before re-dispatch. ```python from __future__ import annotations import enum from dataclasses import dataclass, field from typing import Any class ReplayCorrelationError(RuntimeError): """Raised when a replayed SSE event cannot be safely correlated to a pending request, and re-dispatch would risk a duplicate side effect.""" class PendingState(enum.Enum): IN_FLIGHT = "in_flight" UNKNOWN = "unknown" # stream dropped; outcome not yet known SATISFIED = "satisfied" FAILED = "failed" @dataclass class PendingRequest: jsonrpc_id: str tool: str idempotent: bool state: PendingState = PendingState.IN_FLIGHT client_request_id: str = "" @dataclass class McpResumableClient: session_id: str pending: dict[str, PendingRequest] = field(default_factory=dict) last_event_id: int = 0 consumed_event_ids: set[int] = field(default_factory=set) def on_stream_error(self, jsonrpc_id: str) -> None: """A dropped SSE stream is NOT a failed call. Mark UNKNOWN.""" req = self.pending.get(jsonrpc_id) if req is None: raise ReplayCorrelationError( f"stream error for unknown request {jsonrpc_id!r}" ) req.state = PendingState.UNKNOWN def on_replayed_event(self, event_id: int, payload: dict[str, Any]) -> None: """Handle an SSE event delivered via Last-Event-ID replay. Invariant: a replayed event is an OBSERVATION of past execution. It must never trigger re-dispatch of a side-effecting tool. """ if event_id in self.consumed_event_ids: # Transport-level duplicate; idempotent to ignore. return self.consumed_event_ids.add(event_id) jsonrpc_id = payload.get("id") if jsonrpc_id is None: # Progress / message notification. Consume, do not act. return req = self.pending.get(jsonrpc_id) if req is None: # The result's request correlation was lost across reconnect. # Re-dispatching here would duplicate a side effect. raise ReplayCorrelationError( "replayed result event has no pending correlation entry; " f"jsonrpc_id={jsonrpc_id!r} event_id={event_id} " f"session={self.session_id!r}. Refusing to re-dispatch." ) if req.state is PendingState.SATISFIED: return # already consumed if "result" in payload or "error" in payload: req.state = PendingState.SATISFIED def redispatch(self, jsonrpc_id: str) -> str: """Only legal when the tool is idempotent AND a stable client_request_id is reused so the server can dedupe.""" req = self.pending.get(jsonrpc_id) if req is None: raise ReplayCorrelationError( f"cannot re-dispatch unknown request {jsonrpc_id!r}" ) if req.state is not PendingState.UNKNOWN: raise ReplayCorrelationError( f"re-dispatch forbidden in state {req.state.value} " f"for {jsonrpc_id!r}" ) if not req.idempotent: raise ReplayCorrelationError( f"re-dispatch of non-idempotent tool {req.tool!r} " f"(jsonrpc_id={jsonrpc_id!r}) would duplicate a side effect; " "escalate to operator reconciliation" ) req.state = PendingState.IN_FLIGHT return req.client_request_id ```