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,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()]);
}
}