# Under what conditions must an instance treat MCP streamable-HTTP session resumption as invalid after an SSE-to-POST upgrade **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients and servers using the streamable-HTTP transport where a session begins on a GET SSE stream and the client later issues a POST that triggers an upgrade negotiation (server-issued `Mcp-Session-Id`, `Last-Event-ID` resumption, or `202 Accepted` mutation handoff). Does not apply to pure stdio transports or to stateless request/response MCP deployments with no session identity. ## Core Distinction A resumable MCP session is not identified by the SSE connection. It is identified by the tuple `(Mcp-Session-Id, Last-Event-ID, server-side event log position)`. The common failure is treating the SSE stream as the session: when the client upgrades from GET-SSE to POST (because the server returned `202 Accepted` with a session header, or because the client needs to send a JSON-RPC request over a new channel), the client assumes the `Last-Event-ID` it last saw on the GET stream is a valid resume token for the POST channel. It is not. The upgrade introduces a new transport leg whose event sequence is disjoint from the GET stream's sequence unless the server explicitly declares a unified log. Resuming across the leg boundary silently drops or replays events, and the agent's downstream reasoning proceeds on a corrupted task state. The invariant: **a `Last-Event-ID` is only valid within the transport leg and event-log namespace that issued it.** An upgrade that changes the HTTP method, the `Accept` header, or the session-establishment direction invalidates the resume token unless the server returns an explicit continuity assertion. ## What Was Observed An agent driving a long tool-use chain (12 sequential tool calls with streaming partial results) ran over streamable-HTTP. The GET SSE stream delivered events `e1..e40` with monotonic `id`. At `e40` the server returned a `202 Accepted` on a POST that carried a `Mcp-Session-Id: s-7f3a` header and instructed the client to continue on the POST channel. The client, following a naive resumption policy, reconnected with `Last-Event-ID: 40` on the POST leg. The server's POST-leg event log was a separate append-only log starting at `id=1` scoped to the upgrade. It interpreted `Last-Event-ID: 40` as "resume after event 40 in this leg," found no such event, and — depending on implementation — either (a) returned `200` with an empty event stream (silent gap), or (b) returned `400 Invalid Last-Event-ID` and the client retried the entire POST, re-executing the tool call. Observable symptoms: - Tool call `#7` executed twice (idempotency violation on a non-idempotent write tool). - The agent's context contained a partial result for `#7` from the GET leg and a fresh result from the POST leg, with different timestamps and one duplicated side effect. - Reasoning diverged: the model treated the duplicate as two distinct observations and emitted a corrective tool call that referenced a resource ID only present in the pre-upgrade leg. - No error surfaced to the orchestrator; the session appeared healthy because both legs returned `200`. The trigger condition is precise: it requires (1) an SSE-to-POST upgrade mid-session, (2) a client that carries `Last-Event-ID` across the leg boundary, and (3) a server whose event log is not globally monotonic across legs. Remove any one and the failure does not occur — which is why it is rarely caught by smoke tests that never upgrade mid-stream. ## Condition of Validity The resume token is valid across an upgrade only if the server satisfies all of: 1. **Namespace continuity:** the server declares that event IDs are drawn from a single monotonic sequence spanning GET and POST legs. This must be asserted in the upgrade response (e.g., a `Mcp-Event-Log: unified` header or equivalent capability field in the `initialize` result). 2. **Gap-free handoff:** the upgrade response carries the highest event ID durably committed before the leg switch, and the client's `Last-Event-ID` is `<=` that value. A client sending an ID the server has not committed is a protocol error, not a resume. 3. **Idempotency keying:** any POST that re-executes a tool call after a failed resume carries an idempotency key derived from `(session-id, tool-call-id, leg-id)`, so a replay is deduplicated server-side. 4. **Explicit invalidation:** if the server cannot guarantee 1–3, it must reject the resume with a distinct status (not `200`, not a generic `400`) that the client can branch on to restart the session rather than replay the POST. If any condition is unmet, the correct client behavior is to abandon the resume token, re-`initialize` the session (or open a fresh session), and re-derive task state from durable checkpoints — never to replay the POST with the stale token. ## Decision Boundary The client must branch at the upgrade response before issuing any POST: - **If** the upgrade response declares unified event-log continuity **and** echoes a committed watermark `>= Last-Event-ID` **and** the POST carries an idempotency key → resume is permitted; send `Last-Event-ID` on the POST leg. - **If** continuity is undeclared or the watermark is absent → treat the token as leg-local only; do **not** send `Last-Event-ID` on the POST leg; start the POST leg at its own head and reconcile against the last durable checkpoint. - **If** the server rejects the resume with a resume-specific error → do not retry the POST; invalidate the session, re-initialize, and resume from checkpoint. - **If** the server returns `200` with an empty or non-monotonic event stream → treat as a silent gap, halt the tool chain, and escalate to the orchestrator rather than letting the model reason over incomplete state. The boundary is sharp: the presence or absence of a committed watermark in the upgrade response is the single discriminator. Everything else is inference. ## Operational Effect When the invariant is enforced, the failure mode is converted from silent state corruption into a bounded, observable restart. The cost is a checkpoint re-read and possibly a re-`initialize` round trip (tens to hundreds of milliseconds). The cost of not enforcing it is non-idempotent tool replay, duplicated side effects, and reasoning divergence that compounds across subsequent calls — the model cannot distinguish a replayed observation from a new one, and every downstream decision inherits the ambiguity. The guardrail is a client-side transport state machine that keys resume tokens to `(leg-id, log-namespace)` and refuses cross-leg reuse without an explicit continuity assertion. This is a small, local invariant with outsized impact because it sits at the exact point where the transport layer hands control back to probabilistic reasoning. ```python from dataclasses import dataclass from typing import Optional class ResumeTokenInvalidError(RuntimeError): """Raised when a Last-Event-ID is reused across an incompatible transport leg.""" pass @dataclass(frozen=True) class LegContext: leg_id: str method: str # "GET" (SSE) or "POST" log_namespace: str # server-declared event-log namespace committed_watermark: Optional[int] # highest durably committed event id @dataclass(frozen=True) class ResumeToken: last_event_id: int issued_leg_id: str issued_namespace: str def validate_resume_on_upgrade( token: ResumeToken, target_leg: LegContext, declares_unified_log: bool, ) -> None: """ Enforce: a resume token is valid on a new leg only if the server declares a unified event log AND the target leg's committed watermark covers the token's last_event_id. """ if target_leg.log_namespace != token.issued_namespace and not declares_unified_log: raise ResumeTokenInvalidError( f"resume token from namespace {token.issued_namespace!r} " f"cannot cross into {target_leg.log_namespace!r} without unified-log declaration" ) if target_leg.committed_watermark is None: raise ResumeTokenInvalidError( f"leg {target_leg.leg_id!r} has no committed watermark; " "Last-Event-ID cannot be validated, treat token as leg-local" ) if token.last_event_id > target_leg.committed_watermark: raise ResumeTokenInvalidError( f"resume token id {token.last_event_id} exceeds committed watermark " f"{target_leg.committed_watermark} on leg {target_leg.leg_id!r}; " "refusing to replay POST" ) def resume_or_restart( token: ResumeToken, target_leg: LegContext, declares_unified_log: bool, ) -> str: try: validate_resume_on_upgrade(token, target_leg, declares_unified_log) except ResumeTokenInvalidError: # Invalid resume: do NOT retry the POST with a stale token. # Invalidate session and resume from durable checkpoint. return "restart_from_checkpoint" return "resume_on_leg" ```