Skip to main content

Tool

Trait Tool 

Source
pub trait Tool: Send + Sync {
    // Required methods
    fn schema(&self) -> ToolSchema;
    fn call<'life0, 'life1, 'async_trait>(
        &'life0 self,
        arguments: Value,
        context: ToolContext<'life1>,
    ) -> Pin<Box<dyn Future<Output = Result<ToolResult, ToolError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
}
Expand description

A tool an agent can invoke.

A tool has two perspectives:

  • Tool::schema — the model perspective — tells the model what the tool is and what its arguments look like;
  • Tool::call — parses model-provided arguments into an immediate ToolOutput or an EffectRequest to be executed by an outer harness.

Implementations must be Send + Sync: the agent loop may execute tools concurrently on any thread. Tools that need to flow / share custom content across tools read and write ToolContext::state; tools that do not can ignore it (_state).

§Example

use molo::tool::{Tool, ToolContext, ToolError, ToolOutput, ToolResult, ToolSchema};
use serde_json::json;

// A demo tool: returns a fixed time.
struct TimeTool;

#[molo::async_trait]
impl Tool for TimeTool {
    fn schema(&self) -> ToolSchema {
        ToolSchema::new(
            "time",
            "Return the current time",
            json!({ "type": "object", "properties": {} }),
        )
    }

    async fn call(
        &self,
        _arguments: serde_json::Value,
        _context: ToolContext<'_>,
    ) -> Result<ToolResult, ToolError> {
        Ok(ToolOutput::text("12:00").into())
    }
}

let tool = TimeTool;
assert_eq!(tool.schema().name, "time");

Required Methods§

Source

fn schema(&self) -> ToolSchema

Model perspective: this tool’s definition.

Source

fn call<'life0, 'life1, 'async_trait>( &'life0 self, arguments: Value, context: ToolContext<'life1>, ) -> Pin<Box<dyn Future<Output = Result<ToolResult, ToolError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Execution perspective: run this tool.

arguments is the model-generated arguments JSON parsed by the registry. context carries the run context, source tool-call id/name, and the agent’s shared state.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§