Skip to main content

Tool

Trait Tool 

Source
pub trait Tool: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn spec(&self) -> Result<ToolSpec>;
    fn execute(&self, args: Value) -> Result<Value>;
}
Expand description

Synchronous tool interface for simple Rust tool implementations.

A Tool is intentionally small:

The runtime treats tool arguments as untrusted model output. Implementations should validate inputs, reject ambiguous requests, and avoid logging secrets or other sensitive data derived from user prompts or credentials.

§Examples

use appam::tools::Tool;
use appam::llm::ToolSpec;
use serde_json::{json, Value};
use anyhow::Result;

struct EchoTool;

impl Tool for EchoTool {
    fn name(&self) -> &str {
        "echo"
    }

    fn spec(&self) -> Result<ToolSpec> {
        Ok(serde_json::from_value(json!({
            "type": "function",
            "function": {
                "name": "echo",
                "description": "Echo back the input message",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "message": {
                            "type": "string",
                            "description": "Message to echo"
                        }
                    },
                    "required": ["message"]
                }
            }
        }))?)
    }

    fn execute(&self, args: Value) -> Result<Value> {
        let msg = args["message"].as_str().unwrap_or("");
        Ok(json!({ "output": msg }))
    }
}

Required Methods§

Source

fn name(&self) -> &str

Return the unique, stable function name for this tool.

This name must match the name in the tool specification and is used for routing LLM tool calls to the correct implementation.

Source

fn spec(&self) -> Result<ToolSpec>

Return the tool specification exposed to the model.

The specification includes the function signature, parameter schema, and description. This is typically loaded from a JSON file to maintain a single source of truth.

§Errors

Returns an error if the specification cannot be loaded or parsed.

Source

fn execute(&self, args: Value) -> Result<Value>

Execute the tool with the given JSON arguments.

Arguments are provided as a JSON value matching the schema from spec(). The tool should validate inputs, perform its operation, and return a JSON result.

§Errors

Returns an error if arguments are invalid, execution fails, or results cannot be serialized.

§Security

Tool implementations must validate all inputs, avoid shell injection, and respect sandbox boundaries. Never trust arguments from the LLM.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§