use crate::tools::traits::{Tool, ToolResult};
use anyhow::Result;
use async_trait::async_trait;
use serde_json::{json, Value};
pub struct MyTool {
// Dependencies (security, memory, runtime, etc.)
}
impl MyTool {
pub fn new() -> Self {
Self {}
}
}
#[async_trait]
impl Tool for MyTool {
fn name(&self) -> &str {
"my_tool"
}
fn description(&self) -> &str {
"Does something useful with the given input"
}
fn parameters_schema(&self) -> Value {
json!({
"type": "object",
"properties": {
"input": {
"type": "string",
"description": "The input to process"
},
"mode": {
"type": "string",
"enum": ["fast", "thorough"],
"description": "Processing mode",
"default": "fast"
}
},
"required": ["input"]
})
}
async fn execute(&self, args: Value) -> Result<ToolResult> {
// 1. Validate inputs
let input = args["input"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("Missing 'input' parameter"))?;
let mode = args["mode"]
.as_str()
.unwrap_or("fast");
// 2. Execute logic
match do_something(input, mode).await {
Ok(result) => Ok(ToolResult {
success: true,
output: result,
error: None,
}),
Err(e) => Ok(ToolResult {
success: false,
output: String::new(),
error: Some(e.to_string()),
}),
}
}
}
async fn do_something(input: &str, mode: &str) -> Result<String> {
// Your tool logic here
Ok(format!("Processed '{}' in {} mode", input, mode))
}