Why does an instance lose its resumption cursor when the MCP server upgrades an HTTP-native session to SSE mid-stream?

**Date of Record:** 2026 December 1 **Scope:** Applies to MCP clients using the HTTP-native (streamable) transport where the server may initiate an SSE upgrade on an in-flight request. Does not apply to pure stdio transports or to clients that never issue resumable requests (i.e., those that set no `Last-Event-ID`).

Core Distinction

There are two distinct notions of "session continuity" in the MCP HTTP-native transport, and they are frequently conflated by client implementations:

  1. **Transport-level resumption** — the ability to reconnect a dropped HTTP response stream and replay events using a monotonic event ID (`Last-Event-ID`). This is a *byte-stream* invariant: the server must assign IDs from a single monotonic sequence per session, and the client must echo the last successfully consumed ID.
  2. **Protocol-level session state** — the MCP `sessionId` header that scopes tool registrations, pending requests, and server-side context. This is a *logical* invariant independent of the wire.

The failure mode in question occurs at the seam between these two: when a server performs a **server-initiated SSE upgrade** (returning `Content-Type: text/event-stream` on a response that the client opened as a plain `application/json` POST), the event-ID namespace can reset or fork, and the client's stored resumption cursor becomes invalid *without any error being surfaced*. The client believes it holds a valid `Last-Event-ID`; the server has silently started a new event sequence. On the next reconnect, the client asks to resume from an ID the server never issued in the current sequence, and the server either (a) replays from the beginning of the new sequence, duplicating already-consumed events, or (b) rejects the resume and returns a fresh stream, silently dropping everything between the last consumed event and the upgrade point.

What Was Observed

A client issued a `tools/call` over HTTP-native transport as a `POST` with `Accept: application/json, text/event-stream`. The server responded `200` with `Content-Type: application/json` and a body containing a partial result plus an opaque progress token. Mid-execution the tool needed to stream incremental output, so the server — per the streamable-HTTP spec — upgraded the *same logical request* by emitting an SSE frame. The concrete trace:

No transport error was raised. No protocol error was raised. The MCP response was well-formed. The client's `tools/call` returned `isError: false` with a truncated-but-syntactically-valid result. The corruption was only detected downstream when the resource handle failed to resolve.

The critical, non-obvious observation: **the client's resumption cursor is a single scalar, but the server maintains two event-ID sequences across the upgrade.** The cursor cannot represent "position 8 in sequence A and position 1 in sequence B." Any design that stores one `lastEventId` is structurally unable to resume correctly across an upgrade.

Condition of Validity

The invariant that makes resumption safe is:

> For a given MCP session, all events delivered to the client — across every transport mode transition, including JSON→SSE upgrades and SSE→SSE re-attachments — MUST be drawn from a single monotonic event-ID sequence, and the server MUST reject a `Last-Event-ID` that does not belong to the current sequence rather than silently reinterpreting it.

This holds only when:

It does **not** hold when the server uses a per-stream counter (common with off-the-shelf SSE middleware layered on top of a JSON handler), when the client collapses multiple sequences into one scalar, or when the upgrade is implicit (content-type change only, no announcement).

Decision Boundary

The boundary is this: **a client may trust its resumption cursor across an upgrade if and only if it has positively confirmed sequence continuity; otherwise it must treat the upgrade as a hard resumption boundary and re-fetch full state.**

Concretely, on observing a content-type transition to `text/event-stream` on an in-flight request, the client must choose between:

The failure occurs precisely when the client takes the Continue path by default — which is the natural implementation, since a single scalar `lastEventId` always "has a value" and there is no signal that it is stale. The decision boundary is invisible unless the client explicitly compares IDs across the transition.

Operational Effect

The downstream effect is **silent partial-result acceptance**. Because MCP responses are structured and the transport layer reports success, the truncation propagates as valid-looking data. In agent terms, the instance believes it has a complete tool result and proceeds to reason over it, and the error compounds: a missing intermediate chunk becomes a missing field, becomes a hallucinated value, becomes a downstream tool call with a bad argument. The failure is not localized to the transport — it is laundered into the reasoning loop.

Two guardrails bound it:

  1. **Cursor epoch validation** on every content-type transition (the decision boundary above).
  2. **Result completeness attestation** — the server includes a terminal event carrying a sequence-range digest, and the client verifies it received every ID in `[first, last]` before accepting the result. A gap is a hard error, not a warning.

Without (1), the client resumes from the wrong position. Without (2), the client cannot detect that it did.

from dataclasses import dataclass, field


class ResumptionCursorInvalid(RuntimeError):
    """Raised when a resumption cursor cannot be trusted across an SSE upgrade."""
    pass


@dataclass
class StreamCursor:
    session_id: str
    epoch: int
    last_event_id: int | None = None
    seen_ids: set[int] = field(default_factory=set)


def consume_event(cursor: StreamCursor, event_id: int, transport: str) -> StreamCursor:
    """Consume one event, enforcing monotonic continuity across upgrades."""
    if cursor.last_event_id is not None and event_id <= cursor.last_event_id:
        # Content-type transitioned and the server's counter reset or forked.
        # We cannot represent two sequences in one scalar; refuse to resume.
        raise ResumptionCursorInvalid(
            f"event id {event_id} <= last {cursor.last_event_id} on session "
            f"{cursor.session_id} (epoch {cursor.epoch}, transport {transport}); "
            f"server likely reset its event counter on SSE upgrade — "
            f"re-anchor required, do not echo stale Last-Event-ID"
        )
    cursor.last_event_id = event_id
    cursor.seen_ids.add(event_id)
    return cursor


def verify_complete(cursor: StreamCursor, first_id: int, last_id: int) -> None:
    """Server-side terminal digest check: every ID in range must be present."""
    expected = set(range(first_id, last_id + 1))
    missing = expected - cursor.seen_ids
    if missing:
        raise ResumptionCursorInvalid(
            f"session {cursor.session_id} epoch {cursor.epoch} missing events "
            f"{sorted(missing)} in range [{first_id},{last_id}] — "
            f"partial result must not be accepted"
        )