pub struct Context<'a> {
pub request_id: &'a RequestId,
pub progress_token: Option<&'a ProgressToken>,
pub client_caps: &'a ClientCapabilities,
pub server_caps: &'a ServerCapabilities,
pub protocol_version: ProtocolVersion,
/* private fields */
}Expand description
Request context passed to handler methods.
The context uses lifetime references to avoid 'static requirements
and Arc overhead. This enables:
- Single-threaded async without Arc overhead
!Sendtypes in handlers (important for some runtimes)- Users who need spawning can wrap in Arc themselves
Per the plan: “Request context - passed by reference, NO ’static requirement”
Fields§
§request_id: &'a RequestIdThe request ID for this operation.
progress_token: Option<&'a ProgressToken>Optional progress token for reporting progress.
client_caps: &'a ClientCapabilitiesClient capabilities negotiated during initialization.
server_caps: &'a ServerCapabilitiesServer capabilities advertised during initialization.
protocol_version: ProtocolVersionThe negotiated protocol version.
Use this to check version-specific feature availability via
methods like supports_tasks(), supports_elicitation(), etc.
Implementations§
Source§impl<'a> Context<'a>
impl<'a> Context<'a>
Sourcepub fn new(
request_id: &'a RequestId,
progress_token: Option<&'a ProgressToken>,
client_caps: &'a ClientCapabilities,
server_caps: &'a ServerCapabilities,
protocol_version: ProtocolVersion,
peer: &'a dyn Peer,
) -> Self
pub fn new( request_id: &'a RequestId, progress_token: Option<&'a ProgressToken>, client_caps: &'a ClientCapabilities, server_caps: &'a ServerCapabilities, protocol_version: ProtocolVersion, peer: &'a dyn Peer, ) -> Self
Create a new context with all required references.
Sourcepub fn with_cancellation(
request_id: &'a RequestId,
progress_token: Option<&'a ProgressToken>,
client_caps: &'a ClientCapabilities,
server_caps: &'a ServerCapabilities,
protocol_version: ProtocolVersion,
peer: &'a dyn Peer,
cancel: CancellationToken,
) -> Self
pub fn with_cancellation( request_id: &'a RequestId, progress_token: Option<&'a ProgressToken>, client_caps: &'a ClientCapabilities, server_caps: &'a ServerCapabilities, protocol_version: ProtocolVersion, peer: &'a dyn Peer, cancel: CancellationToken, ) -> Self
Create a new context with a custom cancellation token.
Sourcepub fn for_notification(
client_caps: &'a ClientCapabilities,
server_caps: &'a ServerCapabilities,
protocol_version: ProtocolVersion,
peer: &'a dyn Peer,
) -> Self
pub fn for_notification( client_caps: &'a ClientCapabilities, server_caps: &'a ServerCapabilities, protocol_version: ProtocolVersion, peer: &'a dyn Peer, ) -> Self
Create a context for handling an inbound client notification.
Notifications carry no JSON-RPC request id, so
request_id is a documented sentinel
(__notification__) that must not be treated as a real id. The
context is still outbound-capable: a hook may call
list_roots or send notifications, and those
server-to-client requests allocate their own ids via the peer.
Associate this context with a task.
Every outbound request made through it then carries
_meta["io.modelcontextprotocol/related-task"] with task_id, which
the spec requires of messages related to a task:
All requests, notifications, and responses related to a task MUST include the
io.modelcontextprotocol/related-taskkey in their_metafield […] an elicitation that a task-augmented tool call depends on MUST share the same related task ID with that tool call’s task.
Applied automatically by the task-augmented tool path; call this only when driving a task yourself.
The task this request is part of, if any.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Check if the request has been cancelled.
Sourcepub fn cancelled(&self) -> impl Future<Output = ()> + '_
pub fn cancelled(&self) -> impl Future<Output = ()> + '_
Get a future that completes when the request is cancelled.
Sourcepub const fn cancellation_token(&self) -> &CancellationToken
pub const fn cancellation_token(&self) -> &CancellationToken
Get the cancellation token for this context.
Sourcepub async fn progress(
&self,
current: f64,
total: Option<f64>,
message: Option<&str>,
) -> Result<(), McpError>
pub async fn progress( &self, current: f64, total: Option<f64>, message: Option<&str>, ) -> Result<(), McpError>
Report progress for this operation.
This sends a progress notification to the client if a progress token was provided with the request.
§Arguments
current- Current progress valuetotal- Total progress value (if known)message- Optional progress message
§Errors
Returns an error if the notification could not be sent.
Sourcepub async fn log(
&self,
level: LoggingLevel,
logger: Option<&str>,
data: Value,
) -> Result<(), McpError>
pub async fn log( &self, level: LoggingLevel, logger: Option<&str>, data: Value, ) -> Result<(), McpError>
Emit a notifications/message log to the client at level, optionally
tagged with a logger name and carrying arbitrary JSON data.
§Errors
Returns an error if the notification could not be sent.
Sourcepub async fn request(
&self,
method: impl Into<Cow<'static, str>>,
params: Option<Value>,
) -> Result<Value, McpError>
pub async fn request( &self, method: impl Into<Cow<'static, str>>, params: Option<Value>, ) -> Result<Value, McpError>
Send a request to the client and await its response.
This is the basis for server-initiated requests (e.g. elicitation, sampling). The peer assigns the request id and correlates the response; the request is aborted if this context is cancelled.
§Errors
Returns an error if the request was cancelled, the peer does not support requests, the request timed out, or the response carried a JSON-RPC error.
Sourcepub async fn elicit(
&self,
request: ElicitRequest,
) -> Result<ElicitResult, McpError>
pub async fn elicit( &self, request: ElicitRequest, ) -> Result<ElicitResult, McpError>
Request structured input from the user through the client (form-mode elicitation).
Sends an elicitation/create request and awaits the user’s response
(accept with content, decline, or cancel). This requires the client to
have declared the elicitation capability and the negotiated protocol
version to support elicitation.
§Errors
Returns an error if the client did not declare elicitation support, the negotiated protocol version predates elicitation, the request was cancelled or timed out, or the response could not be parsed.
Sourcepub async fn list_roots(&self) -> Result<Vec<Root>, McpError>
pub async fn list_roots(&self) -> Result<Vec<Root>, McpError>
Request the roots this client exposes (roots/list).
Requires the client to have declared the roots capability.
§Errors
Returns an error if the client did not declare roots support, or the request fails, times out, or the response could not be parsed.
Sourcepub async fn elicit_url(
&self,
request: UrlElicitRequest,
) -> Result<ElicitResult, McpError>
pub async fn elicit_url( &self, request: UrlElicitRequest, ) -> Result<ElicitResult, McpError>
Request a URL-mode elicitation: ask the client to have the user navigate to a URL for an out-of-band interaction (e.g. authorization or payment).
Returns the client’s ElicitResult action once the user consents to
open the URL. When the out-of-band interaction later finishes, notify the
client with ServerNotifier::elicitation_complete(elicitation_id).
Gated on the client’s elicitation.url sub-capability (which is only
declared on 2025-11-25+).
§Security
Per the MCP spec, the caller MUST use an unguessable elicitation_id
bound to a verified user identity and MUST NOT place credentials in the
URL. mcpkit provides the mechanism; associating the id with a user is the
application’s responsibility (see the session-binding helpers, #86).
§Errors
Returns an error if the negotiated protocol version does not support elicitation, the client did not declare URL-mode elicitation, or the request fails.
Sourcepub async fn create_message(
&self,
request: CreateMessageRequest,
) -> Result<CreateMessageResult, McpError>
pub async fn create_message( &self, request: CreateMessageRequest, ) -> Result<CreateMessageResult, McpError>
Request the client to run an LLM completion (sampling).
Sends a sampling/createMessage request and awaits the generated
message. This requires the client to have declared the sampling
capability (sampling is available in every protocol version).
§Errors
Returns an error if the client did not declare sampling support, the request was cancelled or timed out, or the response could not be parsed.
Trait Implementations§
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Context<'a>
impl<'a> !UnwindSafe for Context<'a>
impl<'a> Freeze for Context<'a>
impl<'a> Send for Context<'a>
impl<'a> Sync for Context<'a>
impl<'a> Unpin for Context<'a>
impl<'a> UnsafeUnpin for Context<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more