lc_core/tools/base.rs
1// src/core/tools/base.rs
2//! Tool base traits.
3//!
4//! Python's BaseTool uses a simplified run(input: str) -> str interface.
5
6use crate::runnables::{LcelError, Runnable, RunnableConfig};
7use async_trait::async_trait;
8use schemars::JsonSchema;
9use serde::{de::DeserializeOwned, Serialize};
10use serde_json::Value;
11use std::sync::Arc;
12
13/// Base tool trait (object-safe version).
14///
15/// This is the base interface for tool registries and Agents.
16/// Uses string input/output to simplify LLM calls.
17///
18/// All tools must implement this interface to be used by Agents.
19#[async_trait]
20pub trait BaseTool: Send + Sync {
21 /// Returns the tool name.
22 ///
23 /// Name should be unique and clearly express the tool's purpose.
24 fn name(&self) -> &str;
25
26 /// Returns the tool description.
27 ///
28 /// Description should detail the tool's purpose, input format, and output format.
29 fn description(&self) -> &str;
30
31 /// Execute the tool (string version).
32 ///
33 /// This is the primary interface called by Agents.
34 /// Input is typically a JSON string, output is the execution result.
35 ///
36 /// # Arguments
37 /// * `input` - Tool input (typically JSON-formatted string).
38 ///
39 /// # Returns
40 /// String representation of execution result.
41 async fn run(&self, input: String) -> Result<String, ToolError>;
42
43 /// Returns the input JSON Schema.
44 ///
45 /// Used to describe the tool's input format to the LLM.
46 fn args_schema(&self) -> Option<Value> {
47 None
48 }
49
50 /// Whether to return result directly to user.
51 ///
52 /// If true, tool output is returned directly to user, not passed to Agent.
53 fn return_direct(&self) -> bool {
54 false
55 }
56
57 /// Declared Rule-of-Two risk profile (A2, v0.22.1).
58 ///
59 /// Defaults to an all-false profile, so existing tools are never intercepted. Tools that
60 /// combine an untrusted input source, sensitive access, and world-state change should
61 /// override this so the agent can refuse the call before executing it (see
62 /// [`ToolRiskProfile::count_armed`]).
63 fn risk(&self) -> ToolRiskProfile {
64 ToolRiskProfile::empty()
65 }
66
67 /// Handle execution error.
68 ///
69 /// Returns a friendly error message when tool execution fails.
70 async fn handle_error(&self, error: ToolError) -> String {
71 format!("Tool '{}' execution failed: {}", self.name(), error)
72 }
73}
74
75/// Generic tool trait (type-safe version).
76///
77/// For scenarios requiring type-safe input/output.
78/// Tools implementing this trait can be automatically wrapped as BaseTool.
79#[async_trait]
80pub trait Tool: Send + Sync {
81 /// Input type (must support deserialization and JSON Schema).
82 type Input: DeserializeOwned + JsonSchema + Send + Sync + 'static;
83
84 /// Output type (must support serialization).
85 type Output: Serialize + Send + Sync;
86
87 /// Execute the tool.
88 ///
89 /// # Arguments
90 /// * `input` - Tool input.
91 ///
92 /// # Returns
93 /// Tool output.
94 async fn invoke(&self, input: Self::Input) -> Result<Self::Output, ToolError>;
95
96 /// Returns the input JSON Schema.
97 fn args_schema(&self) -> Option<Value> {
98 use schemars::schema_for;
99 serde_json::to_value(schema_for!(Self::Input)).ok()
100 }
101}
102
103/// Tool error type.
104#[derive(Debug, thiserror::Error)]
105#[non_exhaustive]
106pub enum ToolError {
107 /// Input validation error.
108 #[error("Invalid input: {0}")]
109 InvalidInput(String),
110
111 /// Execution error.
112 #[error("Execution failed: {0}")]
113 ExecutionFailed(String),
114
115 /// Timeout.
116 #[error("Timeout: {0} seconds")]
117 Timeout(u64),
118
119 /// Tool not found.
120 #[error("Tool not found: {0}")]
121 ToolNotFound(String),
122
123 /// MCP transport-layer error (adapted via `MCPToolAdapter`), preserving code/message/data (P1-6).
124 ///
125 /// Not silently downgraded to `ExecutionFailed`: the caller can distinguish connection drop /
126 /// method-not-found / argument errors by `code`.
127 #[error("MCP error [{code}]: {message}")]
128 McpError {
129 /// MCP error code
130 code: i32,
131 /// MCP error message
132 message: String,
133 /// Additional error data (optional)
134 data: Option<Value>,
135 },
136
137 /// Framework-level control abort (0.20.0 S3.1).
138 ///
139 /// Distinct from [`ExecutionFailed`](ToolError::ExecutionFailed): this is the framework
140 /// **refusing to perform** the tool call for control-flow reasons (e.g. the handoff
141 /// cycle / depth guard in `lc-agents`), not the tool running and failing. Callers must
142 /// propagate it **hard** — the agent cannot recover by re-planning, and softening it to
143 /// an observation would defeat the guard it exists to enforce.
144 #[error("Control abort: {0}")]
145 ControlAbort(String),
146}
147
148use super::{ToolDefinition, ToolRiskProfile};
149
150/// Converts BaseTool to ToolDefinition (for function calling).
151///
152/// # Arguments
153/// * `tool` - Tool implementing BaseTool trait.
154///
155/// # Returns
156/// ToolDefinition for bind_tools().
157///
158/// # Example
159/// ```ignore
160/// use langchainrust::{Calculator, BaseTool, to_tool_definition};
161/// use std::sync::Arc;
162///
163/// let calculator = Calculator::new();
164/// let tool_def = to_tool_definition(&calculator);
165/// ```
166pub fn to_tool_definition(tool: &dyn BaseTool) -> ToolDefinition {
167 ToolDefinition::new(tool.name(), tool.description()).with_parameters(
168 tool.args_schema()
169 .unwrap_or(serde_json::json!({"type": "object"})),
170 )
171}
172
173// Runnable form: lets a tool enter an LCEL chain, so `tool.pipe(...)` works.
174// Receives a String (usually JSON input), delegates to `run`; errors go into `LcelError::Tool` via `From<ToolError>`.
175#[async_trait]
176impl Runnable<String, String> for Arc<dyn BaseTool> {
177 type Error = LcelError;
178
179 async fn invoke(
180 &self,
181 input: String,
182 _config: Option<RunnableConfig>,
183 ) -> Result<String, LcelError> {
184 BaseTool::run(&**self, input).await.map_err(LcelError::from)
185 }
186}
187
188#[cfg(test)]
189mod tests {
190 use super::*;
191 use std::sync::Arc;
192
193 /// Simple echo tool: returns `echo: {input}`.
194 struct EchoTool;
195
196 #[async_trait]
197 impl BaseTool for EchoTool {
198 fn name(&self) -> &str {
199 "echo"
200 }
201 fn description(&self) -> &str {
202 "回显输入"
203 }
204 async fn run(&self, input: String) -> Result<String, ToolError> {
205 Ok(format!("echo: {input}"))
206 }
207 }
208
209 #[tokio::test]
210 async fn arc_tool_is_runnable() {
211 let tool: Arc<dyn BaseTool> = Arc::new(EchoTool);
212 let result = tool.invoke("hi".to_string(), None).await.unwrap();
213 assert_eq!(result, "echo: hi");
214 }
215
216 #[tokio::test]
217 async fn arc_tool_pipes() {
218 use crate::runnables::{RunnableExt, RunnableLambda};
219
220 let tool: Arc<dyn BaseTool> = Arc::new(EchoTool);
221 let chain = tool.pipe(RunnableLambda::new_sync(|s: String| s.to_uppercase()));
222 let result = chain.invoke("hi".to_string(), None).await.unwrap();
223 assert_eq!(result, "ECHO: HI");
224 }
225
226 #[tokio::test]
227 async fn arc_tool_error_maps_to_lcel() {
228 struct FailingTool;
229 #[async_trait]
230 impl BaseTool for FailingTool {
231 fn name(&self) -> &str {
232 "fail"
233 }
234 fn description(&self) -> &str {
235 "总是失败"
236 }
237 async fn run(&self, _input: String) -> Result<String, ToolError> {
238 Err(ToolError::ExecutionFailed("boom".to_string()))
239 }
240 }
241
242 let tool: Arc<dyn BaseTool> = Arc::new(FailingTool);
243 let err = tool.invoke("x".to_string(), None).await.unwrap_err();
244 assert!(matches!(err, LcelError::Tool(ref msg) if msg.contains("boom")));
245 }
246}