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,
args: Value,
) -> Pin<Box<dyn Future<Output = Result<ToolResult, ToolError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait;
// Provided methods
fn mutability(&self) -> ToolMutability { ... }
fn call_mutability(&self, _args: &Value) -> ToolMutability { ... }
fn concurrency_safe(&self) -> bool { ... }
fn call_concurrency_safe(&self, _args: &Value) -> bool { ... }
fn execute_with_context<'life0, 'life1, 'async_trait>(
&'life0 self,
args: Value,
_ctx: ToolExecutionContext<'life1>,
) -> Pin<Box<dyn Future<Output = Result<ToolResult, ToolError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn to_schema(&self) -> ToolSchema { ... }
}Expand description
Trait for implementing executable tools.
All tools must implement this trait to be registered with the tool registry.
§Required Methods
name()- Unique tool identifierdescription()- Human-readable tool descriptionparameters_schema()- JSON Schema for tool parametersexecute()- Async tool execution logic
§Provided Methods
to_schema()- Convert tool to LLM-compatible schema
§Example
struct ReadFileTool;
#[async_trait]
impl Tool for ReadFileTool {
fn name(&self) -> &str {
"read_file"
}
fn description(&self) -> &str {
"Read file contents from disk"
}
fn parameters_schema(&self) -> serde_json::Value {
json!({
"type": "object",
"properties": {
"path": {"type": "string"}
},
"required": ["path"]
})
}
async fn execute(&self, args: Value) -> Result<ToolResult, ToolError> {
let path = args["path"].as_str().unwrap();
let content = tokio::fs::read_to_string(path).await?;
Ok(ToolResult {
success: true,
result: content,
display_preference: None,
})
}
}Required Methods§
fn name(&self) -> &str
Sourcefn description(&self) -> &str
fn description(&self) -> &str
Human-readable tool description for LLM.
Sourcefn parameters_schema(&self) -> Value
fn parameters_schema(&self) -> Value
JSON Schema for tool parameters.
Provided Methods§
Sourcefn mutability(&self) -> ToolMutability
fn mutability(&self) -> ToolMutability
Declares whether this tool is read-only or mutating for orchestration and parallel scheduling decisions. Defaults to mutating to stay conservative.
Sourcefn call_mutability(&self, _args: &Value) -> ToolMutability
fn call_mutability(&self, _args: &Value) -> ToolMutability
Args-aware mutability hook. Defaults to the static mutability declaration.
Sourcefn concurrency_safe(&self) -> bool
fn concurrency_safe(&self) -> bool
Declares whether this tool can safely run in parallel with other read-only tools. Defaults to false so tools remain serialized unless they opt in explicitly.
Sourcefn call_concurrency_safe(&self, _args: &Value) -> bool
fn call_concurrency_safe(&self, _args: &Value) -> bool
Args-aware parallel-safety hook. Defaults to the static declaration.
Sourcefn execute_with_context<'life0, 'life1, 'async_trait>(
&'life0 self,
args: Value,
_ctx: ToolExecutionContext<'life1>,
) -> Pin<Box<dyn Future<Output = Result<ToolResult, ToolError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn execute_with_context<'life0, 'life1, 'async_trait>(
&'life0 self,
args: Value,
_ctx: ToolExecutionContext<'life1>,
) -> Pin<Box<dyn Future<Output = Result<ToolResult, ToolError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Execute the tool with a streaming-capable context.
Default implementation falls back to execute() for tools that don’t
need streaming.
Sourcefn to_schema(&self) -> ToolSchema
fn to_schema(&self) -> ToolSchema
Convert tool to LLM-compatible schema.
Creates a ToolSchema suitable for LLM function calling.