Skip to main content

prism_mcp_rs/client/
fluent_tools.rs

1//! Fluent interface for tool operations
2
3use crate::core::enhanced_errors::{McpError, McpResult};
4use crate::protocol::{messages::ListToolsResult, types::CallToolResult};
5use serde_json::Value;
6use std::collections::HashMap;
7
8/// Fluent interface for tool operations
9pub struct ToolsBuilder<'a> {
10    client: &'a crate::client::McpClient,
11    tool_name: Option<String>,
12    arguments: HashMap<String, Value>,
13}
14
15impl<'a> ToolsBuilder<'a> {
16    pub(crate) fn new(client: &'a crate::client::McpClient) -> Self {
17        Self {
18            client,
19            tool_name: None,
20            arguments: HashMap::new(),
21        }
22    }
23
24    /// Set the tool to call
25    pub fn call(mut self, name: impl Into<String>) -> Self {
26        self.tool_name = Some(name.into());
27        self
28    }
29
30    /// Add arguments using JSON value
31    pub fn args(mut self, args: impl Into<Value>) -> Self {
32        if let Value::Object(map) = args.into() {
33            for (k, v) in map {
34                self.arguments.insert(k, v);
35            }
36        }
37        self
38    }
39
40    /// Add a single argument
41    pub fn arg(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
42        self.arguments.insert(key.into(), value.into());
43        self
44    }
45
46    /// Execute the tool call
47    pub async fn execute(self) -> McpResult<CallToolResult> {
48        let tool_name = self
49            .tool_name
50            .ok_or_else(|| McpError::validation("Tool name is required"))?;
51
52        self.client
53            .call_tool(
54                tool_name,
55                if self.arguments.is_empty() {
56                    None
57                } else {
58                    Some(self.arguments)
59                },
60            )
61            .await
62            .map_err(|e| McpError::internal(e.to_string()))
63    }
64
65    /// List available tools
66    pub async fn list(self) -> McpResult<ListToolsResult> {
67        self.client
68            .list_tools(None)
69            .await
70            .map_err(|e| McpError::internal(e.to_string()))
71    }
72}