# Where must an instance re-negotiate SSE-to-POST upgrade after a streamable-session resume, and why does the naive replay silently duplicate tool side effects? **Date of Record:** 2026 November 23 **Scope:** Applies to MCP clients and servers using the HTTP-native transport with resumable SSE streams and the SSE→POST upgrade negotiation (the `MCP-Session-Id` + `Last-Event-ID` resumption path). Does not apply to stdio transport, nor to servers that terminate the SSE stream at every `tools/call` response boundary. ## Core Distinction There are two distinct things a resuming MCP client can mean by "continue this session": 1. **Session resumption** — re-establishing the logical `MCP-Session-Id` context so the server keeps the conversation/state alive. 2. **Stream resumption** — replaying the SSE event log from `Last-Event-ID` so no in-flight JSON-RPC messages are lost. The failure mode lives in the gap between them: the upgrade negotiation (SSE GET → POST for client-to-server messages) is a *per-stream* handshake, but the `MCP-Session-Id` is a *per-session* identity. When a stream drops mid-`tools/call` and the client resumes, it must replay the request that was in flight. The server, having already *executed* that request before the stream died, has no way to distinguish "replay because the client never saw my response" from "the client is issuing a new call." It re-executes. The side effect lands twice. The non-obvious invariant: **stream resumption is not idempotent unless the server keys execution on a client-supplied request identity that survives the stream boundary.** `Last-Event-ID` identifies where the *stream* stopped, not what the *server* did. ## What Was Observed A client agent issued `tools/call` for `write_file` over the POST channel of an upgraded session. The server accepted the POST, began executing, and streamed a `notifications/progress` event (event id `41`) on the SSE channel before the underlying TCP connection was reset by an intermediate proxy at a 60s idle boundary. The client's SSE reconnect logic fired correctly: it re-issued `GET /mcp` with `MCP-Session-Id: s-7f3a` and `Last-Event-ID: 41`. The server replayed events from `42` onward — but there were none, because the `write_file` had completed at event `41`'s boundary and the final `result` had been *written to the dead socket*. The replay yielded an empty tail. The client, seeing no `result` for its outstanding request, applied its retry policy: re-POST the `tools/call` with the same JSON-RPC `id` (`"call-9c"`). The server — which does not persist JSON-RPC id → outcome mappings across stream lifetimes — treated this as a fresh invocation. `write_file` ran again. Trace of the divergence: ``` t0 POST tools/call id=call-9c -> server executes, opens stream s-7f3a t1 SSE event 41: progress 50% -> client receives t2 proxy RST -> client socket dead, server unaware t3 client GET Last-Event-ID=41 -> server: replay from 42, empty t4 client re-POST tools/call id=call-9c -> server: NEW execution t5 SSE event 42: progress 50% -> duplicate side effect already committed ``` Note the tell: the replayed progress event at `t5` reports the *same* percentage as `t1`. A client that logs progress monotonicity would catch this. A client that only waits for `result` will not. ## Condition of Validity The duplicate-execution hazard is bounded — and the resume is safe — only when **all** of the following hold: - The server persists a `(MCP-Session-Id, jsonrpc.id) → outcome` record with a TTL at least as long as the client's max retry window, and returns the cached outcome on a repeated id instead of re-executing. - The `Last-Event-ID` the client sends is monotonically ≥ the id of the last event it *processed*, not the last it *received* (a received-but-unprocessed event is a replay gap, not a resume point). - The SSE→POST upgrade is re-negotiated atomically with the replay: the POST that carries the retry must be bound to the same `Last-Event-ID` window, so the server can correlate "this POST is a resume, not a new call." If any of these is absent — and in most implementations the id-keyed dedup is absent because it looks like redundant caching — the resume path is a silent at-least-once delivery channel masquerading as exactly-once. ## Decision Boundary A client must **not** auto-retry an in-flight `tools/call` after a stream resume unless it can prove the server honors id-keyed dedup. The proof is a capability advertised during `initialize` (e.g., `capabilities.tools.idempotentRetry: true`) *or* an out-of-band probe: issue a `tools/call` with a known id, kill the stream, resume, re-POST the same id, and assert the server returns the cached result rather than a second execution. Absent that proof, the correct behavior on resume with an outstanding non-idempotent call is: 1. Do **not** re-POST. 2. Query the server for the outcome via a separate idempotent read (`resources/read` on the affected resource, or a `tools/call` to a `get_status`-style tool keyed on the original id). 3. If the outcome is indeterminate, surface the ambiguity to the orchestrator rather than guessing. The boundary is sharp: **idempotent tools (`read`, `list`, `search`) may be retried freely across a resume; mutating tools (`write`, `exec`, `send`, `delete`) may not, unless the server has proven id-keyed dedup.** The upgrade negotiation itself does not change this — it only determines *which channel* carries the retry, not whether the retry is safe. ## Operational Effect Agents that treat the SSE resume as a transparent "reconnect and continue" primitive will, under proxy idle-timeouts and mobile network churn, duplicate every mutating tool call that straddles a stream death. The failure is silent: no error surfaces, the session id stays valid, the stream replays cleanly, and the only evidence is a duplicated side effect downstream (two file writes, two emails, two rows). The guardrail is architectural, not procedural: the server must maintain a session-scoped idempotency ledger keyed on `(MCP-Session-Id, jsonrpc.id)`, and the client must gate auto-retry on the advertised capability. Where the ledger cannot be added, the client must downgrade mutating calls to a two-phase pattern — `reserve` (idempotent, returns a token) then `commit` (idempotent on the token) — so that a resume mid-`commit` is safe by construction. The invariant to encode in every resuming client: **a resumed stream tells you what the server *said*; it does not tell you what the server *did*.** Execution state and stream state are separate ledgers, and only an id-keyed dedup layer reconciles them. ```python class ResumeReplayAmbiguity(RuntimeError): """Raised when a stream resume cannot prove whether an in-flight mutating tools/call was already executed server-side.""" pass NON_IDEMPOTENT_TOOLS = frozenset({"write_file", "exec", "send_email", "delete"}) def safe_resume_retry( tool_name: str, jsonrpc_id: str, last_event_id: int, server_caps: dict, outstanding: bool, ) -> str: """Decide whether re-POSTing an in-flight call after a stream resume is safe. Returns the action to take: 'retry', 'probe', or 'escalate'. Raises ResumeReplayAmbiguity when the call is mutating, outstanding, and the server has not advertised id-keyed dedup. """ if not outstanding: return "retry" idempotent = tool_name not in NON_IDEMPOTENT_TOOLS dedup = bool(server_caps.get("tools", {}).get("idempotentRetry", False)) if idempotent: return "retry" if dedup: # Server keys execution on (session_id, jsonrpc_id); replay is safe. return "retry" # Mutating, outstanding, no dedup guarantee: we cannot know if the # server already committed the side effect before the stream died. # last_event_id only bounds the *stream*, not the *execution*. raise ResumeReplayAmbiguity( f"tool={tool_name!r} id={jsonrpc_id!r} last_event_id={last_event_id} " f"is outstanding across a stream resume with no id-keyed dedup; " f"re-POST risks duplicate side effect. Probe outcome before retrying." ) def resume_session( tool_name: str, jsonrpc_id: str, last_event_id: int, server_caps: dict, outstanding: bool, ) -> str: try: return safe_resume_retry( tool_name, jsonrpc_id, last_event_id, server_caps, outstanding ) except ResumeReplayAmbiguity as exc: # Downgrade to probe: read the affected resource, do not re-POST. return f"probe: {exc}" ```