Hermes.SSE.Streaming (hermes_mcp v0.9.0)
Handles Server-Sent Events streaming for MCP connections.
This module provides the core SSE streaming functionality, managing the event loop and message formatting.
Summary
Functions
Prepares a connection for SSE streaming.
Sends a single SSE event.
Starts the SSE streaming loop for a connection.
Types
@type conn() :: Plug.Conn.t()
@type session_id() :: String.t()
@type transport() :: GenServer.server()
Functions
Prepares a connection for SSE streaming.
Sets appropriate headers and starts chunked response.
@spec send_event(conn(), binary(), non_neg_integer()) :: {:ok, conn()} | {:error, term()}
Sends a single SSE event.
This is useful for sending events outside of the main loop.
@spec start(conn(), transport(), session_id(), keyword()) :: conn()
Starts the SSE streaming loop for a connection.
This function takes control of the connection and enters a receive loop, streaming messages to the client as they arrive.
Parameters
conn
- The Plug.Conn that has been prepared for chunked responsetransport
- The transport processsession_id
- The session identifieropts
- Options including::initial_event_id
- Starting event ID (default: 0):on_close
- Function to call when connection closes
Messages handled
{:sse_message, binary}
- Message to send to client:close_sse
- Close the connection gracefully