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.
The same wrapping also turns into a number an argument a model sent as a
string of digits where the schema declares an integer or a
number: models routinely quote ids, and "space_id": "12"
was refused by the same validation (string found, integer
expected) although the tool's Java binding reads it as the number it
means. Any other string, and a declared type other than these two, is left
untouched, so the validation still refuses a value that is not a number.
-
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.static final StringKey of a property's type in a JSON schema. -
Method Summary
Modifier and TypeMethodDescriptionTurns into aLong, in tool arguments, every string of digits whose name the input schema declares inpropertieswith the typeintegerornumber.static ObjectnormalizeParameters(Map<String, Object> inputSchema, Object params) Removes thenulloptional arguments from rawtools/callparameters, then turns into numbers the strings of digits sent for numeric arguments.Removes, from tool arguments, every entry whose value isnulland whose name the input schema declares inpropertieswithout listing it inrequired.static <T> io.modelcontextprotocol.server.McpRequestHandler<T> withNormalizedArguments(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, nor a string of digits for a numeric one.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 bywithNormalizedArguments(McpRequestHandler, Function), resolving each tool among the tools the MCP server exposes.
-
Field Details
-
NAME_PARAM
-
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:
-
TYPE_PARAM
-
-
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 bywithNormalizedArguments(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
-
withNormalizedArguments
public static <T> io.modelcontextprotocol.server.McpRequestHandler<T> withNormalizedArguments(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, nor a string of digits for a numeric one. The tool is resolved only when the arguments hold anullor a string of digits, 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)
-
normalizeParameters
Removes thenulloptional arguments from rawtools/callparameters, then turns into numbers the strings of digits sent for numeric arguments. 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 normalized parameters, the same instance when nothing changed
-
coerceWholeNumberArguments
public static Map<String,Object> coerceWholeNumberArguments(Map<String, Object> inputSchema, Map<String, Object> arguments) Turns into aLong, in tool arguments, every string of digits whose name the input schema declares inpropertieswith the typeintegerornumber. A string that is not a whole number, or one too large for aLong, is left as is for the validation to refuse, and so is an argument the schema does not declare.- Parameters:
inputSchema- the tool's input schema, may benullarguments- the tool arguments, may benull- Returns:
- a copy with those numbers, or the same instance when there was nothing to turn
-
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
-