- list_slots + update_slots for composant/piece slots - list/add/update/remove machine links (component, piece, product) - get_machine_structure with full hierarchy - clone_machine with all links and custom fields - 52 MCP tests pass total Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
163 lines
6.4 KiB
PHP
163 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Mcp\Tool\Machine;
|
|
|
|
use App\Entity\MachineComponentLink;
|
|
use App\Entity\MachinePieceLink;
|
|
use App\Entity\MachineProductLink;
|
|
use App\Mcp\Tool\McpToolHelper;
|
|
use App\Repository\ComposantRepository;
|
|
use App\Repository\MachineComponentLinkRepository;
|
|
use App\Repository\MachinePieceLinkRepository;
|
|
use App\Repository\MachineRepository;
|
|
use App\Repository\PieceRepository;
|
|
use App\Repository\ProductRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Mcp\Capability\Attribute\McpTool;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
|
|
#[McpTool(
|
|
name: 'add_machine_links',
|
|
description: 'Add one or more links (composant, piece, product) to a machine. Each link specifies a type, entityId, and optional parentLinkId / overrides. Requires ROLE_GESTIONNAIRE.',
|
|
)]
|
|
class AddMachineLinksTool
|
|
{
|
|
use McpToolHelper;
|
|
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $em,
|
|
private readonly Security $security,
|
|
private readonly MachineRepository $machines,
|
|
private readonly ComposantRepository $composants,
|
|
private readonly PieceRepository $pieces,
|
|
private readonly ProductRepository $products,
|
|
private readonly MachineComponentLinkRepository $componentLinks,
|
|
private readonly MachinePieceLinkRepository $pieceLinks,
|
|
) {}
|
|
|
|
public function __invoke(string $machineId, array $links): array
|
|
{
|
|
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
|
|
|
$machine = $this->machines->find($machineId);
|
|
if (null === $machine) {
|
|
$this->mcpError('NotFound', "Machine {$machineId} not found.");
|
|
}
|
|
|
|
$created = [];
|
|
|
|
foreach ($links as $linkData) {
|
|
$type = $linkData['type'] ?? '';
|
|
$entityId = $linkData['entityId'] ?? '';
|
|
|
|
switch ($type) {
|
|
case 'composant':
|
|
$composant = $this->composants->find($entityId);
|
|
if (null === $composant) {
|
|
$this->mcpError('NotFound', "Composant {$entityId} not found.");
|
|
}
|
|
|
|
$link = new MachineComponentLink();
|
|
$link->setMachine($machine);
|
|
$link->setComposant($composant);
|
|
|
|
if (!empty($linkData['parentLinkId'])) {
|
|
$parent = $this->componentLinks->find($linkData['parentLinkId']);
|
|
if (null !== $parent) {
|
|
$link->setParentLink($parent);
|
|
}
|
|
}
|
|
if (isset($linkData['nameOverride'])) {
|
|
$link->setNameOverride($linkData['nameOverride']);
|
|
}
|
|
if (isset($linkData['referenceOverride'])) {
|
|
$link->setReferenceOverride($linkData['referenceOverride']);
|
|
}
|
|
if (isset($linkData['prixOverride'])) {
|
|
$link->setPrixOverride($linkData['prixOverride']);
|
|
}
|
|
|
|
$this->em->persist($link);
|
|
$created[] = ['id' => $link->getId(), 'type' => 'composant', 'entityId' => $entityId];
|
|
|
|
break;
|
|
|
|
case 'piece':
|
|
$piece = $this->pieces->find($entityId);
|
|
if (null === $piece) {
|
|
$this->mcpError('NotFound', "Piece {$entityId} not found.");
|
|
}
|
|
|
|
$link = new MachinePieceLink();
|
|
$link->setMachine($machine);
|
|
$link->setPiece($piece);
|
|
$link->setQuantity((int) ($linkData['quantity'] ?? 1));
|
|
|
|
if (!empty($linkData['parentLinkId'])) {
|
|
$parent = $this->componentLinks->find($linkData['parentLinkId']);
|
|
if (null !== $parent) {
|
|
$link->setParentLink($parent);
|
|
}
|
|
}
|
|
if (isset($linkData['nameOverride'])) {
|
|
$link->setNameOverride($linkData['nameOverride']);
|
|
}
|
|
if (isset($linkData['referenceOverride'])) {
|
|
$link->setReferenceOverride($linkData['referenceOverride']);
|
|
}
|
|
if (isset($linkData['prixOverride'])) {
|
|
$link->setPrixOverride($linkData['prixOverride']);
|
|
}
|
|
|
|
$this->em->persist($link);
|
|
$created[] = ['id' => $link->getId(), 'type' => 'piece', 'entityId' => $entityId];
|
|
|
|
break;
|
|
|
|
case 'product':
|
|
$product = $this->products->find($entityId);
|
|
if (null === $product) {
|
|
$this->mcpError('NotFound', "Product {$entityId} not found.");
|
|
}
|
|
|
|
$link = new MachineProductLink();
|
|
$link->setMachine($machine);
|
|
$link->setProduct($product);
|
|
|
|
if (!empty($linkData['parentLinkId'])) {
|
|
$parentProduct = $this->em->getRepository(MachineProductLink::class)->find($linkData['parentLinkId']);
|
|
if (null !== $parentProduct) {
|
|
$link->setParentLink($parentProduct);
|
|
}
|
|
}
|
|
if (!empty($linkData['parentComponentLinkId'])) {
|
|
$parentComp = $this->componentLinks->find($linkData['parentComponentLinkId']);
|
|
if (null !== $parentComp) {
|
|
$link->setParentComponentLink($parentComp);
|
|
}
|
|
}
|
|
if (!empty($linkData['parentPieceLinkId'])) {
|
|
$parentPiece = $this->pieceLinks->find($linkData['parentPieceLinkId']);
|
|
if (null !== $parentPiece) {
|
|
$link->setParentPieceLink($parentPiece);
|
|
}
|
|
}
|
|
|
|
$this->em->persist($link);
|
|
$created[] = ['id' => $link->getId(), 'type' => 'product', 'entityId' => $entityId];
|
|
|
|
break;
|
|
|
|
default:
|
|
$this->mcpError('Validation', "Unknown link type '{$type}'. Expected composant, piece, or product.");
|
|
}
|
|
}
|
|
|
|
$this->em->flush();
|
|
|
|
return $this->jsonResponse(['created' => $created]);
|
|
}
|
|
}
|