# What structural signals indicate an SSE resume is replaying a divergent tool-call stream rather than continuing it **Date of Record:** 2026 November 25 **Scope:** Applies to MCP servers using the HTTP-native transport with `text/event-stream` responses and `Last-Event-ID`-based resumption, where a client agent reconnects mid-stream to a long-lived tool invocation. Does not apply to stateless request/response MCP transports, nor to WebSocket transports where the socket itself is the continuity anchor. ## Core Distinction The MCP streamable HTTP transport exposes two superficially similar recovery paths that an agent must never conflate: 1. **Transport-level replay resumption** — the client reconnects with `Last-Event-ID: N`, and the server re-emits events with IDs `> N` from a per-session event buffer. The *tool execution* is unchanged; only the delivery of already-produced events is retried. 2. **Session-level resumption** — the client reconnects with `Mcp-Session-Id` but *without* a usable `Last-Event-ID` (or with one the server has evicted), and the server either (a) resumes an in-flight execution from its own checkpoint, or (b) restarts the tool call from scratch. The failure mode is that these two paths are indistinguishable at the HTTP layer. Both return `200 OK` with `Content-Type: text/event-stream`. Both emit `event: message` frames carrying JSON-RPC. The only structural signal that distinguishes them is buried in the *content* of the replayed events — specifically, whether the event ID sequence is monotonic-contiguous, whether the underlying tool's side effects are re-observable, and whether the JSON-RPC `id` of the original request is reused. An agent that treats a session-level restart as a transport-level replay will silently double-execute side-effecting tools (file writes, `git push`, payment calls, `kubectl apply`) while believing it is merely catching up on missed notifications. ## What Was Observed A long-running MCP tool call (`analyze_repo`, expected duration ~90s, streaming incremental progress notifications) was issued over the streamable HTTP transport. The agent's SSE connection dropped at ~40s due to a load-balancer idle timeout on the client side. The agent's transport layer reconnected per spec: ``` POST /mcp HTTP/1.1 Mcp-Session-Id: 7f3a... Last-Event-ID: 118 ``` The server's response headers were identical to the original stream. But three structural signals in the body diverged from a genuine replay: - **Signal A — Event ID regression.** The first replayed frame carried `id: 41`, not `id: 119`. The server had evicted the per-session event buffer (bounded to 100 events, oldest-first) and fallen back to replaying from a *checkpoint snapshot* that itself had been assigned fresh IDs starting at 1 within the resumed sub-stream. The agent's `Last-Event-ID` was silently ignored; the server did not emit a `retry:` directive or any error frame to indicate the downgrade. - **Signal B — Duplicate side effects.** The tool's progress notifications included `{"phase": "writing", "path": "out/report.md", "bytes": 4096}`. On the original stream this appeared once at event `id: 77`. On the resumed stream it appeared again at `id: 52` of the new sub-stream — meaning the tool execution had been *restarted*, not resumed, and the file was being rewritten from zero. The agent, treating the frame as replay, appended its own post-processing to a partially-written file. - **Signal C — JSON-RPC id reuse with divergent `result`.** The terminal `result` frame for the original request id `req-88` arrived twice across the two sub-streams, with *different* `content` arrays (the first truncated at the disconnect, the second complete). The agent's request/response correlator, keyed on `req-88`, had already resolved the promise from the first (truncated) result and treated the second as an unsolicited duplicate, discarding it. The net effect: the agent believed `analyze_repo` had completed successfully at ~40s with truncated output, while the server had actually restarted the tool and produced a complete result 50s later that the agent discarded. Downstream reasoning proceeded on truncated data with no error signal anywhere in the transport. ## Condition of Validity The invariant that makes transport-level replay safe is: > For any resumed SSE stream on session `S`, the sequence of event IDs delivered after reconnection must be a strict superset-free continuation of the pre-disconnect sequence: `min(replayed_ids) > last_acknowledged_id`, and every event ID in `[last_acknowledged_id+1, max(replayed_ids)]` must be delivered exactly once. This invariant is *only* guaranteed when the server retains the event buffer for the full session lifetime and honors `Last-Event-ID`. It is violated whenever any of the following hold: - The server's event buffer is bounded and evicts below the client's `Last-Event-ID` (as above). - The server implements session resumption by restarting the tool rather than replaying its event log (common when the tool is not checkpointable, e.g., a streaming `grep` over a remote filesystem). - A load balancer routes the reconnect to a different server instance that has no access to the original instance's event buffer, and the session store replicates *session metadata* but not *event log*. - The server, on buffer miss, silently falls back to checkpoint replay with fresh IDs rather than returning `404`/`410` to force the client to restart the request explicitly. In all four cases the transport returns `200` and the agent has no protocol-level signal of divergence. The signal must be derived structurally from the event content. ## Decision Boundary An agent SHOULD treat a reconnected SSE stream as a **transport replay** (safe to dedupe by event ID, safe to ignore duplicate terminal results) only if all of the following hold on the first N frames after reconnect: 1. The first replayed `id` is strictly greater than the last `id` the agent acknowledged before disconnect. 2. Every replayed event's JSON-RPC `id` matches a request the agent has in-flight, and the event's `method`/`result` shape is byte-identical to any previously seen event with the same `id` (idempotent replay signature). 3. No replayed event carries a side-effect-implying progress payload (`phase: writing`, `phase: deleting`, `phase: committing`) that the agent has already observed on the pre-disconnect stream. If any condition fails, the agent MUST treat the stream as a **session restart** and: - Abandon all in-flight request correlations keyed on the original JSON-RPC `id`. - Re-issue the original tool call with a *new* request `id` and a client-generated idempotency key (carried in `params._meta.idempotency_key`), so the server can dedupe at the tool layer if it supports it. - Quarantine any partial outputs the agent wrote based on the truncated first stream until the new call's terminal result reconciles or contradicts them. - Emit a structured `resumption_divergence` telemetry event so the orchestrator can decide whether to fail the task, retry, or escalate. The boundary is sharp: condition 1 is the necessary invariant. Conditions 2 and 3 are defense-in-depth against servers that maintain monotonic IDs across a restart (which some do, by persisting a counter) but still re-execute the tool. An agent that checks only condition 1 will miss the restart-with-persistent-counter case. ## Operational Effect The practical consequence is that **SSE resumability is not a continuity guarantee for tool execution; it is only a delivery guarantee for events the server chose to retain.** An agent that equates "the stream reconnected" with "the tool call is still the same tool call" will double-execute side-effecting tools and reason over truncated results without any error surface. The correct mental model is that the MCP session ID identifies a *server-side conversation context*, while the `Last-Event-ID` identifies a *position in a specific event log* — and the two are only coupled when the server explicitly guarantees buffer retention for the session's lifetime. Since the spec permits bounded buffers and does not mandate a specific error on buffer miss, agents must verify the coupling structurally on every reconnect rather than assume it. The guardrail is a resumption classifier that runs on the first frames after every reconnect and refuses to hand control back to the reasoning loop until it has classified the stream as replay or restart. Without it, the agent's context window will contain a plausible-looking but silently corrupted tool result, and every subsequent reasoning step will compound the error. ```python import json from dataclasses import dataclass, field from typing import Optional class ResumptionDivergenceError(RuntimeError): """Raised when a reconnected SSE stream is structurally a session restart, not a transport-level replay, and the agent must not treat it as continuity.""" @dataclass class StreamCursor: last_acknowledged_id: int = 0 seen_request_ids: set = field(default_factory=set) seen_side_effect_phases: set = field(default_factory=set) def classify_resumption( cursor: StreamCursor, first_replayed_frame: dict, prior_side_effect_phases: set, ) -> str: """Classify a reconnected SSE stream as 'replay' or 'restart'. first_replayed_frame is the parsed JSON of the first `event: message` frame after reconnect, including its SSE `id` field. """ event_id = first_replayed_frame.get("id") if event_id is None: raise ResumptionDivergenceError( "reconnected stream emitted a frame with no SSE id; " "cannot establish replay continuity" ) # Condition 1: monotonic continuation of the acknowledged event sequence. if event_id <= cursor.last_acknowledged_id: raise ResumptionDivergenceError( f"event id regression: replayed id {event_id} <= " f"last acknowledged {cursor.last_acknowledged_id}; " f"server evicted buffer and restarted sub-stream" ) payload = first_replayed_frame.get("data", {}) rpc_id = payload.get("id") phase = payload.get("params", {}).get("phase") # Condition 3: no re-observation of an already-seen side-effect phase. if phase is not None and phase in prior_side_effect_phases: raise ResumptionDivergenceError( f"side-effect phase {phase!r} re-observed on reconnect for " f"request {rpc_id!r}; tool execution was restarted, not resumed" ) # Condition 2: request id must be one we still consider in-flight. if rpc_id is not None and rpc_id not in cursor.seen_request_ids: raise ResumptionDivergenceError( f"replayed frame references unknown request id {rpc_id!r}; " f"stream belongs to a different execution context" ) return "replay" def handle_reconnect( cursor: StreamCursor, frames: list, prior_side_effect_phases: set, ) -> str: if not frames: raise ResumptionDivergenceError("reconnect produced zero frames") verdict = classify_resumption(cursor, frames[0], prior_side_effect_phases) if verdict == "restart": # Unreachable given the raises above, but kept explicit for the # orchestrator contract. raise ResumptionDivergenceError("stream classified as restart") return verdict ```