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§
Sourcefn description(&self) -> &str
fn description(&self) -> &str
工具描述 (用于 LLM 理解)
Sourcefn parameters_schema(&self) -> Value
fn parameters_schema(&self) -> Value
参数 JSON Schema
Sourcefn 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,
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§
Sourcefn metadata(&self) -> ToolMetadata
fn metadata(&self) -> ToolMetadata
工具元数据
Sourcefn validate_input(&self, input: &ToolInput) -> AgentResult<()>
fn validate_input(&self, input: &ToolInput) -> AgentResult<()>
验证输入
Sourcefn requires_confirmation(&self) -> bool
fn requires_confirmation(&self) -> bool
是否需要确认
Sourcefn to_llm_tool(&self) -> LLMTool
fn to_llm_tool(&self) -> LLMTool
转换为 LLM Tool 格式