Skip to main content

Tool

Trait Tool 

Source
pub trait Tool: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn parameters_schema(&self) -> Value;
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 self,
        input: ToolInput,
        ctx: &'life1 AgentContext,
    ) -> Pin<Box<dyn Future<Output = ToolResult> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn metadata(&self) -> ToolMetadata { ... }
    fn validate_input(&self, input: &ToolInput) -> AgentResult<()> { ... }
    fn requires_confirmation(&self) -> bool { ... }
    fn to_llm_tool(&self) -> LLMTool { ... }
}
Expand description

统一工具 Trait

合并了 ToolExecutor 和 ReActTool 的功能

§示例

use mofa_kernel::agent::components::tool::{Tool, ToolInput, ToolResult, ToolMetadata};

struct Calculator;

#[async_trait]
impl Tool for Calculator {
    fn name(&self) -> &str { "calculator" }
    fn description(&self) -> &str { "Perform arithmetic operations" }
    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "operation": { "type": "string", "enum": ["add", "sub", "mul", "div"] },
                "a": { "type": "number" },
                "b": { "type": "number" }
            },
            "required": ["operation", "a", "b"]
        })
    }

    async fn execute(&self, input: ToolInput, ctx: &CoreAgentContext) -> ToolResult {
        // Implementation
    }

    fn metadata(&self) -> ToolMetadata {
        ToolMetadata::default()
    }
}

Required Methods§

Source

fn name(&self) -> &str

工具名称 (唯一标识符)

Source

fn description(&self) -> &str

工具描述 (用于 LLM 理解)

Source

fn parameters_schema(&self) -> Value

参数 JSON Schema

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, input: ToolInput, ctx: &'life1 AgentContext, ) -> Pin<Box<dyn Future<Output = ToolResult> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

执行工具

Provided Methods§

Source

fn metadata(&self) -> ToolMetadata

工具元数据

Source

fn validate_input(&self, input: &ToolInput) -> AgentResult<()>

验证输入

Source

fn requires_confirmation(&self) -> bool

是否需要确认

Source

fn to_llm_tool(&self) -> LLMTool

转换为 LLM Tool 格式

Implementors§