Files
Inventory/src/Mcp/Tool/EntityHistoryTool.php
Matthieu add3a9a21f fix(mcp) : return CallToolResult to prevent structuredContent serialization issue
Tools now return CallToolResult directly instead of Content arrays,
preventing the MCP SDK from auto-generating structuredContent as a
JSON array (which Claude Code rejects — expects a JSON object/record).
Also adds Accept header to test helpers and SSE response parsing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 17:24:04 +01:00

59 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Mcp\Tool;
use App\Repository\AuditLogRepository;
use DateTimeInterface;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Schema\Result\CallToolResult;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'get_entity_history',
description: 'Get the audit history for a specific entity (machine, piece, composant, product). Returns list of changes with diffs.',
)]
class EntityHistoryTool
{
use McpToolHelper;
private const VALID_TYPES = ['machine', 'piece', 'composant', 'product'];
public function __construct(
private readonly AuditLogRepository $auditLogs,
private readonly Security $security,
) {}
public function __invoke(string $entityType, string $entityId): CallToolResult
{
$this->requireRole($this->security, 'ROLE_VIEWER');
if (!in_array($entityType, self::VALID_TYPES, true)) {
$this->mcpError('Validation', sprintf(
'Invalid entityType "%s". Must be one of: %s',
$entityType,
implode(', ', self::VALID_TYPES),
));
}
$logs = $this->auditLogs->findEntityHistory($entityType, $entityId, 200);
$items = array_map(
static fn ($log) => [
'id' => $log->getId(),
'action' => $log->getAction(),
'diff' => $log->getDiff(),
'actorProfileId' => $log->getActorProfileId(),
'createdAt' => $log->getCreatedAt()->format(DateTimeInterface::ATOM),
],
$logs,
);
return $this->jsonResponse([
'items' => array_values($items),
'total' => count($items),
]);
}
}