Trait Tool
pub trait Tool:
Send
+ Sync
+ 'static {
Show 13 methods
// Required methods
fn spec(&self) -> ToolSpec;
fn call<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 ToolCtx,
args: Value,
) -> Pin<Box<dyn Future<Output = Result<ToolOutput, ToolError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
// Provided methods
fn required_state(&self) -> Vec<StateKey> { ... }
fn events(&self) -> Vec<EventSpec> { ... }
fn asks_user(&self) -> bool { ... }
fn paged(&self) -> bool { ... }
fn time_limit(&self) -> Option<Duration> { ... }
fn retry_policy(&self) -> Option<RetryPolicy> { ... }
fn step_style(&self) -> StepStyle { ... }
fn access(&self, args: &Value) -> ToolAccess { ... }
fn schema_is_foreign(&self) -> bool { ... }
fn read_answer(
&self,
call_id: &str,
args: &Value,
ui: Option<&Value>,
payload: &Value,
) -> AnswerCheck { ... }
fn poll_remote<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 ToolCtx,
task: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<RemotePoll, ToolError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait { ... }
}Expand description
A capability the model may call.
The agent runs every call inside a journaled step: once a call has
returned, its result is recorded and never recomputed, even across a crash
or a change of worker. A call that dies before it is recorded (a crash
mid-call, or ToolError::Transient) runs again, and so does every call
of the same model turn that started in the failed attempt: the calls of a
turn run at the same time, up to the agent’s limit
(LlmAgentBuilder::max_parallel), except where
access says they may not. A tool with side effects must
therefore be safe to retry: idempotent, or guarded by a key derived from
ToolCtx::call_id.
Required Methods§
fn spec(&self) -> ToolSpec
fn spec(&self) -> ToolSpec
Name, description and JSON Schema of the arguments, as shown to the model. The name must be unique among the agent’s tools and stable.
fn call<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 ToolCtx,
args: Value,
) -> Pin<Box<dyn Future<Output = Result<ToolOutput, ToolError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn call<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 ToolCtx,
args: Value,
) -> Pin<Box<dyn Future<Output = Result<ToolOutput, ToolError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Run the tool. args is the model’s argument object (already parsed as
JSON, not validated against the schema: validate it and report bad
input as ToolOutput::error so the model can correct itself).
Runs inside a journaled step; must be safe to retry if it fails before being recorded.
Provided Methods§
fn required_state(&self) -> Vec<StateKey>
fn required_state(&self) -> Vec<StateKey>
The shared state this tool reads with ToolCtx::state, by type.
LlmAgentBuilder::try_build
checks that the agent was given each one (with
LlmAgentBuilder::state), so a
missing dependency fails at startup and not in the middle of a run.
The default is none, which is what a tool written before this method
existed declares.
fn events(&self) -> Vec<EventSpec>
fn events(&self) -> Vec<EventSpec>
The custom events this tool emits with ToolCtx::emit_event (ADR 0050, slice H5): each
is declared here, with its name, version, schema and who may see it, before the tool can emit
it. A name outside ns.name, or in a core namespace (tool, run, adam, …), is a build
error, as is a second declaration of a name that differs; a tool may emit only what it (or the
agent) declared. The default declares none, which is what a tool written before custom events
existed declares. A tool that wraps another (see ToolSet::wrap) must
forward it, as it forwards required_state: one that does not hides
the events of the tool it wraps, and the wrapped tool’s emits are refused as undeclared.
fn asks_user(&self) -> bool
fn asks_user(&self) -> bool
Whether this tool can end a call with ToolError::NeedsInput: it asks the person who
started the run a question, and the run parks until someone answers.
Declare it (true) on every tool that can: the loop reads it to run such a call alone.
Whether the person can be asked is decided per run (ADR 0045 decision 9): a root run
asks, a round of a kept-open session a client shows asks, and any other child run is
answered that nobody can answer it (NOT_ASKABLE) instead of parking for ever. The default
is false, which is what a tool written before this method existed declares.
#[tool(asks_user)] and FnTool::asking_user set it.
It is a declaration, not a guard: nothing stops a tool that says false from returning
NeedsInput anyway, and the run would park. A tool that wraps another (see
ToolSet::wrap) must forward it, as it forwards
required_state.
fn paged(&self) -> bool
fn paged(&self) -> bool
Whether this tool’s results are pages in the paging convention: the tool
takes offset and limit and ends every result with one final line that says where the
next page starts. The loop’s cap on a result (64 KiB) then tells the model to call again
with a later offset. The default is false, and it is a declaration of the tool, never
inferred from its schema: a tool that merely has an offset argument (an MCP tool, say) is
not paged. #[tool(paged)] sets it. A tool that wraps another must forward it.
fn time_limit(&self) -> Option<Duration>
fn time_limit(&self) -> Option<Duration>
The longest one call of this tool may run. Past it the agent fires the call’s
ToolCtx::cancelled token, gives the tool TOOL_STOP_GRACE to stop, drops it, and
answers the model with an error result that says the call did not finish (the result is
journaled, so a replay never waits). None, the default, is DEFAULT_TOOL_TIME_LIMIT.
Declare it when a call can reasonably take longer (a build, a deployment’s command limit) or
must be quicker. A tool that waits on something else, a question or a child run, returns at
once and is not affected. A tool that wraps another (see
ToolSet::wrap) must forward it, as it forwards
required_state.
fn retry_policy(&self) -> Option<RetryPolicy>
fn retry_policy(&self) -> Option<RetryPolicy>
The retry policy of this tool’s retryable failures (ToolError::Transient,
ToolError::Retryable), counted under the key tool:<name> apart from the other steps
of the run (ADR 0061). None, the default, leaves them to the runtime’s policy. A policy
set for the tool by name (LlmAgentBuilder::tool_retry,
an agent folder’s retry:) wins over this one. #[tool(retry(attempts = 3, backoff = "1s"))]
writes it for a function. A tool that wraps another (see
ToolSet::wrap) must forward it, as it forwards
time_limit.
fn step_style(&self) -> StepStyle
fn step_style(&self) -> StepStyle
How a call of this tool is drawn as a step ([RunEvent::Step]): its kind, the label (the
tool’s name when it says none) and an icon. The default is a plain tool: kind tool, the
name as the label, no icon. A tool that hands its work to another agent says
StepKind::Subagent here, so that the steps it reports nest under it
(#[tool(step = "subagent", label = "Helper", icon = "agent")] writes it for a function).
A tool that wraps another (see ToolSet::wrap) must forward it, as
it forwards required_state.
fn access(&self, args: &Value) -> ToolAccess
fn access(&self, args: &Value) -> ToolAccess
What a call with these args shares with the other calls of its model turn: the loop runs
the calls of a turn at the same time and keeps the model’s order only between calls that
conflict (ToolAccess). args are the call’s arguments as the model made them, so the
answer is a function of the recorded turn and the same on every replay.
The default is ToolAccess::Alone: the call runs by itself, after every call before it,
which is what a tool that declares nothing needs, because adam cannot know what it shares.
Declare what the tool really uses (#[tool(parallel = "read", path = "path")] writes it
for a function, FnTool::with_access for a closure). A tool
whose asks_user is true, the session tools, and a tool that can park
the run on a child or a remote task run alone whatever they say here.
A tool that wraps another (see ToolSet::wrap) must forward it, as
it forwards required_state: one that does not makes the tool
Alone, which is safe.
fn schema_is_foreign(&self) -> bool
fn schema_is_foreign(&self) -> bool
Whether the tool’s schema is another system’s (an MCP server’s) and not this program’s own.
A property called after_previous in such a schema is that system’s: the agent keeps it and
never strips it, where a schema of its own with that name is a build error
(BuildError::ReservedArgument). The default is
false. A tool that wraps another must forward it.
fn read_answer(
&self,
call_id: &str,
args: &Value,
ui: Option<&Value>,
payload: &Value,
) -> AnswerCheck
fn read_answer( &self, call_id: &str, args: &Value, ui: Option<&Value>, payload: &Value, ) -> AnswerCheck
Whether the answer to this tool’s own question may be acted on: the loop asks before it hands the answer to the model.
The run is parked on a ToolError::NeedsInput of this tool and a message has arrived.
call_id and args are the call that asked, as the model made it (the history already
holds them, so the question needs no field of its own to be rebuilt), ui is the interface
that went with the question, and payload is the inbound message’s payload: when the person
answered a form, its form member holds the form’s surface and the action’s context
(adam_a2a_runtime::PAYLOAD_FORM). Words alone, and a question that went out as text, carry
no form.
AnswerCheck::Accept, the default, hands the answer over as it came. AnswerCheck::Read
hands over the words the tool read it as. AnswerCheck::Refuse keeps the run parked on a
new question (the refusal’s words and interface) and gives the model nothing: it never sees
the refused answer, and no result is recorded for the call. AnswerCheck::End stops
asking: the call ends with an error result and the model carries on.
The check is a pure function of its arguments, so a replay refuses what it refused.
A tool that wraps another (see ToolSet::wrap) must forward it.
fn poll_remote<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 ToolCtx,
task: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<RemotePoll, ToolError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn poll_remote<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 ToolCtx,
task: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<RemotePoll, ToolError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
How a task this tool started on another system stands. Only a tool that returns
ToolError::AwaitRemote implements it.
The agent calls it, in a journaled step, each time the timer of a run parked on
AwaitRemote fires, with the task the tool returned. The tool
looks (a GetTask to an A2A agent) and answers RemotePoll::Ready with the tool result,
or RemotePoll::Working to be asked again after the next interval. An Err is handled
as for call: Transient or
Retryable retries the step, under the tool’s policy,
Permanent becomes an error result for the model, and the waiting
variants are refused as an error result.
It has no arguments of the call: what it needs to look must be in task. The default
refuses, which is right for a tool that never waits on a remote task. A tool that wraps
another (see ToolSet::wrap) must forward it.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
§impl Tool for SubagentTool
impl Tool for SubagentTool
§fn access(&self, _args: &Value) -> ToolAccess
fn access(&self, _args: &Value) -> ToolAccess
A one-shot subagent parks the run on its child: it runs alone (ADR 0049 decision 14).
§fn step_style(&self) -> StepStyle
fn step_style(&self) -> StepStyle
A call is an agent working for this one: a subagent step, drawn as an agent and called by
the tool’s name as a person reads it (explorer is “Explorer”).