* [Tests] Introduce client-server integration tests * Add an in-process loopback that connects a real client transport to a real server transport, draining both Fiber loops in one synchronous pass instead of polling. A stalled exchange fails where it happened rather than hanging. * Cover protocol version negotiation across both implementations, where the unit tests only ever drive one side against a canned counter-offer. * Cover the server-to-client round-trips end to end: elicitation, sampling, roots, plus progress and logging notifications. * Add the `integration` test suite and a `make integration-tests` target. * [Tests] Run the integration suite in CI The unit job pins `--testsuite=unit`, so the new suite would never have run on GitHub. It goes in that job rather than its own: the loopback leans on Fibers, and the matrix is what covers them down to the PHP 8.1 floor. * [Tests] Run integration tests against a real server process Replaces the in-process loopback with the setup from examples/client: the client spawns a fixture server over the real StdioTransport, so the tests exercise the shipped transports and no longer nest both sides' Fibers. Moves the suite into its own CI job, matrixed over PHP 8.1-8.5.
98 lines
3.4 KiB
PHP
98 lines
3.4 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the official PHP MCP SDK.
|
|
*
|
|
* A collaboration between Symfony and the PHP Foundation.
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Mcp\Tests\Integration;
|
|
|
|
use Mcp\Client\Builder as ClientBuilder;
|
|
use Mcp\Client\Handler\Request\ListRootsRequestHandler;
|
|
use Mcp\Client\Handler\Request\RootsCallbackInterface;
|
|
use Mcp\Schema\ClientCapabilities;
|
|
use Mcp\Schema\Content\TextContent;
|
|
use Mcp\Schema\Request\ListRootsRequest;
|
|
use Mcp\Schema\Result\ListRootsResult;
|
|
use Mcp\Schema\Root;
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
/**
|
|
* Roots: the server asking the client which workspace folders it may touch.
|
|
*
|
|
* @see Fixture/roots.php for the server under test
|
|
*/
|
|
final class RootsTest extends IntegrationTestCase
|
|
{
|
|
#[TestDox('the roots the client exposes reach the tool that asked')]
|
|
public function testRootsReachTheTool(): void
|
|
{
|
|
$client = $this->connect('roots', $this->clientExposing(
|
|
new Root('file:///workspace/app', 'App'),
|
|
new Root('file:///workspace/docs', 'Docs'),
|
|
));
|
|
|
|
$result = $client->callTool('inspect_roots');
|
|
|
|
$this->assertInstanceOf(TextContent::class, $result->content[0]);
|
|
$this->assertSame('file:///workspace/app (App), file:///workspace/docs (Docs)', $result->content[0]->text);
|
|
}
|
|
|
|
#[TestDox('an empty root list is a valid answer, not a failure')]
|
|
public function testEmptyRootList(): void
|
|
{
|
|
$client = $this->connect('roots', $this->clientExposing());
|
|
|
|
$result = $client->callTool('inspect_roots');
|
|
|
|
$this->assertInstanceOf(TextContent::class, $result->content[0]);
|
|
$this->assertSame('', $result->content[0]->text);
|
|
}
|
|
|
|
#[TestDox('a client that does not advertise roots is not asked')]
|
|
public function testCapabilityIsVisibleToTheServer(): void
|
|
{
|
|
$client = $this->connect('roots');
|
|
|
|
$result = $client->callTool('inspect_roots');
|
|
|
|
$this->assertInstanceOf(TextContent::class, $result->content[0]);
|
|
$this->assertSame('unsupported', $result->content[0]->text);
|
|
}
|
|
|
|
#[TestDox('the client can announce that its roots changed')]
|
|
public function testRootsListChangedNotification(): void
|
|
{
|
|
$client = $this->connect('roots', $this->clientExposing(new Root('file:///workspace')));
|
|
|
|
// A notification has no reply, so what this pins down is that sending
|
|
// one mid-session leaves the connection usable.
|
|
$client->sendRootsListChanged();
|
|
|
|
$this->assertTrue($client->isConnected());
|
|
$this->assertInstanceOf(TextContent::class, $client->callTool('inspect_roots')->content[0]);
|
|
}
|
|
|
|
private function clientExposing(Root ...$roots): ClientBuilder
|
|
{
|
|
$callback = new class(array_values($roots)) implements RootsCallbackInterface {
|
|
/** @param list<Root> $roots */
|
|
public function __construct(private readonly array $roots)
|
|
{
|
|
}
|
|
|
|
public function __invoke(ListRootsRequest $request): ListRootsResult
|
|
{
|
|
return new ListRootsResult($this->roots);
|
|
}
|
|
};
|
|
|
|
return $this->clientBuilder()
|
|
->setCapabilities(new ClientCapabilities(roots: true, rootsListChanged: true))
|
|
->addRequestHandler(new ListRootsRequestHandler($callback));
|
|
}
|
|
}
|