pub struct Ctx { /* private fields */ }Expand description
Context of the request being handled. Declare a Ctx parameter in a tool
to receive it; it is excluded from the tool’s input schema.
Cloning is cheap.
Implementations§
Source§impl Ctx
impl Ctx
Sourcepub fn client_supports_apps(&self) -> bool
pub fn client_supports_apps(&self) -> bool
Whether the client can render MCP Apps interfaces (it declared the
extension with the text/html;profile=mcp-app MIME type). A tool
can return a richer text answer when it cannot.
Source§impl Ctx
impl Ctx
Sourcepub fn request_id(&self) -> &RequestId
pub fn request_id(&self) -> &RequestId
The JSON-RPC id of the request.
Sourcepub fn protocol_version(&self) -> &str
pub fn protocol_version(&self) -> &str
The protocol version the client used for this request.
Sourcepub fn client_capabilities(&self) -> &ClientCapabilities
pub fn client_capabilities(&self) -> &ClientCapabilities
The capabilities the client declared for this request.
Sourcepub fn client_info(&self) -> Option<&Implementation>
pub fn client_info(&self) -> Option<&Implementation>
The client’s self-reported identity. Informational only: never use it for security decisions.
Sourcepub fn progress_token(&self) -> Option<&ProgressToken>
pub fn progress_token(&self) -> Option<&ProgressToken>
The progress token, when the client asked for progress notifications.
Sourcepub fn client_supports_extension(&self, id: &str) -> bool
pub fn client_supports_extension(&self, id: &str) -> bool
Whether the client declared support for an extension.
Sourcepub fn cancellation(&self) -> &CancellationToken
pub fn cancellation(&self) -> &CancellationToken
A token cancelled when the client cancels the request (stdio
notifications/cancelled, or the HTTP response stream closing).
Long-running tools should stop work when it fires.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Whether the request has been cancelled.
Sourcepub fn transport(&self) -> &TransportContext
pub fn transport(&self) -> &TransportContext
Values attached by the transport.
Source§impl Ctx
impl Ctx
Sourcepub fn input_round(&self) -> u32
pub fn input_round(&self) -> u32
The number of input rounds completed before this attempt: 0 on the
first call, 1 after the client answered once, and so on.
Sourcepub fn input_response<T: DeserializeOwned>(
&self,
key: &str,
) -> Result<Option<T>, ToolError>
pub fn input_response<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, ToolError>
The client’s answer for key, decoded as T, if it sent one in this
round or an earlier one.
§Errors
Returns an invalid-params error (-32602) if the answer does not
decode as T.
Sourcepub async fn elicit<T: DeserializeOwned + JsonSchema>(
&self,
key: &str,
message: impl Into<String>,
) -> Result<T, ToolError>
pub async fn elicit<T: DeserializeOwned + JsonSchema>( &self, key: &str, message: impl Into<String>, ) -> Result<T, ToolError>
Asks the user for structured input with a form generated from T
(a flat struct of strings, numbers, booleans and enums). Declining or
cancelling becomes a ToolError::user; use
elicit_with_action to handle them.
key identifies the question across rounds and must be unique within
the handler. Several questions can be asked in one round by awaiting
them together (for example with tokio::join!).
§Errors
Fails with an “input required” error when the answer is not available
yet (propagate it with ?), -32021 if the client does not support
form elicitation, and -32602 if the answer is malformed.
Sourcepub async fn elicit_with_action<T: DeserializeOwned + JsonSchema>(
&self,
key: &str,
message: impl Into<String>,
) -> Result<Elicitation<T>, ToolError>
pub async fn elicit_with_action<T: DeserializeOwned + JsonSchema>( &self, key: &str, message: impl Into<String>, ) -> Result<Elicitation<T>, ToolError>
Sourcepub async fn elicit_with_schema(
&self,
key: &str,
message: impl Into<String>,
requested_schema: Value,
) -> Result<Elicitation<Map<String, Value>>, ToolError>
pub async fn elicit_with_schema( &self, key: &str, message: impl Into<String>, requested_schema: Value, ) -> Result<Elicitation<Map<String, Value>>, ToolError>
Like elicit_with_action, with a
hand-written requestedSchema (for default values, titled enums or
multi-select fields, which a derived schema cannot express). The
accepted content comes back as JSON, for the handler to validate.
§Errors
See elicit.
Sourcepub async fn sample(
&self,
key: &str,
params: CreateMessageRequestParams,
) -> Result<CreateMessageResult, ToolError>
👎Deprecated: Sampling is deprecated as of protocol version 2026-07-28 (SEP-2577).
pub async fn sample( &self, key: &str, params: CreateMessageRequestParams, ) -> Result<CreateMessageResult, ToolError>
Sampling is deprecated as of protocol version 2026-07-28 (SEP-2577).
Asks the client to sample from a language model.
§Errors
Fails with an “input required” error when the answer is not available
yet (propagate it with ?), -32021 if the client does not support
sampling, and -32602 if the answer is malformed.
Sourcepub async fn list_roots(&self, key: &str) -> Result<ListRootsResult, ToolError>
👎Deprecated: Roots are deprecated as of protocol version 2026-07-28 (SEP-2577).
pub async fn list_roots(&self, key: &str) -> Result<ListRootsResult, ToolError>
Roots are deprecated as of protocol version 2026-07-28 (SEP-2577).
Asks the client for its filesystem roots.
§Errors
Fails with an “input required” error when the answer is not available
yet (propagate it with ?), -32021 if the client does not support
roots, and -32602 if the answer is malformed.
Source§impl Ctx
impl Ctx
Sourcepub fn progress(&self, progress: f64, total: impl Into<Option<f64>>)
pub fn progress(&self, progress: f64, total: impl Into<Option<f64>>)
Reports progress to the client (notifications/progress), when it
asked for it with a progressToken; otherwise does nothing.
Never blocks: if the client reads slowly, only the latest update is
kept, and it arrives before the result. progress must increase from
one call to the next (smaller values are ignored); total may be
omitted with None.
for (i, file) in files.iter().enumerate() {
ctx.progress(i as f64, files.len() as f64);
load(file).await;
}Sourcepub fn progress_message(
&self,
progress: f64,
total: impl Into<Option<f64>>,
message: impl AsRef<str>,
)
pub fn progress_message( &self, progress: f64, total: impl Into<Option<f64>>, message: impl AsRef<str>, )
Like progress, with a human-readable message.
Sourcepub fn wants_progress(&self) -> bool
pub fn wants_progress(&self) -> bool
Whether the client asked for progress notifications.
Sourcepub fn log(&self, level: LoggingLevel, data: impl Serialize)
👎Deprecated: Logging is deprecated as of protocol version 2026-07-28 (SEP-2577); log to stderr with tracing instead.
pub fn log(&self, level: LoggingLevel, data: impl Serialize)
Logging is deprecated as of protocol version 2026-07-28 (SEP-2577); log to stderr with tracing instead.
Sends a log message to the client (notifications/message), when it
asked for messages at this level or above; otherwise does nothing.
Never blocks; messages beyond the buffer are dropped.
Do not include secrets or internal details: the client sees data.
For the server’s own logs, use tracing (stderr).