Phantom.Session (phantom_mcp v0.3.2)
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.
Elicit input from the client
Closes the connection for the session
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
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.
Unsubscribe the session to a resource.
Types
@type t() :: %Phantom.Session{ allowed_prompts: [String.t()], allowed_resource_templates: [String.t()], allowed_tools: [String.t()], assigns: map(), client_capabilities: %{ elicitation: false | map(), sampling: false | map(), roots: false | map() }, client_info: 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 elicit(t(), Phantom.Elicit.t()) :: {:ok, request_id :: String.t()} | :not_supported
Elicit input from the client
@spec finish(Session.t() | pid()) :: :ok
Closes the connection for the session
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
the progress
and total
can be a integer or float, but must be ever-increasing.
the total
is optional.
https://modelcontextprotocol.io/specification/2025-06-18/basic/utilities/progress
Send a ping to the client
Fetch the current progress token if provided by the client
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.
Unsubscribe the session to a resource.
This is used by the MCP Router when the client requests to subscribe to the provided resource.