Phantom.Session (phantom v0.1.1)

View Source

Represents 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

Closes the SSE stream for the session

Get the PID of the SSE stream for the session id

Notify the client with a log at level "alert"

Notify the client with a log at level "critical"

Notify the client with a log at level "debug"

Notify the client with a log at level "emergency"

Notify the client with a log at level "error"

Notify the client with a log at level "info"

Notify the client with a log at level "notice"

Notify the client with a log at level "warning"

Sends response back to the SSE stream

Sets the log level for the SSE stream. Sets both for the current request for async tasks and the SSE stream

Types

log_level()

@type log_level() ::
  :emergency | :alert | :critical | :error | :warning | :notice | :info | :debug

t()

@type t() :: %Phantom.Session{
  assigns: map(),
  close_after_complete: boolean(),
  id: binary(),
  last_event_id: String.t() | nil,
  pid: pid() | nil,
  prompts: [atom()],
  pubsub: module(),
  requests: map(),
  resource_templates: [atom()],
  router: module(),
  stream_fun: fun(),
  subscribed: map(),
  tools: [atom()],
  tracker: term()
}

Functions

assign(session, map)

@spec assign(t(), map()) :: t()

assign(session, key, value)

@spec assign(t(), atom(), any()) :: t()

finish(pid)

@spec finish(Session.t()) :: :ok

Closes the SSE stream for the session

get_sse_pid(session_id)

Get the PID of the SSE stream for the session id

list()

log(session, atom, domain, payload)

@spec log(Session.t(), log_level(), String.t(), structured_log :: map()) :: :ok

log_alert(session, domain, payload)

@spec log_alert(Session.t(), String.t(), structured_log :: map()) :: :ok

Notify the client with a log at level "alert"

log_critical(session, domain, payload)

@spec log_critical(Session.t(), String.t(), structured_log :: map()) :: :ok

Notify the client with a log at level "critical"

log_debug(session, domain, payload)

@spec log_debug(Session.t(), String.t(), structured_log :: map()) :: :ok

Notify the client with a log at level "debug"

log_emergency(session, domain, payload)

@spec log_emergency(Session.t(), String.t(), structured_log :: map()) :: :ok

Notify the client with a log at level "emergency"

log_error(session, domain, payload)

@spec log_error(Session.t(), String.t(), structured_log :: map()) :: :ok

Notify the client with a log at level "error"

log_info(session, domain, payload)

@spec log_info(Session.t(), String.t(), structured_log :: map()) :: :ok

Notify the client with a log at level "info"

log_notice(session, domain, payload)

@spec log_notice(Session.t(), String.t(), structured_log :: map()) :: :ok

Notify the client with a log at level "notice"

log_warning(session, domain, payload)

@spec log_warning(Session.t(), String.t(), structured_log :: map()) :: :ok

Notify the client with a log at level "warning"

new(session_id, opts \\ [])

@spec new(String.t() | nil, Keyword.t() | map()) :: t()

respond(pid, request_id, payload)

Sends response back to the SSE stream

This should likely be used in conjunction with:

  • Phantom.Tool.response(payload)
  • Phantom.ResourceTemplate.response(payload, resource_template, uri)
  • Phantom.Prompt.response(payload, prompt)

For example:

   session_pid = session.pid
   request_id = request.id

   Task.async(fn ->
    Session.respond(
      session_pid,
      request_id,
      Phantom.Tool.call_response(%{
        type: :audio,
        data: Base.encode64(File.read!("test/support/game-over.wav")),
        mime_type: "audio/wav"
      })
    )
   end)

set_log_level(session, request, level)

@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