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.

  • 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:
  • 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 withoutNullOptionalArguments(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
    • 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's tools/call request handler so that the parameters it receives no longer carry a null for an optional argument. The tool is resolved only when the arguments hold a null, 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)
    • removeNullOptionalParameters

      public static Object removeNullOptionalParameters(Map<String,Object> inputSchema, Object params)
      Removes the null optional arguments from raw tools/call parameters. 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 parameters without the null optional 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 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