From 10bf8a4926f635d3cb366812fc0bc60617a1f3bb Mon Sep 17 00:00:00 2001 From: BasilYes Date: Tue, 12 Aug 2025 09:05:47 +0300 Subject: [PATCH] add missing list tools functiont to client and fix tools/call function --- src/client.rs | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/client.rs b/src/client.rs index 4f0b559..88a20e3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -118,6 +118,30 @@ impl Client { } } + /// List tools on a server + pub async fn list_tools(&mut self) -> Result { + // Create tool call request + let tool_call_request = + JSONRPCRequest::new(self.next_request_id(), "tools/list".to_string(), None); + + let message = JSONRPCMessage::Request(tool_call_request); + self.transport.send(&message).await?; + + // Wait for response with timeout if set + let response: JSONRPCMessage = self.receive_with_timeout().await?; + + match response { + JSONRPCMessage::Response(resp) => { + // Parse the result + serde_json::from_value(resp.result).map_err(MCPError::Serialization) + } + JSONRPCMessage::Error(err) => { + Err(MCPError::Protocol(format!("Tool list failed: {:?}", err))) + } + _ => Err(MCPError::Protocol("Unexpected response type".to_string())), + } + } + /// Call a tool on the server pub async fn call_tool( &mut self, @@ -127,10 +151,10 @@ impl Client { // Create tool call request let tool_call_request = JSONRPCRequest::new( self.next_request_id(), - "tool_call".to_string(), + "tools/call".to_string(), Some(serde_json::json!({ "name": tool_name, - "parameters": serde_json::to_value(params)? + "arguments": serde_json::to_value(params)? })), ); @@ -142,14 +166,8 @@ impl Client { match response { JSONRPCMessage::Response(resp) => { - // Extract the tool result from the response - let result_value = resp.result; - let result = result_value.get("result").ok_or_else(|| { - MCPError::Protocol("Missing 'result' field in response".to_string()) - })?; - // Parse the result - serde_json::from_value(result.clone()).map_err(MCPError::Serialization) + serde_json::from_value(resp.result).map_err(MCPError::Serialization) } JSONRPCMessage::Error(err) => { Err(MCPError::Protocol(format!("Tool call failed: {:?}", err)))