11use std:: pin:: Pin ;
22
3- use anyhow:: { Context as _, Result } ;
3+ use anyhow:: Context as _;
44use serde:: { Deserialize , Serialize } ;
55use tokio:: io:: { AsyncBufReadExt as _, AsyncRead , AsyncWrite , AsyncWriteExt , BufReader } ;
66
@@ -160,23 +160,23 @@ impl McpClient {
160160 }
161161
162162 /// Connect to the MCP server and perform initialization handshake using the configured settings.
163- pub async fn connect ( & mut self ) -> Result < InitializeResult > {
163+ pub async fn connect ( & mut self ) -> anyhow :: Result < InitializeResult > {
164164 self . initialize ( ) . await
165165 }
166166
167167 /// Send a raw JSON-RPC request and get response
168- async fn send_request ( & mut self , request : JsonRpcRequest ) -> Result < JsonRpcResponse > {
168+ async fn send_request ( & mut self , request : JsonRpcRequest ) -> anyhow :: Result < JsonRpcResponse > {
169169 // Serialize request to JSON
170170 let mut request = serde_json:: to_string ( & request) ?;
171171 request. push ( '\n' ) ;
172172
173173 // Write request as line.
174- self . writer . write_all ( request. as_bytes ( ) ) . await ?;
175- self . writer . flush ( ) . await ?;
174+ self . writer . write_all ( request. as_bytes ( ) ) . await . context ( "write" ) ?;
175+ self . writer . flush ( ) . await . context ( "flush" ) ?;
176176
177177 // Read response line.
178178 let mut response_line = String :: new ( ) ;
179- self . reader . read_line ( & mut response_line) . await ?;
179+ self . reader . read_line ( & mut response_line) . await . context ( "read" ) ?;
180180
181181 if response_line. trim ( ) . is_empty ( ) {
182182 anyhow:: bail!( "empty response" ) ;
@@ -189,7 +189,7 @@ impl McpClient {
189189 }
190190
191191 /// Internal helper to send an initialize request.
192- async fn initialize ( & mut self ) -> Result < InitializeResult > {
192+ async fn initialize ( & mut self ) -> anyhow :: Result < InitializeResult > {
193193 let request = JsonRpcRequest {
194194 jsonrpc : "2.0" . to_owned ( ) ,
195195 id : Some ( self . next_id ( ) ) ,
@@ -210,42 +210,38 @@ impl McpClient {
210210 }
211211
212212 /// List available tools.
213- pub async fn list_tools ( & mut self ) -> Result < ToolsListResult > {
213+ pub async fn list_tools ( & mut self ) -> anyhow :: Result < ToolsListResult > {
214214 let request = JsonRpcRequest {
215215 jsonrpc : "2.0" . to_owned ( ) ,
216216 id : Some ( self . next_id ( ) ) ,
217217 method : "tools/list" . to_owned ( ) ,
218218 params : None ,
219219 } ;
220220
221- let response = self . send_request ( request) . await ?;
221+ let response = self . send_request ( request) . await . context ( "send request" ) ?;
222222 if let Some ( error) = response. error {
223223 anyhow:: bail!( "JSON-RPC error {}: {}" , error. code, error. message) ;
224224 }
225225
226- let result = response
227- . result
228- . ok_or_else ( || anyhow:: anyhow!( "missing result in response" ) ) ?;
226+ let result = response. result . context ( "missing result in response" ) ?;
229227 Ok ( serde_json:: from_value ( result) ?)
230228 }
231229
232230 /// Call a tool.
233- pub async fn call_tool ( & mut self , params : ToolCallParams ) -> Result < ToolCallResult > {
231+ pub async fn call_tool ( & mut self , params : ToolCallParams ) -> anyhow :: Result < ToolCallResult > {
234232 let request = JsonRpcRequest {
235233 jsonrpc : "2.0" . to_owned ( ) ,
236234 id : Some ( self . next_id ( ) ) ,
237235 method : "tools/call" . to_owned ( ) ,
238236 params : Some ( serde_json:: to_value ( params) ?) ,
239237 } ;
240238
241- let response = self . send_request ( request) . await ?;
239+ let response = self . send_request ( request) . await . context ( "send request" ) ?;
242240 if let Some ( error) = response. error {
243241 anyhow:: bail!( "JSON-RPC error {}: {}" , error. code, error. message) ;
244242 }
245243
246- let result = response
247- . result
248- . ok_or_else ( || anyhow:: anyhow!( "Missing result in response" ) ) ?;
244+ let result = response. result . context ( "missing result in response" ) ?;
249245 Ok ( serde_json:: from_value ( result) ?)
250246 }
251247
@@ -254,7 +250,7 @@ impl McpClient {
254250 & mut self ,
255251 method : impl Into < String > ,
256252 params : Option < serde_json:: Value > ,
257- ) -> Result < ( ) > {
253+ ) -> anyhow :: Result < ( ) > {
258254 let request = JsonRpcRequest {
259255 jsonrpc : "2.0" . to_owned ( ) ,
260256 id : None , // Notifications have no ID.
0 commit comments