Skip to main content

Tool

Trait Tool 

Source
pub trait Tool: Send + Sync {
    type Input: DeserializeOwned + JsonSchema;

    // Required methods
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn execute(
        &self,
        input: Self::Input,
    ) -> impl Future<Output = Result<ToolResult, ToolError>> + Send;

    // Provided methods
    fn input_schema(&self) -> Value { ... }
    fn format_input_plain(&self, params: &Value) -> String { ... }
    fn format_input_ansi(&self, params: &Value) -> String { ... }
    fn format_input_markdown(&self, params: &Value) -> String { ... }
    fn format_output_plain(&self, result: &ToolResult) -> String { ... }
    fn format_output_ansi(&self, result: &ToolResult) -> String { ... }
    fn format_output_markdown(&self, result: &ToolResult) -> String { ... }
}
Expand description

Trait for implementing tools that can be used by AI agents.

Tools define an input type with #[derive(Deserialize, JsonSchema)] to automatically generate JSON schemas from Rust types, providing excellent developer experience.

§Async Tools Example

use mixtape_core::{Tool, ToolResult, ToolError};
use schemars::JsonSchema;
use serde::Deserialize;
use std::time::Duration;

#[derive(Deserialize, JsonSchema)]
struct DelayInput {
    /// Duration in milliseconds
    ms: u64,
}

struct DelayTool;

impl Tool for DelayTool {
    type Input = DelayInput;

    fn name(&self) -> &str { "delay" }
    fn description(&self) -> &str { "Wait for a duration" }

    fn execute(&self, input: Self::Input) -> impl std::future::Future<Output = Result<ToolResult, ToolError>> + Send {
        async move {
            // Async operations work naturally
            tokio::time::sleep(Duration::from_millis(input.ms)).await;
            Ok(format!("Waited {}ms", input.ms).into())  // Converts to ToolResult::Text
        }
    }
}

§Returning JSON Data

Tools can return structured JSON data:

use mixtape_core::{Tool, ToolResult, ToolError};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, JsonSchema)]
struct CalculateInput {
    a: i32,
    b: i32,
}

#[derive(Serialize)]
struct CalculateOutput {
    sum: i32,
    product: i32,
}

struct CalculateTool;

impl Tool for CalculateTool {
    type Input = CalculateInput;

    fn name(&self) -> &str { "calculate" }
    fn description(&self) -> &str { "Perform calculations" }

    fn execute(&self, input: Self::Input) -> impl std::future::Future<Output = Result<ToolResult, ToolError>> + Send {
        async move {
            let output = CalculateOutput {
                sum: input.a + input.b,
                product: input.a * input.b,
            };
            ToolResult::json(output).map_err(Into::into)
        }
    }
}

Required Associated Types§

Source

type Input: DeserializeOwned + JsonSchema

The input type for this tool. Must implement Deserialize and JsonSchema.

Required Methods§

Source

fn name(&self) -> &str

The name of the tool (e.g., “read_file”, “calculator”)

Source

fn description(&self) -> &str

A description of what the tool does

Source

fn execute( &self, input: Self::Input, ) -> impl Future<Output = Result<ToolResult, ToolError>> + Send

Execute the tool with typed input

Provided Methods§

Source

fn input_schema(&self) -> Value

Get the JSON schema for this tool’s input.

This is automatically implemented using the JsonSchema derive on Input. The schema is generated at runtime from the type definition.

Source

fn format_input_plain(&self, params: &Value) -> String

Format tool input as plain text (for JIRA, logs, copy/paste).

Default implementation shows tool name and parameters with truncation.

Source

fn format_input_ansi(&self, params: &Value) -> String

Format tool input with ANSI colors (for terminal display).

Default implementation shows tool name (bold) and parameters with colors.

Source

fn format_input_markdown(&self, params: &Value) -> String

Format tool input as Markdown (for docs, GitHub, rendered UIs).

Default implementation shows tool name and parameters in markdown format.

Source

fn format_output_plain(&self, result: &ToolResult) -> String

Format tool output as plain text.

Default implementation shows result text with truncation.

Source

fn format_output_ansi(&self, result: &ToolResult) -> String

Format tool output with ANSI colors.

Default implementation shows result with success indicator and truncation.

Source

fn format_output_markdown(&self, result: &ToolResult) -> String

Format tool output as Markdown.

Default implementation shows result in a code block with truncation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§