Class McpToolArgumentUtils
tools/call request, every argument a model sent as
null although the tool's input schema declares it optional.
The MCP SDK validates the arguments against the tool's input schema
(io.modelcontextprotocol.util.ToolInputValidator, called by
McpAsyncServer's tools/call handler) before the tool runs.
An optional integer property may be absent, but null is not
an integer, so a call carrying "space_id": null is refused before
reaching the tool, although models routinely send null for an
optional argument they do not use. Dropping that key ahead of the SDK's
handler makes the call equivalent to one that omitted the argument, both
for the validation and for the tool, whose Java binding already handles an
absent argument.
Only the top-level arguments are considered, and only those the schema
declares in properties without listing them in required: a
null for a required argument is left in place, so the validation
still refuses it, and an argument the schema does not declare is left
untouched, whatever its value.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringKey of the tool arguments in thetools/callparameters.static final StringKey of the tool name in thetools/callparameters.static final StringKey of the declared properties in a JSON schema.static final StringKey of the required property names in a JSON schema. -
Method Summary
Modifier and TypeMethodDescriptionRemoves, from tool arguments, every entry whose value isnulland whose name the input schema declares inpropertieswithout listing it inrequired.static ObjectremoveNullOptionalParameters(Map<String, Object> inputSchema, Object params) Removes thenulloptional arguments from rawtools/callparameters.static <T> io.modelcontextprotocol.server.McpRequestHandler<T> withoutNullOptionalArguments(io.modelcontextprotocol.server.McpRequestHandler<T> delegate, Function<String, reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.Tool>> toolResolver) Wraps the SDK'stools/callrequest handler so that the parameters it receives no longer carry anullfor an optional argument.static voidwrapToolsCallHandler(Map<String, io.modelcontextprotocol.server.McpRequestHandler<?>> handlers, Supplier<io.modelcontextprotocol.server.McpAsyncServer> asyncServerSupplier) Replaces, in a session's request handlers, the SDK'stools/callhandler bywithoutNullOptionalArguments(McpRequestHandler, Function), resolving each tool among the tools the MCP server exposes.
-
Field Details
-
NAME_PARAM
Key of the tool name in thetools/callparameters.- See Also:
-
ARGUMENTS_PARAM
Key of the tool arguments in thetools/callparameters.- See Also:
-
PROPERTIES_PARAM
Key of the declared properties in a JSON schema.- See Also:
-
REQUIRED_PARAM
Key of the required property names in a JSON schema.- See Also:
-
-
Method Details
-
wrapToolsCallHandler
public static void wrapToolsCallHandler(Map<String, io.modelcontextprotocol.server.McpRequestHandler<?>> handlers, Supplier<io.modelcontextprotocol.server.McpAsyncServer> asyncServerSupplier) Replaces, in a session's request handlers, the SDK'stools/callhandler bywithoutNullOptionalArguments(McpRequestHandler, Function), resolving each tool among the tools the MCP server exposes. The server is obtained fromasyncServerSupplieron the first call that needs a tool, then kept. Handlers without atools/callentry (no tool capability) are left unchanged.- Parameters:
handlers- the session's request handlers, by method, modified in placeasyncServerSupplier- supplies the MCP server whose tools are resolved, called once, lazily, since the server bean does not exist yet when the session factory is built
-
withoutNullOptionalArguments
public static <T> io.modelcontextprotocol.server.McpRequestHandler<T> withoutNullOptionalArguments(io.modelcontextprotocol.server.McpRequestHandler<T> delegate, Function<String, reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.Tool>> toolResolver) Wraps the SDK'stools/callrequest handler so that the parameters it receives no longer carry anullfor an optional argument. The tool is resolved only when the arguments hold anull, so the common call goes straight to the delegate.- Type Parameters:
T- the result type of the wrapped handler- Parameters:
delegate- the SDK'stools/callhandler, which validates the arguments and then runs the tooltoolResolver- resolves a tool name to the tool the server exposes, or to an emptyMonofor an unknown name- Returns:
- the wrapping handler, or
nullwhendelegateisnull(no tool capability)
-
removeNullOptionalParameters
Removes thenulloptional arguments from rawtools/callparameters. The streamable transport of mcp-core 2.0.0 hands the request handler aMapdeserialized from the JSON-RPC request; the typedMcpSchema.CallToolRequestbranch is defensive, for a caller that builds the request itself, and is not reached through that transport today. Any other shape is returned as is.- Parameters:
inputSchema- the tool's input schemaparams- thetools/callparameters- Returns:
- the parameters without the
nulloptional arguments, the same instance when there was nothing to remove
-
removeNullOptionalArguments
public static Map<String,Object> removeNullOptionalArguments(Map<String, Object> inputSchema, Map<String, Object> arguments) Removes, from tool arguments, every entry whose value isnulland whose name the input schema declares inpropertieswithout listing it inrequired.- Parameters:
inputSchema- the tool's input schema, may benullarguments- the tool arguments, may benull- Returns:
- a copy without those entries, or the same instance when there was nothing to remove
-