Skip to content

feat: proc macro for implementing Tool #352

Closed
@cbrit

Description

@cbrit
  • I have looked for existing issues (including closed) about this

Feature Request

Add a proc macro for easily implementing the Tool trait with automatic parameter validation and error handling.

Motivation

Currently, implementing the Tool trait requires a lot of boilerplate code. Obfuscation can be bad, but the macro can act as a shorthand that has all the same info as a function signature, so nothing is really obfuscated except struct creation. Also, by adding this feature in a separate crate it can be optional and not affect core functionality.

Proposal

Add a #[rig_tool] proc macro that:

  • Automatically generates a Tool implementation from an async function
  • Provides attribute-based configuration for:
    • Tool description
    • Parameter descriptions
      (tool name is function name automatically)

Example usage:

#[rig_tool(
    description = "Perform basic arithmetic operations",
    params(
        x = "First number in the calculation",
        y = "Second number in the calculation",
        operation = "The operation to perform"
    )
)]
async fn calculator(x: i32, y: i32, operation: String) -> Result<i32, rig::tool::ToolError> {
    match operation.as_str() {
        "add" => Ok(x + y),
        "subtract" => Ok(x - y),
        "multiply" => Ok(x * y),
        "divide" => {
             if y == 0 {
                 Err(rig::tool::ToolError::ToolCallError(
                     "Division by zero".into(),
                 ))
             } else {
                 Ok(x / y)
             }
         }
         _ => Err(rig::tool::ToolError::ToolCallError(
             format!("Unknown operation: {}", operation).into(),
         )),
     }
}

Alternatives

Manual Implementation: Continue requiring developers to implement the Tool trait manually.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions