[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.
This commit is contained in:
parent
cbceef06eb
commit
8c06dcecc6
3 changed files with 2443 additions and 248 deletions
124
tests/Unit/Client/Stateless/HeaderFactoryTest.php
Normal file
124
tests/Unit/Client/Stateless/HeaderFactoryTest.php
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
||||
119
tests/Unit/Client/Stateless/InputRequestResolverTest.php
Normal file
119
tests/Unit/Client/Stateless/InputRequestResolverTest.php
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<?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\Handler\Request\RequestHandlerInterface;
|
||||
use Mcp\Client\Stateless\InputRequestResolver;
|
||||
use Mcp\Exception\RuntimeException;
|
||||
use Mcp\Schema\Enum\ElicitAction;
|
||||
use Mcp\Schema\JsonRpc\Request;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\ElicitRequest;
|
||||
use Mcp\Schema\Result\ElicitResult;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class InputRequestResolverTest extends TestCase
|
||||
{
|
||||
#[TestDox('a result with no resultType is complete, not an ask')]
|
||||
public function testDefaultResultTypeIsComplete(): void
|
||||
{
|
||||
$this->assertNull(InputRequestResolver::asked(['content' => []]));
|
||||
$this->assertNull(InputRequestResolver::asked(['resultType' => 'complete']));
|
||||
}
|
||||
|
||||
#[TestDox('an input_required result is an ask, even with an empty map')]
|
||||
public function testInputRequiredIsAnAsk(): void
|
||||
{
|
||||
$this->assertSame([], InputRequestResolver::asked(['resultType' => 'input_required']));
|
||||
$this->assertSame(
|
||||
['confirm' => ['method' => 'elicitation/create']],
|
||||
InputRequestResolver::asked([
|
||||
'resultType' => 'input_required',
|
||||
'inputRequests' => ['confirm' => ['method' => 'elicitation/create']],
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
#[TestDox('answers each ask under the key the server asked it under')]
|
||||
public function testAnswersAreKeyedByTheServersKey(): void
|
||||
{
|
||||
$responses = $this->resolver()->resolve([
|
||||
'confirm' => self::elicitation('Confirm?'),
|
||||
'name' => self::elicitation('Your name?'),
|
||||
]);
|
||||
|
||||
$this->assertSame(['confirm', 'name'], array_keys($responses));
|
||||
$this->assertSame('accept', $responses['confirm']['action']);
|
||||
}
|
||||
|
||||
#[TestDox('refuses an ask the client has no handler for')]
|
||||
public function testUnhandledAskIsRefused(): void
|
||||
{
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('Client does not handle "elicitation/create" requests.');
|
||||
|
||||
(new InputRequestResolver([]))->resolve(['confirm' => self::elicitation('Confirm?')]);
|
||||
}
|
||||
|
||||
#[TestDox('refuses an ask that is not a request a client can answer')]
|
||||
public function testUnanswerableMethodIsRefused(): void
|
||||
{
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('which is not a request a client can answer');
|
||||
|
||||
$this->resolver()->resolve(['x' => ['method' => 'tools/call', 'params' => []]]);
|
||||
}
|
||||
|
||||
#[TestDox('refuses an ask with no method to answer')]
|
||||
public function testMethodlessAskIsRefused(): void
|
||||
{
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('without a method to answer');
|
||||
|
||||
$this->resolver()->resolve(['x' => ['params' => []]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function elicitation(string $message): array
|
||||
{
|
||||
return [
|
||||
'method' => 'elicitation/create',
|
||||
'params' => [
|
||||
'message' => $message,
|
||||
'requestedSchema' => [
|
||||
'type' => 'object',
|
||||
'properties' => ['confirmed' => ['type' => 'boolean']],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function resolver(): InputRequestResolver
|
||||
{
|
||||
return new InputRequestResolver([
|
||||
new class implements RequestHandlerInterface {
|
||||
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, []));
|
||||
}
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
||||
124
tests/Unit/Client/Stateless/ToolCatalogTest.php
Normal file
124
tests/Unit/Client/Stateless/ToolCatalogTest.php
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
<?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\ToolCatalog;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ToolCatalogTest extends TestCase
|
||||
{
|
||||
#[TestDox('mirrors an annotated argument into its header')]
|
||||
public function testMirrorsAnnotatedArguments(): void
|
||||
{
|
||||
$catalog = new ToolCatalog();
|
||||
$catalog->record([self::annotatedTool()]);
|
||||
|
||||
$this->assertSame(
|
||||
['Region' => 'us-west1', 'Priority' => '7', 'Verbose' => 'false'],
|
||||
$catalog->headersFor('search', ['region' => 'us-west1', 'priority' => 7, 'verbose' => false]),
|
||||
);
|
||||
}
|
||||
|
||||
#[TestDox('an argument that is absent or null contributes no header')]
|
||||
public function testOmittedArgumentsAreNotMirrored(): void
|
||||
{
|
||||
$catalog = new ToolCatalog();
|
||||
$catalog->record([self::annotatedTool()]);
|
||||
|
||||
// An omitted header is how "no value" is said; an empty one would
|
||||
// assert that the argument was present and empty.
|
||||
$this->assertSame(
|
||||
['Region' => 'us-west1'],
|
||||
$catalog->headersFor('search', ['region' => 'us-west1', 'verbose' => null]),
|
||||
);
|
||||
}
|
||||
|
||||
#[TestDox('an unannotated argument is never mirrored')]
|
||||
public function testUnannotatedArgumentsAreNotMirrored(): void
|
||||
{
|
||||
$catalog = new ToolCatalog();
|
||||
$catalog->record([self::annotatedTool()]);
|
||||
|
||||
$this->assertSame([], $catalog->headersFor('search', ['query' => 'SELECT 1']));
|
||||
}
|
||||
|
||||
#[TestDox('a tool the client never listed is not second-guessed')]
|
||||
public function testUnknownToolIsNeitherMirroredNorRejected(): void
|
||||
{
|
||||
$catalog = new ToolCatalog();
|
||||
|
||||
$this->assertSame([], $catalog->headersFor('unlisted', ['region' => 'x']));
|
||||
$this->assertFalse($catalog->isRejected('unlisted'));
|
||||
}
|
||||
|
||||
#[TestDox('a malformed annotation drops that tool and leaves the rest usable')]
|
||||
public function testMalformedToolIsDroppedAlone(): void
|
||||
{
|
||||
$catalog = new ToolCatalog();
|
||||
|
||||
$usable = $catalog->record([
|
||||
self::annotatedTool(),
|
||||
[
|
||||
'name' => 'broken',
|
||||
'inputSchema' => [
|
||||
'type' => 'object',
|
||||
'properties' => ['data' => ['type' => 'object', 'x-mcp-header' => 'Data']],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(['search'], array_column($usable, 'name'));
|
||||
$this->assertTrue($catalog->isRejected('broken'));
|
||||
$this->assertFalse($catalog->isRejected('search'));
|
||||
$this->assertStringContainsString('only string, integer and boolean', (string) $catalog->reasonFor('broken'));
|
||||
}
|
||||
|
||||
#[TestDox('a later listing replaces what was known about a tool')]
|
||||
public function testRelistingReplacesTheVerdict(): void
|
||||
{
|
||||
$catalog = new ToolCatalog();
|
||||
$catalog->record([[
|
||||
'name' => 'search',
|
||||
'inputSchema' => [
|
||||
'type' => 'object',
|
||||
'properties' => ['region' => ['type' => 'object', 'x-mcp-header' => 'Region']],
|
||||
],
|
||||
]]);
|
||||
|
||||
$this->assertTrue($catalog->isRejected('search'));
|
||||
|
||||
$catalog->record([self::annotatedTool()]);
|
||||
|
||||
$this->assertFalse($catalog->isRejected('search'));
|
||||
$this->assertSame(['Region' => 'eu'], $catalog->headersFor('search', ['region' => 'eu']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function annotatedTool(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'search',
|
||||
'inputSchema' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'region' => ['type' => 'string', 'x-mcp-header' => 'Region'],
|
||||
'priority' => ['type' => 'integer', 'x-mcp-header' => 'Priority'],
|
||||
'verbose' => ['type' => 'boolean', 'x-mcp-header' => 'Verbose'],
|
||||
'query' => ['type' => 'string'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue