tool

Attribute Macro tool 

Source
#[tool]
Expand description

Converts an async function into an AI tool that can be called by language models.

This procedural macro generates the necessary boilerplate code to make your function callable through the aither::llm::Tool trait.

§Arguments

  • description (required): A clear description of what the tool does. This helps the AI model decide when to use this tool.
  • rename (optional): A custom name for the tool. If not provided, uses the function name.

§Examples

§Basic Usage (No Parameters)

use aither::Result;
use aither_derive::tool;

#[tool(description = "Get the current system time")]
pub async fn current_time() -> Result<String> {
    Ok(chrono::Utc::now().to_rfc3339())
}

§With Simple Parameters

#[tool(description = "Calculate the sum of two numbers")]
pub async fn add(a: f64, b: f64) -> Result<f64> {
    Ok(a + b)
}

§With Complex Parameters

use schemars::JsonSchema;
use serde::Deserialize;

#[derive(JsonSchema, Deserialize)]
pub struct EmailRequest {
    /// Recipient email address
    pub to: String,
    /// Email subject line
    pub subject: String,
    /// Email body content
    pub body: String,
}

#[tool(description = "Send an email to a recipient")]
pub async fn send_email(request: EmailRequest) -> Result<String> {
    // Your email sending logic here
    Ok(format!("Email sent to {}", request.to))
}

§With Custom Name

#[tool(
    description = "Perform complex mathematical calculations",
    rename = "calculator"
)]
pub async fn complex_math_function(expression: String) -> Result<f64> {
    // Your calculation logic here
    Ok(42.0)
}

§Generated Code

For a function named search, the macro generates:

  1. A SearchArgs struct (if the function has multiple parameters)
  2. A Search struct that implements aither::llm::Tool
  3. All necessary trait implementations for JSON schema generation and deserialization

§Requirements

  • Function must be async
  • Return type must be Result<T> where T implements serde::Serialize
  • Parameters must implement serde::Deserialize and schemars::JsonSchema
  • No self parameters (only free functions are supported)
  • No lifetime parameters or generics

§Errors

The macro will produce compile-time errors if:

  • The function is not async
  • The function has self parameters
  • The function has more than the supported number of parameters
  • Required attributes are missing