[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
|
|
@ -12,8 +12,13 @@
|
|||
namespace Mcp\Example\Server\ClientCommunication;
|
||||
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Mcp\Schema\Content\SamplingMessage;
|
||||
use Mcp\Schema\Content\TextContent;
|
||||
use Mcp\Schema\Enum\LoggingLevel;
|
||||
use Mcp\Schema\Enum\Role;
|
||||
use Mcp\Schema\Request\CreateSamplingMessageRequest;
|
||||
use Mcp\Schema\Request\ListRootsRequest;
|
||||
use Mcp\Schema\Result\InputRequiredResult;
|
||||
use Mcp\Server\RequestContext;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
|
@ -31,10 +36,14 @@ final class ClientAwareService
|
|||
* Demonstrates the server side of the "roots" client capability: the tool
|
||||
* issues a roots/list request that the client answers from its own handler.
|
||||
*
|
||||
* @return array{status: string, message: string, roots?: list<array{uri: string, name: string|null}>}
|
||||
* Written the 2026-07-28 way: the ask is returned and the answer read off
|
||||
* the retry. A handshake-era client reaches the same tool — the SDK sends
|
||||
* the `roots/list` request over that connection and re-enters this method.
|
||||
*
|
||||
* @return array{status: string, message: string, roots?: list<array{uri: string, name: string|null}>}|InputRequiredResult
|
||||
*/
|
||||
#[McpTool(name: 'inspect_workspace_roots', description: 'Ask the client for its workspace roots via a roots/list request.')]
|
||||
public function inspectWorkspaceRoots(RequestContext $context): array
|
||||
public function inspectWorkspaceRoots(RequestContext $context): array|InputRequiredResult
|
||||
{
|
||||
$clientGateway = $context->getClientGateway();
|
||||
|
||||
|
|
@ -45,7 +54,11 @@ final class ClientAwareService
|
|||
];
|
||||
}
|
||||
|
||||
$result = $clientGateway->listRoots();
|
||||
$result = $context->getInputContext()?->rootsResult('roots');
|
||||
|
||||
if (null === $result) {
|
||||
return new InputRequiredResult(['roots' => new ListRootsRequest()]);
|
||||
}
|
||||
|
||||
$roots = [];
|
||||
foreach ($result->roots as $root) {
|
||||
|
|
@ -62,36 +75,48 @@ final class ClientAwareService
|
|||
}
|
||||
|
||||
/**
|
||||
* @return array{incident: string, recommended_actions: string, model: string}
|
||||
* @return array{incident: string, recommended_actions: string, model: string}|InputRequiredResult
|
||||
*/
|
||||
#[McpTool(name: 'coordinate_incident_response', description: 'Coordinate an incident response with logging, progress, and sampling.')]
|
||||
public function coordinateIncident(RequestContext $context, string $incidentTitle): array
|
||||
public function coordinateIncident(RequestContext $context, string $incidentTitle): array|InputRequiredResult
|
||||
{
|
||||
$clientGateway = $context->getClientGateway();
|
||||
$clientGateway->log(LoggingLevel::Warning, \sprintf('Incident triage started: %s', $incidentTitle));
|
||||
|
||||
$steps = [
|
||||
'Collecting telemetry',
|
||||
'Assessing scope',
|
||||
'Coordinating responders',
|
||||
];
|
||||
// A retry re-enters this method from the top, so the triage work below
|
||||
// must run only once: check for the answer first, before repeating logs,
|
||||
// progress notifications and simulated work the client already saw.
|
||||
$result = $context->getInputContext()?->samplingResult('recommendation');
|
||||
|
||||
foreach ($steps as $index => $step) {
|
||||
$progress = ($index + 1) / \count($steps);
|
||||
if (null === $result) {
|
||||
$clientGateway->log(LoggingLevel::Warning, \sprintf('Incident triage started: %s', $incidentTitle));
|
||||
|
||||
$clientGateway->progress($progress, 1, $step);
|
||||
$steps = [
|
||||
'Collecting telemetry',
|
||||
'Assessing scope',
|
||||
'Coordinating responders',
|
||||
];
|
||||
|
||||
usleep(180_000); // Simulate work being done
|
||||
foreach ($steps as $index => $step) {
|
||||
$progress = ($index + 1) / \count($steps);
|
||||
|
||||
$clientGateway->progress($progress, 1, $step);
|
||||
|
||||
usleep(180_000); // Simulate work being done
|
||||
}
|
||||
|
||||
$prompt = \sprintf(
|
||||
'Provide a concise response strategy for incident "%s" based on the steps completed: %s.',
|
||||
$incidentTitle,
|
||||
implode(', ', $steps)
|
||||
);
|
||||
|
||||
return new InputRequiredResult(['recommendation' => new CreateSamplingMessageRequest(
|
||||
messages: [new SamplingMessage(Role::User, new TextContent($prompt))],
|
||||
maxTokens: 350,
|
||||
temperature: 0.5,
|
||||
)]);
|
||||
}
|
||||
|
||||
$prompt = \sprintf(
|
||||
'Provide a concise response strategy for incident "%s" based on the steps completed: %s.',
|
||||
$incidentTitle,
|
||||
implode(', ', $steps)
|
||||
);
|
||||
|
||||
$result = $clientGateway->sample($prompt, 350, 90, ['temperature' => 0.5]);
|
||||
|
||||
$recommendation = $result->content instanceof TextContent ? trim((string) $result->content->text) : '';
|
||||
|
||||
$clientGateway->log(LoggingLevel::Info, \sprintf('Incident triage completed for %s', $incidentTitle));
|
||||
|
|
|
|||
Loading…
Reference in a new issue