feat(mcp) : add CRUD tools for Pieces, Composants, Machines
- 5 tools each: list, get, create, update, delete - Piece: includes typePiece, constructeurs, prix (string) - Composant: includes typeComposant, constructeurs, prix (string) - Machine: includes site (required), constructeurs - 40 MCP tests pass total Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
74
src/Mcp/Tool/Machine/CreateMachineTool.php
Normal file
74
src/Mcp/Tool/Machine/CreateMachineTool.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Entity\Machine;
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
use App\Repository\SiteRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'create_machine',
|
||||
description: 'Create a new machine. siteId is required. prix must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class CreateMachineTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
private readonly SiteRepository $sites,
|
||||
private readonly ConstructeurRepository $constructeurs,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param string[] $constructeurIds
|
||||
*/
|
||||
public function __invoke(
|
||||
string $name,
|
||||
string $siteId,
|
||||
string $reference = '',
|
||||
string $prix = '',
|
||||
array $constructeurIds = [],
|
||||
): array {
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$site = $this->sites->find($siteId);
|
||||
if (!$site) {
|
||||
$this->mcpError('not_found', "Site not found: {$siteId}");
|
||||
}
|
||||
|
||||
$machine = new Machine();
|
||||
$machine->setName($name);
|
||||
$machine->setSite($site);
|
||||
|
||||
if ('' !== $reference) {
|
||||
$machine->setReference($reference);
|
||||
}
|
||||
if ('' !== $prix) {
|
||||
$machine->setPrix($prix);
|
||||
}
|
||||
|
||||
foreach ($constructeurIds as $cId) {
|
||||
$c = $this->constructeurs->find($cId);
|
||||
if (!$c) {
|
||||
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
||||
}
|
||||
$machine->addConstructeur($c);
|
||||
}
|
||||
|
||||
$this->em->persist($machine);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $machine->getId(),
|
||||
'name' => $machine->getName(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
42
src/Mcp/Tool/Machine/DeleteMachineTool.php
Normal file
42
src/Mcp/Tool/Machine/DeleteMachineTool.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\MachineRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'delete_machine',
|
||||
description: 'Delete a machine by ID. Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class DeleteMachineTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly MachineRepository $machines,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $machineId): array
|
||||
{
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$machine = $this->machines->find($machineId);
|
||||
|
||||
if (!$machine) {
|
||||
$this->mcpError('not_found', "Machine not found: {$machineId}");
|
||||
}
|
||||
|
||||
$this->em->remove($machine);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse(['deleted' => true, 'id' => $machineId]);
|
||||
}
|
||||
}
|
||||
58
src/Mcp/Tool/Machine/GetMachineTool.php
Normal file
58
src/Mcp/Tool/Machine/GetMachineTool.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\MachineRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'get_machine',
|
||||
description: 'Get a single machine by ID with all its details, including site and constructeurs.',
|
||||
)]
|
||||
class GetMachineTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly MachineRepository $machines,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $machineId): array
|
||||
{
|
||||
$machine = $this->machines->find($machineId);
|
||||
|
||||
if (!$machine) {
|
||||
$this->mcpError('not_found', "Machine not found: {$machineId}");
|
||||
}
|
||||
|
||||
$constructeurs = [];
|
||||
foreach ($machine->getConstructeurs() as $c) {
|
||||
$constructeurs[] = [
|
||||
'id' => $c->getId(),
|
||||
'name' => $c->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
$site = null;
|
||||
if ($machine->getSite()) {
|
||||
$site = [
|
||||
'id' => $machine->getSite()->getId(),
|
||||
'name' => $machine->getSite()->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $machine->getId(),
|
||||
'name' => $machine->getName(),
|
||||
'reference' => $machine->getReference(),
|
||||
'prix' => $machine->getPrix(),
|
||||
'site' => $site,
|
||||
'constructeurs' => $constructeurs,
|
||||
'createdAt' => $machine->getCreatedAt()->format('Y-m-d H:i:s'),
|
||||
'updatedAt' => $machine->getUpdatedAt()->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
55
src/Mcp/Tool/Machine/ListMachinesTool.php
Normal file
55
src/Mcp/Tool/Machine/ListMachinesTool.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\MachineRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'list_machines',
|
||||
description: 'List machines with pagination. Filterable by name or reference.',
|
||||
)]
|
||||
class ListMachinesTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly MachineRepository $machines,
|
||||
) {}
|
||||
|
||||
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
|
||||
{
|
||||
$p = $this->paginationParams($page, $limit);
|
||||
|
||||
$countQb = $this->machines->createQueryBuilder('m')
|
||||
->select('COUNT(m.id)')
|
||||
;
|
||||
|
||||
$qb = $this->machines->createQueryBuilder('m')
|
||||
->select('m.id', 'm.name', 'm.reference', 'm.prix')
|
||||
->orderBy('m.name', 'ASC')
|
||||
;
|
||||
|
||||
if ('' !== $search) {
|
||||
$countQb->andWhere('LOWER(m.name) LIKE LOWER(:search) OR LOWER(m.reference) LIKE LOWER(:search)')
|
||||
->setParameter('search', "%{$search}%")
|
||||
;
|
||||
$qb->andWhere('LOWER(m.name) LIKE LOWER(:search) OR LOWER(m.reference) LIKE LOWER(:search)')
|
||||
->setParameter('search', "%{$search}%")
|
||||
;
|
||||
}
|
||||
|
||||
$total = (int) $countQb->getQuery()->getSingleScalarResult();
|
||||
|
||||
$items = $qb->setFirstResult($p['offset'])
|
||||
->setMaxResults($p['limit'])
|
||||
->getQuery()
|
||||
->getArrayResult()
|
||||
;
|
||||
|
||||
return $this->paginatedResponse($items, $total, $p['page'], $p['limit']);
|
||||
}
|
||||
}
|
||||
85
src/Mcp/Tool/Machine/UpdateMachineTool.php
Normal file
85
src/Mcp/Tool/Machine/UpdateMachineTool.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
use App\Repository\MachineRepository;
|
||||
use App\Repository\SiteRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'update_machine',
|
||||
description: 'Update an existing machine. Only provided fields are changed. prix must be a string. Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class UpdateMachineTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly MachineRepository $machines,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
private readonly SiteRepository $sites,
|
||||
private readonly ConstructeurRepository $constructeurs,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param null|string[] $constructeurIds
|
||||
*/
|
||||
public function __invoke(
|
||||
string $machineId,
|
||||
?string $name = null,
|
||||
?string $reference = null,
|
||||
?string $prix = null,
|
||||
?string $siteId = null,
|
||||
?array $constructeurIds = null,
|
||||
): array {
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$machine = $this->machines->find($machineId);
|
||||
|
||||
if (!$machine) {
|
||||
$this->mcpError('not_found', "Machine not found: {$machineId}");
|
||||
}
|
||||
|
||||
if (null !== $name) {
|
||||
$machine->setName($name);
|
||||
}
|
||||
if (null !== $reference) {
|
||||
$machine->setReference($reference);
|
||||
}
|
||||
if (null !== $prix) {
|
||||
$machine->setPrix($prix);
|
||||
}
|
||||
|
||||
if (null !== $siteId) {
|
||||
$site = $this->sites->find($siteId);
|
||||
if (!$site) {
|
||||
$this->mcpError('not_found', "Site not found: {$siteId}");
|
||||
}
|
||||
$machine->setSite($site);
|
||||
}
|
||||
|
||||
if (null !== $constructeurIds) {
|
||||
foreach ($machine->getConstructeurs()->toArray() as $existing) {
|
||||
$machine->removeConstructeur($existing);
|
||||
}
|
||||
foreach ($constructeurIds as $cId) {
|
||||
$c = $this->constructeurs->find($cId);
|
||||
if (!$c) {
|
||||
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
||||
}
|
||||
$machine->addConstructeur($c);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse(['id' => $machine->getId(), 'name' => $machine->getName()]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user