Skip to content

Commit d712c96

Browse files
committed
refactor(McpToolUtils): Use builder pattern instead of deprecated constructors
- Update SyncToolSpecification creation to use builder pattern - Update AsyncToolSpecification creation to use builder pattern - Improve code readability and maintainability Signed-off-by: codeboyzhou <[email protected]>
1 parent aa590e8 commit d712c96

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

mcp/common/src/main/java/org/springframework/ai/mcp/McpToolUtils.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import io.modelcontextprotocol.client.McpAsyncClient;
2727
import io.modelcontextprotocol.client.McpSyncClient;
2828
import io.modelcontextprotocol.server.McpServerFeatures;
29-
import io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification;
3029
import io.modelcontextprotocol.server.McpSyncServerExchange;
3130
import io.modelcontextprotocol.spec.McpSchema;
3231
import io.modelcontextprotocol.spec.McpSchema.Role;
@@ -170,21 +169,21 @@ public static McpServerFeatures.SyncToolSpecification toSyncToolSpecification(To
170169
var tool = new McpSchema.Tool(toolCallback.getToolDefinition().name(),
171170
toolCallback.getToolDefinition().description(), toolCallback.getToolDefinition().inputSchema());
172171

173-
return new McpServerFeatures.SyncToolSpecification(tool, (exchange, request) -> {
172+
return McpServerFeatures.SyncToolSpecification.builder().tool(tool).callHandler((exchange, request) -> {
174173
try {
175174
String callResult = toolCallback.call(ModelOptionsUtils.toJsonString(request),
176175
new ToolContext(Map.of(TOOL_CONTEXT_MCP_EXCHANGE_KEY, exchange)));
177176
if (mimeType != null && mimeType.toString().startsWith("image")) {
178-
return new McpSchema.CallToolResult(List
179-
.of(new McpSchema.ImageContent(List.of(Role.ASSISTANT), null, callResult, mimeType.toString())),
180-
false);
177+
McpSchema.Annotations annotations = new McpSchema.Annotations(List.of(Role.ASSISTANT), null);
178+
return new McpSchema.CallToolResult(
179+
List.of(new McpSchema.ImageContent(annotations, callResult, mimeType.toString())), false);
181180
}
182181
return new McpSchema.CallToolResult(List.of(new McpSchema.TextContent(callResult)), false);
183182
}
184183
catch (Exception e) {
185184
return new McpSchema.CallToolResult(List.of(new McpSchema.TextContent(e.getMessage())), true);
186185
}
187-
});
186+
}).build();
188187
}
189188

190189
/**
@@ -287,10 +286,13 @@ public static McpServerFeatures.AsyncToolSpecification toAsyncToolSpecification(
287286

288287
McpServerFeatures.SyncToolSpecification syncToolSpecification = toSyncToolSpecification(toolCallback, mimeType);
289288

290-
return new AsyncToolSpecification(syncToolSpecification.tool(),
291-
(exchange, map) -> Mono
292-
.fromCallable(() -> syncToolSpecification.call().apply(new McpSyncServerExchange(exchange), map))
293-
.subscribeOn(Schedulers.boundedElastic()));
289+
return McpServerFeatures.AsyncToolSpecification.builder()
290+
.tool(syncToolSpecification.tool())
291+
.callHandler((exchange, request) -> Mono
292+
.fromCallable(
293+
() -> syncToolSpecification.callHandler().apply(new McpSyncServerExchange(exchange), request))
294+
.subscribeOn(Schedulers.boundedElastic()))
295+
.build();
294296
}
295297

296298
/**

0 commit comments

Comments
 (0)