Skip to content

Commit 428b8fe

Browse files
committed
Refactor tool specification handling to use maps for improved lookup efficiency.
1 parent efa1b33 commit 428b8fe

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

mcp-core/src/main/java/io/modelcontextprotocol/server/McpStatelessAsyncServer.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,30 +370,29 @@ public Mono<Void> addTools(List<McpStatelessServerFeatures.AsyncToolSpecificatio
370370
return Mono.empty();
371371
}
372372

373-
List<McpStatelessServerFeatures.AsyncToolSpecification> wrappedToolSpecifications;
373+
Map<String, McpStatelessServerFeatures.AsyncToolSpecification> wrappedToolSpecificationsByName;
374374
try {
375-
wrappedToolSpecifications = sanitizeToolSpecifications(toolSpecifications);
375+
wrappedToolSpecificationsByName = sanitizeToolSpecifications(toolSpecifications);
376376
}
377377
catch (IllegalArgumentException e) {
378378
return Mono.error(e);
379379
}
380380
if (this.serverCapabilities.tools() == null) {
381381
return Mono.error(new IllegalStateException("Server must be configured with tool capabilities"));
382382
}
383-
Set<String> toolNames = new HashSet<>(
384-
wrappedToolSpecifications.stream().map(tool -> tool.tool().name()).toList());
385383

386384
return Mono.defer(() -> {
387-
this.tools.removeIf(toolSpecification -> toolNames.contains(toolSpecification.tool().name()));
388-
this.tools.addAll(wrappedToolSpecifications);
385+
this.tools.removeIf(
386+
toolSpecification -> wrappedToolSpecificationsByName.containsKey(toolSpecification.tool().name()));
387+
this.tools.addAll(wrappedToolSpecificationsByName.values());
389388

390-
logger.debug("Added tool handlers: {}", toolNames);
389+
logger.debug("Added tool handlers: {}", wrappedToolSpecificationsByName.keySet());
391390

392391
return Mono.empty();
393392
});
394393
}
395394

396-
private List<McpStatelessServerFeatures.AsyncToolSpecification> sanitizeToolSpecifications(
395+
private Map<String, McpStatelessServerFeatures.AsyncToolSpecification> sanitizeToolSpecifications(
397396
List<McpStatelessServerFeatures.AsyncToolSpecification> toolSpecifications) {
398397
LinkedHashMap<String, McpStatelessServerFeatures.AsyncToolSpecification> toolSpecificationsByName = new LinkedHashMap<>();
399398

@@ -411,7 +410,7 @@ private List<McpStatelessServerFeatures.AsyncToolSpecification> sanitizeToolSpec
411410
toolSpecificationsByName.put(wrappedToolSpecification.tool().name(), wrappedToolSpecification);
412411
}
413412

414-
return new ArrayList<>(toolSpecificationsByName.values());
413+
return toolSpecificationsByName;
415414
}
416415

417416
/**

0 commit comments

Comments
 (0)