refactor: Replace Map<String, Object> with CallToolRequest in StructuredOutputCallToolHandler

Follow up to #357 to update tool call handler signature from Map<String, Object>
to McpSchema.CallToolRequest for consistency with changes in #375.

- Change BiFunction parameter from Map<String, Object> to McpSchema.CallToolRequest for better type safety
- Update method signature to accept CallToolRequest instead of raw arguments map
- Replace toolSpecification.call() with toolSpecification.callHandler()
- Migrate to builder pattern for AsyncToolSpecification construction

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
Christian Tzolov 2025-07-17 13:59:09 +02:00 • committed by Christian Tzolov
commit 53f7b77edf

View file

@ -313,9 +313,9 @@ public class McpAsyncServer {
} }
private static class StructuredOutputCallToolHandler private static class StructuredOutputCallToolHandler
implements BiFunction<McpAsyncServerExchange, Map<String, Object>, Mono<McpSchema.CallToolResult>> { implements BiFunction<McpAsyncServerExchange, McpSchema.CallToolRequest, Mono<McpSchema.CallToolResult>> {
private final BiFunction<McpAsyncServerExchange, Map<String, Object>, Mono<McpSchema.CallToolResult>> delegateCallToolResult; private final BiFunction<McpAsyncServerExchange, McpSchema.CallToolRequest, Mono<McpSchema.CallToolResult>> delegateCallToolResult;
private final JsonSchemaValidator jsonSchemaValidator; private final JsonSchemaValidator jsonSchemaValidator;
@ -323,7 +323,7 @@ public class McpAsyncServer {
public StructuredOutputCallToolHandler(JsonSchemaValidator jsonSchemaValidator, public StructuredOutputCallToolHandler(JsonSchemaValidator jsonSchemaValidator,
Map<String, Object> outputSchema, Map<String, Object> outputSchema,
BiFunction<McpAsyncServerExchange, Map<String, Object>, Mono<McpSchema.CallToolResult>> delegateHandler) { BiFunction<McpAsyncServerExchange, McpSchema.CallToolRequest, Mono<McpSchema.CallToolResult>> delegateHandler) {
Assert.notNull(jsonSchemaValidator, "JsonSchemaValidator must not be null"); Assert.notNull(jsonSchemaValidator, "JsonSchemaValidator must not be null");
Assert.notNull(delegateHandler, "Delegate call tool result handler must not be null"); Assert.notNull(delegateHandler, "Delegate call tool result handler must not be null");
@ -334,9 +334,9 @@ public class McpAsyncServer {
} }
@Override @Override
public Mono<CallToolResult> apply(McpAsyncServerExchange exchange, Map<String, Object> arguments) { public Mono<CallToolResult> apply(McpAsyncServerExchange exchange, McpSchema.CallToolRequest request) {
return this.delegateCallToolResult.apply(exchange, arguments).map(result -> { return this.delegateCallToolResult.apply(exchange, request).map(result -> {
if (outputSchema == null) { if (outputSchema == null) {
if (result.structuredContent() != null) { if (result.structuredContent() != null) {
@ -398,7 +398,7 @@ public class McpAsyncServer {
private static McpServerFeatures.AsyncToolSpecification withStructuredOutputHandling( private static McpServerFeatures.AsyncToolSpecification withStructuredOutputHandling(
JsonSchemaValidator jsonSchemaValidator, McpServerFeatures.AsyncToolSpecification toolSpecification) { JsonSchemaValidator jsonSchemaValidator, McpServerFeatures.AsyncToolSpecification toolSpecification) {
if (toolSpecification.call() instanceof StructuredOutputCallToolHandler) { if (toolSpecification.callHandler() instanceof StructuredOutputCallToolHandler) {
// If the tool is already wrapped, return it as is // If the tool is already wrapped, return it as is
return toolSpecification; return toolSpecification;
} }
@ -408,9 +408,11 @@ public class McpAsyncServer {
return toolSpecification; return toolSpecification;
} }
return new McpServerFeatures.AsyncToolSpecification(toolSpecification.tool(), return McpServerFeatures.AsyncToolSpecification.builder()
new StructuredOutputCallToolHandler(jsonSchemaValidator, toolSpecification.tool().outputSchema(), .tool(toolSpecification.tool())
toolSpecification.call())); .callHandler(new StructuredOutputCallToolHandler(jsonSchemaValidator,
toolSpecification.tool().outputSchema(), toolSpecification.callHandler()))
.build();
} }
/** /**