RequestContext

Struct RequestContext 

Source
pub struct RequestContext {
    pub request_id: String,
    pub user_id: Option<String>,
    pub session_id: Option<String>,
    pub client_id: Option<String>,
    pub timestamp: Timestamp,
    pub start_time: Instant,
    pub metadata: Arc<HashMap<String, Value>>,
    pub cancellation_token: Option<Arc<CancellationToken>>,
    /* private fields */
}
Expand description

Context information for request processing

Fields§

§request_id: String

Unique request identifier

§user_id: Option<String>

User identifier (if authenticated)

§session_id: Option<String>

Session identifier

§client_id: Option<String>

Client identifier

§timestamp: Timestamp

Request timestamp

§start_time: Instant

Request start time for performance tracking

§metadata: Arc<HashMap<String, Value>>

Custom metadata

§cancellation_token: Option<Arc<CancellationToken>>

Cancellation token

Implementations§

Source§

impl RequestContext

Source

pub fn new() -> RequestContext

Create a new request context

§Examples
use turbomcp_core::RequestContext;

let ctx = RequestContext::new();
assert!(!ctx.request_id.is_empty());
assert!(ctx.user_id.is_none());
assert!(ctx.session_id.is_none());
assert!(ctx.metadata.is_empty());
Source

pub fn is_authenticated(&self) -> bool

Return true if the request is authenticated according to context metadata

§Examples
use turbomcp_core::RequestContext;

let ctx = RequestContext::new()
    .with_metadata("authenticated", true);
assert!(ctx.is_authenticated());

let unauth_ctx = RequestContext::new();
assert!(!unauth_ctx.is_authenticated());
Source

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

Return user id if present

Source

pub fn roles(&self) -> Vec<String>

Return roles from auth.roles metadata, if present

Source

pub fn has_any_role<S>(&self, required: &[S]) -> bool
where S: AsRef<str>,

Return true if the user has any of the required roles

Source

pub fn with_id(id: impl Into<String>) -> RequestContext

Create a request context with specific ID

Source

pub fn with_user_id(self, user_id: impl Into<String>) -> RequestContext

Set the user ID

Source

pub fn with_session_id(self, session_id: impl Into<String>) -> RequestContext

Set the session ID

Source

pub fn with_client_id(self, client_id: impl Into<String>) -> RequestContext

Set the client ID

Source

pub fn with_metadata( self, key: impl Into<String>, value: impl Into<Value>, ) -> RequestContext

Add metadata

Source

pub fn with_cancellation_token( self, token: Arc<CancellationToken>, ) -> RequestContext

Set cancellation token

Source

pub fn elapsed(&self) -> Duration

Get elapsed time since request started

Source

pub fn is_cancelled(&self) -> bool

Check if request is cancelled

Source

pub fn get_metadata(&self, key: &str) -> Option<&Value>

Get metadata value

Source

pub fn derive(&self) -> RequestContext

Clone with new request ID (for sub-requests)

Source

pub fn with_elicitation_context( self, context: ElicitationContext, ) -> RequestContext

Add elicitation context for server-initiated user input

Source

pub fn elicitation_context(&self) -> Option<ElicitationContext>

Get elicitation context if present

Source

pub fn with_completion_context( self, context: CompletionContext, ) -> RequestContext

Add completion context for autocompletion requests

Source

pub fn completion_context(&self) -> Option<CompletionContext>

Get completion context if present

Source

pub fn with_resource_template_context( self, context: ResourceTemplateContext, ) -> RequestContext

Add resource template context for parameterized resources

Source

pub fn resource_template_context(&self) -> Option<ResourceTemplateContext>

Get resource template context if present

Source

pub fn with_ping_context(self, context: PingContext) -> RequestContext

Add ping context for health monitoring

Source

pub fn ping_context(&self) -> Option<PingContext>

Get ping context if present

Source

pub fn with_bidirectional_context( self, context: BidirectionalContext, ) -> RequestContext

Add bidirectional communication context

Source

pub fn bidirectional_context(&self) -> Option<BidirectionalContext>

Get bidirectional context if present

Source

pub fn is_server_initiated(&self) -> bool

Check if this is a server-initiated request

Source

pub fn is_client_initiated(&self) -> bool

Check if this is a client-initiated request (default/traditional)

Source

pub fn communication_direction(&self) -> CommunicationDirection

Get communication direction

Source

pub fn for_elicitation(schema: Value, prompt: Option<String>) -> RequestContext

Create context for server-initiated elicitation request

Source

pub fn for_completion(completion_ref: CompletionReference) -> RequestContext

Create context for completion request

Source

pub fn for_resource_template( template_name: String, uri_template: String, ) -> RequestContext

Create context for resource template operation

Source

pub fn for_ping(origin: PingOrigin) -> RequestContext

Create context for ping request

Trait Implementations§

Source§

impl Clone for RequestContext

Source§

fn clone(&self) -> RequestContext

Returns a duplicate of the value. Read more
1.0.0 · 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<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for RequestContext

Source§

fn default() -> RequestContext

Returns the “default value” for a type. Read more
Source§

impl RequestContextExt for RequestContext

Source§

fn with_enhanced_client_id(self, client_id: ClientId) -> RequestContext

Set client ID using ClientId enum
Source§

fn extract_client_id( self, extractor: &ClientIdExtractor, headers: Option<&HashMap<String, String>>, query_params: Option<&HashMap<String, String>>, ) -> RequestContext

Extract and set client ID from headers and query params
Source§

fn get_enhanced_client_id(&self) -> Option<ClientId>

Get the enhanced client ID
Source§

impl SamplingExt for RequestContext

Source§

fn create_message<'life0, 'async_trait>( &'life0 self, request: CreateMessageRequest, ) -> Pin<Box<dyn Future<Output = ServerResult<CreateMessageResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Send a sampling/createMessage request to the client
Source§

fn elicit<'life0, 'async_trait>( &'life0 self, request: ElicitRequest, ) -> Pin<Box<dyn Future<Output = ServerResult<ElicitResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Send an elicitation request to the client for user input
Source§

fn list_roots<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ServerResult<ListRootsResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

List client’s root capabilities

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<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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,