|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\MCP\Concerns; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Actions\Action; |
| 6 | +use Binaryk\LaravelRestify\Http\Requests\ActionRequest; |
| 7 | +use Binaryk\LaravelRestify\MCP\Requests\McpActionRequest; |
| 8 | +use Binaryk\LaravelRestify\MCP\Requests\McpRequest; |
| 9 | +use Laravel\Mcp\Server\Tools\ToolInputSchema; |
| 10 | + |
| 11 | +/** |
| 12 | + * @mixin \Binaryk\LaravelRestify\Repositories\Repository |
| 13 | + */ |
| 14 | +trait McpActionTool |
| 15 | +{ |
| 16 | + public function actionTool(Action $action, array $arguments, McpActionRequest $actionRequest): array |
| 17 | + { |
| 18 | + $actionRequest->merge($arguments); |
| 19 | + |
| 20 | + $this->sanitizeToolRequest($actionRequest, $arguments); |
| 21 | + |
| 22 | + if ($id = $actionRequest->input('id')) { |
| 23 | + if (!$action->authorizedToRun($actionRequest, $actionRequest->findModelOrFail($id))) { |
| 24 | + return [ |
| 25 | + 'error' => 'Not authorized to run this action', |
| 26 | + 'getter' => $action->uriKey(), |
| 27 | + ]; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + // Set up the action request context based on action type |
| 33 | + if (!$action->isStandalone()) { |
| 34 | + if (isset($arguments['id'])) { |
| 35 | + // Single model action (show context) |
| 36 | + $actionRequest->merge(['id' => $arguments['id']]); |
| 37 | + } elseif (isset($arguments['repositories'])) { |
| 38 | + // Multiple models action (index context) |
| 39 | + $actionRequest->merge(['repositories' => $arguments['repositories']]); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Check authorization |
| 44 | + if (!$action->authorizedToSee($actionRequest)) { |
| 45 | + return [ |
| 46 | + 'error' => 'Not authorized to see this action', |
| 47 | + 'action' => $action->uriKey(), |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + $result = $action->handleRequest($actionRequest); |
| 53 | + |
| 54 | + return [ |
| 55 | + 'success' => true, |
| 56 | + 'action' => $action->uriKey(), |
| 57 | + 'result' => $result->getData(), |
| 58 | + ]; |
| 59 | + } catch (\Exception $e) { |
| 60 | + return [ |
| 61 | + 'error' => $e->getMessage(), |
| 62 | + 'action' => $action->uriKey(), |
| 63 | + ]; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static function actionToolSchema(Action $action, ToolInputSchema $schema, McpActionRequest $mcpRequest): void |
| 68 | + { |
| 69 | + $modelName = class_basename(static::$model); |
| 70 | + |
| 71 | + // Add action-specific validation rules |
| 72 | + $actionRules = $action->rules(); |
| 73 | + foreach ($actionRules as $field => $rules) { |
| 74 | + $rulesArray = is_array($rules) ? $rules : explode('|', $rules); |
| 75 | + $isRequired = in_array('required', $rulesArray); |
| 76 | + |
| 77 | + // Determine field type based on rules |
| 78 | + if (in_array('boolean', $rulesArray)) { |
| 79 | + $fieldSchema = $schema->boolean($field); |
| 80 | + } elseif (in_array('integer', $rulesArray) || in_array('numeric', $rulesArray)) { |
| 81 | + $fieldSchema = $schema->number($field); |
| 82 | + } elseif (in_array('array', $rulesArray)) { |
| 83 | + $fieldSchema = $schema->array($field); |
| 84 | + } else { |
| 85 | + $fieldSchema = $schema->string($field); |
| 86 | + } |
| 87 | + |
| 88 | + if ($isRequired) { |
| 89 | + $fieldSchema->required(); |
| 90 | + } |
| 91 | + |
| 92 | + $fieldSchema->description("Action parameter: {$field}"); |
| 93 | + } |
| 94 | + |
| 95 | + // Add context-specific fields based on action type |
| 96 | + if ($action->isStandalone()) { |
| 97 | + // Standalone actions don't need ID or repositories |
| 98 | + $schema->string('include') |
| 99 | + ->description('Comma-separated list of relationships to include in response'); |
| 100 | + } else { |
| 101 | + // Check if it's primarily a show action or index action |
| 102 | + $shownOnShow = $action->isShownOnShow($mcpRequest, app(static::class)); |
| 103 | + $shownOnIndex = $action->isShownOnIndex($mcpRequest, app(static::class)); |
| 104 | + |
| 105 | + if ($shownOnShow && !$shownOnIndex) { |
| 106 | + // Show action - requires single ID |
| 107 | + $schema->string('id') |
| 108 | + ->description("The ID of the {$modelName} to perform the action on") |
| 109 | + ->required(); |
| 110 | + |
| 111 | + $schema->string('include') |
| 112 | + ->description('Comma-separated list of relationships to include'); |
| 113 | + } else { |
| 114 | + // Index action - requires repositories array |
| 115 | + $schema->string('repositories') |
| 116 | + ->description("Array of {$modelName} IDs to perform the action on. e.g. repositories=[1,2,3]") |
| 117 | + ->required(); |
| 118 | + |
| 119 | + $schema->string('include') |
| 120 | + ->description('Comma-separated list of relationships to include'); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments