modelcontextprotocol_server/tools/
mod.rs

1// mcp-server/src/tools/mod.rs
2use std::collections::HashMap;
3use std::sync::Arc;
4use anyhow::Result;
5use tokio::sync::RwLock;
6use mcp_protocol::types::tool::{Tool, ToolCallResult};
7
8/// Tool handler function type
9pub type ToolHandler = Arc<dyn Fn(serde_json::Value) -> Result<ToolCallResult> + Send + Sync>;
10
11/// Tool manager for registering and executing tools
12pub struct ToolManager {
13    tools: Arc<RwLock<HashMap<String, (Tool, ToolHandler)>>>,
14}
15
16impl ToolManager {
17    /// Create a new tool manager
18    pub fn new() -> Self {
19        Self {
20            tools: Arc::new(RwLock::new(HashMap::new())),
21        }
22    }
23    
24    /// Register a new tool
25    pub fn register_tool(&self, tool: Tool, handler: impl Fn(serde_json::Value) -> Result<ToolCallResult> + Send + Sync + 'static) {
26        let tools = self.tools.clone();
27        let handler = Arc::new(handler);
28        
29        tokio::spawn(async move {
30            let mut tools = tools.write().await;
31            tools.insert(tool.name.clone(), (tool, handler));
32        });
33    }
34    
35    /// Get all registered tools
36    pub async fn list_tools(&self) -> Vec<Tool> {
37        let tools = self.tools.read().await;
38        tools.values().map(|(tool, _)| tool.clone()).collect()
39    }
40    
41    /// Execute a tool
42    pub async fn execute_tool(&self, name: &str, arguments: serde_json::Value) -> Result<ToolCallResult> {
43        let tools = self.tools.read().await;
44        let (_, handler) = tools.get(name).ok_or_else(|| anyhow::anyhow!("Tool not found: {}", name))?;
45        
46        handler(arguments)
47    }
48}
49
50impl Default for ToolManager {
51    fn default() -> Self {
52        Self::new()
53    }
54}