Skip to main content

Module tool

Module tool 

Source
Expand description

Tool trait and built-in tools for agent execution.

This module provides the core Tool trait for defining executable tools that can be registered with a ToolRegistry and called by AI agents.

§Defining Custom Tools

Implement the Tool trait to create custom tools:

use async_trait::async_trait;
use limit_agent::{Tool, AgentError};
use serde_json::{json, Value};

struct CalculatorTool;

#[async_trait]
impl Tool for CalculatorTool {
    fn name(&self) -> &str {
        "calculate"
    }
     
    async fn execute(&self, args: Value) -> Result<Value, AgentError> {
        let a = args["a"].as_f64().unwrap_or(0.0);
        let b = args["b"].as_f64().unwrap_or(0.0);
        let op = args["op"].as_str().unwrap_or("+");
         
        let result = match op {
            "+" => a + b,
            "-" => a - b,
            "*" => a * b,
            "/" => a / b,
            _ => return Err(AgentError::ToolError("Unknown operator".into())),
        };
         
        Ok(json!({ "result": result }))
    }
}

§Built-in Tools

  • EchoTool — Simple echo tool for testing, returns its input unchanged

Structs§

EchoTool
A simple echo tool that returns its input unchanged.

Traits§

Tool
Trait for defining executable tools.