feat(mcp) : add business tools — search, history, comments, custom fields, documents, model types
- search_inventory: global search across all 6 entity types - get_entity_history + get_activity_log: audit trail access - 4 comment tools: list, create, resolve, unresolved count - 3 custom field tools: list values, upsert, delete - 2 document tools: list, delete (upload via REST only) - 6 model type tools: list, get, create, update, delete, sync - 69 MCP tests pass total Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
113
src/Mcp/Tool/SearchInventoryTool.php
Normal file
113
src/Mcp/Tool/SearchInventoryTool.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool;
|
||||
|
||||
use App\Repository\ComposantRepository;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
use App\Repository\MachineRepository;
|
||||
use App\Repository\PieceRepository;
|
||||
use App\Repository\ProductRepository;
|
||||
use App\Repository\SiteRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'search_inventory',
|
||||
description: 'Global search across all inventory entities (machines, pieces, composants, products, sites, constructeurs). Searches by name and reference (when available). Returns a flat list of matching results.',
|
||||
)]
|
||||
class SearchInventoryTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
private const ALLOWED_TYPES = ['machine', 'piece', 'composant', 'product', 'site', 'constructeur'];
|
||||
|
||||
public function __construct(
|
||||
private readonly MachineRepository $machines,
|
||||
private readonly PieceRepository $pieces,
|
||||
private readonly ComposantRepository $composants,
|
||||
private readonly ProductRepository $products,
|
||||
private readonly SiteRepository $sites,
|
||||
private readonly ConstructeurRepository $constructeurs,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $query, string $types = '', int $limit = 20): array
|
||||
{
|
||||
$query = trim($query);
|
||||
if ('' === $query) {
|
||||
return $this->jsonResponse([]);
|
||||
}
|
||||
|
||||
$limit = min(100, max(1, $limit));
|
||||
$searchTypes = $this->resolveTypes($types);
|
||||
$results = [];
|
||||
|
||||
foreach ($searchTypes as $type) {
|
||||
$results = array_merge($results, match ($type) {
|
||||
'machine' => $this->searchWithReference($this->machines, 'm', 'machine', $query),
|
||||
'piece' => $this->searchWithReference($this->pieces, 'p', 'piece', $query),
|
||||
'composant' => $this->searchWithReference($this->composants, 'c', 'composant', $query),
|
||||
'product' => $this->searchWithReference($this->products, 'p', 'product', $query),
|
||||
'site' => $this->searchNameOnly($this->sites, 's', 'site', $query),
|
||||
'constructeur' => $this->searchNameOnly($this->constructeurs, 'c', 'constructeur', $query),
|
||||
});
|
||||
}
|
||||
|
||||
$results = array_slice($results, 0, $limit);
|
||||
|
||||
return $this->jsonResponse($results);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function resolveTypes(string $types): array
|
||||
{
|
||||
if ('' === trim($types)) {
|
||||
return self::ALLOWED_TYPES;
|
||||
}
|
||||
|
||||
$requested = array_map('trim', explode(',', strtolower($types)));
|
||||
|
||||
return array_values(array_intersect($requested, self::ALLOWED_TYPES));
|
||||
}
|
||||
|
||||
private function searchWithReference(object $repository, string $alias, string $type, string $search): array
|
||||
{
|
||||
$qb = $repository->createQueryBuilder($alias)
|
||||
->select("{$alias}.id", "{$alias}.name", "{$alias}.reference")
|
||||
->where("LOWER({$alias}.name) LIKE LOWER(:search)")
|
||||
->orWhere("LOWER({$alias}.reference) LIKE LOWER(:search)")
|
||||
->setParameter('search', "%{$search}%")
|
||||
->orderBy("{$alias}.name", 'ASC')
|
||||
;
|
||||
|
||||
$rows = $qb->getQuery()->getArrayResult();
|
||||
|
||||
return array_map(fn (array $row) => [
|
||||
'type' => $type,
|
||||
'id' => $row['id'],
|
||||
'name' => $row['name'],
|
||||
'reference' => $row['reference'] ?? null,
|
||||
], $rows);
|
||||
}
|
||||
|
||||
private function searchNameOnly(object $repository, string $alias, string $type, string $search): array
|
||||
{
|
||||
$qb = $repository->createQueryBuilder($alias)
|
||||
->select("{$alias}.id", "{$alias}.name")
|
||||
->where("LOWER({$alias}.name) LIKE LOWER(:search)")
|
||||
->setParameter('search', "%{$search}%")
|
||||
->orderBy("{$alias}.name", 'ASC')
|
||||
;
|
||||
|
||||
$rows = $qb->getQuery()->getArrayResult();
|
||||
|
||||
return array_map(fn (array $row) => [
|
||||
'type' => $type,
|
||||
'id' => $row['id'],
|
||||
'name' => $row['name'],
|
||||
'reference' => null,
|
||||
], $rows);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user