Skip to content

API Reference parrot.mcp.transports.streamable_http

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.mcp.transports.streamable_http

View source

MCP Streamable HTTP transport (spec revision 2025-03-26).

Implements the single-endpoint Streamable HTTP transport required by Claude.ai custom connectors and the official MCP SDK client:

  • POST {base_path}: JSON-RPC messages (single or batch). Bodies with only notifications/responses are acknowledged with 202. Bodies with requests are answered as application/json, or as an SSE stream when the client's Accept header includes text/event-stream.
  • GET {base_path}: opens the session's server-to-client SSE stream. Supports resumability via Last-Event-ID: buffered events after that id are replayed in order, so a client that disconnected during a long-running tools/call (e.g. launching an agent flow) can reconnect and still collect the result.
  • DELETE {base_path}: terminates the session.

Sessions are identified by the Mcp-Session-Id header minted on initialize. Request dispatch in SSE mode runs as asyncio tasks that record their responses in a per-stream event buffer regardless of connection state, which is what makes disconnect-and-resume possible.

Streams, not sessions, own their events. Each request-bearing POST opens its own stream and only that stream's SSE response writes its events, so a concurrently open GET stream can never consume a response the POST still owes its client (the spec requires the response to travel on the stream that carried the request). Event ids are {stream_id}:{sequence}, so a client that reconnects with Last-Event-ID names the stream it was cut off from and receives that stream and nothing else. A plain GET is the session's server-to-client stream; it additionally adopts streams orphaned by a disconnected client, which is what lets a client collect a long tool call's result after a drop without having kept the event id.

The event store is in-memory and per-process, matching how the rest of the MCP server stack keeps state; a shared (e.g. Redis-backed) store for multi-worker deployments is a documented follow-up.

Attribute ASSUMED_HEADER_VERSION

Type: str

Alias AuthMethod

Re-exported from parrot.mcp.config.AuthMethod.

Alias HttpMCPServer

Re-exported from parrot.mcp.transports.http.HttpMCPServer.

Alias InMemorySessionStore

Re-exported from parrot.mcp.session_store.InMemorySessionStore.

Attribute KEEP_ALIVE_INTERVAL

Type: float

Attribute LOCALHOST_HOSTS

Type: frozenset[str]

Alias MCPServerConfig

Re-exported from parrot.mcp.config.MCPServerConfig.

Class McpStreamSession

State for one Streamable HTTP session.

Parameter Type Default Description
session_id str
protocol_version str
created_at float
last_seen float
principal Any None
streams dict[str, StreamBuffer] dict()
tasks dict[Any, asyncio.Task] dict()
wakeup asyncio.Event asyncio.Event()
live_stream bool False

Fields:

Field Type Default Description
session_id str
protocol_version str
created_at float
last_seen float
principal Any None
streams dict[str, StreamBuffer] field(default_factory=dict)
tasks dict[Any, asyncio.Task] field(default_factory=dict)
wakeup asyncio.Event field(default_factory=asyncio.Event)
live_stream bool False

Method McpStreamSession.open_stream

Create (or fetch) a buffer for one outbound stream.

Opening a stream first reclaims spent ones, so a session that issues many SSE POSTs does not accumulate buffers for the whole of its TTL.

open_stream(max_events: int, stream_id: str | None = None, max_streams: int = 64) -> StreamBuffer

Parameter Type Default Description
max_events int
stream_id str | None None
max_streams int 64

Returns: StreamBuffer

Method McpStreamSession.prune_streams

Keep at most max_streams streams, evicting the least useful.

Streams are not dropped merely because their events were delivered — a client may still resume one from an earlier Last-Event-ID. They are dropped only to stay under the cap, and then spent streams (no attached response, nothing undelivered) go first, oldest to newest, before any stream that still owes someone data. The server stream is permanent.

prune_streams(max_streams: int) -> None

Parameter Type Default Description
max_streams int

Method McpStreamSession.is_busy

True when the session has a live stream or unfinished work.

is_busy() -> bool

Returns: bool

Attribute PRUNE_INTERVAL

Type: float

Attribute SERVER_STREAM

Type: str

Alias SUPPORTED_PROTOCOL_VERSIONS

Re-exported from parrot.mcp.server_base.SUPPORTED_PROTOCOL_VERSIONS.

Alias SessionStore

Re-exported from parrot.mcp.session_store.SessionStore.

Alias SessionStoreUnavailable

Re-exported from parrot.mcp.session_store.SessionStoreUnavailable.

Class StreamBuffer

Ordered, bounded buffer of outbound messages for one SSE stream.

Sequence numbers increase monotonically per stream and combine with the stream id to form the SSE id: field, so a client can resume with Last-Event-ID after a disconnect. The buffer is a ring: once max_events is exceeded the oldest events are dropped (no longer replayable).

Parameter Type Default Description
stream_id str
max_events int 1000

Attributes:

Attribute Type Description
attached bool

Method StreamBuffer.append

Buffer an outbound message.

append(message: dict[str, Any]) -> StreamEvent

Parameter Type Default Description
message dict[str, Any]

Returns: StreamEvent

Method StreamBuffer.events_after

Return buffered events with a sequence greater than sequence.

events_after(sequence: int) -> list[StreamEvent]

Parameter Type Default Description
sequence int

Returns: list[StreamEvent]

Method StreamBuffer.undelivered

Return buffered events not yet written to any stream.

undelivered() -> list[StreamEvent]

Returns: list[StreamEvent]

Class StreamEvent

One buffered outbound JSON-RPC message with its SSE event id.

Parameter Type Default Description
stream_id str
sequence int
message dict[str, Any]
delivered bool False

Fields:

Field Type Default Description
stream_id str
sequence int
message dict[str, Any]
delivered bool False

Attributes:

Attribute Type Description
event_id str SSE id: value — scoped to the stream that produced it.

Method StreamEvent.to_sse

Serialize as an SSE message event carrying the event id.

to_sse() -> bytes

Returns: bytes

Class StreamableHttpMCPServer

MCP server speaking the Streamable HTTP transport on aiohttp.

Extends :class:HttpMCPServer (reusing auth, OAuth routes, and the parent-app/standalone mounting logic) with the 2025-03-26 transport semantics: session ids, Accept negotiation, SSE responses, a resumable GET stream, and DELETE session termination.

Parameter Type Default Description
config MCPServerConfig
parent_app web.Application | None None
session_store SessionStore | None None

Method StreamableHttpMCPServer.start

Start the server and its background session sweeper.

start()

Method StreamableHttpMCPServer.stop

Stop the server, cancelling pending dispatch tasks.

stop()

Attribute TEARDOWN_TIMEOUT

Type: float

Function parse_event_id

Split a Last-Event-ID into its (stream_id, sequence) parts.

parse_event_id(raw: str) -> tuple[str, int] | None

Parameter Type Default Description
raw str The header value sent by the client.

Returns: tuple[str, int] | None — The parsed pair, or None when the value is not a well-formed event id this server could have issued.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally