Trait ToolFunction

Source
pub trait ToolFunction: Send + Sync {
    // Required method
    fn execute<'life0, 'async_trait>(
        &'life0 self,
        input: Value,
    ) -> Pin<Box<dyn Future<Output = Result<ToolResult, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn validate_input(
        &self,
        _input: &Value,
    ) -> Result<(), Box<dyn Error + Send + Sync>> { ... }
    fn timeout_seconds(&self) -> u64 { ... }
}
Expand description

Trait for implementing tool functions.

This trait allows you to define custom tools that Claude can call. Each tool receives structured input and returns a structured result.

Required Methods§

Source

fn execute<'life0, 'async_trait>( &'life0 self, input: Value, ) -> Pin<Box<dyn Future<Output = Result<ToolResult, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute the tool with the given input.

§Arguments
  • input - JSON input parameters from Claude
§Returns

A ToolResult containing the execution result, or an error if execution fails.

Provided Methods§

Source

fn validate_input( &self, _input: &Value, ) -> Result<(), Box<dyn Error + Send + Sync>>

Validate input before execution (optional).

Override this method to provide custom validation logic beyond the JSON schema validation.

Source

fn timeout_seconds(&self) -> u64

Get the tool’s timeout in seconds (optional).

Override to set a custom timeout for this tool. Default is 30 seconds.

Implementors§

Source§

impl<F> ToolFunction for SimpleTool<F>
where F: Fn(Value) -> Pin<Box<dyn Future<Output = Result<ToolResult, Box<dyn Error + Send + Sync>>> + Send>> + Send + Sync,