Skip to content

Commit 380c4a0

Browse files
judusCodeWithKyrian
authored andcommitted
Normalize 'arguments' in CallToolRequest::fromRequest()
Fixes runtime issue where 'arguments' could be a stdClass when omitted or sent as {}. Casts to array and defaults to [] to ensure strict constructor contract. Related to php-mcp/server#53
1 parent 6b6c962 commit 380c4a0

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/Request/CallToolRequest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,27 @@ public static function fromRequest(Request $request): static
5252
throw new \InvalidArgumentException('Request is not a call tool request');
5353
}
5454

55-
$params = $request->params;
55+
$params = $request->params ?? [];
5656

57-
if (! isset($params['name']) || ! is_string($params['name'])) {
57+
if (!isset($params['name']) || !is_string($params['name'])) {
5858
throw new \InvalidArgumentException("Missing or invalid 'name' parameter for tools/call.");
5959
}
6060

61-
$arguments = $params['arguments'] ?? new \stdClass();
62-
if (! is_array($arguments) && ! $arguments instanceof \stdClass) {
63-
throw new \InvalidArgumentException("Parameter 'arguments' must be an object/array for tools/call.");
61+
$arguments = $params['arguments'] ?? [];
62+
63+
if ($arguments instanceof \stdClass) {
64+
$arguments = (array) $arguments;
65+
}
66+
67+
if (!is_array($arguments)) {
68+
throw new \InvalidArgumentException("Parameter 'arguments' must be an array.");
6469
}
6570

66-
return new static($request->id, $params['name'], $arguments, $params['_meta'] ?? null);
71+
return new static(
72+
$request->id,
73+
$params['name'],
74+
$arguments,
75+
$params['_meta'] ?? null
76+
);
6777
}
6878
}

0 commit comments

Comments
 (0)