The request context handed to every server handler.
Besides carrying the negotiated session metadata and the user state from
Urchin.Server.init/1, the context exposes the side-effecting capabilities a
handler may use while processing a request:
- progress and log notifications related to the in-flight request
- server-initiated requests to the client: sampling, elicitation, roots
- a best-effort cancellation check
Notifications and server-initiated requests are emitted on the stream that carries the originating request, as required by the transport spec.
Summary
Functions
Stores a value in the context assigns.
Returns the validated OAuth claims for the originating request, or nil.
Returns true if the originating request has been cancelled by the client.
Issues a sampling/createMessage request to the client and waits for the result.
Issues an elicitation/create request to the client and waits for the result.
Issues a roots/list request to the client and waits for the result.
Sends a notifications/message log entry to the client.
Sends an arbitrary JSON-RPC notification on the originating request stream.
Sends a progress notification for the originating request.
Issues an arbitrary server-to-client request and blocks until the client responds.
Returns the user state established by Urchin.Server.init/1.
Types
@type t() :: %Urchin.Context{ assigns: map(), auth: Urchin.Auth.Claims.t() | nil, cancelled_ref: reference() | nil, client_capabilities: map() | nil, client_info: map() | nil, min_log_level: String.t(), owner: pid() | nil, params: map(), progress_token: String.t() | number() | nil, protocol_version: String.t() | nil, request_id: Urchin.JSONRPC.id() | nil, session: pid() | nil, state: term(), uri: String.t() | nil }
Functions
Stores a value in the context assigns.
@spec auth(t()) :: Urchin.Auth.Claims.t() | nil
Returns the validated OAuth claims for the originating request, or nil.
Populated only when the transport is configured with :auth (or an upstream
Urchin.Auth.Plug ran). Handlers use it for per-tool authorization:
if Urchin.Auth.Claims.has_scope?(Urchin.Context.auth(ctx), "files:write") do
# ...
end
Returns true if the originating request has been cancelled by the client.
Cancellation also terminates the handler process, so this is a cooperative, best-effort check for long-running loops.
@spec create_message(t(), map(), timeout()) :: {:ok, map()} | {:error, Urchin.Error.t()}
Issues a sampling/createMessage request to the client and waits for the result.
Returns {:ok, result_map} or {:error, Urchin.Error.t()}. Requires the client to
have advertised the sampling capability.
@spec elicit(t(), map(), timeout()) :: {:ok, map()} | {:error, Urchin.Error.t()}
Issues an elicitation/create request to the client and waits for the result.
Returns {:ok, result_map} or {:error, Urchin.Error.t()}.
@spec list_roots(t(), timeout()) :: {:ok, map()} | {:error, Urchin.Error.t()}
Issues a roots/list request to the client and waits for the result.
Returns {:ok, %{"roots" => [...]}} or {:error, Urchin.Error.t()}.
Sends a notifications/message log entry to the client.
Messages below the level requested by the client via logging/setLevel are
dropped. level is one of ["debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"].
Options
:logger- optional logger name
Sends an arbitrary JSON-RPC notification on the originating request stream.
Sends a progress notification for the originating request.
No-op when the client did not supply a progressToken for the request.
Options
:total- total units of work, if known:message- human-readable progress message
Issues an arbitrary server-to-client request and blocks until the client responds.
The request is written to the originating request's stream; the matching response, delivered on a later client POST, is correlated by the session.
Returns the user state established by Urchin.Server.init/1.