|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * STDIO Client Elicitation Example. |
| 14 | + * |
| 15 | + * This example demonstrates how a client responds to server-initiated |
| 16 | + * elicitation/create requests. The server's tools ask the user for additional |
| 17 | + * information mid-execution; the client below prompts interactively on STDIN |
| 18 | + * and accepts defaults when the user presses Enter. |
| 19 | + * |
| 20 | + * Run against the elicitation demo server: |
| 21 | + * php examples/client/stdio_elicitation.php |
| 22 | + */ |
| 23 | + |
| 24 | +require_once __DIR__.'/../../vendor/autoload.php'; |
| 25 | + |
| 26 | +use Mcp\Client; |
| 27 | +use Mcp\Client\Handler\Request\ElicitationCallbackInterface; |
| 28 | +use Mcp\Client\Handler\Request\ElicitationRequestHandler; |
| 29 | +use Mcp\Client\Transport\StdioTransport; |
| 30 | +use Mcp\Schema\ClientCapabilities; |
| 31 | +use Mcp\Schema\Content\TextContent; |
| 32 | +use Mcp\Schema\Elicitation\BooleanSchemaDefinition; |
| 33 | +use Mcp\Schema\Elicitation\EnumSchemaDefinition; |
| 34 | +use Mcp\Schema\Elicitation\NumberSchemaDefinition; |
| 35 | +use Mcp\Schema\Elicitation\StringSchemaDefinition; |
| 36 | +use Mcp\Schema\Enum\ElicitAction; |
| 37 | +use Mcp\Schema\Request\ElicitRequest; |
| 38 | +use Mcp\Schema\Result\ElicitResult; |
| 39 | + |
| 40 | +$elicitationRequestHandler = new ElicitationRequestHandler(new class implements ElicitationCallbackInterface { |
| 41 | + public function __invoke(ElicitRequest $request): ElicitResult |
| 42 | + { |
| 43 | + echo "\n[ELICIT] {$request->message}\n"; |
| 44 | + |
| 45 | + $content = []; |
| 46 | + foreach ($request->requestedSchema->properties as $name => $definition) { |
| 47 | + $default = $this->defaultFor($definition); |
| 48 | + $label = $this->labelFor($definition, $name); |
| 49 | + |
| 50 | + if (null !== $default) { |
| 51 | + $display = is_bool($default) ? ($default ? 'true' : 'false') : (string) $default; |
| 52 | + echo " {$label} [{$display}]: "; |
| 53 | + } else { |
| 54 | + echo " {$label}: "; |
| 55 | + } |
| 56 | + |
| 57 | + $rawInput = fgets(\STDIN); |
| 58 | + $input = false === $rawInput ? '' : trim($rawInput); |
| 59 | + $value = '' === $input ? $default : $this->cast($definition, $input); |
| 60 | + |
| 61 | + $content[$name] = $value; |
| 62 | + } |
| 63 | + |
| 64 | + return new ElicitResult(ElicitAction::Accept, $content); |
| 65 | + } |
| 66 | + |
| 67 | + private function defaultFor(object $definition): mixed |
| 68 | + { |
| 69 | + return match (true) { |
| 70 | + $definition instanceof EnumSchemaDefinition => $definition->default ?? $definition->enum[0], |
| 71 | + $definition instanceof NumberSchemaDefinition => $definition->default ?? $definition->minimum ?? ($definition->integerOnly ? 1 : 1.0), |
| 72 | + $definition instanceof BooleanSchemaDefinition => $definition->default ?? false, |
| 73 | + $definition instanceof StringSchemaDefinition => $definition->default ?? ('date' === $definition->format ? date('Y-m-d') : ''), |
| 74 | + default => null, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + private function labelFor(object $definition, string $name): string |
| 79 | + { |
| 80 | + $title = match (true) { |
| 81 | + $definition instanceof EnumSchemaDefinition => $definition->title, |
| 82 | + $definition instanceof NumberSchemaDefinition => $definition->title, |
| 83 | + $definition instanceof BooleanSchemaDefinition => $definition->title, |
| 84 | + $definition instanceof StringSchemaDefinition => $definition->title, |
| 85 | + default => null, |
| 86 | + }; |
| 87 | + |
| 88 | + return $title ?? $name; |
| 89 | + } |
| 90 | + |
| 91 | + private function cast(object $definition, string $input): mixed |
| 92 | + { |
| 93 | + return match (true) { |
| 94 | + $definition instanceof BooleanSchemaDefinition => filter_var($input, \FILTER_VALIDATE_BOOLEAN), |
| 95 | + $definition instanceof NumberSchemaDefinition => $definition->integerOnly ? (int) $input : (float) $input, |
| 96 | + default => $input, |
| 97 | + }; |
| 98 | + } |
| 99 | +}); |
| 100 | + |
| 101 | +$client = Client::builder() |
| 102 | + ->setClientInfo('STDIO Elicitation Test', '1.0.0') |
| 103 | + ->setInitTimeout(30) |
| 104 | + ->setRequestTimeout(120) |
| 105 | + ->setCapabilities(new ClientCapabilities(elicitation: true)) |
| 106 | + ->addRequestHandler($elicitationRequestHandler) |
| 107 | + ->build(); |
| 108 | + |
| 109 | +$transport = new StdioTransport( |
| 110 | + command: 'php', |
| 111 | + args: [__DIR__.'/../server/elicitation/server.php'], |
| 112 | +); |
| 113 | + |
| 114 | +try { |
| 115 | + echo "Connecting to MCP server...\n"; |
| 116 | + $client->connect($transport); |
| 117 | + |
| 118 | + $serverInfo = $client->getServerInfo(); |
| 119 | + echo 'Connected to: '.($serverInfo->name ?? 'unknown')."\n\n"; |
| 120 | + |
| 121 | + echo "Calling 'book_restaurant'...\n"; |
| 122 | + $result = $client->callTool( |
| 123 | + name: 'book_restaurant', |
| 124 | + arguments: ['restaurantName' => 'The Test Kitchen'], |
| 125 | + ); |
| 126 | + |
| 127 | + echo "\nResult:\n"; |
| 128 | + foreach ($result->content as $content) { |
| 129 | + if ($content instanceof TextContent) { |
| 130 | + echo $content->text."\n"; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + echo "\nCalling 'confirm_action'...\n"; |
| 135 | + $result = $client->callTool( |
| 136 | + name: 'confirm_action', |
| 137 | + arguments: ['actionDescription' => 'Delete all temporary files'], |
| 138 | + ); |
| 139 | + |
| 140 | + echo "\nResult:\n"; |
| 141 | + foreach ($result->content as $content) { |
| 142 | + if ($content instanceof TextContent) { |
| 143 | + echo $content->text."\n"; |
| 144 | + } |
| 145 | + } |
| 146 | +} catch (Throwable $e) { |
| 147 | + echo "Error: {$e->getMessage()}\n"; |
| 148 | + echo $e->getTraceAsString()."\n"; |
| 149 | +} finally { |
| 150 | + $client->disconnect(); |
| 151 | +} |
0 commit comments