|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * HTTP Client Communication Example. |
| 5 | + * |
| 6 | + * This example demonstrates server-to-client communication over HTTP: |
| 7 | + * - Logging notifications |
| 8 | + * - Progress notifications (via SSE streaming) |
| 9 | + * - Sampling requests (mocked LLM response) |
| 10 | + * |
| 11 | + * Usage: |
| 12 | + * 1. Start the server: php -S 127.0.0.1:8000 examples/server/client-communication/server.php |
| 13 | + * 2. Run this script: php examples/client/http_client_communication.php |
| 14 | + * |
| 15 | + * Note: PHP's built-in server works for listing tools, calling tools, and receiving |
| 16 | + * progress/logging notifications. However, sampling requires a concurrent-capable server |
| 17 | + * (e.g., Symfony CLI, PHP-FPM) since the server must process the client's sampling |
| 18 | + * response while the original tool request is still pending. |
| 19 | + * |
| 20 | + * Eg. symfony serve --passthru=examples/server/client-communication/server.php --no-tls |
| 21 | + */ |
| 22 | + |
| 23 | +declare(strict_types=1); |
| 24 | + |
| 25 | +/* |
| 26 | + * This file is part of the official PHP MCP SDK. |
| 27 | + * |
| 28 | + * A collaboration between Symfony and the PHP Foundation. |
| 29 | + * |
| 30 | + * For the full copyright and license information, please view the LICENSE |
| 31 | + * file that was distributed with this source code. |
| 32 | + */ |
| 33 | + |
| 34 | +require_once __DIR__.'/../../vendor/autoload.php'; |
| 35 | + |
| 36 | +use Mcp\Client; |
| 37 | +use Mcp\Client\Handler\Notification\LoggingNotificationHandler; |
| 38 | +use Mcp\Client\Handler\Request\SamplingCallbackInterface; |
| 39 | +use Mcp\Client\Handler\Request\SamplingRequestHandler; |
| 40 | +use Mcp\Client\Transport\HttpTransport; |
| 41 | +use Mcp\Schema\ClientCapabilities; |
| 42 | +use Mcp\Schema\Content\TextContent; |
| 43 | +use Mcp\Schema\Enum\Role; |
| 44 | +use Mcp\Schema\Notification\LoggingMessageNotification; |
| 45 | +use Mcp\Schema\Request\CreateSamplingMessageRequest; |
| 46 | +use Mcp\Schema\Result\CreateSamplingMessageResult; |
| 47 | + |
| 48 | +$endpoint = 'http://127.0.0.1:8000'; |
| 49 | + |
| 50 | +$loggingNotificationHandler = new LoggingNotificationHandler(static function (LoggingMessageNotification $n) { |
| 51 | + echo "[LOG {$n->level->value}] {$n->data}\n"; |
| 52 | +}); |
| 53 | + |
| 54 | +$samplingRequestHandler = new SamplingRequestHandler(new class implements SamplingCallbackInterface { |
| 55 | + public function __invoke(CreateSamplingMessageRequest $request): CreateSamplingMessageResult |
| 56 | + { |
| 57 | + echo "[SAMPLING] Server requested LLM sampling (max {$request->maxTokens} tokens)\n"; |
| 58 | + |
| 59 | + $mockResponse = 'Based on the incident analysis, I recommend: 1) Activate the on-call team, '. |
| 60 | + '2) Isolate affected systems, 3) Begin root cause analysis, 4) Prepare stakeholder communication.'; |
| 61 | + |
| 62 | + return new CreateSamplingMessageResult( |
| 63 | + role: Role::Assistant, |
| 64 | + content: new TextContent($mockResponse), |
| 65 | + model: 'mock-gpt-4', |
| 66 | + stopReason: 'end_turn', |
| 67 | + ); |
| 68 | + } |
| 69 | +}); |
| 70 | + |
| 71 | +$client = Client::builder() |
| 72 | + ->setClientInfo('HTTP Client Communication Test', '1.0.0') |
| 73 | + ->setInitTimeout(30) |
| 74 | + ->setRequestTimeout(120) |
| 75 | + ->setCapabilities(new ClientCapabilities(sampling: true)) |
| 76 | + ->addNotificationHandler($loggingNotificationHandler) |
| 77 | + ->addRequestHandler($samplingRequestHandler) |
| 78 | + ->build(); |
| 79 | + |
| 80 | +$transport = new HttpTransport(endpoint: $endpoint); |
| 81 | + |
| 82 | +try { |
| 83 | + echo "Connecting to MCP server at {$endpoint}...\n"; |
| 84 | + $client->connect($transport); |
| 85 | + |
| 86 | + $serverInfo = $client->getServerInfo(); |
| 87 | + echo 'Connected to: '.($serverInfo->name ?? 'unknown')."\n\n"; |
| 88 | + |
| 89 | + echo "Available tools:\n"; |
| 90 | + $toolsResult = $client->listTools(); |
| 91 | + foreach ($toolsResult->tools as $tool) { |
| 92 | + echo " - {$tool->name}\n"; |
| 93 | + } |
| 94 | + echo "\n"; |
| 95 | + |
| 96 | + echo "Calling 'run_dataset_quality_checks'...\n\n"; |
| 97 | + $result = $client->callTool( |
| 98 | + name: 'run_dataset_quality_checks', |
| 99 | + arguments: ['dataset' => 'sales_transactions_q4'], |
| 100 | + onProgress: static function (float $progress, ?float $total, ?string $message) { |
| 101 | + $percent = $total > 0 ? round(($progress / $total) * 100) : '?'; |
| 102 | + echo "[PROGRESS {$percent}%] {$message}\n"; |
| 103 | + } |
| 104 | + ); |
| 105 | + |
| 106 | + echo "\nResult:\n"; |
| 107 | + foreach ($result->content as $content) { |
| 108 | + if ($content instanceof TextContent) { |
| 109 | + echo $content->text."\n"; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + echo "\nCalling 'coordinate_incident_response'...\n\n"; |
| 114 | + $result = $client->callTool( |
| 115 | + name: 'coordinate_incident_response', |
| 116 | + arguments: ['incidentTitle' => 'Database connection pool exhausted'], |
| 117 | + onProgress: static function (float $progress, ?float $total, ?string $message) { |
| 118 | + $percent = $total > 0 ? round(($progress / $total) * 100) : '?'; |
| 119 | + echo "[PROGRESS {$percent}%] {$message}\n"; |
| 120 | + } |
| 121 | + ); |
| 122 | + |
| 123 | + echo "\nResult:\n"; |
| 124 | + foreach ($result->content as $content) { |
| 125 | + if ($content instanceof TextContent) { |
| 126 | + echo $content->text."\n"; |
| 127 | + } |
| 128 | + } |
| 129 | +} catch (Throwable $e) { |
| 130 | + echo "Error: {$e->getMessage()}\n"; |
| 131 | + echo $e->getTraceAsString()."\n"; |
| 132 | +} finally { |
| 133 | + $client->disconnect(); |
| 134 | +} |
0 commit comments