Skip to content

Commit dee8cb5

Browse files
committed
Merge branch 'fix/relatd-mcp-performance' of github.com:BinarCode/laravel-restify into fix/relatd-mcp-performance
2 parents 2265c63 + 314266f commit dee8cb5

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

tests/MCP/McpFieldsIntegrationTest.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ protected function getPackageProviders($app): array
3131

3232
public function test_repository_uses_mcp_specific_field_methods(): void
3333
{
34-
$repository = new class extends Repository {
34+
$repository = new class extends Repository
35+
{
3536
public static $model = Post::class;
3637

3738
public function fields(RestifyRequest $request): array
@@ -67,7 +68,7 @@ public function fieldsForMcpShow(RestifyRequest $request): array
6768
// Regular request should use fields() method
6869
$regularRequest = new RestifyRequest;
6970
$regularFields = $repository->collectFields($regularRequest);
70-
$regularFieldNames = $regularFields->map(fn($field) => $field->getAttribute())->toArray();
71+
$regularFieldNames = $regularFields->map(fn ($field) => $field->getAttribute())->toArray();
7172

7273
$this->assertCount(2, $regularFields);
7374
$this->assertContains('title', $regularFieldNames);
@@ -78,7 +79,7 @@ public function fieldsForMcpShow(RestifyRequest $request): array
7879
// MCP index request should use fieldsForMcpIndex() method
7980
$mcpIndexRequest = new McpRequest(['params' => ['name' => 'posts-index-tool']]);
8081
$mcpIndexFields = $repository->collectFields($mcpIndexRequest);
81-
$mcpIndexFieldNames = $mcpIndexFields->map(fn($field) => $field->getAttribute())->toArray();
82+
$mcpIndexFieldNames = $mcpIndexFields->map(fn ($field) => $field->getAttribute())->toArray();
8283

8384
$this->assertCount(4, $mcpIndexFields);
8485
$this->assertContains('title', $mcpIndexFieldNames);
@@ -90,7 +91,7 @@ public function fieldsForMcpShow(RestifyRequest $request): array
9091
// MCP show request should use fieldsForMcpShow() method
9192
$mcpShowRequest = new McpRequest(['params' => ['name' => 'posts-show-tool']]);
9293
$mcpShowFields = $repository->collectFields($mcpShowRequest);
93-
$mcpShowFieldNames = $mcpShowFields->map(fn($field) => $field->getAttribute())->toArray();
94+
$mcpShowFieldNames = $mcpShowFields->map(fn ($field) => $field->getAttribute())->toArray();
9495

9596
$this->assertCount(5, $mcpShowFields);
9697
$this->assertContains('title', $mcpShowFieldNames);
@@ -103,7 +104,8 @@ public function fieldsForMcpShow(RestifyRequest $request): array
103104

104105
public function test_mcp_request_falls_back_to_regular_methods_when_mcp_methods_missing(): void
105106
{
106-
$repository = new class extends Repository {
107+
$repository = new class extends Repository
108+
{
107109
public static $model = Post::class;
108110

109111
public function fields(RestifyRequest $request): array
@@ -126,7 +128,7 @@ public function fieldsForIndex(RestifyRequest $request): array
126128
// MCP request without fieldsForMcpIndex should fall back to fieldsForIndex
127129
$mcpIndexRequest = new McpRequest(['params' => ['name' => 'posts-index-tool']]);
128130
$mcpIndexFields = $repository->collectFields($mcpIndexRequest);
129-
$mcpIndexFieldNames = $mcpIndexFields->map(fn($field) => $field->getAttribute())->toArray();
131+
$mcpIndexFieldNames = $mcpIndexFields->map(fn ($field) => $field->getAttribute())->toArray();
130132

131133
$this->assertCount(2, $mcpIndexFields);
132134
$this->assertContains('title', $mcpIndexFieldNames);
@@ -136,7 +138,7 @@ public function fieldsForIndex(RestifyRequest $request): array
136138
// MCP request without fieldsForMcpShow should fall back to fields()
137139
$mcpShowRequest = new McpRequest(['params' => ['name' => 'posts-show-tool']]);
138140
$mcpShowFields = $repository->collectFields($mcpShowRequest);
139-
$mcpShowFieldNames = $mcpShowFields->map(fn($field) => $field->getAttribute())->toArray();
141+
$mcpShowFieldNames = $mcpShowFields->map(fn ($field) => $field->getAttribute())->toArray();
140142

141143
$this->assertCount(2, $mcpShowFields);
142144
$this->assertContains('title', $mcpShowFieldNames);
@@ -146,7 +148,8 @@ public function fieldsForIndex(RestifyRequest $request): array
146148

147149
public function test_mcp_getter_request_uses_fields_for_mcp_getter(): void
148150
{
149-
$repository = new class extends Repository {
151+
$repository = new class extends Repository
152+
{
150153
public static $model = Post::class;
151154

152155
public function fields(RestifyRequest $request): array
@@ -168,7 +171,7 @@ public function fieldsForMcpGetter(RestifyRequest $request): array
168171

169172
$mcpGetterRequest = new McpRequest(['params' => ['name' => 'analytics-getter-tool']]);
170173
$mcpGetterFields = $repository->collectFields($mcpGetterRequest);
171-
$mcpGetterFieldNames = $mcpGetterFields->map(fn($field) => $field->getAttribute())->toArray();
174+
$mcpGetterFieldNames = $mcpGetterFields->map(fn ($field) => $field->getAttribute())->toArray();
172175

173176
$this->assertCount(3, $mcpGetterFields);
174177
$this->assertContains('title', $mcpGetterFieldNames);
@@ -179,7 +182,8 @@ public function fieldsForMcpGetter(RestifyRequest $request): array
179182
public function test_mcp_http_integration_uses_mcp_specific_fields(): void
180183
{
181184
// Create test repository with MCP tools enabled
182-
$mcpRepository = new class extends Repository {
185+
$mcpRepository = new class extends Repository
186+
{
183187
use HasMcpTools;
184188

185189
public static $model = Post::class;
@@ -200,8 +204,8 @@ public function fieldsForMcpIndex(RestifyRequest $request): array
200204
Field::make('title'),
201205
Field::make('description'),
202206
Field::make('user_id'),
203-
Field::make('mcp_metadata')->resolveCallback(fn() => 'mcp-specific-data'),
204-
Field::make('internal_tracking')->resolveCallback(fn() => 'internal-123'),
207+
Field::make('mcp_metadata')->resolveCallback(fn () => 'mcp-specific-data'),
208+
Field::make('internal_tracking')->resolveCallback(fn () => 'internal-123'),
205209
];
206210
}
207211

@@ -237,8 +241,8 @@ public function mcpAllowsIndex(): bool
237241

238242
// Find our expected tool name
239243
$availableTools = collect($toolsData['result']['tools'])->pluck('name')->toArray();
240-
$indexToolName = collect($availableTools)->filter(fn($name) => str_contains($name,
241-
'test-posts') && str_contains($name, 'index'))->first();
244+
$indexToolName = collect($availableTools)->filter(fn ($name) => str_contains($name,
245+
'test-posts') && str_contains($name, 'index'))->first();
242246

243247
$this->assertNotNull($indexToolName,
244248
'Expected test-posts index tool not found. Available tools: '.implode(', ', $availableTools));
@@ -304,7 +308,8 @@ public function mcpAllowsIndex(): bool
304308
public function test_mcp_http_integration_with_relationships_uses_mcp_specific_fields(): void
305309
{
306310
// Create simple MCP-enabled Post repository
307-
$mcpPostRepository = new class extends Repository {
311+
$mcpPostRepository = new class extends Repository
312+
{
308313
use HasMcpTools;
309314

310315
public static $model = Post::class;
@@ -335,8 +340,8 @@ public function fieldsForMcpIndex(RestifyRequest $request): array
335340
Field::make('title'),
336341
Field::make('description'),
337342
Field::make('user_id'),
338-
Field::make('mcp_post_metadata')->resolveCallback(fn() => 'post-mcp-specific-data'),
339-
Field::make('post_analytics')->resolveCallback(fn() => 'post-analytics-data'),
343+
Field::make('mcp_post_metadata')->resolveCallback(fn () => 'post-mcp-specific-data'),
344+
Field::make('post_analytics')->resolveCallback(fn () => 'post-analytics-data'),
340345
BelongsTo::make('user'), // Will use the MCP-enabled UserRepository
341346
];
342347
}
@@ -381,11 +386,10 @@ public function mcpAllowsIndex(): bool
381386

382387
$toolsData = $toolsResponse->json();
383388

384-
385389
// Find the post index tool name
386390
$availableTools = collect($toolsData['result']['tools'])->pluck('name')->toArray();
387391
$postIndexToolName = collect($availableTools)->filter(
388-
fn($name) => str_contains($name, 'test-posts-with-user') && str_contains($name, 'index')
392+
fn ($name) => str_contains($name, 'test-posts-with-user') && str_contains($name, 'index')
389393
)->first();
390394

391395
$this->assertNotNull($postIndexToolName,
@@ -462,7 +466,8 @@ public function mcpAllowsIndex(): bool
462466
public function test_mcp_relationship_respects_has_mcp_tools_trait(): void
463467
{
464468
// Create MCP-enabled Post repository that references the regular user
465-
$mcpPostRepository = new class extends Repository {
469+
$mcpPostRepository = new class extends Repository
470+
{
466471
use HasMcpTools;
467472

468473
public static $model = Post::class;
@@ -472,7 +477,7 @@ public function test_mcp_relationship_respects_has_mcp_tools_trait(): void
472477
public static function include(): array
473478
{
474479
return [
475-
BelongsTo::make('user', UserWithoutMcpToolsFields::class)
480+
BelongsTo::make('user', UserWithoutMcpToolsFields::class),
476481
];
477482
}
478483

@@ -522,7 +527,7 @@ public function mcpAllowsIndex(): bool
522527
$toolsData = $toolsResponse->json();
523528
$availableTools = collect($toolsData['result']['tools'])->pluck('name')->toArray();
524529
$postIndexToolName = collect($availableTools)->filter(
525-
fn($name) => str_contains($name, 'test-posts-regular-user') && str_contains($name, 'index')
530+
fn ($name) => str_contains($name, 'test-posts-regular-user') && str_contains($name, 'index')
526531
)->first();
527532

528533
$this->assertNotNull($postIndexToolName);
@@ -559,7 +564,6 @@ public function mcpAllowsIndex(): bool
559564
}
560565
}
561566

562-
563567
class UserWithMcpIndexFields extends Repository
564568
{
565569
use HasMcpTools;
@@ -581,9 +585,9 @@ public function fieldsForMcpIndex(RestifyRequest $request): array
581585
return [
582586
Field::make('name'),
583587
Field::make('email'),
584-
Field::make('user_mcp_data')->resolveCallback(fn() => 'user-mcp-specific-data'),
585-
Field::make('internal_user_tracking')->resolveCallback(fn() => 'user-internal-123'),
586-
Field::make('admin_notes')->resolveCallback(fn() => 'admin-access-only'),
588+
Field::make('user_mcp_data')->resolveCallback(fn () => 'user-mcp-specific-data'),
589+
Field::make('internal_user_tracking')->resolveCallback(fn () => 'user-internal-123'),
590+
Field::make('admin_notes')->resolveCallback(fn () => 'admin-access-only'),
587591
];
588592
}
589593

0 commit comments

Comments
 (0)