<?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;
use Mcp\Client\Handler\Request\RequestHandlerInterface;
use Mcp\Client\Transport\HttpTransport;
use Mcp\Schema\ClientCapabilities;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Enum\ElicitAction;
use Mcp\Schema\Enum\ProtocolVersion;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Schema\JsonRpc\Response;
use Mcp\Schema\Request\ElicitRequest;
use Mcp\Schema\Result\CallToolResult;
use Mcp\Schema\Result\ElicitResult;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
* Base for tests that drive one example server from both protocol eras.
*
* The example is started once, on one port, and every client in the test
* reaches it at that one URL — which is the property being tested. Nothing here
* mounts a second endpoint or restarts anything between eras.
*
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
abstract class DualEraExampleTestCase extends TestCase
{
protected const ANSWERS = [];
private ?Process $server = null;
private int $port;
abstract protected static function server(): string;
abstract protected static function portBase(): int;
protected function setUp(): void
{
if (\PHP_VERSION_ID < 80200) {
$this->markTestSkipped('php -S does not reliably fork multiple workers on PHP 8.1 (PHP_CLI_SERVER_WORKERS); see php/php-src#9400.');
}
$this->port = static::portBase() + (getmypid() % 200);
$this->server = new Process(
['php', '-S', \sprintf('127.0.0.1:%d', $this->port), static::server()],
env: ['PHP_CLI_SERVER_WORKERS' => '4'],
);
$this->server->start();
$deadline = microtime(true) + 5;
while (microtime(true) < $deadline) {
if (@fsockopen('127.0.0.1', $this->port, $errno, $error, 0.1)) {
return;
}
usleep(50_000);
}
$this->fail(\sprintf('The example server did not start: %s', $this->server->getErrorOutput()));
}
protected function tearDown(): void
{
$this->server?->stop();
}
* @return iterable<string, array{ProtocolVersion}>
*/
public static function provideEras(): iterable
{
yield 'the handshake era' => [ProtocolVersion::V2025_11_25];
yield 'the modern era' => [ProtocolVersion::V2026_07_28];
}
protected static function text(CallToolResult $result): string
{
$first = $result->content[0] ?? null;
self::assertInstanceOf(TextContent::class, $first);
return $first->text;
}
protected function connect(ProtocolVersion $era, bool $elicitation = false): Client
{
$builder = Client::builder()
->setClientInfo('dual-era-integration-client', '1.0.0')
->setProtocolVersion($era)
->setRequestTimeout(10);
if ($elicitation) {
$answers = static::ANSWERS;
$builder->setCapabilities(new ClientCapabilities(elicitation: true));
$builder->addRequestHandler(new class($answers) implements RequestHandlerInterface {
public function __construct(private readonly array $answers)
{
}
public function supports(Request $request): bool
{
return $request instanceof ElicitRequest;
}
public function handle(Request $request): Response
{
return new Response($request->getId(), new ElicitResult(ElicitAction::Accept, $this->answers));
}
});
}
$client = $builder->build();
$client->connect(new HttpTransport(\sprintf('http://127.0.0.1:%d/', $this->port)));
return $client;
}
}