|
| 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 | +require_once dirname(__DIR__, 2).'/vendor/autoload.php'; |
| 13 | + |
| 14 | +use Mcp\Client; |
| 15 | +use Mcp\Client\Handler\Request\RequestHandlerInterface; |
| 16 | +use Mcp\Client\Transport\HttpTransport; |
| 17 | +use Mcp\Schema\ClientCapabilities; |
| 18 | +use Mcp\Schema\Enum\ElicitAction; |
| 19 | +use Mcp\Schema\JsonRpc\Request; |
| 20 | +use Mcp\Schema\JsonRpc\Response; |
| 21 | +use Mcp\Schema\Request\ElicitRequest; |
| 22 | +use Mcp\Schema\Result\ElicitResult; |
| 23 | +use Mcp\Tests\Conformance\FileLogger; |
| 24 | + |
| 25 | +$url = $argv[1] ?? null; |
| 26 | +$scenario = getenv('MCP_CONFORMANCE_SCENARIO') ?: null; |
| 27 | + |
| 28 | +if (!$url || !$scenario) { |
| 29 | + fwrite(\STDERR, "Usage: MCP_CONFORMANCE_SCENARIO=<scenario> php client.php <server-url>\n"); |
| 30 | + exit(1); |
| 31 | +} |
| 32 | + |
| 33 | +@mkdir(__DIR__.'/logs', 0777, true); |
| 34 | +$logger = new FileLogger(__DIR__.'/logs/client-conformance.log', true); |
| 35 | +$logger->info(sprintf('Starting client conformance test: scenario=%s, url=%s', $scenario, $url)); |
| 36 | + |
| 37 | +$builder = Client::builder() |
| 38 | + ->setClientInfo('mcp-conformance-test-client', '1.0.0') |
| 39 | + ->setInitTimeout(30) |
| 40 | + ->setRequestTimeout(60) |
| 41 | + ->setLogger($logger); |
| 42 | + |
| 43 | +if ('elicitation-sep1034-client-defaults' === $scenario) { |
| 44 | + $builder->setCapabilities(new ClientCapabilities(elicitation: true)); |
| 45 | + $builder->addRequestHandler(new class($logger) implements RequestHandlerInterface { |
| 46 | + public function __construct(private readonly Psr\Log\LoggerInterface $logger) |
| 47 | + { |
| 48 | + } |
| 49 | + |
| 50 | + public function supports(Request $request): bool |
| 51 | + { |
| 52 | + return $request instanceof ElicitRequest; |
| 53 | + } |
| 54 | + |
| 55 | + public function handle(Request $request): Response |
| 56 | + { |
| 57 | + $this->logger->info('Received elicitation request, accepting with empty content'); |
| 58 | + |
| 59 | + return new Response($request->getId(), new ElicitResult(ElicitAction::Accept, [])); |
| 60 | + } |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +$client = $builder->build(); |
| 65 | +$transport = new HttpTransport($url, logger: $logger); |
| 66 | + |
| 67 | +try { |
| 68 | + $client->connect($transport); |
| 69 | + $logger->info('Connected to server'); |
| 70 | + |
| 71 | + $toolsResult = $client->listTools(); |
| 72 | + $logger->info(sprintf('Listed %d tools', count($toolsResult->tools))); |
| 73 | + |
| 74 | + switch ($scenario) { |
| 75 | + case 'initialize': |
| 76 | + break; |
| 77 | + |
| 78 | + case 'tools_call': |
| 79 | + $toolName = $toolsResult->tools[0]->name ?? 'test-tool'; |
| 80 | + $client->callTool($toolName, []); |
| 81 | + $logger->info(sprintf('Called tool: %s', $toolName)); |
| 82 | + break; |
| 83 | + |
| 84 | + case 'elicitation-sep1034-client-defaults': |
| 85 | + $toolName = $toolsResult->tools[0]->name ?? 'test_client_elicitation_defaults'; |
| 86 | + $client->callTool($toolName, []); |
| 87 | + $logger->info(sprintf('Called tool: %s', $toolName)); |
| 88 | + break; |
| 89 | + |
| 90 | + default: |
| 91 | + $logger->warning(sprintf('Unknown scenario: %s', $scenario)); |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + $client->disconnect(); |
| 96 | + $logger->info('Disconnected'); |
| 97 | + exit(0); |
| 98 | +} catch (Throwable $e) { |
| 99 | + $logger->error(sprintf('Error: %s', $e->getMessage())); |
| 100 | + fwrite(\STDERR, sprintf("Error: %s\n%s\n", $e->getMessage(), $e->getTraceAsString())); |
| 101 | + |
| 102 | + try { |
| 103 | + $client->disconnect(); |
| 104 | + } catch (Throwable $ignored) { |
| 105 | + } |
| 106 | + |
| 107 | + exit(1); |
| 108 | +} |
0 commit comments