php-sdk/tests/Unit/Client/Stateless/HeaderFactoryTest.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

124 lines
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\Unit\Client\Stateless;
use Mcp\Client\Stateless\HeaderFactory;
use Mcp\Client\Stateless\ToolCatalog;
use Mcp\Schema\Enum\ProtocolVersion;
use Mcp\Server\Stateless\StandardHeaderValidator;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* What the client puts on the wire, checked against the server that reads it.
*
* Every case asserts the emitted headers and then feeds them straight to
* {@see StandardHeaderValidator}: agreeing with a hand-written expectation is
* not the point, agreeing with the other side is.
*/
final class HeaderFactoryTest extends TestCase
{
#[TestDox('declares the revision and the method on every request')]
public function testStandardHeaders(): void
{
$headers = $this->headersFor(['method' => 'tools/list', 'params' => []]);
$this->assertSame('2026-07-28', $headers['MCP-Protocol-Version']);
$this->assertSame('tools/list', $headers['Mcp-Method']);
$this->assertArrayNotHasKey('Mcp-Name', $headers);
}
#[TestDox('names the subject of a request that addresses one')]
public function testNameHeader(): void
{
$payload = ['method' => 'tools/call', 'params' => ['name' => 'search', 'arguments' => []]];
$this->assertSame('search', $this->headersFor($payload)['Mcp-Name']);
$this->assertNull($this->validate($payload));
}
#[TestDox('wraps a subject that is not header-safe, and the server unwraps it')]
public function testUnsafeNameRoundTrips(): void
{
$payload = ['method' => 'resources/read', 'params' => ['uri' => 'file:///café.txt']];
$this->assertSame('=?base64?ZmlsZTovLy9jYWbDqS50eHQ=?=', $this->headersFor($payload)['Mcp-Name']);
$this->assertNull($this->validate($payload));
}
#[TestDox('mirrors annotated tool arguments the server can verify')]
public function testMirroredArguments(): void
{
$payload = [
'method' => 'tools/call',
'params' => [
'name' => 'search',
'arguments' => ['region' => ' padded ', 'priority' => 7],
],
];
$headers = $this->headersFor($payload);
$this->assertSame('=?base64?IHBhZGRlZCA=?=', $headers['Mcp-Param-Region']);
$this->assertSame('7', $headers['Mcp-Param-Priority']);
$this->assertNull($this->validate($payload));
}
#[TestDox('a response carries the revision but nothing to mirror')]
public function testResponseCarriesOnlyTheVersion(): void
{
$headers = $this->headersFor(['id' => 1, 'result' => []]);
$this->assertSame(['MCP-Protocol-Version' => '2026-07-28'], $headers);
}
/**
* @param array<string, mixed> $payload
*
* @return array<string, string>
*/
private function headersFor(array $payload): array
{
return (new HeaderFactory($this->catalog()))->forMessage($payload, ProtocolVersion::V2026_07_28);
}
/**
* @param array<string, mixed> $payload
*
* @return string|null the server's reason to reject, or null when it agrees
*/
private function validate(array $payload): ?string
{
return (new StandardHeaderValidator())->validate(
$payload['method'],
$payload['params'] ?? null,
$this->headersFor($payload),
);
}
private function catalog(): ToolCatalog
{
$catalog = new ToolCatalog();
$catalog->record([[
'name' => 'search',
'inputSchema' => [
'type' => 'object',
'properties' => [
'region' => ['type' => 'string', 'x-mcp-header' => 'Region'],
'priority' => ['type' => 'integer', 'x-mcp-header' => 'Priority'],
],
],
]]);
return $catalog;
}
}