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>
95 lines
2.8 KiB
PHP
95 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Mcp\Tool\Piece;
|
|
|
|
use App\Mcp\Tool\McpToolHelper;
|
|
use App\Repository\ConstructeurRepository;
|
|
use App\Repository\ModelTypeRepository;
|
|
use App\Repository\PieceRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Mcp\Capability\Attribute\McpTool;
|
|
use Mcp\Schema\Result\CallToolResult;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
|
|
#[McpTool(
|
|
name: 'update_piece',
|
|
description: 'Update an existing piece. Only provided fields are changed. prix must be a string. Requires ROLE_GESTIONNAIRE.',
|
|
)]
|
|
class UpdatePieceTool
|
|
{
|
|
use McpToolHelper;
|
|
|
|
public function __construct(
|
|
private readonly PieceRepository $pieces,
|
|
private readonly EntityManagerInterface $em,
|
|
private readonly Security $security,
|
|
private readonly ModelTypeRepository $modelTypes,
|
|
private readonly ConstructeurRepository $constructeurs,
|
|
) {}
|
|
|
|
/**
|
|
* @param null|string[] $constructeurIds
|
|
*/
|
|
public function __invoke(
|
|
string $pieceId,
|
|
?string $name = null,
|
|
?string $reference = null,
|
|
?string $description = null,
|
|
?string $prix = null,
|
|
?string $modelTypeId = null,
|
|
?array $constructeurIds = null,
|
|
): CallToolResult {
|
|
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
|
|
|
$piece = $this->pieces->find($pieceId);
|
|
|
|
if (!$piece) {
|
|
$this->mcpError('not_found', "Piece not found: {$pieceId}");
|
|
}
|
|
|
|
if (null !== $name) {
|
|
$piece->setName($name);
|
|
}
|
|
if (null !== $reference) {
|
|
$piece->setReference($reference);
|
|
}
|
|
if (null !== $description) {
|
|
$piece->setDescription($description);
|
|
}
|
|
if (null !== $prix) {
|
|
$piece->setPrix($prix);
|
|
}
|
|
|
|
if (null !== $modelTypeId) {
|
|
if ('' === $modelTypeId) {
|
|
$piece->setTypePiece(null);
|
|
} else {
|
|
$modelType = $this->modelTypes->find($modelTypeId);
|
|
if (!$modelType) {
|
|
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
|
|
}
|
|
$piece->setTypePiece($modelType);
|
|
}
|
|
}
|
|
|
|
if (null !== $constructeurIds) {
|
|
foreach ($piece->getConstructeurs()->toArray() as $existing) {
|
|
$piece->removeConstructeur($existing);
|
|
}
|
|
foreach ($constructeurIds as $cId) {
|
|
$c = $this->constructeurs->find($cId);
|
|
if (!$c) {
|
|
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
|
}
|
|
$piece->addConstructeur($c);
|
|
}
|
|
}
|
|
|
|
$this->em->flush();
|
|
|
|
return $this->jsonResponse(['id' => $piece->getId(), 'name' => $piece->getName()]);
|
|
}
|
|
}
|