|
17 | 17 | import io.modelcontextprotocol.json.McpJsonDefaults; |
18 | 18 | import io.modelcontextprotocol.json.McpJsonMapper; |
19 | 19 | import io.modelcontextprotocol.json.TypeRef; |
| 20 | +import io.modelcontextprotocol.json.schema.JsonSchemaValidator; |
20 | 21 | import io.modelcontextprotocol.modern.JsonRpc.JSONRPCMessage; |
21 | 22 | import io.modelcontextprotocol.modern.JsonRpc.JSONRPCNotification; |
22 | 23 | import io.modelcontextprotocol.modern.JsonRpc.JSONRPCRequest; |
|
35 | 36 | import io.modelcontextprotocol.modern.McpSchema.ServerCapabilities; |
36 | 37 | import io.modelcontextprotocol.modern.McpSchema.UnsupportedProtocolVersionData; |
37 | 38 | import io.modelcontextprotocol.modern.server.feature.McpChangePublisher; |
| 39 | +import io.modelcontextprotocol.modern.server.feature.McpServerTool; |
| 40 | +import io.modelcontextprotocol.modern.server.feature.ToolsFeature; |
38 | 41 | import io.modelcontextprotocol.util.Assert; |
39 | 42 | import org.slf4j.Logger; |
40 | 43 | import org.slf4j.LoggerFactory; |
@@ -338,6 +341,10 @@ public static final class Builder { |
338 | 341 |
|
339 | 342 | private final List<McpFeature> features = new ArrayList<>(); |
340 | 343 |
|
| 344 | + private final Map<String, McpServerTool> tools = new LinkedHashMap<>(); |
| 345 | + |
| 346 | + private JsonSchemaValidator jsonSchemaValidator; |
| 347 | + |
341 | 348 | private long discoverTtlMs = 0L; |
342 | 349 |
|
343 | 350 | private CacheScope discoverCacheScope = CacheScope.PRIVATE; |
@@ -382,6 +389,34 @@ public Builder features(List<? extends McpFeature> features) { |
382 | 389 | return this; |
383 | 390 | } |
384 | 391 |
|
| 392 | + /** |
| 393 | + * Registers a tool, served in registration order by a {@link ToolsFeature} built |
| 394 | + * at {@link #build()}. No other feature may serve {@code tools/*} then. |
| 395 | + */ |
| 396 | + public Builder tool(McpServerTool tool) { |
| 397 | + Assert.notNull(tool, "tool must not be null"); |
| 398 | + if (this.tools.putIfAbsent(tool.tool().name(), tool) != null) { |
| 399 | + throw new IllegalArgumentException("Tool '" + tool.tool().name() + "' is already registered"); |
| 400 | + } |
| 401 | + return this; |
| 402 | + } |
| 403 | + |
| 404 | + public Builder tools(List<McpServerTool> tools) { |
| 405 | + Assert.notNull(tools, "tools must not be null"); |
| 406 | + tools.forEach(this::tool); |
| 407 | + return this; |
| 408 | + } |
| 409 | + |
| 410 | + /** |
| 411 | + * The validator for the arguments and structured output of tools registered with |
| 412 | + * {@link #tool}. Defaults to {@link McpJsonDefaults#getSchemaValidator()}. |
| 413 | + */ |
| 414 | + public Builder jsonSchemaValidator(JsonSchemaValidator jsonSchemaValidator) { |
| 415 | + Assert.notNull(jsonSchemaValidator, "jsonSchemaValidator must not be null"); |
| 416 | + this.jsonSchemaValidator = jsonSchemaValidator; |
| 417 | + return this; |
| 418 | + } |
| 419 | + |
385 | 420 | /** |
386 | 421 | * Registers {@code subscriptions/listen}, backed by {@code publisher}, for the |
387 | 422 | * tools, prompts and resources features that are registered. |
@@ -418,6 +453,12 @@ public McpServer build() { |
418 | 453 | McpJsonMapper mapper = this.jsonMapper != null ? this.jsonMapper : McpJsonDefaults.getMapper(); |
419 | 454 |
|
420 | 455 | List<McpFeature> allFeatures = new ArrayList<>(this.features); |
| 456 | + if (!this.tools.isEmpty()) { |
| 457 | + JsonSchemaValidator validator = this.jsonSchemaValidator != null ? this.jsonSchemaValidator |
| 458 | + : McpJsonDefaults.getSchemaValidator(); |
| 459 | + allFeatures.add(registeredWith("tool(...)", ToolsFeature.ofTools(List.copyOf(this.tools.values()), |
| 460 | + mapper, validator, 0L, CacheScope.PRIVATE))); |
| 461 | + } |
421 | 462 |
|
422 | 463 | ServerCapabilities.Builder capabilitiesBuilder = ServerCapabilities.builder(); |
423 | 464 | for (McpFeature feature : allFeatures) { |
@@ -462,6 +503,22 @@ public McpServer build() { |
462 | 503 | Set.copyOf(inputRequiredMethods), codec, subscriptionsFeature); |
463 | 504 | } |
464 | 505 |
|
| 506 | + // Checked here rather than by the method collision check in build(), whose |
| 507 | + // "served by both ToolsFeature and ToolsFeature" would not say which |
| 508 | + // registration to move. |
| 509 | + private McpFeature registeredWith(String shortcut, McpFeature feature) { |
| 510 | + for (McpFeature explicit : this.features) { |
| 511 | + for (String method : feature.methods()) { |
| 512 | + if (explicit.methods().contains(method)) { |
| 513 | + throw new IllegalStateException("Primitives registered with " + shortcut |
| 514 | + + " cannot be combined with " + explicit.getClass().getName() + ", which also serves '" |
| 515 | + + method + "'; register them on that feature's repository instead"); |
| 516 | + } |
| 517 | + } |
| 518 | + } |
| 519 | + return feature; |
| 520 | + } |
| 521 | + |
465 | 522 | } |
466 | 523 |
|
467 | 524 | } |
0 commit comments