-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: MCP tools support ToolContext #3831
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
base: main
Are you sure you want to change the base?
Conversation
Just an idea regarding the spring.ai.mcp.client.toolcallback.tool-call-history.enabled=true That way, the out-of-the-box user experience would be that the server only receives what the user explicitly writes into the ToolContext. And if the user wants to transmit additional metadata (such as On the other hand, one could argue that the default behavior of the ToolContext would then change depending on whether the tool call is executed locally or via MCP, which in turn would be an argument for including it by default. Tough decision, but I just thought I’d think out loud about it for a moment 🤔 |
/** | ||
* The keys that will not be sent to the MCP Server inside * the `_meta` field of | ||
* {@link io.modelcontextprotocol.spec.McpSchema.CallToolRequest} | ||
*/ | ||
// Remember to update META-INF/additional-spring-configuration-metadata.json if | ||
// you change this default values. | ||
private Set<String> excludedToolContextKeys = Set.of(TOOL_CALL_HISTORY); |
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.
That way, the out-of-the-box user experience would be that the server only receives what the user explicitly writes into the ToolContext. And if the user wants to transmit additional metadata (such as TOOL_CALL_HISTORY), they can explicitly enable that on the client side.
@msievers I did think about the scenario you mentioned.
The default value is spring.ai.mcp.client.toolcallback.excluded-tool-context-keys=TOOL_CALL_HISTORY
, and users can override it. It also makes it easier for us to add more defaults in the future.
I'm not sure if we need a separate field just for handling TOOL_CALL_HISTORY
.
5fac03f
to
7db214a
Compare
- Added `excludedToolContextKeys` to `McpClientCommonProperties.ToolCallback`. This will let us control which `ToolContext` fields should not be sent to the server. By default, `excludedToolContextKeys` is set to `TOOL_CALL_HISTORY`. - Added `AbstractMcpToolCallback` to hold common code for `McpToolCallback` - Updated `AsyncMcpToolCallback` and `SyncMcpToolCallback` to pass `ToolContext` via `_meta` using key `ai.springframework.org/tool_context` - Updated `McpToolUtils` to help server tools extract `ToolContext` from MCP Client requests - Updated `AsyncMcpToolCallbackProvider` and `SyncMcpToolCallbackProvider` to use the Builder design pattern and add the `excludedToolContextKeys` field - Updated the corresponding test cases Signed-off-by: YunKui Lu <[email protected]>
7db214a
to
9053c65
Compare
...-mcp-client-common/src/main/resources/META-INF/additional-spring-configuration-metadata.json
Show resolved
Hide resolved
...springframework/ai/mcp/client/common/autoconfigure/properties/McpClientCommonProperties.java
Show resolved
Hide resolved
if (request.meta().containsKey(DEFAULT_MCP_META_TOOL_CONTEXT_KEY)) { | ||
// Get the McpClient tool context from the | ||
// `ai.springframework.org/tool_context` key in the _meta. | ||
Map<String, Object> toolContext = OBJECT_MAPPER.convertValue( |
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.
Could ModelOptionsUtils.objectToMap
perhaps be used here instead of a dedicated ObjectMapper
? I'm just asking because I noticed that in other parts, ModelOptionsUtils
is often used when it comes to parameter conversions.
Map<String, Object> toolContext = ModelOptionsUtils.objectToMap(request.meta().get(DEFAULT_MCP_META_TOOL_CONTEXT_KEY));
excludedToolContextKeys
toMcpClientCommonProperties.ToolCallback
. This will let us control whichToolContext
fields should not be sent to the server. By default,excludedToolContextKeys
is set toTOOL_CALL_HISTORY
.AbstractMcpToolCallback
to hold common code forMcpToolCallback
AsyncMcpToolCallback
andSyncMcpToolCallback
to passToolContext
via_meta
using keyai.springframework.org/tool_context
McpToolUtils
to help server tools extractToolContext
from MCP Client requestsAsyncMcpToolCallbackProvider
andSyncMcpToolCallbackProvider
to use the Builder design pattern and add theexcludedToolContextKeys
fieldPR description
The Spring AI MCP client now sends
ToolContext
data inside the_meta
field ofCallToolRequest
, using the keyai.springframework.org/tool_context
.By default, the
TOOL_CALL_HISTORY
entry inToolContext
is excluded from being sent to the server. This can be customized using the configspring.ai.mcp.client.toolcallback.excluded-tool-context-keys
.On the server side, Spring AI reads the
ToolContext
from the_meta
field using the same keyai.springframework.org/tool_context
, and passes it into the tool method.Example
Client
Server
Resolves #3505