Anubis.Server.Session.ServerRequests (anubis_mcp v1.14.0)

Copy Markdown

Engine for server-initiated requests (sampling, roots, elicitation).

Owns the lifecycle of requests the server sends to the client: capability validation, encoding, transport send, timeout tracking, and routing the response back to the server module callback. The three subsystems share one generic flow; what varies between them (wire method, client capability, timeout message, result shaping, log events) lives in the per-kind configuration below.

State is kept in the Session's server_requests map; this module transforms it and performs the side effects (timers, transport, logging) at the edges. Anubis.Server.Session delegates to this module and supplies a frame_fn to prepare the callback frame lazily.

Summary

Functions

Handles a client error response for a tracked server-initiated request.

Routes a client response for a tracked server-initiated request.

Handles a timeout for a tracked server-initiated request.

Sends a server-initiated request of the given kind to the client.

Returns true if the given request ID belongs to a tracked server-initiated request.

Types

frame_fn()

@type frame_fn() :: (state() -> Anubis.Server.Frame.t())

kind()

@type kind() :: :sampling | :roots | :elicitation

state()

@type state() :: map()

Functions

handle_error(map, state)

@spec handle_error(map(), state()) :: {:noreply, state()}

Handles a client error response for a tracked server-initiated request.

handle_response(map, state, frame_fn)

@spec handle_response(map(), state(), frame_fn()) ::
  {:noreply, state()} | {:stop, term(), state()}

Routes a client response for a tracked server-initiated request.

Stops the timeout timer, untracks the request, shapes the result per the kind configuration, and invokes the configured server module callback with the frame built by frame_fn.

handle_timeout(kind, request_id, state)

@spec handle_timeout(kind(), term(), state()) :: {:noreply, state()}

Handles a timeout for a tracked server-initiated request.

Kinds configured with cancel_on_timeout first attempt to notify the client with notifications/cancelled before dropping the request.

send_request(kind, params, timeout, state, extra \\ %{})

@spec send_request(kind(), map(), non_neg_integer(), state(), map()) ::
  {:noreply, state()}

Sends a server-initiated request of the given kind to the client.

Generates the request ID, arms the timeout timer, tracks the request in state.server_requests, validates the client capability, and encodes and sends the request. On any failure the timer is cancelled and the request untracked. extra is merged into the tracked request info (elicitation uses it for the requested_schema needed to validate the response).

server_request?(request_id, arg2)

@spec server_request?(term(), state()) :: boolean()

Returns true if the given request ID belongs to a tracked server-initiated request.