pub trait Tool: Send + Sync {
// Required methods
fn name(&self) -> &str;
fn description(&self) -> &str;
fn parameters_schema(&self) -> Value;
fn execute<'life0, 'async_trait>(
&'life0 self,
params: Value,
) -> Pin<Box<dyn Future<Output = ToolOutput> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}Expand description
A tool that can be executed by the agent
Tools are the primary way the agent interacts with the outside world. Each tool must be:
Send- Can be transferred between threadsSync- Can be shared between threads
§Implementing a Tool
- Define a struct for your tool
- Implement the
Tooltrait - Register it with
ToolRegistry
§Thread Safety
The Send + Sync bounds are required because:
- Tools may be called from different async tasks
- The registry holds tools as
Arc<dyn Tool> - Multiple requests may use the same tool concurrently
Required Methods§
Sourcefn name(&self) -> &str
fn name(&self) -> &str
The unique name of this tool
This is used by the LLM to call the tool and must be unique within a registry.
Sourcefn description(&self) -> &str
fn description(&self) -> &str
A human-readable description of what this tool does
This is shown to the LLM to help it decide when to use the tool. Be clear and concise about the tool’s purpose and capabilities.
Sourcefn parameters_schema(&self) -> Value
fn parameters_schema(&self) -> Value
JSON Schema describing the tool’s parameters
This schema is sent to the LLM so it knows what parameters to provide. Use standard JSON Schema format.
§Example
{
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The file path to read"
}
},
"required": ["path"]
}Sourcefn execute<'life0, 'async_trait>(
&'life0 self,
params: Value,
) -> Pin<Box<dyn Future<Output = ToolOutput> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn execute<'life0, 'async_trait>(
&'life0 self,
params: Value,
) -> Pin<Box<dyn Future<Output = ToolOutput> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Execute the tool with the given parameters
Parameters are passed as a JSON value matching the schema
returned by parameters_schema().
§Returns
A ToolOutput indicating success or failure with a message.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".