feat: MCP tools for events (list/get/create/update/delete/done/ack); EventRules shared by /api and manual

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
This commit is contained in:
nikita.hohlov
2026-09-05 06:10:41 -03:00
parent 450c165fce
commit ed45a1d516
18 changed files with 711 additions and 56 deletions

View File

@@ -0,0 +1,50 @@
<?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);
}
}