1use anyhow::Result;
2use std::{collections::HashMap, sync::Arc};
3use tokio::sync::mpsc;
4
5use crate::Part;
6use crate::{
7 ToolCall, ToolDefinition, auth::AuthMetadata, events::AgentEvent, stores::SessionStore,
8};
9
10#[derive(Debug, Clone)]
12pub struct ToolContext {
13 pub agent_id: String,
15 pub session_id: String,
17 pub task_id: String,
19 pub run_id: String,
21 pub thread_id: String,
23 pub user_id: String,
25 pub session_store: Arc<dyn SessionStore>,
27 pub event_tx: Option<Arc<mpsc::Sender<AgentEvent>>>,
29
30 pub metadata: Option<HashMap<String, serde_json::Value>>,
32}
33
34#[async_trait::async_trait]
36pub trait Tool: Send + Sync + std::fmt::Debug + std::any::Any {
37 fn get_name(&self) -> String;
38
39 fn get_tool_definition(&self) -> ToolDefinition {
41 ToolDefinition {
42 name: self.get_name(),
43 description: self.get_description(),
44 parameters: self.get_parameters(),
45 output_schema: None,
46 examples: self.get_tool_examples(),
47 }
48 }
49
50 fn get_parameters(&self) -> serde_json::Value;
51 fn get_description(&self) -> String;
52
53 fn get_tool_examples(&self) -> Option<String> {
54 None
55 }
56
57 fn is_external(&self) -> bool {
59 false }
61
62 fn is_mcp(&self) -> bool {
64 false }
66
67 fn is_sync(&self) -> bool {
68 false }
70
71 fn is_final(&self) -> bool {
72 false }
74
75 fn needs_executor_context(&self) -> bool {
77 false }
79
80 fn get_auth_metadata(&self) -> Option<Box<dyn AuthMetadata>> {
82 None }
84
85 fn get_plugin_name(&self) -> Option<String> {
89 None }
91
92 async fn execute(
94 &self,
95 tool_call: ToolCall,
96 context: Arc<ToolContext>,
97 ) -> Result<Vec<Part>, anyhow::Error>;
98
99 fn execute_sync(
101 &self,
102 _tool_call: ToolCall,
103 _context: Arc<ToolContext>,
104 ) -> Result<Vec<Part>, anyhow::Error> {
105 Err(anyhow::anyhow!("Sync execution not supported"))
106 }
107}