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:
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||
namespace App\Tests\Mcp\Tool;
|
||||
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@@ -18,98 +17,13 @@ class DashboardStatsToolTest extends AbstractApiTestCase
|
||||
$this->createMachine(name: 'Machine Stats 1', site: $site);
|
||||
$this->createMachine(name: 'Machine Stats 2', site: $site);
|
||||
|
||||
$profile = $this->createProfile(roles: ['ROLE_VIEWER'], password: 'test123');
|
||||
$session = $this->initMcpSession($profile->getId(), 'test123');
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
$data = $this->callMcpTool($session, 'get_dashboard_stats');
|
||||
|
||||
$response = $session['client']->request('POST', '/_mcp', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Profile-Id' => $profile->getId(),
|
||||
'X-Profile-Password' => 'test123',
|
||||
'Mcp-Session-Id' => $session['sessionId'],
|
||||
],
|
||||
'body' => json_encode([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'tools/call',
|
||||
'params' => [
|
||||
'name' => 'get_dashboard_stats',
|
||||
'arguments' => new stdClass(),
|
||||
],
|
||||
'id' => 2,
|
||||
]),
|
||||
]);
|
||||
|
||||
// First list tools to see what's registered
|
||||
$listResponse = $session['client']->request('POST', '/_mcp', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Profile-Id' => $profile->getId(),
|
||||
'X-Profile-Password' => 'test123',
|
||||
'Mcp-Session-Id' => $session['sessionId'],
|
||||
],
|
||||
'body' => json_encode([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'tools/list',
|
||||
'params' => new stdClass(),
|
||||
'id' => 3,
|
||||
]),
|
||||
]);
|
||||
$toolsList = $listResponse->toArray(false);
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$data = $response->toArray(false);
|
||||
$this->assertArrayHasKey('result', $data, 'Tools list: '.json_encode($toolsList).' | Call response: '.json_encode($data));
|
||||
|
||||
// Parse the text content from the MCP response
|
||||
$content = $data['result']['content'][0]['text'] ?? '';
|
||||
$stats = json_decode($content, true);
|
||||
$this->assertIsArray($stats);
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$stats = $data['_parsed'];
|
||||
$this->assertGreaterThanOrEqual(2, $stats['machines']);
|
||||
$this->assertArrayHasKey('sites', $stats);
|
||||
$this->assertArrayHasKey('unresolvedComments', $stats);
|
||||
}
|
||||
|
||||
private function initMcpSession(string $profileId, string $password): array
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Step 1: Initialize MCP session
|
||||
$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,
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$sessionId = $response->getHeaders()['mcp-session-id'][0] ?? '';
|
||||
$this->assertNotEmpty($sessionId, 'MCP session ID should be returned');
|
||||
|
||||
// Step 2: Send initialized notification
|
||||
$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];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user