php-sdk/tests/Integration/HandshakeTest.php
Christopher Hertel 8c06dcecc6
[Client] Speak the 2026-07-28 lifecycle (#456)
* [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.
2026-08-19 02:41:02 +02:00

90 lines
3.8 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\Schema\Enum\ProtocolVersion;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\TestDox;
/**
* What the two sides settle on before anything else can happen.
*
* @see Fixture/handshake.php for the server under test
*/
final class HandshakeTest extends IntegrationTestCase
{
#[TestDox('client and server agree on a revision')]
#[DataProvider('provideNegotiations')]
public function testNegotiatedVersion(?ProtocolVersion $clientVersion, ?ProtocolVersion $serverVersion, ProtocolVersion $expected): void
{
$client = $this->clientBuilder();
if (null !== $clientVersion) {
$client->setProtocolVersion($clientVersion);
}
$connected = $this->connect(
'handshake',
$client,
null !== $serverVersion ? ['MCP_INTEGRATION_PROTOCOL_VERSION' => $serverVersion->value] : [],
);
$this->assertSame($expected, $connected->getProtocolVersion());
}
/**
* @return iterable<string, array{?ProtocolVersion, ?ProtocolVersion, ProtocolVersion}>
*/
public static function provideNegotiations(): iterable
{
$latest = ProtocolVersion::latestHandshake();
yield 'both unconfigured' => [null, null, $latest];
// Whichever end of the supported range it sits at.
foreach (ProtocolVersion::handshakeVersions() as $version) {
yield \sprintf('client asks for %s', $version->value) => [$version, null, $version];
}
// A pinned server answers with its pin, and the client continues on it.
yield 'server pins an older revision' => [ProtocolVersion::V2025_11_25, ProtocolVersion::V2025_03_26, ProtocolVersion::V2025_03_26];
yield 'server pins a newer revision' => [ProtocolVersion::V2024_11_05, ProtocolVersion::V2025_11_25, ProtocolVersion::V2025_11_25];
yield 'both pin the same revision' => [ProtocolVersion::V2025_06_18, ProtocolVersion::V2025_06_18, ProtocolVersion::V2025_06_18];
// A modern client does not negotiate at all: it skips the handshake and
// states its revision on every request, so what it was configured with
// is what it reports. This server never answers `server/discover`, so
// there is nothing to reconcile against either.
yield 'client configured modern' => [ProtocolVersion::V2026_07_28, null, ProtocolVersion::V2026_07_28];
yield 'both configured modern' => [ProtocolVersion::V2026_07_28, ProtocolVersion::V2026_07_28, ProtocolVersion::V2026_07_28];
// The server end still falls back: a handshake-era client offered a
// revision, and `initialize` cannot answer with a modern one.
yield 'server configured modern' => [ProtocolVersion::V2025_06_18, ProtocolVersion::V2026_07_28, ProtocolVersion::V2025_06_18];
}
#[TestDox('the handshake carries the server identity to the client')]
public function testServerInfoIsExchanged(): void
{
$client = $this->connect('handshake');
$this->assertSame('integration-server', $client->getServerInfo()->name);
$this->assertSame('1.0.0', $client->getServerInfo()->version);
$this->assertSame('Be brief.', $client->getInstructions());
$this->assertTrue($client->isConnected());
}
#[TestDox('the negotiated revision is unset before the handshake')]
public function testProtocolVersionIsNullBeforeConnecting(): void
{
$this->assertNull($this->clientBuilder()->build()->getProtocolVersion());
}
}