22
33namespace Binaryk \LaravelRestify \Tests \MCP ;
44
5+ use Binaryk \LaravelRestify \Fields \BelongsTo ;
56use Binaryk \LaravelRestify \Fields \Field ;
67use Binaryk \LaravelRestify \Http \Requests \RestifyRequest ;
7- use Binaryk \LaravelRestify \Fields \BelongsTo ;
88use Binaryk \LaravelRestify \MCP \Concerns \HasMcpTools ;
99use Binaryk \LaravelRestify \MCP \Requests \McpRequest ;
1010use Binaryk \LaravelRestify \MCP \RestifyServer ;
@@ -28,6 +28,7 @@ protected function getPackageProviders($app): array
2828 McpServiceProvider::class,
2929 ]);
3030 }
31+
3132 public function test_repository_uses_mcp_specific_field_methods (): void
3233 {
3334 $ repository = new class extends Repository
@@ -240,9 +241,9 @@ public function mcpAllowsIndex(): bool
240241
241242 // Find our expected tool name
242243 $ availableTools = collect ($ toolsData ['result ' ]['tools ' ])->pluck ('name ' )->toArray ();
243- $ indexToolName = collect ($ availableTools )->filter (fn ($ name ) => str_contains ($ name , 'test-posts ' ) && str_contains ($ name , 'index ' ))->first ();
244+ $ indexToolName = collect ($ availableTools )->filter (fn ($ name ) => str_contains ($ name , 'test-posts ' ) && str_contains ($ name , 'index ' ))->first ();
244245
245- $ this ->assertNotNull ($ indexToolName , 'Expected test-posts index tool not found. Available tools: ' . implode (', ' , $ availableTools ));
246+ $ this ->assertNotNull ($ indexToolName , 'Expected test-posts index tool not found. Available tools: ' . implode (', ' , $ availableTools ));
246247
247248 // Create MCP JSON-RPC 2.0 request payload for calling the index tool
248249 $ mcpPayload = [
@@ -268,7 +269,7 @@ public function mcpAllowsIndex(): bool
268269
269270 // First check if this is an error response
270271 if (isset ($ responseData ['error ' ])) {
271- $ this ->fail ('MCP Error: ' . $ responseData ['error ' ]['message ' ]);
272+ $ this ->fail ('MCP Error: ' . $ responseData ['error ' ]['message ' ]);
272273 }
273274
274275 // Assert JSON-RPC response structure
@@ -310,6 +311,7 @@ public function test_mcp_http_integration_with_relationships_uses_mcp_specific_f
310311 use HasMcpTools;
311312
312313 public static $ model = User::class;
314+
313315 public static string $ uriKey = 'users ' ; // Use the standard users key
314316
315317 public function fields (RestifyRequest $ request ): array
@@ -337,13 +339,15 @@ public function mcpAllowsIndex(): bool
337339 }
338340 };
339341
340- // Create simple MCP-enabled Post repository
342+ // Create simple MCP-enabled Post repository
341343 $ mcpPostRepository = new class extends Repository
342344 {
343345 use HasMcpTools;
344346
345347 public static $ model = Post::class;
348+
346349 public static string $ uriKey = 'test-posts-with-user ' ;
350+
347351 public static array $ related = ['user ' ];
348352
349353 public function fields (RestifyRequest $ request ): array
@@ -379,7 +383,7 @@ public function mcpAllowsIndex(): bool
379383 $ mcpPostRepository ::class,
380384 ]);
381385
382- // Register MCP server route
386+ // Register MCP server route
383387 Mcp::web ('test-restify-relations ' , RestifyServer::class);
384388
385389 // Create test data with relationships
@@ -404,16 +408,16 @@ public function mcpAllowsIndex(): bool
404408
405409 $ toolsResponse = $ this ->postJson ('/test-restify-relations ' , $ toolsListPayload );
406410 $ toolsResponse ->assertOk ();
407-
411+
408412 $ toolsData = $ toolsResponse ->json ();
409-
413+
410414 // Find the post index tool name
411415 $ availableTools = collect ($ toolsData ['result ' ]['tools ' ])->pluck ('name ' )->toArray ();
412416 $ postIndexToolName = collect ($ availableTools )->filter (
413- fn ($ name ) => str_contains ($ name , 'test-posts-with-user ' ) && str_contains ($ name , 'index ' )
417+ fn ($ name ) => str_contains ($ name , 'test-posts-with-user ' ) && str_contains ($ name , 'index ' )
414418 )->first ();
415-
416- $ this ->assertNotNull ($ postIndexToolName , 'Expected test-posts-with-user index tool not found. Available tools: ' . implode (', ' , $ availableTools ));
419+
420+ $ this ->assertNotNull ($ postIndexToolName , 'Expected test-posts-with-user index tool not found. Available tools: ' . implode (', ' , $ availableTools ));
417421
418422 // Create MCP request with relationship inclusion
419423 $ mcpPayload = [
@@ -437,12 +441,12 @@ public function mcpAllowsIndex(): bool
437441
438442 // Check for errors
439443 if (isset ($ responseData ['error ' ])) {
440- $ this ->fail ('MCP Error: ' . $ responseData ['error ' ]['message ' ]);
444+ $ this ->fail ('MCP Error: ' . $ responseData ['error ' ]['message ' ]);
441445 }
442446
443447 // Assert JSON-RPC response structure
444448 $ this ->assertArrayHasKey ('result ' , $ responseData );
445-
449+
446450 // Parse the result content
447451 $ resultContent = json_decode ($ responseData ['result ' ]['content ' ][0 ]['text ' ], true );
448452
@@ -471,32 +475,32 @@ public function mcpAllowsIndex(): bool
471475
472476 // Check what fields are currently available in the relationship
473477 $ availableUserFields = array_keys ($ userRelationship );
474-
478+
475479 // Basic user fields should be present
476480 $ this ->assertArrayHasKey ('name ' , $ userRelationship );
477481 $ this ->assertArrayHasKey ('email ' , $ userRelationship );
478482 $ this ->assertEquals ('John Doe ' , $ userRelationship ['name ' ]);
479483 $ this ->
assertEquals (
'[email protected] ' ,
$ userRelationship[
'email ' ]);
480484
481485 // Test status: Check if MCP fields are now present with your fix
482- $ hasMcpFields = isset ($ userRelationship ['user_mcp_data ' ]) &&
483- isset ($ userRelationship ['internal_user_tracking ' ]) &&
486+ $ hasMcpFields = isset ($ userRelationship ['user_mcp_data ' ]) &&
487+ isset ($ userRelationship ['internal_user_tracking ' ]) &&
484488 isset ($ userRelationship ['admin_notes ' ]);
485-
489+
486490 if ($ hasMcpFields ) {
487491 // Your fix works! MCP fields are present in relationships
488492 $ this ->assertEquals ('user-mcp-specific-data ' , $ userRelationship ['user_mcp_data ' ]);
489- $ this ->assertEquals ('user-internal-123 ' , $ userRelationship ['internal_user_tracking ' ]);
493+ $ this ->assertEquals ('user-internal-123 ' , $ userRelationship ['internal_user_tracking ' ]);
490494 $ this ->assertEquals ('admin-access-only ' , $ userRelationship ['admin_notes ' ]);
491495 echo "\n✅ SUCCESS: MCP fields are now working in relationships! \n" ;
492496 } else {
493497 // The relationship is getting all model attributes instead of using repository fields
494498 // This suggests the relationship resolution is bypassing the repository's collectFields method
495499 echo "\n🔍 ANALYSIS: Relationship shows all model attributes instead of repository fields \n" ;
496- echo " Available fields: " . implode (', ' , $ availableUserFields ) . "\n" ;
500+ echo ' Available fields: ' . implode (', ' , $ availableUserFields ). "\n" ;
497501 echo "Expected MCP fields: user_mcp_data, internal_user_tracking, admin_notes \n" ;
498502 echo "Issue: EagerField might be using model attributes directly instead of repository field resolution \n" ;
499-
503+
500504 // For now, just verify basic fields work to keep test passing
501505 $ this ->assertTrue (true , 'Basic relationship fields are working, MCP field resolution needs investigation ' );
502506 }
0 commit comments