[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
1 changed files with 2443 additions and 248 deletions
|
|
@ -17,6 +17,9 @@ use Mcp\Schema\Elicitation\ElicitationSchema;
|
|||
use Mcp\Schema\Elicitation\EnumSchemaDefinition;
|
||||
use Mcp\Schema\Elicitation\NumberSchemaDefinition;
|
||||
use Mcp\Schema\Elicitation\StringSchemaDefinition;
|
||||
use Mcp\Schema\Request\ElicitRequest;
|
||||
use Mcp\Schema\Result\ElicitResult;
|
||||
use Mcp\Schema\Result\InputRequiredResult;
|
||||
use Mcp\Server\RequestContext;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
|
@ -43,10 +46,10 @@ final class ElicitationHandlers
|
|||
* - String field with date format for reservation date
|
||||
* - Enum field for dietary restrictions with human-readable labels
|
||||
*
|
||||
* @return array{status: string, message: string, booking?: array{party_size: int, date: string, dietary: string}}
|
||||
* @return array{status: string, message: string, booking?: array{party_size: int, date: string, dietary: string}}|InputRequiredResult
|
||||
*/
|
||||
#[McpTool(name: 'book_restaurant', description: 'Book a restaurant reservation, collecting details via elicitation.')]
|
||||
public function bookRestaurant(RequestContext $context, string $restaurantName): array
|
||||
public function bookRestaurant(RequestContext $context, string $restaurantName): array|InputRequiredResult
|
||||
{
|
||||
if (!$context->getClientGateway()->supportsElicitation()) {
|
||||
return [
|
||||
|
|
@ -55,8 +58,6 @@ final class ElicitationHandlers
|
|||
];
|
||||
}
|
||||
|
||||
$client = $context->getClientGateway();
|
||||
|
||||
$this->logger->info(\sprintf('Starting reservation process for restaurant: %s', $restaurantName));
|
||||
|
||||
$schema = new ElicitationSchema(
|
||||
|
|
@ -85,12 +86,19 @@ final class ElicitationHandlers
|
|||
required: ['party_size', 'date'],
|
||||
);
|
||||
|
||||
$result = $client->elicit(
|
||||
message: \sprintf('Please provide your reservation details for %s:', $restaurantName),
|
||||
requestedSchema: $schema,
|
||||
timeout: 120,
|
||||
$result = $this->ask(
|
||||
$context,
|
||||
'details',
|
||||
\sprintf('Please provide your reservation details for %s:', $restaurantName),
|
||||
$schema,
|
||||
);
|
||||
|
||||
// Modern era, first round: the ask travels back as the result and the
|
||||
// client retries this whole call carrying the answer.
|
||||
if ($result instanceof InputRequiredResult) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ($result->isDeclined()) {
|
||||
$this->logger->info('User declined to provide reservation details.');
|
||||
|
||||
|
|
@ -154,10 +162,10 @@ final class ElicitationHandlers
|
|||
*
|
||||
* Demonstrates the simplest elicitation pattern - a yes/no confirmation.
|
||||
*
|
||||
* @return array{status: string, message: string}
|
||||
* @return array{status: string, message: string}|InputRequiredResult
|
||||
*/
|
||||
#[McpTool(name: 'confirm_action', description: 'Request user confirmation before proceeding with an action.')]
|
||||
public function confirmAction(RequestContext $context, string $actionDescription): array
|
||||
public function confirmAction(RequestContext $context, string $actionDescription): array|InputRequiredResult
|
||||
{
|
||||
if (!$context->getClientGateway()->supportsElicitation()) {
|
||||
return [
|
||||
|
|
@ -166,8 +174,6 @@ final class ElicitationHandlers
|
|||
];
|
||||
}
|
||||
|
||||
$client = $context->getClientGateway();
|
||||
|
||||
$schema = new ElicitationSchema(
|
||||
properties: [
|
||||
'confirm' => new BooleanSchemaDefinition(
|
||||
|
|
@ -179,11 +185,17 @@ final class ElicitationHandlers
|
|||
required: ['confirm'],
|
||||
);
|
||||
|
||||
$result = $client->elicit(
|
||||
message: \sprintf('Are you sure you want to: %s?', $actionDescription),
|
||||
requestedSchema: $schema,
|
||||
$result = $this->ask(
|
||||
$context,
|
||||
'confirmation',
|
||||
\sprintf('Are you sure you want to: %s?', $actionDescription),
|
||||
$schema,
|
||||
);
|
||||
|
||||
if ($result instanceof InputRequiredResult) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if (!$result->isAccepted()) {
|
||||
return [
|
||||
'status' => 'not_confirmed',
|
||||
|
|
@ -222,10 +234,10 @@ final class ElicitationHandlers
|
|||
*
|
||||
* Demonstrates elicitation with optional fields and enum with labels.
|
||||
*
|
||||
* @return array{status: string, message: string, feedback?: array{rating: string, comments: string}}
|
||||
* @return array{status: string, message: string, feedback?: array{rating: string, comments: string}}|InputRequiredResult
|
||||
*/
|
||||
#[McpTool(name: 'collect_feedback', description: 'Collect user feedback via elicitation form.')]
|
||||
public function collectFeedback(RequestContext $context, string $topic): array
|
||||
public function collectFeedback(RequestContext $context, string $topic): array|InputRequiredResult
|
||||
{
|
||||
if (!$context->getClientGateway()->supportsElicitation()) {
|
||||
return [
|
||||
|
|
@ -234,8 +246,6 @@ final class ElicitationHandlers
|
|||
];
|
||||
}
|
||||
|
||||
$client = $context->getClientGateway();
|
||||
|
||||
$schema = new ElicitationSchema(
|
||||
properties: [
|
||||
'rating' => new EnumSchemaDefinition(
|
||||
|
|
@ -253,11 +263,17 @@ final class ElicitationHandlers
|
|||
required: ['rating'],
|
||||
);
|
||||
|
||||
$result = $client->elicit(
|
||||
message: \sprintf('Please provide your feedback about: %s', $topic),
|
||||
requestedSchema: $schema,
|
||||
$result = $this->ask(
|
||||
$context,
|
||||
'feedback',
|
||||
\sprintf('Please provide your feedback about: %s', $topic),
|
||||
$schema,
|
||||
);
|
||||
|
||||
if ($result instanceof InputRequiredResult) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if (!$result->isAccepted()) {
|
||||
return [
|
||||
'status' => 'skipped',
|
||||
|
|
@ -288,4 +304,26 @@ final class ElicitationHandlers
|
|||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask the user one question.
|
||||
*
|
||||
* Written the way revision 2026-07-28 asks: the question is *returned*, the
|
||||
* client answers it and retries the whole call, and the answer comes back
|
||||
* through the input context under the same key. Nothing here names an era —
|
||||
* on a handshake-era connection the SDK fulfils the same ask over that
|
||||
* connection's own channel and re-enters the tool with the answer.
|
||||
*
|
||||
* The caller gets an {@see ElicitResult} once there is one, or an
|
||||
* {@see InputRequiredResult} to hand straight back to its own caller.
|
||||
*/
|
||||
private function ask(
|
||||
RequestContext $context,
|
||||
string $key,
|
||||
string $message,
|
||||
ElicitationSchema $schema,
|
||||
): ElicitResult|InputRequiredResult {
|
||||
return $context->getInputContext()?->elicitResult($key)
|
||||
?? new InputRequiredResult([$key => new ElicitRequest($message, $schema)]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue