From 108057bd757a7620a100a51ae33db0eac97c6d9e Mon Sep 17 00:00:00 2001 From: Kyrian Obikwelu Date: Tue, 24 Jun 2025 10:02:16 +0100 Subject: [PATCH 1/2] fix: handle empty parameters for tools without arguments --- examples/02-discovery-http-userprofile/McpElements.php | 6 ++++++ examples/02-discovery-http-userprofile/server.php | 4 ++-- src/Utils/SchemaValidator.php | 8 ++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/02-discovery-http-userprofile/McpElements.php b/examples/02-discovery-http-userprofile/McpElements.php index 51cc47b..d371713 100644 --- a/examples/02-discovery-http-userprofile/McpElements.php +++ b/examples/02-discovery-http-userprofile/McpElements.php @@ -99,6 +99,12 @@ public function sendWelcomeMessage(string $userId, ?string $customMessage = null return ['success' => true, 'message_sent' => $message]; } + #[McpTool(name: 'test_tool_without_params')] + public function testToolWithoutParams() + { + return ['success' => true, 'message' => 'Test tool without params']; + } + /** * Generates a prompt to write a bio for a user. * diff --git a/examples/02-discovery-http-userprofile/server.php b/examples/02-discovery-http-userprofile/server.php index 8912623..2b924ce 100644 --- a/examples/02-discovery-http-userprofile/server.php +++ b/examples/02-discovery-http-userprofile/server.php @@ -74,8 +74,8 @@ public function log($level, \Stringable|string $message, array $context = []): v $server->discover(__DIR__, ['.']); - $transport = new HttpServerTransport('127.0.0.1', 8080, 'mcp'); - // $transport = new StreamableHttpServerTransport('127.0.0.1', 8080, 'mcp'); + // $transport = new HttpServerTransport('127.0.0.1', 8080, 'mcp'); + $transport = new StreamableHttpServerTransport('127.0.0.1', 8080, 'mcp'); $server->listen($transport); diff --git a/src/Utils/SchemaValidator.php b/src/Utils/SchemaValidator.php index bb7cad8..a1e4995 100644 --- a/src/Utils/SchemaValidator.php +++ b/src/Utils/SchemaValidator.php @@ -32,6 +32,10 @@ public function __construct(LoggerInterface $logger) */ public function validateAgainstJsonSchema(mixed $data, array|object $schema): array { + if (is_array($data) && empty($data)) { + $data = new \stdClass(); + } + try { // --- Schema Preparation --- if (is_array($schema)) { @@ -192,7 +196,7 @@ private function formatValidationError(ValidationError $error): string switch (strtolower($keyword)) { case 'required': $missing = $args['missing'] ?? []; - $formattedMissing = implode(', ', array_map(fn ($p) => "`{$p}`", $missing)); + $formattedMissing = implode(', ', array_map(fn($p) => "`{$p}`", $missing)); $message = "Missing required properties: {$formattedMissing}."; break; case 'type': @@ -286,7 +290,7 @@ private function formatValidationError(ValidationError $error): string break; case 'additionalProperties': // Corrected casing $unexpected = $args['properties'] ?? []; - $formattedUnexpected = implode(', ', array_map(fn ($p) => "`{$p}`", $unexpected)); + $formattedUnexpected = implode(', ', array_map(fn($p) => "`{$p}`", $unexpected)); $message = "Object contains unexpected additional properties: {$formattedUnexpected}."; break; case 'format': From c7f0604a478fbe9307917dbdf1042e9d2bdb1e42 Mon Sep 17 00:00:00 2001 From: Kyrian Obikwelu Date: Tue, 24 Jun 2025 10:05:27 +0100 Subject: [PATCH 2/2] fix: update validation error keyword for empty data object against schema --- tests/Unit/Utils/SchemaValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Utils/SchemaValidatorTest.php b/tests/Unit/Utils/SchemaValidatorTest.php index a32a50f..5ba6769 100644 --- a/tests/Unit/Utils/SchemaValidatorTest.php +++ b/tests/Unit/Utils/SchemaValidatorTest.php @@ -233,7 +233,7 @@ function getValidData(): array $errors = $this->validator->validateAgainstJsonSchema($data, $schema); expect($errors)->not->toBeEmpty() - ->and($errors[0]['keyword'])->toBe('type'); + ->and($errors[0]['keyword'])->toBe('required'); }); test('handles empty schema (allows anything)', function () {