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

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