Files
Inventory/tests/Mcp/Tool/DashboardStatsToolTest.php
Matthieu e335f4c24c feat(mcp) : add stdio auth, dashboard stats PoC tool, and helper trait
- McpStdioAuthSubscriber for console transport auth via env vars
- DashboardStatsTool as PoC (validates MCP protocol flow)
- McpToolHelper trait for shared pagination/error utilities
- Key learning: #[McpTool] must be on CLASS, not method for __invoke

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 14:18:09 +01:00

116 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Mcp\Tool;
use App\Tests\AbstractApiTestCase;
use stdClass;
/**
* @internal
*/
class DashboardStatsToolTest extends AbstractApiTestCase
{
public function testGetDashboardStatsReturnsCounters(): void
{
$site = $this->createSite();
$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');
$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->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];
}
}