pub struct RequestContext {
pub request_id: String,
pub transport: TransportType,
pub user_id: Option<String>,
pub session_id: Option<String>,
pub client_id: Option<String>,
pub metadata: HashMap<String, Value>,
pub principal: Option<Principal>,
pub session: Option<Arc<dyn McpSession>>,
pub headers: Option<HashMap<String, String>>,
pub start_time: Option<Instant>,
pub cancellation_token: Option<Arc<dyn Cancellable>>,
}Expand description
Canonical per-request context.
Carries request identity, transport information, authentication principal,
arbitrary typed metadata, and — when the transport supports bidirectional
communication — an McpSession handle that enables server-to-client
operations such as sampling and elicitation.
§Thread Safety
RequestContext is Send + Sync on native targets. On WASM targets the
Send/Sync bounds are dropped (single-threaded runtime).
Fields§
§request_id: StringUnique request identifier (JSON-RPC id as string, or generated UUID).
transport: TransportTypeTransport type that received this request.
user_id: Option<String>Authenticated user identifier, if the request was authenticated.
session_id: Option<String>Session identifier for stateful transports (HTTP + session cookie, WS, Streamable HTTP, etc.).
client_id: Option<String>Client application identifier reported by the peer.
metadata: HashMap<String, Value>Rich typed metadata (headers, trace IDs, custom per-request data).
principal: Option<Principal>Authenticated principal, if auth is configured and succeeded.
session: Option<Arc<dyn McpSession>>Bidirectional session handle for server-to-client requests.
Populated by the server dispatcher before routing; None on
unidirectional transports (e.g., stateless HTTP) or when the request
is being synthesized (tests, examples).
headers: Option<HashMap<String, String>>HTTP-layer headers for HTTP/WebSocket transports.
Populated by the transport; None for non-HTTP transports. Uses
hashbrown::HashMap so it stays available in no_std / WASM builds.
start_time: Option<Instant>std only.Wall-clock moment at which the server began processing the request.
Used for elapsed() measurements and tracing spans.
cancellation_token: Option<Arc<dyn Cancellable>>std only.Cooperative-cancellation handle.
Tool bodies should check ctx.is_cancelled() during long operations
and abort early. The server wires a tokio_util::sync::CancellationToken
in here (via the Cancellable blanket impl in turbomcp-server).
Implementations§
Source§impl RequestContext
impl RequestContext
Sourcepub fn new() -> RequestContext
pub fn new() -> RequestContext
Create a new request context with a freshly generated UUID and Stdio transport.
For WASM/no_std builds the request ID is empty; call
Self::with_id explicitly to set one.
Sourcepub fn with_id_and_transport(
request_id: impl Into<String>,
transport: TransportType,
) -> RequestContext
pub fn with_id_and_transport( request_id: impl Into<String>, transport: TransportType, ) -> RequestContext
Create a context with the given ID and transport.
Sourcepub fn with_id(request_id: impl Into<String>) -> RequestContext
pub fn with_id(request_id: impl Into<String>) -> RequestContext
Create a context with an explicit request ID (Stdio transport).
Sourcepub fn stdio() -> RequestContext
pub fn stdio() -> RequestContext
Create a context for STDIO transport with a fresh UUID.
Sourcepub fn http() -> RequestContext
pub fn http() -> RequestContext
Create a context for HTTP transport with a fresh UUID.
Sourcepub fn websocket() -> RequestContext
pub fn websocket() -> RequestContext
Create a context for WebSocket transport with a fresh UUID.
Sourcepub fn tcp() -> RequestContext
pub fn tcp() -> RequestContext
Create a context for TCP transport with a fresh UUID.
Sourcepub fn unix() -> RequestContext
pub fn unix() -> RequestContext
Create a context for Unix domain socket transport with a fresh UUID.
Sourcepub fn wasm() -> RequestContext
pub fn wasm() -> RequestContext
Create a context for WASM transport with a fresh UUID.
Sourcepub fn channel() -> RequestContext
pub fn channel() -> RequestContext
Create a context for in-process channel transport with a fresh UUID.
Source§impl RequestContext
impl RequestContext
Sourcepub fn with_request_id(self, id: impl Into<String>) -> RequestContext
pub fn with_request_id(self, id: impl Into<String>) -> RequestContext
Set the request ID.
Sourcepub fn with_transport(self, transport: TransportType) -> RequestContext
pub fn with_transport(self, transport: TransportType) -> RequestContext
Set the transport type.
Sourcepub fn with_user_id(self, user_id: impl Into<String>) -> RequestContext
pub fn with_user_id(self, user_id: impl Into<String>) -> RequestContext
Set the authenticated user ID.
Sourcepub fn with_session_id(self, session_id: impl Into<String>) -> RequestContext
pub fn with_session_id(self, session_id: impl Into<String>) -> RequestContext
Set the session ID.
Sourcepub fn with_client_id(self, client_id: impl Into<String>) -> RequestContext
pub fn with_client_id(self, client_id: impl Into<String>) -> RequestContext
Set the client ID.
Sourcepub fn with_principal(self, principal: Principal) -> RequestContext
pub fn with_principal(self, principal: Principal) -> RequestContext
Set the authenticated principal.
Sourcepub fn with_metadata(
self,
key: impl Into<String>,
value: impl Into<Value>,
) -> RequestContext
pub fn with_metadata( self, key: impl Into<String>, value: impl Into<Value>, ) -> RequestContext
Attach a metadata key/value pair.
Accepts any value convertible to serde_json::Value, so string
literals, numbers, and structured data all work.
Sourcepub fn with_session(self, session: Arc<dyn McpSession>) -> RequestContext
pub fn with_session(self, session: Arc<dyn McpSession>) -> RequestContext
Attach a bidirectional session handle.
Sourcepub fn with_headers(self, headers: HashMap<String, String>) -> RequestContext
pub fn with_headers(self, headers: HashMap<String, String>) -> RequestContext
Attach HTTP headers (case-sensitive keys; header does
case-insensitive lookup).
Sourcepub fn with_start_time(self, start: Instant) -> RequestContext
Available on crate feature std only.
pub fn with_start_time(self, start: Instant) -> RequestContext
std only.Mark the request start time.
Sourcepub fn with_cancellation_token(
self,
token: Arc<dyn Cancellable>,
) -> RequestContext
Available on crate feature std only.
pub fn with_cancellation_token( self, token: Arc<dyn Cancellable>, ) -> RequestContext
std only.Attach a cancellation handle.
Source§impl RequestContext
impl RequestContext
Sourcepub fn insert_metadata(
&mut self,
key: impl Into<String>,
value: impl Into<Value>,
)
pub fn insert_metadata( &mut self, key: impl Into<String>, value: impl Into<Value>, )
Mutable metadata insert.
Sourcepub fn set_principal(&mut self, principal: Principal)
pub fn set_principal(&mut self, principal: Principal)
Mutable principal setter.
Sourcepub fn clear_principal(&mut self)
pub fn clear_principal(&mut self)
Clear the authenticated principal.
Sourcepub fn set_session(&mut self, session: Arc<dyn McpSession>)
pub fn set_session(&mut self, session: Arc<dyn McpSession>)
Mutable session setter.
Source§impl RequestContext
impl RequestContext
Sourcepub fn request_id(&self) -> &str
pub fn request_id(&self) -> &str
Request ID.
Sourcepub fn has_request_id(&self) -> bool
pub fn has_request_id(&self) -> bool
Returns true when a non-empty request ID is set.
Sourcepub fn transport(&self) -> TransportType
pub fn transport(&self) -> TransportType
Transport type.
Sourcepub fn session_id(&self) -> Option<&str>
pub fn session_id(&self) -> Option<&str>
Session ID, if present.
Sourcepub fn get_metadata(&self, key: &str) -> Option<&Value>
pub fn get_metadata(&self, key: &str) -> Option<&Value>
Rich metadata lookup.
Sourcepub fn get_metadata_str(&self, key: &str) -> Option<&str>
pub fn get_metadata_str(&self, key: &str) -> Option<&str>
Rich metadata lookup, downcast to &str for string values.
Sourcepub fn has_metadata(&self, key: &str) -> bool
pub fn has_metadata(&self, key: &str) -> bool
Returns true when a metadata key is set.
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Returns true when the request is authenticated.
A request is considered authenticated when it has either a principal
or a user_id. Callers with richer auth semantics should read the
principal directly.
Sourcepub fn subject(&self) -> Option<&str>
pub fn subject(&self) -> Option<&str>
Authenticated subject (principal subject, falling back to user_id).
Sourcepub fn session(&self) -> Option<&Arc<dyn McpSession>>
pub fn session(&self) -> Option<&Arc<dyn McpSession>>
Session handle, if attached.
Sourcepub fn has_session(&self) -> bool
pub fn has_session(&self) -> bool
Returns true when a bidirectional session is attached.
Sourcepub fn headers(&self) -> Option<&HashMap<String, String>>
pub fn headers(&self) -> Option<&HashMap<String, String>>
All HTTP headers, if the transport captured any.
Sourcepub fn elapsed(&self) -> Option<Duration>
Available on crate feature std only.
pub fn elapsed(&self) -> Option<Duration>
std only.Elapsed time since the request started (if start_time was set).
Sourcepub fn is_cancelled(&self) -> bool
Available on crate feature std only.
pub fn is_cancelled(&self) -> bool
std only.Returns true when the request has been marked for cancellation.
Source§impl RequestContext
impl RequestContext
Sourcepub async fn sample(
&self,
request: CreateMessageRequest,
) -> Result<CreateMessageResult, McpError>
pub async fn sample( &self, request: CreateMessageRequest, ) -> Result<CreateMessageResult, McpError>
Request LLM sampling from the connected client.
Requires a bidirectional session; returns
McpError::capability_not_supported on unidirectional transports.
Sourcepub async fn elicit_form(
&self,
message: impl Into<String>,
schema: Value,
) -> Result<ElicitResult, McpError>
pub async fn elicit_form( &self, message: impl Into<String>, schema: Value, ) -> Result<ElicitResult, McpError>
Request form-based user input from the client.
Trait Implementations§
Source§impl Clone for RequestContext
impl Clone for RequestContext
Source§fn clone(&self) -> RequestContext
fn clone(&self) -> RequestContext
1.0.0 (const: unstable) · 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§fn default() -> RequestContext
fn default() -> 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
client_id from a structured super::client::ClientId and record
the authentication method + authenticated flag in metadata.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
Self::with_enhanced_client_id.Source§fn get_enhanced_client_id(&self) -> Option<ClientId>
fn get_enhanced_client_id(&self) -> Option<ClientId>
super::client::ClientId from the context.