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

conn()

@type conn() :: Plug.Conn.t()

session_id()

@type session_id() :: String.t()

transport()

@type transport() :: GenServer.server()

Functions

prepare_connection(conn)

@spec prepare_connection(conn()) :: conn()

Prepares a connection for SSE streaming.

Sets appropriate headers and starts chunked response.

send_event(conn, data, event_id)

@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.

start(conn, transport, session_id, opts \\ [])

@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 response
  • transport - The transport process
  • session_id - The session identifier
  • opts - 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