Skip to content

Commit 108057b

Browse files
fix: handle empty parameters for tools without arguments
1 parent c3af99f commit 108057b

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

examples/02-discovery-http-userprofile/McpElements.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public function sendWelcomeMessage(string $userId, ?string $customMessage = null
9999
return ['success' => true, 'message_sent' => $message];
100100
}
101101

102+
#[McpTool(name: 'test_tool_without_params')]
103+
public function testToolWithoutParams()
104+
{
105+
return ['success' => true, 'message' => 'Test tool without params'];
106+
}
107+
102108
/**
103109
* Generates a prompt to write a bio for a user.
104110
*

examples/02-discovery-http-userprofile/server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public function log($level, \Stringable|string $message, array $context = []): v
7474

7575
$server->discover(__DIR__, ['.']);
7676

77-
$transport = new HttpServerTransport('127.0.0.1', 8080, 'mcp');
78-
// $transport = new StreamableHttpServerTransport('127.0.0.1', 8080, 'mcp');
77+
// $transport = new HttpServerTransport('127.0.0.1', 8080, 'mcp');
78+
$transport = new StreamableHttpServerTransport('127.0.0.1', 8080, 'mcp');
7979

8080
$server->listen($transport);
8181

src/Utils/SchemaValidator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function __construct(LoggerInterface $logger)
3232
*/
3333
public function validateAgainstJsonSchema(mixed $data, array|object $schema): array
3434
{
35+
if (is_array($data) && empty($data)) {
36+
$data = new \stdClass();
37+
}
38+
3539
try {
3640
// --- Schema Preparation ---
3741
if (is_array($schema)) {
@@ -192,7 +196,7 @@ private function formatValidationError(ValidationError $error): string
192196
switch (strtolower($keyword)) {
193197
case 'required':
194198
$missing = $args['missing'] ?? [];
195-
$formattedMissing = implode(', ', array_map(fn ($p) => "`{$p}`", $missing));
199+
$formattedMissing = implode(', ', array_map(fn($p) => "`{$p}`", $missing));
196200
$message = "Missing required properties: {$formattedMissing}.";
197201
break;
198202
case 'type':
@@ -286,7 +290,7 @@ private function formatValidationError(ValidationError $error): string
286290
break;
287291
case 'additionalProperties': // Corrected casing
288292
$unexpected = $args['properties'] ?? [];
289-
$formattedUnexpected = implode(', ', array_map(fn ($p) => "`{$p}`", $unexpected));
293+
$formattedUnexpected = implode(', ', array_map(fn($p) => "`{$p}`", $unexpected));
290294
$message = "Object contains unexpected additional properties: {$formattedUnexpected}.";
291295
break;
292296
case 'format':

0 commit comments

Comments
 (0)