Skip to content

A nullable tool parameter with a default ignores an explicit null argument #640

Closed

Description

@acencyril

Description

ReferenceHandler::prepareArguments() tests isset($arguments[$paramName]), so an argument the client explicitly sends as null is treated as missing: a parameter like ?string $category = 'books' receives 'books', not null. The schema allows null, the client sent null, the handler runs with another value.

How to reproduce

final class Shop
{
    public function search(?string $category = 'books'): string
    {
        return var_export($category, true);
    }
}

// Server::builder()->setServerInfo('repro', '1.0.0')->addTool([Shop::class, 'search'], 'search')->build()->run($transport);
// tools/call {"name":"search","arguments":{"category":null}}

Actual: "text":"'books'" Expected: "text":"NULL"

Possible solution

Use array_key_exists($paramName, $arguments) in src/Capability/Registry/ReferenceHandler.php (line 145 at 5d311d6); castArgumentType() already returns null for a nullable type.

Versions: mcp/sdk main (5d311d6) and v0.9.0, PHP 8.3.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions