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>
64 lines
1.9 KiB
PHP
64 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Mcp\Tool\Document;
|
|
|
|
use App\Mcp\Tool\McpToolHelper;
|
|
use App\Repository\DocumentRepository;
|
|
use Mcp\Capability\Attribute\McpTool;
|
|
use Mcp\Schema\Result\CallToolResult;
|
|
|
|
#[McpTool(
|
|
name: 'list_documents',
|
|
description: 'List documents attached to a given entity. entityType must be one of: site, machine, composant, piece, product.',
|
|
)]
|
|
class ListDocumentsTool
|
|
{
|
|
use McpToolHelper;
|
|
|
|
private const ENTITY_FIELDS = [
|
|
'site' => 'site',
|
|
'machine' => 'machine',
|
|
'composant' => 'composant',
|
|
'piece' => 'piece',
|
|
'product' => 'product',
|
|
];
|
|
|
|
public function __construct(
|
|
private readonly DocumentRepository $documents,
|
|
) {}
|
|
|
|
public function __invoke(string $entityType, string $entityId): CallToolResult
|
|
{
|
|
if (!isset(self::ENTITY_FIELDS[$entityType])) {
|
|
$this->mcpError('validation', "Invalid entityType '{$entityType}'. Must be one of: site, machine, composant, piece, product.");
|
|
}
|
|
|
|
$field = self::ENTITY_FIELDS[$entityType];
|
|
|
|
$docs = $this->documents->findBy([$field => $entityId], ['createdAt' => 'DESC']);
|
|
|
|
$items = [];
|
|
foreach ($docs as $doc) {
|
|
$items[] = [
|
|
'id' => $doc->getId(),
|
|
'name' => $doc->getName(),
|
|
'filename' => $doc->getFilename(),
|
|
'fileUrl' => '/api/documents/'.$doc->getId().'/file',
|
|
'downloadUrl' => '/api/documents/'.$doc->getId().'/download',
|
|
'mimeType' => $doc->getMimeType(),
|
|
'size' => $doc->getSize(),
|
|
'createdAt' => $doc->getCreatedAt()->format('Y-m-d H:i:s'),
|
|
];
|
|
}
|
|
|
|
return $this->jsonResponse([
|
|
'entityType' => $entityType,
|
|
'entityId' => $entityId,
|
|
'items' => $items,
|
|
'total' => count($items),
|
|
]);
|
|
}
|
|
}
|