pub struct ToolContext<'a> {
pub tool_call_id: &'a str,
pub args: Value,
pub llm: Option<&'a AgentLlmConfig>,
pub provider: Option<Arc<dyn LlmProvider>>,
pub working_dir: Option<&'a PathBuf>,
/* private fields */
}Expand description
Per-invocation context passed to Tool::execute methods.
Carries everything a tool needs: the call ID for building results, raw JSON arguments, cancellation support, and the working directory for path resolution.
§Key fields
| Field | Purpose |
|---|---|
tool_call_id | Echo into ToolResult::tool_call_id so results match calls |
args | Raw JSON arguments sent by the LLM — parse with parse_args |
working_dir | Chat workspace root; relative paths resolve against this |
Fields§
§tool_call_id: &'a strThe unique ID for this tool call. Echo into ToolResult::tool_call_id when returning.
args: ValueRaw JSON arguments from the LLM. Use parse_args to deserialize.
llm: Option<&'a AgentLlmConfig>Current parent configuration, including its cache routing identity.
provider: Option<Arc<dyn LlmProvider>>Current provider for tools that delegate model requests.
working_dir: Option<&'a PathBuf>Working directory for the current agent (typically chat workspace). Relative paths resolve here.
Implementations§
Source§impl<'a> ToolContext<'a>
impl<'a> ToolContext<'a>
Sourcepub fn new(
tool_call_id: &'a str,
args: Value,
working_dir: Option<&'a PathBuf>,
cancel_token: &'a CancellationToken,
) -> Self
pub fn new( tool_call_id: &'a str, args: Value, working_dir: Option<&'a PathBuf>, cancel_token: &'a CancellationToken, ) -> Self
Create a new tool execution context.
Sourcepub fn with_llm(self, llm: &'a AgentLlmConfig) -> Self
pub fn with_llm(self, llm: &'a AgentLlmConfig) -> Self
Pass the current agent configuration to the tool.
Sourcepub fn with_provider(self, provider: Arc<dyn LlmProvider>) -> Self
pub fn with_provider(self, provider: Arc<dyn LlmProvider>) -> Self
Pass the current provider to tools that delegate model requests.
Sourcepub fn with_progress(self, tx: UnboundedSender<String>) -> Self
pub fn with_progress(self, tx: UnboundedSender<String>) -> Self
Attach a progress channel for streaming incremental output.
Sourcepub fn with_background_request(self, request: &'a BackgroundRequest) -> Self
pub fn with_background_request(self, request: &'a BackgroundRequest) -> Self
Attach the user’s control for backgrounding this running call.
pub fn background_request(&self) -> Option<&BackgroundRequest>
Sourcepub fn progress_sender(&self) -> Option<UnboundedSender<String>>
pub fn progress_sender(&self) -> Option<UnboundedSender<String>>
Clone the live output destination when execution moves to an owned task.
Sourcepub fn progress(&self, line: &str)
pub fn progress(&self, line: &str)
Send a progress line through the channel (no-op if no channel is set).
Sourcepub fn parse_args<T: DeserializeOwned>(&self, tool_name: &str) -> Result<T>
pub fn parse_args<T: DeserializeOwned>(&self, tool_name: &str) -> Result<T>
Parse the raw JSON arguments into a typed value without consuming them.
The tool name is included in errors, along with the deserialization error. This does not validate the tool’s JSON schema, so keep the schema in sync with the argument type.
use std::path::PathBuf;
use flashmind_types::tool::ToolContext;
use serde::Deserialize;
use serde_json::json;
use tokio_util::sync::CancellationToken;
#[derive(Deserialize)]
struct ReadArgs {
path: PathBuf,
}
let cancel = CancellationToken::new();
let context = ToolContext::new("call", json!({"path": "src/main.rs"}), None, &cancel);
let args: ReadArgs = context.parse_args("read")?;
assert_eq!(args.path, PathBuf::from("src/main.rs"));Sourcepub fn cancel_token(&self) -> &CancellationToken
pub fn cancel_token(&self) -> &CancellationToken
Access the cancellation token for cooperative cancellation checks.
For long-running operations, prefer child_token to get
a derived token that can be passed to async sub-operations.
Sourcepub fn child_token(&self) -> CancellationToken
pub fn child_token(&self) -> CancellationToken
Create a child cancellation token. Cancels when the parent turn is cancelled. Use this for spawned tasks so they’re automatically cancelled when the user aborts.