Class McpToolArgumentUtils

java.lang.Object
io.meeds.mcp.server.util.McpToolArgumentUtils

public final class McpToolArgumentUtils extends Object
Removes, from a 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 Details

    • NAME_PARAM

      public static final String NAME_PARAM
      Key of the tool name in the tools/call parameters.
      See Also:
    • ARGUMENTS_PARAM

      public static final String ARGUMENTS_PARAM
      Key of the tool arguments in the tools/call parameters.
      See Also:
    • PROPERTIES_PARAM

      public static final String PROPERTIES_PARAM
      Key of the declared properties in a JSON schema.
      See Also:
    • REQUIRED_PARAM

      public static final String REQUIRED_PARAM
      Key of the required property names in a JSON schema.
      See Also:
    • TYPE_PARAM

      public static final String TYPE_PARAM
      Key of a property's type 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's tools/call handler by withNormalizedArguments(McpRequestHandler, Function), resolving each tool among the tools the MCP server exposes. The server is obtained from asyncServerSupplier on the first call that needs a tool, then kept. Handlers without a tools/call entry (no tool capability) are left unchanged.
      Parameters:
      handlers - the session's request handlers, by method, modified in place
      asyncServerSupplier - 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's tools/call request handler so that the parameters it receives no longer carry a null for an optional argument, nor a string of digits for a numeric one. The tool is resolved only when the arguments hold a null or 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's tools/call handler, which validates the arguments and then runs the tool
      toolResolver - resolves a tool name to the tool the server exposes, or to an empty Mono for an unknown name
      Returns:
      the wrapping handler, or null when delegate is null (no tool capability)
    • normalizeParameters

      public static Object normalizeParameters(Map<String,Object> inputSchema, Object params)
      Removes the null optional arguments from raw tools/call parameters, 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 a Map deserialized from the JSON-RPC request; the typed McpSchema.CallToolRequest branch 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 schema
      params - the tools/call parameters
      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 a Long, in tool arguments, every string of digits whose name the input schema declares in properties with the type integer or number. A string that is not a whole number, or one too large for a Long, 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 be null
      arguments - the tool arguments, may be null
      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 is null and whose name the input schema declares in properties without listing it in required.
      Parameters:
      inputSchema - the tool's input schema, may be null
      arguments - the tool arguments, may be null
      Returns:
      a copy without those entries, or the same instance when there was nothing to remove