-
Notifications
You must be signed in to change notification settings - Fork 872
feat: refactor the tool specifications #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,6 +208,12 @@ private AsyncSpecification(McpServerTransportProvider transportProvider) { | |
| this.transportProvider = transportProvider; | ||
| } | ||
|
|
||
| private void AssertNotDupplicateTool(String toolName) { | ||
| if (this.tools.stream().anyMatch(toolSpec -> toolSpec.tool().name().equals(toolName))) { | ||
| throw new IllegalArgumentException("Tool with name '" + toolName + "' is already registered."); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets the URI template manager factory to use for creating URI templates. This | ||
| * allows for custom URI template parsing and variable extraction. | ||
|
|
@@ -329,6 +335,7 @@ public AsyncSpecification tool(McpSchema.Tool tool, | |
| BiFunction<McpAsyncServerExchange, Map<String, Object>, Mono<CallToolResult>> handler) { | ||
| Assert.notNull(tool, "Tool must not be null"); | ||
| Assert.notNull(handler, "Handler must not be null"); | ||
| AssertNotDupplicateTool(tool.name()); | ||
|
|
||
| this.tools.add(new McpServerFeatures.AsyncToolSpecification(tool, handler).toToolCall()); | ||
|
|
||
|
|
@@ -353,6 +360,7 @@ public AsyncSpecification toolCall(McpSchema.Tool tool, | |
|
|
||
| Assert.notNull(tool, "Tool must not be null"); | ||
| Assert.notNull(handler, "Handler must not be null"); | ||
| AssertNotDupplicateTool(tool.name()); | ||
|
|
||
| this.tools.add(new McpServerFeatures.AsyncToolCallSpecification(tool, handler)); | ||
|
|
||
|
|
@@ -374,6 +382,12 @@ public AsyncSpecification toolCall(McpSchema.Tool tool, | |
| @Deprecated | ||
| public AsyncSpecification tools(List<McpServerFeatures.AsyncToolSpecification> toolSpecifications) { | ||
| Assert.notNull(toolSpecifications, "Tool handlers list must not be null"); | ||
|
|
||
| for (var tool : toolSpecifications) { | ||
| AssertNotDupplicateTool(tool.tool().name()); | ||
| this.tools.add(tool.toToolCall()); | ||
| } | ||
|
|
||
| this.tools.addAll(toolSpecifications.stream().map(s -> s.toToolCall()).toList()); | ||
| return this; | ||
| } | ||
|
|
@@ -390,7 +404,11 @@ public AsyncSpecification tools(List<McpServerFeatures.AsyncToolSpecification> t | |
| */ | ||
| public AsyncSpecification toolCalls(List<McpServerFeatures.AsyncToolCallSpecification> toolCallSpecifications) { | ||
| Assert.notNull(toolCallSpecifications, "Tool handlers list must not be null"); | ||
| this.tools.addAll(toolCallSpecifications); | ||
|
|
||
| for (var tool : toolCallSpecifications) { | ||
| AssertNotDupplicateTool(tool.tool().name()); | ||
| this.tools.add(tool); | ||
| } | ||
| return this; | ||
| } | ||
|
Comment on lines
+391
to
407
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hadn't considered until reviewing this just now, but this should probably do some minimal validation for things like duplicate tool names. Unsure if that's already planned as a more comprehensive backlog thing or if we should do this now.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @LucaButBoring , while we had some duplication validation for the runtime tool addition we lacked such for the initial tool registration. |
||
|
|
||
|
|
@@ -415,7 +433,9 @@ public AsyncSpecification toolCalls(List<McpServerFeatures.AsyncToolCallSpecific | |
| @Deprecated | ||
| public AsyncSpecification tools(McpServerFeatures.AsyncToolSpecification... toolSpecifications) { | ||
| Assert.notNull(toolSpecifications, "Tool handlers list must not be null"); | ||
|
|
||
| for (McpServerFeatures.AsyncToolSpecification tool : toolSpecifications) { | ||
| AssertNotDupplicateTool(tool.tool().name()); | ||
| this.tools.add(tool.toToolCall()); | ||
| } | ||
| return this; | ||
|
|
@@ -430,7 +450,9 @@ public AsyncSpecification tools(McpServerFeatures.AsyncToolSpecification... tool | |
| */ | ||
| public AsyncSpecification toolCalls(McpServerFeatures.AsyncToolCallSpecification... toolCallSpecifications) { | ||
| Assert.notNull(toolCallSpecifications, "Tool handlers list must not be null"); | ||
|
|
||
| for (McpServerFeatures.AsyncToolCallSpecification tool : toolCallSpecifications) { | ||
| AssertNotDupplicateTool(tool.tool().name()); | ||
| this.tools.add(tool); | ||
| } | ||
| return this; | ||
|
|
@@ -763,6 +785,12 @@ private SyncSpecification(McpServerTransportProvider transportProvider) { | |
| this.transportProvider = transportProvider; | ||
| } | ||
|
|
||
| private void AssertNotDupplicateTool(String toolName) { | ||
| if (this.tools.stream().anyMatch(toolSpec -> toolSpec.tool().name().equals(toolName))) { | ||
| throw new IllegalArgumentException("Tool with name '" + toolName + "' is already registered."); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets the URI template manager factory to use for creating URI templates. This | ||
| * allows for custom URI template parsing and variable extraction. | ||
|
|
@@ -883,6 +911,7 @@ public SyncSpecification tool(McpSchema.Tool tool, | |
| BiFunction<McpSyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> handler) { | ||
| Assert.notNull(tool, "Tool must not be null"); | ||
| Assert.notNull(handler, "Handler must not be null"); | ||
| AssertNotDupplicateTool(tool.name()); | ||
|
|
||
| this.tools.add(new McpServerFeatures.SyncToolSpecification(tool, handler).toToolCall()); | ||
|
|
||
|
|
@@ -906,6 +935,7 @@ public SyncSpecification toolCall(McpSchema.Tool tool, | |
| BiFunction<McpSyncServerExchange, McpSchema.CallToolRequest, McpSchema.CallToolResult> handler) { | ||
| Assert.notNull(tool, "Tool must not be null"); | ||
| Assert.notNull(handler, "Handler must not be null"); | ||
| AssertNotDupplicateTool(tool.name()); | ||
|
|
||
| this.tools.add(new McpServerFeatures.SyncToolCallSpecification(tool, handler)); | ||
|
|
||
|
|
@@ -927,7 +957,14 @@ public SyncSpecification toolCall(McpSchema.Tool tool, | |
| @Deprecated | ||
| public SyncSpecification tools(List<McpServerFeatures.SyncToolSpecification> toolSpecifications) { | ||
| Assert.notNull(toolSpecifications, "Tool handlers list must not be null"); | ||
| this.tools.addAll(toolSpecifications.stream().map(s -> s.toToolCall()).toList()); | ||
|
|
||
| for (var tool : toolSpecifications) { | ||
| String toolName = tool.tool().name(); | ||
| AssertNotDupplicateTool(toolName); // Check against existing tools | ||
| this.tools.add(tool.toToolCall()); // Add the tool call specification | ||
| // directly | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
|
|
@@ -942,7 +979,12 @@ public SyncSpecification tools(List<McpServerFeatures.SyncToolSpecification> too | |
| */ | ||
| public SyncSpecification toolCalls(List<McpServerFeatures.SyncToolCallSpecification> toolCallSpecifications) { | ||
| Assert.notNull(toolCallSpecifications, "Tool handlers list must not be null"); | ||
| this.tools.addAll(toolCallSpecifications); | ||
|
|
||
| for (var tool : toolCallSpecifications) { | ||
| AssertNotDupplicateTool(tool.tool().name()); | ||
| this.tools.add(tool); | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
|
|
@@ -969,7 +1011,9 @@ public SyncSpecification toolCalls(List<McpServerFeatures.SyncToolCallSpecificat | |
| @Deprecated | ||
| public SyncSpecification tools(McpServerFeatures.SyncToolSpecification... toolSpecifications) { | ||
| Assert.notNull(toolSpecifications, "Tool handlers list must not be null"); | ||
|
|
||
| for (McpServerFeatures.SyncToolSpecification tool : toolSpecifications) { | ||
| AssertNotDupplicateTool(tool.tool().name()); | ||
| this.tools.add(tool.toToolCall()); | ||
| } | ||
| return this; | ||
|
|
@@ -985,7 +1029,9 @@ public SyncSpecification tools(McpServerFeatures.SyncToolSpecification... toolSp | |
| */ | ||
| public SyncSpecification toolCalls(McpServerFeatures.SyncToolCallSpecification... toolCallSpecifications) { | ||
| Assert.notNull(toolCallSpecifications, "Tool handlers list must not be null"); | ||
|
|
||
| for (McpServerFeatures.SyncToolCallSpecification tool : toolCallSpecifications) { | ||
| AssertNotDupplicateTool(tool.tool().name()); | ||
| this.tools.add(tool); | ||
| } | ||
| return this; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: method name ->
assertNotDuplicateTool