Skip to content

Commit fe268a5

Browse files
committed
fix(mcp): update tests to use callHandler instead of deprecated call method
- Replace deprecated call() method with callHandler() in ToolUtilsTests and McpToolUtils - Fix NullPointerException in sync/async tool specification tests - Update MCP API usage to match current implementation Resolves test failures in spring-ai-mcp module. Signed-off-by: yinh <[email protected]>
1 parent 5b7302a commit fe268a5

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@ public static McpServerFeatures.AsyncToolSpecification toAsyncToolSpecification(
320320

321321
McpServerFeatures.SyncToolSpecification syncToolSpecification = toSyncToolSpecification(toolCallback, mimeType);
322322

323-
return new AsyncToolSpecification(syncToolSpecification.tool(),
324-
(exchange, map) -> Mono
325-
.fromCallable(() -> syncToolSpecification.call().apply(new McpSyncServerExchange(exchange), map))
323+
return new AsyncToolSpecification(syncToolSpecification.tool(), null,
324+
(exchange, request) -> Mono.fromCallable(
325+
() -> syncToolSpecification.callHandler().apply(new McpSyncServerExchange(exchange), request))
326326
.subscribeOn(Schedulers.boundedElastic()));
327327
}
328328

mcp/common/src/test/java/org/springframework/ai/mcp/ToolUtilsTests.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification;
2727
import io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification;
2828
import io.modelcontextprotocol.server.McpSyncServerExchange;
29+
import io.modelcontextprotocol.spec.McpSchema;
2930
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
3031
import io.modelcontextprotocol.spec.McpSchema.Implementation;
3132
import io.modelcontextprotocol.spec.McpSchema.ListToolsResult;
@@ -226,7 +227,10 @@ void toSyncToolSpecificationShouldConvertSingleCallback() {
226227
assertThat(toolSpecification).isNotNull();
227228
assertThat(toolSpecification.tool().name()).isEqualTo("test");
228229

229-
CallToolResult result = toolSpecification.call().apply(mock(McpSyncServerExchange.class), Map.of());
230+
McpSchema.CallToolRequest mockRequest = mock(McpSchema.CallToolRequest.class);
231+
when(mockRequest.arguments()).thenReturn(Map.of());
232+
233+
CallToolResult result = toolSpecification.callHandler().apply(mock(McpSyncServerExchange.class), mockRequest);
230234
TextContent content = (TextContent) result.content().get(0);
231235
assertThat(content.text()).isEqualTo("success");
232236
assertThat(result.isError()).isFalse();
@@ -239,7 +243,11 @@ void toSyncToolSpecificationShouldHandleError() {
239243
SyncToolSpecification toolSpecification = McpToolUtils.toSyncToolSpecification(callback);
240244

241245
assertThat(toolSpecification).isNotNull();
242-
CallToolResult result = toolSpecification.call().apply(mock(McpSyncServerExchange.class), Map.of());
246+
247+
McpSchema.CallToolRequest mockRequest = mock(McpSchema.CallToolRequest.class);
248+
when(mockRequest.arguments()).thenReturn(Map.of());
249+
250+
CallToolResult result = toolSpecification.callHandler().apply(mock(McpSyncServerExchange.class), mockRequest);
243251
TextContent content = (TextContent) result.content().get(0);
244252
assertThat(content.text()).isEqualTo("error");
245253
assertThat(result.isError()).isTrue();
@@ -267,7 +275,10 @@ void toAsyncToolSpecificationShouldConvertSingleCallback() {
267275
assertThat(toolSpecification).isNotNull();
268276
assertThat(toolSpecification.tool().name()).isEqualTo("test");
269277

270-
StepVerifier.create(toolSpecification.call().apply(mock(McpAsyncServerExchange.class), Map.of()))
278+
McpSchema.CallToolRequest mockRequest = mock(McpSchema.CallToolRequest.class);
279+
when(mockRequest.arguments()).thenReturn(Map.of());
280+
281+
StepVerifier.create(toolSpecification.callHandler().apply(mock(McpAsyncServerExchange.class), mockRequest))
271282
.assertNext(result -> {
272283
TextContent content = (TextContent) result.content().get(0);
273284
assertThat(content.text()).isEqualTo("success");
@@ -283,7 +294,11 @@ void toAsyncToolSpecificationShouldHandleError() {
283294
AsyncToolSpecification toolSpecification = McpToolUtils.toAsyncToolSpecification(callback);
284295

285296
assertThat(toolSpecification).isNotNull();
286-
StepVerifier.create(toolSpecification.call().apply(mock(McpAsyncServerExchange.class), Map.of()))
297+
298+
McpSchema.CallToolRequest mockRequest = mock(McpSchema.CallToolRequest.class);
299+
when(mockRequest.arguments()).thenReturn(Map.of());
300+
301+
StepVerifier.create(toolSpecification.callHandler().apply(mock(McpAsyncServerExchange.class), mockRequest))
287302
.assertNext(result -> {
288303
TextContent content = (TextContent) result.content().get(0);
289304
assertThat(content.text()).isEqualTo("error");

0 commit comments

Comments
 (0)