|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\MCP\Tools\Operations; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\MCP\Requests\McpRequest; |
| 6 | +use Binaryk\LaravelRestify\Repositories\Repository; |
| 7 | +use Generator; |
| 8 | +use Laravel\Mcp\Server\Tool; |
| 9 | +use Laravel\Mcp\Server\Tools\Annotations\Title; |
| 10 | +use Laravel\Mcp\Server\Tools\ToolInputSchema; |
| 11 | +use Laravel\Mcp\Server\Tools\ToolResult; |
| 12 | + |
| 13 | +#[Title('GetMyProfile')] |
| 14 | +class ProfileTool extends Tool |
| 15 | +{ |
| 16 | + protected Repository $repository; |
| 17 | + |
| 18 | + public function __construct(string $repositoryClass) |
| 19 | + { |
| 20 | + $this->repository = app($repositoryClass); |
| 21 | + } |
| 22 | + |
| 23 | + public function name(): string |
| 24 | + { |
| 25 | + $uriKey = $this->repository->uriKey(); |
| 26 | + |
| 27 | + return "{$uriKey}-profile-tool"; |
| 28 | + } |
| 29 | + |
| 30 | + public function description(): string |
| 31 | + { |
| 32 | + $modelName = class_basename($this->repository::$model); |
| 33 | + |
| 34 | + return "Get the current authenticated user profile including {$modelName} and relationship information."; |
| 35 | + } |
| 36 | + |
| 37 | + public function schema(ToolInputSchema $schema): ToolInputSchema |
| 38 | + { |
| 39 | + $relatedOptions = $this->repository::collectRelated() |
| 40 | + ->intoAssoc() |
| 41 | + ->keys() |
| 42 | + ->toArray(); |
| 43 | + |
| 44 | + $schema->string('include') |
| 45 | + ->description('Comma-separated list of relationships to include in the response. Available options: '.implode(', ', $relatedOptions).' (e.g., include=employee,roles.permissions)'); |
| 46 | + |
| 47 | + return $schema; |
| 48 | + } |
| 49 | + |
| 50 | + public function handle(array $arguments): ToolResult|Generator |
| 51 | + { |
| 52 | + $user = auth()->user(); |
| 53 | + |
| 54 | + if (! $user) { |
| 55 | + return ToolResult::json([ |
| 56 | + 'error' => 'No authenticated user found', |
| 57 | + ]); |
| 58 | + } |
| 59 | + |
| 60 | + $arguments['id'] = $user->id; |
| 61 | + |
| 62 | + $this->repository->request = app(McpRequest::class); |
| 63 | + |
| 64 | + $result = $this->repository->indexTool($arguments, app(McpRequest::class)); |
| 65 | + |
| 66 | + return ToolResult::json($result); |
| 67 | + } |
| 68 | +} |
0 commit comments