Skip to content

Commit aaa9597

Browse files
committed
fix: wrapper json schema object
1 parent cc4c9be commit aaa9597

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/MCP/Concerns/WrapperToolHelpers.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@
22

33
namespace Binaryk\LaravelRestify\MCP\Concerns;
44

5+
use Illuminate\JsonSchema\JsonSchemaTypeFactory;
6+
57
trait WrapperToolHelpers
68
{
79
/**
810
* Format operation schema for display.
11+
*
12+
* Wraps the schema properties in an ObjectType before serialization to properly
13+
* handle required fields and other attributes according to JSON Schema spec.
14+
* This matches how Laravel MCP's Tool::toArray() works.
915
*/
1016
protected function formatSchemaForDisplay(array $schema): array
1117
{
12-
$formatted = [];
13-
14-
foreach ($schema as $key => $value) {
15-
if (is_object($value) && method_exists($value, 'toArray')) {
16-
$formatted[$key] = $value->toArray();
17-
} else {
18-
$formatted[$key] = $value;
19-
}
20-
}
18+
$schemaFactory = new JsonSchemaTypeFactory;
19+
$objectType = $schemaFactory->object($schema);
2120

22-
return $formatted;
21+
return $objectType->toArray();
2322
}
2423

2524
/**
2625
* Generate examples from operation schema.
26+
*
27+
* After wrapping in ObjectType, the schema has a 'properties' key containing all fields.
2728
*/
2829
protected function generateExamplesFromSchema(array $schema, string $operationType): array
2930
{
3031
$examples = [];
32+
$properties = $schema['properties'] ?? [];
3133

3234
switch ($operationType) {
3335
case 'index':
@@ -39,7 +41,7 @@ protected function generateExamplesFromSchema(array $schema, string $operationTy
3941
],
4042
];
4143

42-
if (isset($schema['search'])) {
44+
if (isset($properties['search'])) {
4345
$examples[] = [
4446
'description' => 'Search with pagination',
4547
'parameters' => [
@@ -50,7 +52,7 @@ protected function generateExamplesFromSchema(array $schema, string $operationTy
5052
];
5153
}
5254

53-
if (isset($schema['include'])) {
55+
if (isset($properties['include'])) {
5456
$examples[] = [
5557
'description' => 'With relationships',
5658
'parameters' => [
@@ -70,7 +72,7 @@ protected function generateExamplesFromSchema(array $schema, string $operationTy
7072
],
7173
];
7274

73-
if (isset($schema['include'])) {
75+
if (isset($properties['include'])) {
7476
$examples[] = [
7577
'description' => 'Show with relationships',
7678
'parameters' => [
@@ -83,7 +85,7 @@ protected function generateExamplesFromSchema(array $schema, string $operationTy
8385

8486
case 'store':
8587
$exampleParams = [];
86-
foreach ($schema as $key => $field) {
88+
foreach ($properties as $key => $field) {
8789
if ($key === 'include') {
8890
continue;
8991
}
@@ -101,7 +103,7 @@ protected function generateExamplesFromSchema(array $schema, string $operationTy
101103

102104
case 'update':
103105
$exampleParams = ['id' => '1'];
104-
foreach ($schema as $key => $field) {
106+
foreach ($properties as $key => $field) {
105107
if (in_array($key, ['id', 'include'])) {
106108
continue;
107109
}

tests/MCP/WrapperToolsIntegrationTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,14 @@ public function mcpAllowsIndex(): bool
441441
$this->assertArrayHasKey('examples', $resultContent);
442442

443443
// Assert schema contains expected fields
444+
// Schema is now wrapped in ObjectType following JSON Schema spec
444445
$schema = $resultContent['schema'];
445-
$this->assertArrayHasKey('page', $schema);
446-
$this->assertArrayHasKey('perPage', $schema);
447-
$this->assertArrayHasKey('search', $schema);
448-
$this->assertArrayHasKey('include', $schema);
446+
$this->assertEquals('object', $schema['type']);
447+
$this->assertArrayHasKey('properties', $schema);
448+
$this->assertArrayHasKey('page', $schema['properties']);
449+
$this->assertArrayHasKey('perPage', $schema['properties']);
450+
$this->assertArrayHasKey('search', $schema['properties']);
451+
$this->assertArrayHasKey('include', $schema['properties']);
449452

450453
// Assert examples are provided
451454
$this->assertNotEmpty($resultContent['examples']);
@@ -729,7 +732,8 @@ public function mcpAllowsStore(): bool
729732
$detailsResult = json_decode($detailsResponse->json()['result']['content'][0]['text'], true);
730733

731734
$this->assertArrayHasKey('schema', $detailsResult);
732-
$this->assertArrayHasKey('title', $detailsResult['schema']);
735+
// Schema is now wrapped in ObjectType, so check for properties
736+
$this->assertArrayHasKey('properties', $detailsResult['schema']);
733737

734738
// Step 4: Execute the store operation
735739
$executePayload = [

0 commit comments

Comments
 (0)