Skip to main content

ToolBox

Trait ToolBox 

Source
pub trait ToolBox:
    Send
    + Sync
    + 'static {
    // Required methods
    fn tools_definitions(&self) -> Result<Vec<Tool>, ToolError>;
    fn call_tool<'life0, 'async_trait>(
        &'life0 self,
        tool_name: String,
        arguments: Value,
    ) -> Pin<Box<dyn Future<Output = ToolResult> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

Manages a collection of callable Tool instances.

Implementors of ToolBox provide a way to group related tools and expose them to the agent for invocation. The ToolBox is responsible for defining the available tools and executing them when requested.

Important: This trait requires the use of the #[async_trait::async_trait] attribute macro for proper asynchronous behavior and dyn ToolBox compatibility.

For most use cases, implementing this trait can be significantly simplified by using the #[toolbox] attribute macro. This macro automatically generates the necessary ToolBox implementation for a struct based on its methods.

Required Methods§

Source

fn tools_definitions(&self) -> Result<Vec<Tool>, ToolError>

Returns a list of all Tool instances contained within this ToolBox. These definitions include the tool’s name, description, and parameters, which are used by the language model to decide which tool to call.

The schema field of the Tool can be conveniently generated from Rust structs using the schemars crate.

This method is typically invoked internally by the Agent structure to discover the available tools and their parameters.

Source

fn call_tool<'life0, 'async_trait>( &'life0 self, tool_name: String, arguments: Value, ) -> Pin<Box<dyn Future<Output = ToolResult> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Calls a specific tool by its name with the given parameters.

This method is the entry point for executing a tool’s functionality. It is typically invoked internally by the Agent structure when the language model determines that a tool needs to be called. The arguments are provided as a serde_json::Value. You can easily deserialize this Value into a Rust struct (e.g., the same struct used to generate the JSON schema in tools_definitions) using the serde crate. The arguments provided by the agent will conform to the JSON schema defined for the tool.

For example, to deserialize the arguments:

ⓘ
let args: ToolArguments = serde_json::from_value(arguments)?;

Replace ToolArguments with the actual struct type corresponding to your tool’s schema.

§Arguments
  • tool_name - The name of the tool to call.
  • arguments - A JSON Value containing the arguments for the tool call.
§Returns

A Result containing the tool’s output as a String on success, or a ToolError if the tool call fails or the tool is not found.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§