MCP.Transport.StreamableHTTP.Server (MCP Elixir SDK v1.1.0)

Copy Markdown View Source

Streamable HTTP server transport for MCP.

A GenServer that implements the MCP.Transport behaviour, bridging between an HTTP Plug endpoint and the MCP.Server. Incoming HTTP requests are delivered to this transport, which forwards them to the MCP.Server. Outgoing messages from the MCP.Server are routed back to the appropriate HTTP response.

Supports two modes for pending requests:

  • Sync — the Plug process blocks on deliver_message/2 waiting for the response. Used for simple request/response.
  • Stream — the Plug process registers as a stream endpoint via register_stream/3 and receives SSE events asynchronously. Used for tool calls that may send notifications/requests during execution.

Options

  • :owner (required) — pid to receive {:mcp_message, map} and {:mcp_transport_closed, reason} messages
  • :session_id — the session ID for this transport

Summary

Functions

Returns a specification to start this module under a supervisor.

Delivers an incoming JSON-RPC message to this transport (synchronous).

Delivers an incoming JSON-RPC message to this transport (async).

Registers a Plug connection for SSE streaming (GET endpoint).

Registers a Plug process as an SSE stream endpoint for a request.

Sends a message with options. Supports related_request_id for routing notifications and requests to the correct SSE stream.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

deliver_message(pid, message)

Delivers an incoming JSON-RPC message to this transport (synchronous).

Called by the Plug when an HTTP POST arrives. For JSON-RPC requests, the caller blocks until the MCP.Server sends a response.

Returns {:ok, response_message} for requests, or :accepted for notifications and responses.

deliver_message_async(pid, message)

Delivers an incoming JSON-RPC message to this transport (async).

The message is forwarded to the MCP.Server but the caller does not block for a response. Used with register_stream/3 for SSE streaming mode.

register_sse_conn(pid, conn)

Registers a Plug connection for SSE streaming (GET endpoint).

Server-initiated messages will be streamed to this connection.

register_stream(pid, request_id, stream_pid)

Registers a Plug process as an SSE stream endpoint for a request.

The Plug process will receive:

  • {:sse_event, sse_encoded_data} — intermediate SSE events
  • {:sse_done, sse_encoded_data} — final response event (stream should close)

send_message(pid, message, opts)

Sends a message with options. Supports related_request_id for routing notifications and requests to the correct SSE stream.