Skip to content

Commit 58ed5ea

Browse files
committed
feat(mcp): handle ToolContext in MCP tool callbacks
- Implement the new ToolCallback.call(String, ToolContext) method in both Sync and Async MCP tool callbacks. - Since MCP tools don't support tool context, the implementation ignores the context parameter and delegates to the existing call(String) method. Added test to verify the behavior. Resolves #2378 Signed-off-by: Christian Tzolov <[email protected]>
1 parent 28bceb3 commit 58ed5ea

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
2323
import io.modelcontextprotocol.spec.McpSchema.Tool;
2424

25+
import org.springframework.ai.chat.model.ToolContext;
2526
import org.springframework.ai.model.ModelOptionsUtils;
2627
import org.springframework.ai.tool.ToolCallback;
2728
import org.springframework.ai.tool.definition.ToolDefinition;
@@ -110,4 +111,10 @@ public String call(String functionInput) {
110111
.block();
111112
}
112113

114+
@Override
115+
public String call(String toolArguments, ToolContext toolContext) {
116+
// ToolContext is not supported by the MCP tools
117+
return this.call(toolArguments);
118+
}
119+
113120
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
2424
import io.modelcontextprotocol.spec.McpSchema.Tool;
2525

26+
import org.springframework.ai.chat.model.ToolContext;
2627
import org.springframework.ai.model.ModelOptionsUtils;
2728
import org.springframework.ai.tool.ToolCallback;
2829
import org.springframework.ai.tool.definition.ToolDefinition;
@@ -111,4 +112,10 @@ public String call(String functionInput) {
111112
return ModelOptionsUtils.toJsonString(response.content());
112113
}
113114

115+
@Override
116+
public String call(String toolArguments, ToolContext toolContext) {
117+
// ToolContext is not supported by the MCP tools
118+
return this.call(toolArguments);
119+
}
120+
114121
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.ai.mcp;
1818

19+
import java.util.Map;
20+
1921
import io.modelcontextprotocol.client.McpSyncClient;
2022
import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
2123
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
@@ -25,6 +27,8 @@
2527
import org.mockito.Mock;
2628
import org.mockito.junit.jupiter.MockitoExtension;
2729

30+
import org.springframework.ai.chat.model.ToolContext;
31+
2832
import static org.assertj.core.api.Assertions.assertThat;
2933
import static org.mockito.ArgumentMatchers.any;
3034
import static org.mockito.Mockito.mock;
@@ -71,4 +75,20 @@ void callShouldHandleJsonInputAndOutput() {
7175
assertThat(response).isNotNull();
7276
}
7377

78+
@Test
79+
void callShoulIngroeToolContext() {
80+
// Arrange
81+
when(tool.name()).thenReturn("testTool");
82+
CallToolResult callResult = mock(CallToolResult.class);
83+
when(mcpClient.callTool(any(CallToolRequest.class))).thenReturn(callResult);
84+
85+
SyncMcpToolCallback callback = new SyncMcpToolCallback(mcpClient, tool);
86+
87+
// Act
88+
String response = callback.call("{\"param\":\"value\"}", new ToolContext(Map.of("foo", "bar")));
89+
90+
// Assert
91+
assertThat(response).isNotNull();
92+
}
93+
7494
}

0 commit comments

Comments
 (0)