Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
51 lines
1.9 KiB
PHP
51 lines
1.9 KiB
PHP
<?php
|
||
|
||
namespace App\Mcp\Tools;
|
||
|
||
use App\Ingest\ManualEvents;
|
||
use App\Models\Event;
|
||
use App\Models\User;
|
||
use Closure;
|
||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||
use Laravel\Mcp\Request;
|
||
use Laravel\Mcp\Response;
|
||
use Laravel\Mcp\ResponseFactory;
|
||
use Laravel\Mcp\Server\Tool;
|
||
|
||
/** Общее для тулов агента: пользователь токена и его события. */
|
||
abstract class AgentTool extends Tool
|
||
{
|
||
/** Пользователь, которого положил AuthenticateAgent — тот же атрибут, что у RemoteUser. */
|
||
protected function user(): User
|
||
{
|
||
return request()->attributes->get('user');
|
||
}
|
||
|
||
/** Событие по `id` из аргументов; чужое или несуществующее — ошибка для модели, а не 500. */
|
||
protected function withEvent(Request $request, Closure $then): Response|ResponseFactory
|
||
{
|
||
$id = (int) $request->validate(['id' => ['required', 'integer']])['id'];
|
||
try {
|
||
$event = $this->user()->ownEvent($id);
|
||
} catch (ModelNotFoundException) {
|
||
return Response::error('Событие не найдено');
|
||
}
|
||
|
||
return $then($event);
|
||
}
|
||
|
||
/** То же, но только своё (manual) событие — для правки и снятия. */
|
||
protected function withOwnEvent(Request $request, Closure $then): Response|ResponseFactory
|
||
{
|
||
return $this->withEvent($request, fn (Event $event) => ManualEvents::owns($event)
|
||
? $then($event)
|
||
: Response::error("Это событие источника {$event->source->name}: правит и снимает его только источник. Можно закрыть через mark_done."));
|
||
}
|
||
|
||
/** @param array<string,mixed> $data */
|
||
protected function respond(array $data): ResponseFactory
|
||
{
|
||
return Response::structured($data);
|
||
}
|
||
}
|