* [Client] Speak the 2026-07-28 lifecycle On that revision the client opens with server/discover instead of initialize, stamps each request's _meta with the protocol version, its own capabilities and client info, and sends the standard Mcp-Method / Mcp-Name / Mcp-Param-* headers an intermediary routes on. ToolCatalog is what knows, from the tool list, which arguments a call has to mirror. An input_required result is answered rather than surfaced: InputRequestResolver asks the host's elicitation, sampling and roots handlers and retries the same request carrying inputResponses and the requestState the server sent. McpHeader holds the header names and the =?base64?…?= sentinel, so the two sides no longer keep their own copies of the same rules. * Fix CI baseline drift and address Copilot review findings Drop stale expected-failure entries the new client fixture now passes. Copilot review: fix McpHeader round-trip for a wrapper-shaped literal, encode empty/numeric-keyed inputResponses as a JSON object, stop the incident example repeating side effects on MRTR retry, and reset ToolCatalog on reconnect instead of carrying the previous server's verdicts. * Skip DualEraExampleTestCase on PHP 8.1 PHP_CLI_SERVER_WORKERS does not reliably fork multiple php -S workers there (php/php-src#9400), reproducing the single-worker deadlock the extra workers exist to avoid.
144 lines
4.9 KiB
PHP
144 lines
4.9 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;
|
|
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
|
|
{
|
|
/** Answers whatever the server elicits, so a tool that asks can complete. */
|
|
protected const ANSWERS = [];
|
|
|
|
private ?Process $server = null;
|
|
private int $port;
|
|
|
|
/** Absolute path to the example's server script. */
|
|
abstract protected static function server(): string;
|
|
|
|
/** Port range base, so concurrent test classes do not collide. */
|
|
abstract protected static function portBase(): int;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
// PHP_CLI_SERVER_WORKERS does not reliably fork more than one worker on
|
|
// PHP 8.1 (see php/php-src#9400), which reproduces the very deadlock the
|
|
// extra workers exist to avoid: every request below hangs to the test
|
|
// timeout instead of running. Skip until either PHP 8.1 is dropped or
|
|
// this suite stops depending on `php -S` for multi-worker concurrency.
|
|
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);
|
|
|
|
// More than one worker because the handshake era needs it: a tool that
|
|
// asks the client mid-call holds its SSE response open while the client
|
|
// POSTs the answer on a second connection. One worker deadlocks there —
|
|
// a property of `php -S`, not of the server.
|
|
$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 {
|
|
/** @param array<string, mixed> $answers */
|
|
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;
|
|
}
|
|
}
|