feat(mcp) : add CRUD tools for Sites, Constructeurs, Products

- 5 tools each: list, get, create, update, delete
- McpToolHelper extracted to AbstractApiTestCase for reuse
- DashboardStatsToolTest simplified to use base helpers
- 22 MCP tests pass

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-03-16 14:31:15 +01:00
parent e335f4c24c
commit 4f1e136dc5
20 changed files with 1184 additions and 90 deletions

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Entity\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'create_constructeur',
description: 'Create a new constructeur (manufacturer/supplier). Requires ROLE_GESTIONNAIRE.',
)]
class CreateConstructeurTool
{
use McpToolHelper;
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(
string $name,
string $email = '',
string $phone = '',
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$constructeur = new Constructeur();
$constructeur->setName($name);
$constructeur->setEmail('' !== $email ? $email : null);
$constructeur->setPhone('' !== $phone ? $phone : null);
$this->em->persist($constructeur);
$this->em->flush();
return $this->jsonResponse([
'id' => $constructeur->getId(),
'name' => $constructeur->getName(),
]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'delete_constructeur',
description: 'Delete a constructeur by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteConstructeurTool
{
use McpToolHelper;
public function __construct(
private readonly ConstructeurRepository $constructeurs,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(string $constructeurId): array
{
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$constructeur = $this->constructeurs->find($constructeurId);
if (!$constructeur) {
$this->mcpError('not_found', "Constructeur not found: {$constructeurId}");
}
$this->em->remove($constructeur);
$this->em->flush();
return $this->jsonResponse(['deleted' => true, 'id' => $constructeurId]);
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'get_constructeur',
description: 'Get a single constructeur (manufacturer/supplier) by ID with all its details.',
)]
class GetConstructeurTool
{
use McpToolHelper;
public function __construct(
private readonly ConstructeurRepository $constructeurs,
) {}
public function __invoke(string $constructeurId): array
{
$constructeur = $this->constructeurs->find($constructeurId);
if (!$constructeur) {
$this->mcpError('not_found', "Constructeur not found: {$constructeurId}");
}
return $this->jsonResponse([
'id' => $constructeur->getId(),
'name' => $constructeur->getName(),
'email' => $constructeur->getEmail(),
'phone' => $constructeur->getPhone(),
'createdAt' => $constructeur->getCreatedAt()->format('Y-m-d H:i:s'),
'updatedAt' => $constructeur->getUpdatedAt()->format('Y-m-d H:i:s'),
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'list_constructeurs',
description: 'List all constructeurs (manufacturers/suppliers) with pagination. Filterable by name.',
)]
class ListConstructeursTool
{
use McpToolHelper;
public function __construct(
private readonly ConstructeurRepository $constructeurs,
) {}
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
{
$p = $this->paginationParams($page, $limit);
$countQb = $this->constructeurs->createQueryBuilder('c')
->select('COUNT(c.id)')
;
$qb = $this->constructeurs->createQueryBuilder('c')
->select('c.id', 'c.name', 'c.email', 'c.phone')
->orderBy('c.name', 'ASC')
;
if ('' !== $search) {
$countQb->andWhere('LOWER(c.name) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
$qb->andWhere('LOWER(c.name) 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']);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'update_constructeur',
description: 'Update an existing constructeur. Only provided fields are changed. Requires ROLE_GESTIONNAIRE.',
)]
class UpdateConstructeurTool
{
use McpToolHelper;
public function __construct(
private readonly ConstructeurRepository $constructeurs,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(
string $constructeurId,
?string $name = null,
?string $email = null,
?string $phone = null,
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$constructeur = $this->constructeurs->find($constructeurId);
if (!$constructeur) {
$this->mcpError('not_found', "Constructeur not found: {$constructeurId}");
}
if (null !== $name) {
$constructeur->setName($name);
}
if (null !== $email) {
$constructeur->setEmail($email);
}
if (null !== $phone) {
$constructeur->setPhone($phone);
}
$this->em->flush();
return $this->jsonResponse(['id' => $constructeur->getId(), 'name' => $constructeur->getName()]);
}
}

View File

@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Entity\Product;
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_product',
description: 'Create a new product. supplierPrice must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
)]
class CreateProductTool
{
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 $supplierPrice = '',
string $modelTypeId = '',
array $constructeurIds = [],
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$product = new Product();
$product->setName($name);
if ('' !== $reference) {
$product->setReference($reference);
}
if ('' !== $supplierPrice) {
$product->setSupplierPrice($supplierPrice);
}
if ('' !== $modelTypeId) {
$modelType = $this->modelTypes->find($modelTypeId);
if (!$modelType) {
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
}
$product->setTypeProduct($modelType);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$product->addConstructeur($c);
}
$this->em->persist($product);
$this->em->flush();
return $this->jsonResponse([
'id' => $product->getId(),
'name' => $product->getName(),
]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'delete_product',
description: 'Delete a product by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteProductTool
{
use McpToolHelper;
public function __construct(
private readonly ProductRepository $products,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(string $productId): array
{
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$product = $this->products->find($productId);
if (!$product) {
$this->mcpError('not_found', "Product not found: {$productId}");
}
$this->em->remove($product);
$this->em->flush();
return $this->jsonResponse(['deleted' => true, 'id' => $productId]);
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ProductRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'get_product',
description: 'Get a single product by ID with all its details, including typeProduct and constructeurs.',
)]
class GetProductTool
{
use McpToolHelper;
public function __construct(
private readonly ProductRepository $products,
) {}
public function __invoke(string $productId): array
{
$product = $this->products->find($productId);
if (!$product) {
$this->mcpError('not_found', "Product not found: {$productId}");
}
$constructeurs = [];
foreach ($product->getConstructeurs() as $c) {
$constructeurs[] = [
'id' => $c->getId(),
'name' => $c->getName(),
];
}
$typeProduct = null;
if ($product->getTypeProduct()) {
$typeProduct = [
'id' => $product->getTypeProduct()->getId(),
'name' => $product->getTypeProduct()->getName(),
];
}
return $this->jsonResponse([
'id' => $product->getId(),
'name' => $product->getName(),
'reference' => $product->getReference(),
'supplierPrice' => $product->getSupplierPrice(),
'typeProduct' => $typeProduct,
'constructeurs' => $constructeurs,
'createdAt' => $product->getCreatedAt()->format('Y-m-d H:i:s'),
'updatedAt' => $product->getUpdatedAt()->format('Y-m-d H:i:s'),
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ProductRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'list_products',
description: 'List products with pagination. Filterable by name or reference.',
)]
class ListProductsTool
{
use McpToolHelper;
public function __construct(
private readonly ProductRepository $products,
) {}
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
{
$p = $this->paginationParams($page, $limit);
$countQb = $this->products->createQueryBuilder('pr')
->select('COUNT(pr.id)')
;
$qb = $this->products->createQueryBuilder('pr')
->select('pr.id', 'pr.name', 'pr.reference', 'pr.supplierPrice')
->orderBy('pr.name', 'ASC')
;
if ('' !== $search) {
$countQb->andWhere('LOWER(pr.name) LIKE LOWER(:search) OR LOWER(pr.reference) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
$qb->andWhere('LOWER(pr.name) LIKE LOWER(:search) OR LOWER(pr.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']);
}
}

View File

@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\ModelTypeRepository;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'update_product',
description: 'Update an existing product. Only provided fields are changed. supplierPrice must be a string. Requires ROLE_GESTIONNAIRE.',
)]
class UpdateProductTool
{
use McpToolHelper;
public function __construct(
private readonly ProductRepository $products,
private readonly EntityManagerInterface $em,
private readonly Security $security,
private readonly ModelTypeRepository $modelTypes,
private readonly ConstructeurRepository $constructeurs,
) {}
/**
* @param null|string[] $constructeurIds
*/
public function __invoke(
string $productId,
?string $name = null,
?string $reference = null,
?string $supplierPrice = null,
?string $modelTypeId = null,
?array $constructeurIds = null,
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$product = $this->products->find($productId);
if (!$product) {
$this->mcpError('not_found', "Product not found: {$productId}");
}
if (null !== $name) {
$product->setName($name);
}
if (null !== $reference) {
$product->setReference($reference);
}
if (null !== $supplierPrice) {
$product->setSupplierPrice($supplierPrice);
}
if (null !== $modelTypeId) {
if ('' === $modelTypeId) {
$product->setTypeProduct(null);
} else {
$modelType = $this->modelTypes->find($modelTypeId);
if (!$modelType) {
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
}
$product->setTypeProduct($modelType);
}
}
if (null !== $constructeurIds) {
foreach ($product->getConstructeurs()->toArray() as $existing) {
$product->removeConstructeur($existing);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$product->addConstructeur($c);
}
}
$this->em->flush();
return $this->jsonResponse(['id' => $product->getId(), 'name' => $product->getName()]);
}
}

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Entity\Site;
use App\Mcp\Tool\McpToolHelper;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'create_site',
description: 'Create a new industrial site. Requires ROLE_GESTIONNAIRE.',
)]
class CreateSiteTool
{
use McpToolHelper;
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(
string $name,
string $contactName = '',
string $contactPhone = '',
string $contactAddress = '',
string $contactPostalCode = '',
string $contactCity = '',
string $color = '',
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$site = new Site();
$site->setName($name);
$site->setContactName($contactName);
$site->setContactPhone($contactPhone);
$site->setContactAddress($contactAddress);
$site->setContactPostalCode($contactPostalCode);
$site->setContactCity($contactCity);
$site->setColor($color);
$this->em->persist($site);
$this->em->flush();
return $this->jsonResponse([
'id' => $site->getId(),
'name' => $site->getName(),
]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\SiteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'delete_site',
description: 'Delete a site by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteSiteTool
{
use McpToolHelper;
public function __construct(
private readonly SiteRepository $sites,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(string $siteId): array
{
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$site = $this->sites->find($siteId);
if (!$site) {
$this->mcpError('not_found', "Site not found: {$siteId}");
}
$this->em->remove($site);
$this->em->flush();
return $this->jsonResponse(['deleted' => true, 'id' => $siteId]);
}
}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\SiteRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'get_site',
description: 'Get a single site by ID with all its details (name, contact info, address).',
)]
class GetSiteTool
{
use McpToolHelper;
public function __construct(
private readonly SiteRepository $sites,
) {}
public function __invoke(string $siteId): array
{
$site = $this->sites->find($siteId);
if (!$site) {
$this->mcpError('not_found', "Site not found: {$siteId}");
}
return $this->jsonResponse([
'id' => $site->getId(),
'name' => $site->getName(),
'contactName' => $site->getContactName(),
'contactPhone' => $site->getContactPhone(),
'contactAddress' => $site->getContactAddress(),
'contactPostalCode' => $site->getContactPostalCode(),
'contactCity' => $site->getContactCity(),
'color' => $site->getColor(),
'createdAt' => $site->getCreatedAt()->format('Y-m-d H:i:s'),
'updatedAt' => $site->getUpdatedAt()->format('Y-m-d H:i:s'),
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\SiteRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'list_sites',
description: 'List all industrial sites with pagination. Sites contain machines. Filterable by name.',
)]
class ListSitesTool
{
use McpToolHelper;
public function __construct(
private readonly SiteRepository $sites,
) {}
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
{
$p = $this->paginationParams($page, $limit);
$countQb = $this->sites->createQueryBuilder('s')
->select('COUNT(s.id)')
;
$qb = $this->sites->createQueryBuilder('s')
->select('s.id', 's.name', 's.contactName', 's.contactCity', 's.contactPhone')
->orderBy('s.name', 'ASC')
;
if ('' !== $search) {
$countQb->andWhere('LOWER(s.name) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
$qb->andWhere('LOWER(s.name) 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']);
}
}

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\SiteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'update_site',
description: 'Update an existing site. Only provided fields are changed. Requires ROLE_GESTIONNAIRE.',
)]
class UpdateSiteTool
{
use McpToolHelper;
public function __construct(
private readonly SiteRepository $sites,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(
string $siteId,
?string $name = null,
?string $contactName = null,
?string $contactPhone = null,
?string $contactAddress = null,
?string $contactPostalCode = null,
?string $contactCity = null,
?string $color = null,
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$site = $this->sites->find($siteId);
if (!$site) {
$this->mcpError('not_found', "Site not found: {$siteId}");
}
if (null !== $name) {
$site->setName($name);
}
if (null !== $contactName) {
$site->setContactName($contactName);
}
if (null !== $contactPhone) {
$site->setContactPhone($contactPhone);
}
if (null !== $contactAddress) {
$site->setContactAddress($contactAddress);
}
if (null !== $contactPostalCode) {
$site->setContactPostalCode($contactPostalCode);
}
if (null !== $contactCity) {
$site->setContactCity($contactCity);
}
if (null !== $color) {
$site->setColor($color);
}
$this->em->flush();
return $this->jsonResponse(['id' => $site->getId(), 'name' => $site->getName()]);
}
}