pub trait Tool: Send + Sync {
// Required methods
fn definition(&self) -> ToolDefinition;
fn execute<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
ctx: &'life1 ToolCallContext,
control: &'life2 CallControl,
store: Option<&'life3 dyn ArtifactStore>,
) -> Pin<Box<dyn Future<Output = ToolResultPayload> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait;
// Provided method
fn metadata(&self) -> ToolMetadata { ... }
}Expand description
The tool port: one callable tool the model can invoke. Implementations live outside the kernel; the driver’s executor dispatches them. A tool returns only its recorded result. Handling unknown outcomes and retaining output belong to the execution consumer, not tool declarations.
§Requirements on execute
An executor may poll every call of one batch on a single task, with nothing spawned. An implementation therefore:
- must not block the thread. Blocking or CPU-heavy work runs on a
blocking thread (for example
tokio::task::spawn_blocking) and the tool awaits it; - must watch its
CallControlitself. A turn stop — cancellation or the turn deadline — drops the call future: the executor stops polling, a started call settles asToolResultStatus::UnknownOutcome. Once the executor observes the stop it polls the call no further, so a value returned after that is not recorded. Returning promptly on cancellation releases the resources the future still owns; it does not make the result observed; - must return, cleanup included, by its effective call deadline to
report
ToolResultStatus::TimedOutorToolResultStatus::Cancelled. The executor enforces a call deadline withtimeout_at, which polls the call before its own timer and drops the future once the deadline has expired. A tool that races the same deadline and returns without awaiting anything further is therefore recorded. A tool that awaits cleanup after it stops has to stop early enough for that cleanup to finish before the deadline; otherwise the future is dropped mid-cleanup and the call settles asUnknownOutcome.
Dropping the future runs on the executor’s task, so Drop must not
block and must be idempotent. A future that starts external work — a
spawned task, a child process, a remote operation — cleans it up in
Drop.
For a tool that meets these, a stop takes effect at its next yield point. A tool that holds the thread also holds its sibling calls and the executor’s own stop and timeout checks, so nothing is guaranteed for it: not cancellation, not the deadline, not a per-call timeout.
Calls of one batch are concurrent, not parallel, and their number is not bounded. A tool that needs a limit shares a semaphore across its calls.
Required Methods§
Sourcefn definition(&self) -> ToolDefinition
fn definition(&self) -> ToolDefinition
The model-facing ToolDefinition for this tool.
Sourcefn execute<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
ctx: &'life1 ToolCallContext,
control: &'life2 CallControl,
store: Option<&'life3 dyn ArtifactStore>,
) -> Pin<Box<dyn Future<Output = ToolResultPayload> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn execute<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
ctx: &'life1 ToolCallContext,
control: &'life2 CallControl,
store: Option<&'life3 dyn ArtifactStore>,
) -> Pin<Box<dyn Future<Output = ToolResultPayload> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Runs one call under the call’s CallControl, returning the
recorded result. store is the host’s ArtifactStore when one is
configured: a tool that produces media or oversized output persists
the bytes there and returns a reference; a tool that does not need
storage ignores it.
Provided Methods§
Sourcefn metadata(&self) -> ToolMetadata
fn metadata(&self) -> ToolMetadata
Non-persisted metadata carried beside Tool::definition. An executor
reads it once when binding the tool and exposes it to tool processors
through crate::ProcessorContext::tool and
crate::ProcessorContext::tool_metadata. The default is empty.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".