@@ -77,7 +77,7 @@ public class DefaultMcpClient implements McpClient {
7777 private final Object toolsLock = LockUtils .newSynchronizedLock ();
7878 private final Map <Long , Consumer <JsonRpc .Response <Long >>> responseConsumers = new ConcurrentHashMap <>();
7979 private final Map <Long , Boolean > pendingRequests = new ConcurrentHashMap <>();
80- private final Map <Long , Object > pendingResults = new ConcurrentHashMap <>();
80+ private final Map <Long , Result > pendingResults = new ConcurrentHashMap <>();
8181
8282 private volatile Subscription subscription ;
8383 private volatile ThreadPoolScheduler pingScheduler ;
@@ -197,10 +197,6 @@ private void initializedMcpServer(JsonRpc.Response<Long> response) {
197197 response .error ());
198198 throw new IllegalStateException (response .error ().toString ());
199199 }
200- synchronized (this .initializedLock ) {
201- this .initialized = true ;
202- this .initializedLock .notifyAll ();
203- }
204200 this .recordServerSchema (response );
205201 HttpClassicClientRequest request =
206202 this .client .createRequest (HttpRequestMethod .POST , this .baseUri + this .messageEndpoint );
@@ -225,6 +221,10 @@ private void initializedMcpServer(JsonRpc.Response<Long> response) {
225221 } catch (IOException e ) {
226222 throw new IllegalStateException (e );
227223 }
224+ synchronized (this .initializedLock ) {
225+ this .initialized = true ;
226+ this .initializedLock .notifyAll ();
227+ }
228228 this .pingScheduler = ThreadPoolScheduler .custom ()
229229 .threadPoolName ("mcp-client-ping-" + this .name )
230230 .awaitTermination (3 , TimeUnit .SECONDS )
@@ -262,16 +262,24 @@ public List<Tool> getTools() {
262262 while (this .pendingRequests .get (requestId )) {
263263 ThreadUtils .sleep (100 );
264264 }
265- synchronized (this .toolsLock ) {
266- return this .tools ;
265+ Result result = this .pendingResults .remove (requestId );
266+ this .pendingRequests .remove (requestId );
267+ if (result .isSuccess ()) {
268+ synchronized (this .toolsLock ) {
269+ return this .tools ;
270+ }
271+ } else {
272+ throw new IllegalStateException (result .getError ());
267273 }
274+
268275 }
269276
270277 private void getTools0 (JsonRpc .Response <Long > response ) {
271278 if (response .error () != null ) {
272- log . error ("Failed to get tools list from MCP server. [sessionId={}, response={}]" ,
279+ String error = StringUtils . format ("Failed to get tools list from MCP server. [sessionId={0 }, response={1 }]" ,
273280 this .sessionId ,
274281 response );
282+ this .pendingResults .put (response .id (), Result .error (error ));
275283 this .pendingRequests .put (response .id (), false );
276284 return ;
277285 }
@@ -283,6 +291,7 @@ private void getTools0(JsonRpc.Response<Long> response) {
283291 .map (rawTool -> ObjectUtils .<Tool >toCustomObject (rawTool , Tool .class ))
284292 .toList ());
285293 }
294+ this .pendingResults .put (response .id (), Result .success (this .tools ));
286295 this .pendingRequests .put (response .id (), false );
287296 }
288297
@@ -303,32 +312,46 @@ public Object callTool(String name, Map<String, Object> arguments) {
303312 while (this .pendingRequests .get (requestId )) {
304313 ThreadUtils .sleep (100 );
305314 }
306- return this .pendingResults .get (requestId );
315+ Result result = this .pendingResults .remove (requestId );
316+ this .pendingRequests .remove (requestId );
317+ if (result .isSuccess ()) {
318+ return result .getContent ();
319+ } else {
320+ throw new IllegalStateException (result .getError ());
321+ }
307322 }
308323
309324 private void callTools0 (JsonRpc .Response <Long > response ) {
310325 if (response .error () != null ) {
311- log .error ("Failed to call tool from MCP server. [sessionId={}, response={}]" , this .sessionId , response );
326+ String error = StringUtils .format ("Failed to call tool from MCP server. [sessionId={0}, response={1}]" ,
327+ this .sessionId ,
328+ response );
329+ this .pendingResults .put (response .id (), Result .error (error ));
312330 this .pendingRequests .put (response .id (), false );
313331 return ;
314332 }
315333 Map <String , Object > result = cast (response .result ());
316334 boolean isError = cast (result .get ("isError" ));
317335 if (isError ) {
318- log .error ("Failed to call tool from MCP server. [sessionId={}, result={}]" , this .sessionId , result );
336+ String error = StringUtils .format ("Failed to call tool from MCP server. [sessionId={0}, result={1}]" ,
337+ this .sessionId ,
338+ result );
339+ this .pendingResults .put (response .id (), Result .error (error ));
319340 this .pendingRequests .put (response .id (), false );
320341 return ;
321342 }
322343 List <Map <String , Object >> rawContents = cast (result .get ("content" ));
323344 if (CollectionUtils .isEmpty (rawContents )) {
324- log .error ("Failed to call tool from MCP server: no result returned. [sessionId={}, result={}]" ,
345+ String error = StringUtils .format (
346+ "Failed to call tool from MCP server: no result returned. [sessionId={0}, result={1}]" ,
325347 this .sessionId ,
326348 result );
349+ this .pendingResults .put (response .id (), Result .error (error ));
327350 this .pendingRequests .put (response .id (), false );
328351 return ;
329352 }
330353 Map <String , Object > rawContent = rawContents .get (0 );
331- this .pendingResults .put (response .id (), rawContent .get ("text" ));
354+ this .pendingResults .put (response .id (), Result . success ( rawContent .get ("text" ) ));
332355 this .pendingRequests .put (response .id (), false );
333356 }
334357
0 commit comments