pub struct ToolContext { /* private fields */ }Expand description
Context passed to tool executions.
MCP-backed tool calls receive a request-scoped cancellation token through this
context. Non-MCP callers that construct ToolContext::default or
ToolContext::new receive a never-cancelled context so existing direct,
native, and NAPI entrypoints remain source-compatible.
Tool authors should prefer these helpers instead of wiring cancellation by hand:
ToolContext::run_cancellablefor a single async operation that should abort promptly when the request is cancelled.ToolContext::cancelledwhen cancellation must participate in a largertokio::select!.ToolContext::is_cancelledfor quick boundary checks before starting more work.ToolContext::cancellation_tokenwhen an owned token clone must cross.awaitboundaries inside aBoxFuture<'static>implementation.
Cancellation maps to ToolError::Cancelled. Returning that error means the tool
stopped because the caller cancelled the request, not because the tool failed.
For subprocess-managing tools, request cancellation should trigger explicit cleanup before returning. Dropping a future remains a backstop, not the primary cooperative cleanup path.
Implementations§
Source§impl ToolContext
impl ToolContext
Sourcepub fn with_cancel(cancel: CancellationToken) -> Self
pub fn with_cancel(cancel: CancellationToken) -> Self
Create a context backed by the supplied cancellation token.
Sourcepub fn cancellation_token(&self) -> CancellationToken
pub fn cancellation_token(&self) -> CancellationToken
Clone the request cancellation token for use across .await boundaries.
Sourcepub fn cancelled(&self) -> WaitForCancellationFutureOwned
pub fn cancelled(&self) -> WaitForCancellationFutureOwned
Return an owned future that resolves when the request is cancelled.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Check whether the request has already been cancelled.
Trait Implementations§
Source§impl Clone for ToolContext
impl Clone for ToolContext
Source§fn clone(&self) -> ToolContext
fn clone(&self) -> ToolContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more