php-sdk/examples/client/stdio_client_communication.php
Christopher Hertel 43815e38a8
[Schema][Server] Harden sampling tools against the spec (#420)
* harden sampling tools against the spec

Report tool-flow violations as -32602 instead of dropping the request,
accept resource_link in tool results, gate tools on sampling.tools, and
reject empty content.

* normalize keyed content arrays to lists

array_filter() and friends preserve keys, and a keyed array serializes as a
JSON object instead of the content-block array the schema requires.
2026-08-15 04:13:53 +02:00

124 lines
4.1 KiB
PHP

<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* STDIO Client Communication Example.
*
* This example demonstrates server-to-client communication:
* - Logging notifications
* - Progress notifications
* - Sampling requests (mocked LLM response)
*
* Usage: php examples/client/stdio_client_communication.php
*/
require_once __DIR__.'/../../vendor/autoload.php';
use Mcp\Client;
use Mcp\Client\Handler\Notification\LoggingNotificationHandler;
use Mcp\Client\Handler\Request\SamplingCallbackInterface;
use Mcp\Client\Handler\Request\SamplingRequestHandler;
use Mcp\Client\Transport\StdioTransport;
use Mcp\Schema\ClientCapabilities;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Enum\Role;
use Mcp\Schema\Notification\LoggingMessageNotification;
use Mcp\Schema\Request\CreateSamplingMessageRequest;
use Mcp\Schema\Result\CreateSamplingMessageResult;
$loggingNotificationHandler = new LoggingNotificationHandler(static function (LoggingMessageNotification $n) {
echo "[LOG {$n->level->value}] {$n->data}\n";
});
$samplingRequestHandler = new SamplingRequestHandler(new class implements SamplingCallbackInterface {
public function __invoke(CreateSamplingMessageRequest $request): CreateSamplingMessageResult
{
echo "[SAMPLING] Server requested LLM sampling (max {$request->maxTokens} tokens)\n";
$mockResponse = 'Based on the incident analysis, I recommend: 1) Activate the on-call team, '.
'2) Isolate affected systems, 3) Begin root cause analysis, 4) Prepare stakeholder communication.';
return new CreateSamplingMessageResult(
role: Role::Assistant,
content: new TextContent($mockResponse),
model: 'mock-gpt-4',
stopReason: 'endTurn',
);
}
});
$client = Client::builder()
->setClientInfo('STDIO Client Communication Test', '1.0.0')
->setInitTimeout(30)
->setRequestTimeout(120)
->setCapabilities(new ClientCapabilities(sampling: true))
->addNotificationHandler($loggingNotificationHandler)
->addRequestHandler($samplingRequestHandler)
->build();
$transport = new StdioTransport(
command: 'php',
args: [__DIR__.'/../server/client-communication/server.php'],
);
try {
echo "Connecting to MCP server...\n";
$client->connect($transport);
$serverInfo = $client->getServerInfo();
echo 'Connected to: '.($serverInfo->name ?? 'unknown')."\n\n";
echo "Available tools:\n";
$toolsResult = $client->listTools();
foreach ($toolsResult->tools as $tool) {
echo " - {$tool->name}\n";
}
echo "\n";
echo "Calling 'run_dataset_quality_checks'...\n\n";
$result = $client->callTool(
name: 'run_dataset_quality_checks',
arguments: ['dataset' => 'customer_orders_2024'],
onProgress: static function (float $progress, ?float $total, ?string $message) {
$percent = $total > 0 ? round(($progress / $total) * 100) : '?';
echo "[PROGRESS {$percent}%] {$message}\n";
}
);
echo "\nResult:\n";
foreach ($result->content as $content) {
if ($content instanceof TextContent) {
echo $content->text."\n";
}
}
echo "\nCalling 'coordinate_incident_response'...\n\n";
$result = $client->callTool(
name: 'coordinate_incident_response',
arguments: ['incidentTitle' => 'Database connection pool exhausted'],
onProgress: static function (float $progress, ?float $total, ?string $message) {
$percent = $total > 0 ? round(($progress / $total) * 100) : '?';
echo "[PROGRESS {$percent}%] {$message}\n";
}
);
echo "\nResult:\n";
foreach ($result->content as $content) {
if ($content instanceof TextContent) {
echo $content->text."\n";
}
}
} catch (Throwable $e) {
echo "Error: {$e->getMessage()}\n";
echo $e->getTraceAsString()."\n";
} finally {
$client->disconnect();
}