pub struct McpContext {
pub client_name: Option<String>,
pub client_version: Option<String>,
pub session_id: Option<String>,
pub auth_claims: Option<String>,
pub progress_token: Option<String>,
/* private fields */
}Expand description
Per-request context passed to tool handlers registered via
McpServer::tool_with_context — caller identity and session info that a
plain Fn(&str) -> ... tool handler has no way to see.
Constructed in McpServer::execute from the current request’s headers
plus whatever clientInfo was recorded for this session at initialize
time (see McpServer::handle_request_with_context).
Fields§
§client_name: Option<String>clientInfo.name sent in this session’s initialize call, if the
client sent one and this request carries a recognized Mcp-Session-Id.
client_version: Option<String>clientInfo.version sent in this session’s initialize call, under
the same conditions as client_name.
session_id: Option<String>The Mcp-Session-Id header on this request, if present — the value
the server minted and returned in the initialize response header
for this session (see the module docs’ Sessions section).
auth_claims: Option<String>Verified JWT claims as a JSON string. Not populated by anything in
this crate yet — reserved for a future JWT-auth integration
(MCP_TODO.md TODO-11/TODO-13); always None today.
progress_token: Option<String>The raw JSON value of params._meta.progressToken from the
triggering tools/call request, if the client sent one — a spec
string | number, so this is stored pre-rendered (already correctly
quoted if it’s a string) rather than decoded, and spliced back
verbatim by Self::report_progress. None for anything other than
a tools/call whose caller asked for progress updates.
Implementations§
Source§impl McpContext
impl McpContext
Sourcepub fn report_progress(
&self,
progress: f64,
total: Option<f64>,
message: Option<&str>,
)
pub fn report_progress( &self, progress: f64, total: Option<f64>, message: Option<&str>, )
Push a notifications/progress event over the SSE channel for this
request’s progressToken, if the client asked for progress updates
(params._meta.progressToken on the triggering tools/call) and
this context was built through a live McpServer (via execute(),
not a bare McpContext { .. } — see the sse_clients field doc).
Silently does nothing in either case — a handler doesn’t need to branch on whether progress reporting is actually wired up before calling this; it’s always safe to call.
total and message are both optional, matching the spec’s
notifications/progress shape: {"progressToken":...,"progress":..., "total":...,"message":"..."} (with total/message omitted when not
given here).
use rust_web_server::mcp::{McpContent, McpServer};
let server = McpServer::new("my-server", "1.0")
.tool_with_context("long_job", "Do something slow", "{}", |ctx, _args| {
ctx.report_progress(0.0, Some(100.0), Some("starting"));
// ... do work ...
ctx.report_progress(100.0, Some(100.0), Some("done"));
Ok(McpContent::text("done"))
});Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
true if the client sent notifications/cancelled referencing this
tools/call’s request id.
Rust cannot forcibly interrupt a running synchronous closure — same
limitation crate::timeout::with_timeout documents — so this is
cooperative cancellation: nothing happens automatically. A handler
that does its own iterative work (processing a batch of items, say)
can check this between steps and return early; a handler that never
checks it runs to completion regardless, exactly as before this
existed.
Always false if the client never sent a cancellation, if this
wasn’t dispatched as a tools/call (the only method cancellation
applies to), or if ctx was built without a live server behind it.
use rust_web_server::mcp::{McpContent, McpServer};
let server = McpServer::new("my-server", "1.0")
.tool_with_context("process_batch", "Process many items", "{}", |ctx, _args| {
for i in 0..1_000_000 {
if ctx.is_cancelled() {
return Err("cancelled by client".to_string());
}
// ... process item i ...
}
Ok(McpContent::text("done"))
});Trait Implementations§
Source§impl Clone for McpContext
impl Clone for McpContext
Source§fn clone(&self) -> McpContext
fn clone(&self) -> McpContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more