2121import java .util .List ;
2222import java .util .Map ;
2323import java .util .Set ;
24+ import java .util .concurrent .CompletionException ;
2425import java .util .concurrent .ConcurrentHashMap ;
26+ import java .util .concurrent .ExecutionException ;
2527import java .util .concurrent .atomic .AtomicReference ;
2628import java .util .function .Consumer ;
2729import java .util .stream .Collectors ;
@@ -333,17 +335,13 @@ public void initializeProxies(Consumer<JsonRpcResponse> responseWriter) {
333335 }
334336
335337 for (McpServerProxy proxy : proxies .values ()) {
336- try {
337- if (initRequest != null ) {
338- proxy .initialize (responseWriter , initRequest , protocolVersion );
339- }
338+ if (initRequest != null ) {
339+ proxy .initialize (responseWriter , initRequest , protocolVersion );
340+ }
340341
341- List <ToolInfo > proxyTools = proxy .listTools ();
342- for (var toolInfo : proxyTools ) {
343- tools .put (toolInfo .getName (), new Tool (toolInfo , proxy .name (), proxy ));
344- }
345- } catch (Exception e ) {
346- LOG .error ("Failed to initialize proxy: " + proxy .name (), e );
342+ List <ToolInfo > proxyTools = proxy .listTools ();
343+ for (var toolInfo : proxyTools ) {
344+ tools .put (toolInfo .getName (), new Tool (toolInfo , proxy .name (), proxy ));
347345 }
348346 }
349347 }
@@ -457,8 +455,9 @@ private JsonRpcResponse createErrorResponse(JsonRpcRequest req, Exception except
457455 return createErrorResponse (req , exception , true ); //TODO change the default to false.
458456 }
459457
460- private JsonRpcResponse createErrorResponse (JsonRpcRequest req , Exception exception , boolean sendStackTrace ) {
458+ private JsonRpcResponse createErrorResponse (JsonRpcRequest req , Throwable exception , boolean sendStackTrace ) {
461459 String s ;
460+ exception = unwrapException (exception );
462461 if (sendStackTrace ) {
463462 try (var sw = new StringWriter ();
464463 var pw = new PrintWriter (sw )) {
@@ -474,6 +473,14 @@ private JsonRpcResponse createErrorResponse(JsonRpcRequest req, Exception except
474473 return createErrorResponse (req , s );
475474 }
476475
476+ private Throwable unwrapException (Throwable exception ) {
477+ return switch (exception ) {
478+ case CompletionException ce when ce .getCause () != null -> ce .getCause ();
479+ case ExecutionException ee when ee .getCause () != null -> ee .getCause ();
480+ default -> exception ;
481+ };
482+ }
483+
477484 private JsonRpcResponse createErrorResponse (JsonRpcRequest req , String s ) {
478485 var error = JsonRpcErrorResponse .builder ()
479486 .code (500 )
0 commit comments