Skip to main content

synaps_cli/mcp/
tool.rs

1//! McpTool — bridges an MCP server tool into the native Tool trait.
2use serde_json::Value;
3use std::sync::Arc;
4use tokio::sync::Mutex;
5use crate::{Result, RuntimeError, Tool, ToolContext};
6
7use super::connection::McpConnection;
8
9/// A dynamic tool backed by an MCP server connection.
10/// The connection is shared (Arc<Mutex<>>) across all tools from the same server.
11pub struct McpTool {
12    pub(super) tool_name: String,
13    /// Original tool name as the MCP server knows it (without prefix).
14    pub(super) server_tool_name: String,
15    pub(super) server_name: String,
16    pub(super) description: String,
17    pub(super) input_schema: Value,
18    pub(super) connection: Arc<Mutex<McpConnection>>,
19}
20
21#[async_trait::async_trait]
22impl Tool for McpTool {
23    fn name(&self) -> &str {
24        &self.tool_name
25    }
26    
27    fn description(&self) -> &str {
28        &self.description
29    }
30    
31    fn parameters(&self) -> Value {
32        self.input_schema.clone()
33    }
34    
35    async fn execute(&self, params: Value, _ctx: ToolContext) -> Result<String> {
36        let mut conn = self.connection.lock().await;
37        conn.call_tool(&self.server_tool_name, params).await
38            .map_err(|e| RuntimeError::Tool(format!(
39                "MCP tool '{}' (server '{}') failed: {}", self.tool_name, self.server_name, e
40            )))
41    }
42}