Skip to content

InvalidParams error when all fields in the tool schema are optional #400

Open
@nirinchev

Description

@nirinchev

Describe the bug
When registering a tool with a schema, consisting of all optional fields, some models will try calling the tool without supplying arguments in the request. This, however, fails validation because the zod object being created is non-optional:

paramsSchema === undefined ? undefined : z.object(paramsSchema),
.

Not sure if this is an issue with the server itself or the agent calling it, but would be nice if there was a way to give a hint to the model that the argument field is required for that particular tool.

To Reproduce
Unfortunately, this doesn't reproduce with all models/agents, but we've seen it happen somewhat consistently with Windsurf.

Steps to reproduce the behavior:

  1. Register a tool with an all-optional schema:
    server.tool(
        "fetch-records",
        "Fetches database records",
        { limit: z.number().optional().describe("The number of records to fetch. Default is 20.") },
        ({ limit }) => {
            return {
                content: [{ type: "text", text: `Fetched ${limit ?? 20} records` }],
            };
        }
    );
  2. Ask the agent to call the tool.
  3. Observe the agent receiving MCP error -32602: Invalid arguments for tool [...] "expected": "object", "received": "undefined", "path": []

Expected behavior
I guess I would expect the protocol to be more explicit about when fields are required vs when they're not, or alternatively, we could fallback to an empty object when arguments is not provided in the request, but expected due to the tool schema.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions