@@ -381,24 +381,23 @@ public Mono<Void> addTools(List<McpServerFeatures.AsyncToolSpecification> toolSp
381381 return Mono .empty ();
382382 }
383383
384- List < McpServerFeatures .AsyncToolSpecification > wrappedToolSpecifications ;
384+ Map < String , McpServerFeatures .AsyncToolSpecification > wrappedToolSpecificationsByName ;
385385 try {
386- wrappedToolSpecifications = sanitizeToolSpecifications (toolSpecifications );
386+ wrappedToolSpecificationsByName = sanitizeToolSpecifications (toolSpecifications );
387387 }
388388 catch (IllegalArgumentException e ) {
389389 return Mono .error (e );
390390 }
391391 if (this .serverCapabilities .tools () == null ) {
392392 return Mono .error (new IllegalStateException ("Server must be configured with tool capabilities" ));
393393 }
394- Set <String > toolNames = new HashSet <>(
395- wrappedToolSpecifications .stream ().map (tool -> tool .tool ().name ()).toList ());
396394
397395 return Mono .defer (() -> {
398- this .tools .removeIf (toolSpecification -> toolNames .contains (toolSpecification .tool ().name ()));
399- this .tools .addAll (wrappedToolSpecifications );
396+ this .tools .removeIf (
397+ toolSpecification -> wrappedToolSpecificationsByName .containsKey (toolSpecification .tool ().name ()));
398+ this .tools .addAll (wrappedToolSpecificationsByName .values ());
400399
401- logger .debug ("Added tool handlers: {}" , toolNames );
400+ logger .debug ("Added tool handlers: {}" , wrappedToolSpecificationsByName . keySet () );
402401
403402 if (this .serverCapabilities .tools ().listChanged ()) {
404403 return notifyToolsListChanged ();
@@ -407,7 +406,7 @@ public Mono<Void> addTools(List<McpServerFeatures.AsyncToolSpecification> toolSp
407406 });
408407 }
409408
410- private List < McpServerFeatures .AsyncToolSpecification > sanitizeToolSpecifications (
409+ private Map < String , McpServerFeatures .AsyncToolSpecification > sanitizeToolSpecifications (
411410 List <McpServerFeatures .AsyncToolSpecification > toolSpecifications ) {
412411 LinkedHashMap <String , McpServerFeatures .AsyncToolSpecification > toolSpecificationsByName = new LinkedHashMap <>();
413412
@@ -425,7 +424,7 @@ private List<McpServerFeatures.AsyncToolSpecification> sanitizeToolSpecification
425424 toolSpecificationsByName .put (wrappedToolSpecification .tool ().name (), wrappedToolSpecification );
426425 }
427426
428- return new ArrayList <>( toolSpecificationsByName . values ()) ;
427+ return toolSpecificationsByName ;
429428 }
430429
431430 private static class StructuredOutputCallToolHandler
0 commit comments