22
33namespace Binaryk \LaravelRestify \MCP \Concerns ;
44
5+ use Illuminate \JsonSchema \JsonSchemaTypeFactory ;
6+
57trait 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 }
0 commit comments