Hermes.Server.Session (hermes_mcp v0.9.0)
Manages state for the Hermes MCP server base implementation.
This module provides a structured representation of server state during the MCP lifecycle, including initialization status, protocol negotiation, and server capabilities.
State Structure
Each server state includes:
protocol_version
: Negotiated MCP protocol versionframe
: Server frame (similar to LiveView socket)initialized
: Whether the server has completed initializationclient_info
: Client information received during initializationclient_capabilities
: Client capabilities received during initialization
Summary
Functions
Returns a specification to start this module under a supervisor.
Retrieves the current state of a session.
Guard to check if a session has been initialized.
Marks the session as initialized.
Creates a new server state with the given options.
Updates the log level.
Starts a new session agent with initial state.
Updates state after successful initialization handshake.
Types
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
@spec get(GenServer.name()) :: t()
Retrieves the current state of a session.
Parameters
session
- The session agent name or PID
Returns
The current session state as a %Session{}
struct.
Examples
iex> session_state = Session.get(session_name)
%Session{initialized: true, protocol_version: "2025-03-26"}
Guard to check if a session has been initialized.
Examples
iex> session = %Session{initialized: true}
iex> is_initialized(session)
true
@spec mark_initialized(GenServer.name()) :: :ok
Marks the session as initialized.
@spec new(Enumerable.t()) :: t()
Creates a new server state with the given options.
Parameters
opts
- Map containing the initialization options
@spec set_log_level(GenServer.name(), String.t()) :: :ok
Updates the log level.
@spec start_link(keyword()) :: Agent.on_start()
Starts a new session agent with initial state.
@spec update_from_initialization(GenServer.name(), String.t(), map(), map()) :: :ok
Updates state after successful initialization handshake.
This function:
- Sets the negotiated protocol version
- Stores client information and capabilities
- Marks the server as initialized
Parameters
state
- The current server stateprotocol_version
- The negotiated protocol versionclient_info
- Client information from the initialize requestclient_capabilities
- Client capabilities from the initialize request
Examples
iex> client_info = %{"name" => "hello", "version" => "1.0.0"}
iex> capabilities = %{"sampling" => %{}}
iex> Hermes.Server.Session.update_from_initialization(session, "2025-03-26", client_info, capabilities)
:ok