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 a single MCP request, carried through its entire lifecycle.
This struct contains essential metadata for processing, logging, and tracing a request, including unique identifiers, authentication information, and mechanisms for cancellation and server-initiated communication.
Fields§
§request_id: StringA unique identifier for the request, typically a UUID.
user_id: Option<String>The identifier for the user making the request, if authenticated.
session_id: Option<String>The identifier for the session to which this request belongs.
client_id: Option<String>The identifier for the client application making the request.
timestamp: TimestampThe timestamp when the request was received.
start_time: InstantThe Instant when request processing started, used for performance tracking.
metadata: Arc<HashMap<String, Value>>A collection of custom metadata for application-specific use cases.
cancellation_token: Option<Arc<CancellationToken>>A token that can be used to signal cancellation of the request.
Implementations§
Source§impl RequestContext
impl RequestContext
Sourcepub fn with_id(id: impl Into<String>) -> Self
pub fn with_id(id: impl Into<String>) -> Self
Creates a new RequestContext with a specific request ID.
Sourcepub fn with_user_id(self, user_id: impl Into<String>) -> Self
pub fn with_user_id(self, user_id: impl Into<String>) -> Self
Sets the user ID for this context, returning the modified context.
§Example
let ctx = RequestContext::new().with_user_id("user-123");
assert_eq!(ctx.user_id, Some("user-123".to_string()));Sourcepub fn with_session_id(self, session_id: impl Into<String>) -> Self
pub fn with_session_id(self, session_id: impl Into<String>) -> Self
Sets the session ID for this context, returning the modified context.
Sourcepub fn with_client_id(self, client_id: impl Into<String>) -> Self
pub fn with_client_id(self, client_id: impl Into<String>) -> Self
Sets the client ID for this context, returning the modified context.
Sourcepub fn with_metadata(
self,
key: impl Into<String>,
value: impl Into<Value>,
) -> Self
pub fn with_metadata( self, key: impl Into<String>, value: impl Into<Value>, ) -> Self
Adds a key-value pair to the metadata, returning the modified context.
§Example
let ctx = RequestContext::new().with_metadata("tenant", json!("acme-corp"));
assert_eq!(ctx.get_metadata("tenant"), Some(&json!("acme-corp")));Sourcepub fn get_metadata(&self, key: &str) -> Option<&Value>
pub fn get_metadata(&self, key: &str) -> Option<&Value>
Retrieves a value from the metadata by key.
Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the elapsed time since the request processing started.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Checks if the request has been marked for cancellation.
Sourcepub fn with_server_to_client(
self,
capabilities: Arc<dyn ServerToClientRequests>,
) -> Self
pub fn with_server_to_client( self, capabilities: Arc<dyn ServerToClientRequests>, ) -> Self
Sets the server-to-client requests interface for this context.
This enables tools to make server-initiated requests (sampling, elicitation, roots) with full context propagation for tracing and attribution. This is typically called by the server implementation.
Sourcepub fn with_cancellation_token(self, token: Arc<CancellationToken>) -> Self
pub fn with_cancellation_token(self, token: Arc<CancellationToken>) -> Self
Sets the cancellation token for cooperative cancellation. This is typically called by the server implementation.
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Checks if the request is from an authenticated client. This is determined by metadata set during the authentication process.
Trait Implementations§
Source§impl Clone for RequestContext
impl Clone for RequestContext
Source§fn clone(&self) -> RequestContext
fn clone(&self) -> RequestContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RequestContext
impl Debug for RequestContext
Source§impl Default for RequestContext
impl Default for RequestContext
Source§impl RequestContextExt for RequestContext
impl RequestContextExt for RequestContext
Source§fn with_enhanced_client_id(self, client_id: ClientId) -> Self
fn with_enhanced_client_id(self, client_id: ClientId) -> Self
ClientId enum, which includes the method of identification.Source§fn extract_client_id(
self,
extractor: &ClientIdExtractor,
headers: Option<&HashMap<String, String>>,
query_params: Option<&HashMap<String, String>>,
) -> Self
fn extract_client_id( self, extractor: &ClientIdExtractor, headers: Option<&HashMap<String, String>>, query_params: Option<&HashMap<String, String>>, ) -> Self
Source§fn get_enhanced_client_id(&self) -> Option<ClientId>
fn get_enhanced_client_id(&self) -> Option<ClientId>
ClientId enum from the context, if available.