Skip to main content

claude_rust_tools/application/
registry.rs

1use std::collections::HashMap;
2use std::sync::Arc;
3
4use claude_rust_types::Tool;
5use serde_json::{Value, json};
6
7pub struct ToolRegistry {
8    tools: HashMap<String, Arc<dyn Tool>>,
9}
10
11impl ToolRegistry {
12    pub fn new() -> Self {
13        Self {
14            tools: HashMap::new(),
15        }
16    }
17
18    pub fn register(&mut self, tool: Arc<dyn Tool>) {
19        self.tools.insert(tool.name().to_string(), tool);
20    }
21
22    pub fn get(&self, name: &str) -> Option<&Arc<dyn Tool>> {
23        self.tools.get(name)
24    }
25
26    pub fn tool_definitions(&self) -> Vec<Value> {
27        self.tools
28            .values()
29            .map(|t| {
30                json!({
31                    "name": t.name(),
32                    "description": t.description(),
33                    "input_schema": t.input_schema(),
34                })
35            })
36            .collect()
37    }
38
39    /// Return tool definitions filtered by a predicate on the tool trait.
40    pub fn tool_definitions_filtered<F>(&self, predicate: F) -> Vec<Value>
41    where
42        F: Fn(&dyn Tool) -> bool,
43    {
44        self.tools
45            .values()
46            .filter(|t| predicate(t.as_ref()))
47            .map(|t| {
48                json!({
49                    "name": t.name(),
50                    "description": t.description(),
51                    "input_schema": t.input_schema(),
52                })
53            })
54            .collect()
55    }
56
57    pub fn tool_names(&self) -> Vec<String> {
58        let mut names: Vec<String> = self.tools.keys().cloned().collect();
59        names.sort();
60        names
61    }
62
63    pub fn clone_excluding(&self, exclude: &[&str]) -> Self {
64        let tools = self
65            .tools
66            .iter()
67            .filter(|(name, _)| !exclude.contains(&name.as_str()))
68            .map(|(k, v)| (k.clone(), v.clone()))
69            .collect();
70        Self { tools }
71    }
72}
73
74impl Default for ToolRegistry {
75    fn default() -> Self {
76        Self::new()
77    }
78}