Phantom.Session (phantom_mcp v0.2.0)
View SourceRepresents the state of the MCP session. This is the state across the conversation and is the bridge between the various transports (HTTP, stdio) to persistence, even if stateless.
Summary
Functions
Set an allow-list of usable Prompts for the session
Set an allow-list of usable Resource Templates for the session
Set an allow-list of usable Tools for the session
Assign state to the session.
Assign state to the session.
Closes the connection for the session
Get the PID of the SSE stream for the session id
List all sessions with SSE streams open.
Notify the client with a log at level "alert" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "alert"
Notify the client with a log at level "critical" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "critical"
Notify the client with a log at level "debug" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "debug"
Notify the client with a log at level "emergency" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "emergency"
Notify the client with a log at level "error" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "error"
Notify the client with a log at level "info" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "info"
Notify the client with a log at level "notice" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "notice"
Notify the client with a log at level "warning" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "warning"
Builds a new session with the provided session ID.
Send a notification to the client
Send a progress notification to the client
Send a ping to the client
Fetch the current progress token if provided by the client
The PubSub topic Phantom will listen to for resource updates "phantom:resources"
Sends response back to the stream
Sets the log level for the SSE stream. Sets both for the current request for async tasks and the SSE stream
Subscribe the session to a resource.
Types
@type log_level() ::
:emergency | :alert | :critical | :error | :warning | :notice | :info | :debug
@type t() :: %Phantom.Session{ allowed_prompts: [String.t()], allowed_resource_templates: [String.t()], allowed_tools: [String.t()], assigns: map(), close_after_complete: boolean(), id: binary(), last_event_id: String.t() | nil, pid: pid() | nil, pubsub: module(), request: Phantom.Request.t() | nil, requests: map(), router: module(), stream_fun: fun(), tracker: term(), transport_pid: pid() }
Functions
Set an allow-list of usable Prompts for the session
Set an allow-list of usable Resource Templates for the session
Set an allow-list of usable Tools for the session
Assign state to the session.
Assign state to the session.
@spec finish(Session.t()) :: :ok
Closes the connection for the session
Get the PID of the SSE stream for the session id
List all sessions with SSE streams open.
@spec log_alert(structured_log :: map()) :: :ok
Notify the client with a log at level "alert" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "alert"
@spec log_critical(structured_log :: map()) :: :ok
Notify the client with a log at level "critical" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "critical"
@spec log_debug(structured_log :: map()) :: :ok
Notify the client with a log at level "debug" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "debug"
@spec log_emergency(structured_log :: map()) :: :ok
Notify the client with a log at level "emergency" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "emergency"
@spec log_error(structured_log :: map()) :: :ok
Notify the client with a log at level "error" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "error"
@spec log_info(structured_log :: map()) :: :ok
Notify the client with a log at level "info" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "info"
@spec log_notice(structured_log :: map()) :: :ok
Notify the client with a log at level "notice" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "notice"
@spec log_warning(structured_log :: map()) :: :ok
Notify the client with a log at level "warning" with domain "server".
Note: this requires the session
variable to be within scope
Notify the client with a log at level "warning"
Builds a new session with the provided session ID.
This is used for adapters such as Phantom.Plug
. If a
session ID is not provided, it will generate one using UUIDv7
.
Send a notification to the client
Send a progress notification to the client
Send a ping to the client
Fetch the current progress token if provided by the client
The PubSub topic Phantom will listen to for resource updates "phantom:resources"
Sends response back to the stream
This should likely be used in conjunction with:
Phantom.Tool.response(payload)
Phantom.Resource.response(payload)
Phantom.Prompt.response(payload)
For example:
session_pid = session.pid
request_id = request.id
Task.async(fn ->
Session.respond(
session_pid,
request_id,
Phantom.Tool.audio(
File.read!("priv/static/game-over.wav"),
mime_type: "audio/wav"
)
)
end)
See respond/2
@spec set_log_level(Session.t(), Phantom.Request.t(), String.t()) :: :ok
Sets the log level for the SSE stream. Sets both for the current request for async tasks and the SSE stream
Subscribe the session to a resource.
This is used by the MCP Router when the client requests to subscribe to the provided resource.