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:
80
src/Mcp/Tool/Piece/CreatePieceTool.php
Normal file
80
src/Mcp/Tool/Piece/CreatePieceTool.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Piece;
|
||||
|
||||
use App\Entity\Piece;
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
use App\Repository\ModelTypeRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'create_piece',
|
||||
description: 'Create a new piece. prix must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class CreatePieceTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
private readonly ModelTypeRepository $modelTypes,
|
||||
private readonly ConstructeurRepository $constructeurs,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param string[] $constructeurIds
|
||||
*/
|
||||
public function __invoke(
|
||||
string $name,
|
||||
string $reference = '',
|
||||
string $description = '',
|
||||
string $prix = '',
|
||||
string $modelTypeId = '',
|
||||
array $constructeurIds = [],
|
||||
): array {
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$piece = new Piece();
|
||||
$piece->setName($name);
|
||||
|
||||
if ('' !== $reference) {
|
||||
$piece->setReference($reference);
|
||||
}
|
||||
if ('' !== $description) {
|
||||
$piece->setDescription($description);
|
||||
}
|
||||
if ('' !== $prix) {
|
||||
$piece->setPrix($prix);
|
||||
}
|
||||
|
||||
if ('' !== $modelTypeId) {
|
||||
$modelType = $this->modelTypes->find($modelTypeId);
|
||||
if (!$modelType) {
|
||||
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
|
||||
}
|
||||
$piece->setTypePiece($modelType);
|
||||
}
|
||||
|
||||
foreach ($constructeurIds as $cId) {
|
||||
$c = $this->constructeurs->find($cId);
|
||||
if (!$c) {
|
||||
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
||||
}
|
||||
$piece->addConstructeur($c);
|
||||
}
|
||||
|
||||
$this->em->persist($piece);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $piece->getId(),
|
||||
'name' => $piece->getName(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
42
src/Mcp/Tool/Piece/DeletePieceTool.php
Normal file
42
src/Mcp/Tool/Piece/DeletePieceTool.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Piece;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\PieceRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'delete_piece',
|
||||
description: 'Delete a piece by ID. Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class DeletePieceTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly PieceRepository $pieces,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $pieceId): array
|
||||
{
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$piece = $this->pieces->find($pieceId);
|
||||
|
||||
if (!$piece) {
|
||||
$this->mcpError('not_found', "Piece not found: {$pieceId}");
|
||||
}
|
||||
|
||||
$this->em->remove($piece);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse(['deleted' => true, 'id' => $pieceId]);
|
||||
}
|
||||
}
|
||||
59
src/Mcp/Tool/Piece/GetPieceTool.php
Normal file
59
src/Mcp/Tool/Piece/GetPieceTool.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Piece;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\PieceRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'get_piece',
|
||||
description: 'Get a single piece by ID with all its details, including typePiece and constructeurs.',
|
||||
)]
|
||||
class GetPieceTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly PieceRepository $pieces,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $pieceId): array
|
||||
{
|
||||
$piece = $this->pieces->find($pieceId);
|
||||
|
||||
if (!$piece) {
|
||||
$this->mcpError('not_found', "Piece not found: {$pieceId}");
|
||||
}
|
||||
|
||||
$constructeurs = [];
|
||||
foreach ($piece->getConstructeurs() as $c) {
|
||||
$constructeurs[] = [
|
||||
'id' => $c->getId(),
|
||||
'name' => $c->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
$typePiece = null;
|
||||
if ($piece->getTypePiece()) {
|
||||
$typePiece = [
|
||||
'id' => $piece->getTypePiece()->getId(),
|
||||
'name' => $piece->getTypePiece()->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $piece->getId(),
|
||||
'name' => $piece->getName(),
|
||||
'reference' => $piece->getReference(),
|
||||
'description' => $piece->getDescription(),
|
||||
'prix' => $piece->getPrix(),
|
||||
'typePiece' => $typePiece,
|
||||
'constructeurs' => $constructeurs,
|
||||
'createdAt' => $piece->getCreatedAt()->format('Y-m-d H:i:s'),
|
||||
'updatedAt' => $piece->getUpdatedAt()->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
55
src/Mcp/Tool/Piece/ListPiecesTool.php
Normal file
55
src/Mcp/Tool/Piece/ListPiecesTool.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Piece;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\PieceRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'list_pieces',
|
||||
description: 'List pieces with pagination. Filterable by name or reference.',
|
||||
)]
|
||||
class ListPiecesTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly PieceRepository $pieces,
|
||||
) {}
|
||||
|
||||
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
|
||||
{
|
||||
$p = $this->paginationParams($page, $limit);
|
||||
|
||||
$countQb = $this->pieces->createQueryBuilder('pi')
|
||||
->select('COUNT(pi.id)')
|
||||
;
|
||||
|
||||
$qb = $this->pieces->createQueryBuilder('pi')
|
||||
->select('pi.id', 'pi.name', 'pi.reference', 'pi.prix')
|
||||
->orderBy('pi.name', 'ASC')
|
||||
;
|
||||
|
||||
if ('' !== $search) {
|
||||
$countQb->andWhere('LOWER(pi.name) LIKE LOWER(:search) OR LOWER(pi.reference) LIKE LOWER(:search)')
|
||||
->setParameter('search', "%{$search}%")
|
||||
;
|
||||
$qb->andWhere('LOWER(pi.name) LIKE LOWER(:search) OR LOWER(pi.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']);
|
||||
}
|
||||
}
|
||||
93
src/Mcp/Tool/Piece/UpdatePieceTool.php
Normal file
93
src/Mcp/Tool/Piece/UpdatePieceTool.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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 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,
|
||||
): array {
|
||||
$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()]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user