Skip to main content

RequestContext

Struct RequestContext 

Source
pub struct RequestContext { /* private fields */ }
Expand description

Everything a handler is allowed to know about the call it is serving.

Implementations§

Source§

impl RequestContext

Source

pub fn for_request( peer: &PeerSession, id: RequestId, cancel: CancellationToken, progress: Option<ProgressToken>, ) -> Self

Binds one in-flight request to the connection serving it.

Clones the session, which is two Arc bumps and two Copy fields. That is why a server with a thousand in-flight requests pays a thousand pointer bumps rather than a thousand deep copies of the profile.

The state map is empty. A test that needs state should use for_request_with_state; a production handler gets the map from the Server it was built with.

Source

pub fn for_request_with_state( peer: &PeerSession, id: RequestId, cancel: CancellationToken, progress: Option<ProgressToken>, state: Arc<StateMap>, ) -> Self

Binds one in-flight request to the connection serving it, with the server’s registered state.

The state map is the one the Server accumulated through with_state calls. The context holds an Arc rather than the values, so a thousand concurrent requests share one allocation rather than each deep-copying the database pool.

Source

pub fn input_response(&self, key: &str) -> Option<JsonObject>

The client’s answer to the input request filed under key.

None on a first round, and on a retry that did not answer that key — which a handler should treat as “ask again” rather than as an error, because SEP-2322 has the server re-request what is missing.

Source

pub const fn has_input_responses(&self) -> bool

Whether this request is a retry carrying inputResponses at all.

Source

pub fn request_state(&self) -> Option<&str>

The requestState this server sealed on the previous round.

Already verified: a state that did not open was refused before the handler ran, so anything returned here is the server’s own.

Source

pub fn seal_state(&self, state: &str) -> Result<String, GatedError>

Seals state for an InputRequired result’s requestState.

§Errors

GatedError::NoStateKey when the server was built without a key and no transport supplied one, so there is nothing to seal under.

Source

pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T>

Looks up a state value by its concrete type.

The state map is keyed by TypeId, and the value is stored as Arc<dyn Any>, so the lookup is two HashMap operations and one downcast. The Send + Sync bounds are the same the Server::with_state builder requires, so a T registered here is reachable here.

Returns None for a type that was never registered. That is the right answer for a state the handler treats as optional; a handler that requires a piece of state should .expect the result with a message naming the missing type, or it should be registered at server construction.

§Examples
ⓘ
#[tool]
async fn search(ctx: RequestContext, query: String) -> Result<JsonObject, ToolError> {
    let db = ctx.state::<MyPool>().expect("MyPool was registered");
    db.search(query).await
}
Source

pub const fn peer(&self) -> &PeerSession

The connection serving this request.

Source

pub const fn request_id(&self) -> &RequestId

The request id, for correlating anything the handler emits itself.

Source

pub const fn cancellation(&self) -> &CancellationToken

The caller’s cancellation token, so a long handler can observe it.

Source

pub const fn progress_token(&self) -> Option<&ProgressToken>

The progress token the client opened, if it opened one.

Source

pub async fn elicit( &self, params: &JsonObject, ) -> Result<JsonObject, GatedError>

Asks the peer for extra input from the user.

Gated on Feature::Elicitation. The gate runs first, so a session that never negotiated it writes nothing at all — see GatedError::wrote_nothing.

§Errors

GatedError::FeatureUnavailable when elicitation was not negotiated, having written nothing. GatedError::MalformedAnswer when the exchange did not complete, which is the one case here where the request did reach the wire first.

Source

pub async fn sample( &self, params: &JsonObject, ) -> Result<JsonObject, GatedError>

Asks the peer to run a model on the server’s behalf.

Gated on Feature::Sampling, on the same terms as InputRequired::elicit.

§Errors

As InputRequired::elicit.

Source

pub async fn create_task( &self, params: &JsonObject, ) -> Result<JsonObject, GatedError>

Opens a long-running task for this request.

Gated on Feature::Tasks, which enters the core protocol in 2025-11-25 and moves to an extension in 2026-07-28 — so the same call is available, unavailable, and available-elsewhere across three revisions, and the gate is what makes that legible.

§Errors

As InputRequired::elicit.

Source

pub async fn report_progress( &self, params: &JsonObject, ) -> Result<(), GatedError>

Reports progress on a request the client opened a channel for.

Not feature-gated, and deliberately: progress is core to every revision from 2024-11-05 onward, so a Feature for it would be a fiction. What can be absent is the token — the client chose whether to open the channel, and a report without one has nowhere to go.

§Errors

GatedError::NoProgressToken when the client did not open a progress channel for this request, having written nothing.

Source

pub async fn log( &self, level: LogLevel, message: &str, ) -> Result<(), GatedError>

Emits a notifications/message log record.

The fourth of the server-side channels a handler can open, alongside progress, sampling and elicitation, and the only one that needs no token from the caller: a log record is addressed to the peer as a whole rather than to a request, so there is nothing to correlate it against. That is what makes it the odd one out here — every other gated operation has a precondition the peer establishes first, and this one has none.

Gated on Feature::Logging, and like the other gates it runs before anything is written, so a session that never negotiated logging emits nothing at all rather than a record the peer will ignore.

§Errors

GatedError::FeatureUnavailable when logging was not negotiated, having written nothing. GatedError::MalformedAnswer when the notification did not reach the peer.

Trait Implementations§

Source§

impl Clone for RequestContext

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RequestContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Prints what a post-mortem needs and nothing else.

Handwritten because the derived form would print the request id in full, and a request id in a log line is exactly what the redaction rule exists to prevent. The id goes through RequestId::redacted.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more