Skip to content

Cannot pass null to nullable enum parameters #298

@SBNTT

Description

@SBNTT

Describe the bug
When an MCP tool is defined with a nullable Enum parameter (?MyEnum $param = null), the generated JSON schema includes "null" in the type array, but does not include null in the enum array.

Because JSON Schema requires the value to strictly match one of the elements in the enum array when the keyword is present, validation fails if a client sends an explicit null payload for this parameter.

To Reproduce

enum MyEnum: string
{
    case FOO = 'FOO';
    case BAR = 'BAR';
}

#[McpTool]
public function myTool(?MyEnum $myEnum = null): int
{
    return 42;
}

Actual behavior
The tools/list endpoint returns the following schema:

{
  "tools": [
    {
      "name": "myTool",
      "inputSchema": {
        "type": "object",
        "properties": {
          "myEnum": {
            "type": [
              "null",
              "string"
            ],
            "default": null,
            "enum": [
              "FOO",
              "BAR"
            ]
          }
        }
      }
    }
  ]
}

When calling the tool with an explicit null

{"name": "myTool", "arguments": {"myEnum": null}}

the following error is thrown:

{
  error: "MCP error -32602: Invalid parameters for tool 'myTool': Property '/myEnum': Value must be one of the allowed values: "FOO", "BAR"."
}

Expected behavior
The generated schema should accept explicit null payloads when the enum type is nullable

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