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

@@ -25,6 +25,7 @@ use App\Entity\Profile;
use App\Entity\Site;
use App\Enum\ModelCategory;
use Doctrine\ORM\EntityManagerInterface;
use stdClass;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
abstract class AbstractApiTestCase extends ApiTestCase
@@ -85,6 +86,93 @@ abstract class AbstractApiTestCase extends ApiTestCase
return static::createClient();
}
// ── MCP helpers ──────────────────────────────────────────────────
/**
* @return array{client: Client, sessionId: string}
*/
protected function createMcpClient(string $role = 'ROLE_VIEWER'): array
{
$profile = $this->createProfile(roles: [$role], password: self::DEFAULT_PASSWORD);
return $this->initMcpSession($profile->getId(), self::DEFAULT_PASSWORD);
}
/**
* @return array{client: Client, sessionId: string}
*/
protected function initMcpSession(string $profileId, string $password): array
{
$client = static::createClient();
$response = $client->request('POST', '/_mcp', [
'headers' => [
'Content-Type' => 'application/json',
'X-Profile-Id' => $profileId,
'X-Profile-Password' => $password,
],
'body' => json_encode([
'jsonrpc' => '2.0',
'method' => 'initialize',
'params' => [
'protocolVersion' => '2025-03-26',
'capabilities' => new stdClass(),
'clientInfo' => ['name' => 'test', 'version' => '1.0'],
],
'id' => 1,
]),
]);
$sessionId = $response->getHeaders()['mcp-session-id'][0] ?? '';
$client->request('POST', '/_mcp', [
'headers' => [
'Content-Type' => 'application/json',
'X-Profile-Id' => $profileId,
'X-Profile-Password' => $password,
'Mcp-Session-Id' => $sessionId,
],
'body' => json_encode([
'jsonrpc' => '2.0',
'method' => 'notifications/initialized',
]),
]);
return ['client' => $client, 'sessionId' => $sessionId, 'profileId' => $profileId, 'password' => $password];
}
/**
* @return array<string, mixed>
*/
protected function callMcpTool(array $session, string $toolName, array $arguments = []): array
{
$response = $session['client']->request('POST', '/_mcp', [
'headers' => [
'Content-Type' => 'application/json',
'X-Profile-Id' => $session['profileId'],
'X-Profile-Password' => $session['password'],
'Mcp-Session-Id' => $session['sessionId'],
],
'body' => json_encode([
'jsonrpc' => '2.0',
'method' => 'tools/call',
'params' => [
'name' => $toolName,
'arguments' => empty($arguments) ? new stdClass() : $arguments,
],
'id' => random_int(10, 9999),
]),
]);
$data = $response->toArray(false);
if (isset($data['result']['content'][0]['text'])) {
$data['_parsed'] = json_decode($data['result']['content'][0]['text'], true);
}
return $data;
}
// ── Factory helpers ─────────────────────────────────────────────
protected function createProfile(