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() -> Self

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: AsRef<str>>(&self, required: &[S]) -> bool

Return true if the user has any of the required roles

Source

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

Create a request context with specific ID

Source

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

Set the user ID

Source

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

Set the session ID

Source

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

Set the client ID

Source

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

Add metadata

Source

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

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) -> Self

Clone with new request ID (for sub-requests)

Source

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

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) -> Self

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, ) -> Self

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) -> Self

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) -> Self

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>) -> Self

Create context for server-initiated elicitation request

Source

pub fn for_completion(completion_ref: CompletionReference) -> Self

Create context for completion request

Source

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

Create context for resource template operation

Source

pub fn for_ping(origin: PingOrigin) -> Self

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

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

impl Default for RequestContext

Source§

fn default() -> Self

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

impl RequestContextExt for RequestContext

Source§

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

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>>, ) -> Self

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

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

Get the enhanced client ID

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, 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