php-sdk/examples/client/stdio_discovery_calculator.php
Kyrian Obikwelu 3ef864c444
[Client] Feat: Implement MCP client component (#192)
* feat(client): add complete MCP client SDK with STDIO and HTTP transports

Add Client, Builder, ClientProtocol, ClientSession, and transport
implementations for communicating with MCP servers. Supports tools, resources, prompts, and real-time progress/logging notifications.

* refactor(examples): reorganize examples into server/ and client/ folders

* feat: add SamplingRequestHandler for handling server sampling requests

* feat: add setLoggingLevel method to control server logging verbosity

* refactor: make progress token dependent on request ID

* docs: Update server IP in usage instructions and add notes on sampling server requirements.

* feat: add complete() method for completion/complete requests

* refactor: properly parse and type initialize result

* refactor: standardize method names

* fix: revert changes to server related components

* fix: revert changes to server related components

* refactor: Restructure client transports and handlers into dedicated namespaces.

* fix: cs and php-stan errors fixes(partial)

* feat: Refactor request handlers to return `Response` or `Error` objects directly, adding a `SamplingException` and error logging for sampling requests.

* feat: Replace `ClientTransportInterface` with `TransportInterface` and move response result deserialization to client methods.

* refactor: remove redundant timeout check for intialize on transports

The processFiber that executes per tick while fiber is suspended already handles timeouts while waiting

* feat: broaden symfony/http-client dependency to support Symfony 5.4 to 8.0

* Update examples/client/README.md

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>

* Update examples/client/README.md

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>

* refactor: remove outgoing message queue from session and simplify transport send method to directly dispatch messages.

* refactor: Replace ClientSession with ClientState for clearer intent on runtime state management

* refactor: rename connectAndInitialize to connect for consistency

* chore: cs fix

* chore: use SamplingCallbackInterface instead of a closure for sampling request handler

---------

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
2026-03-10 17:22:40 +01:00

89 lines
2.6 KiB
PHP

<?php
/**
* Simple STDIO Client Example.
*
* This example demonstrates how to use the MCP client with a STDIO transport
* to communicate with an MCP server running as a child process.
*
* Usage: php examples/client/stdio_discovery_calculator.php
*/
declare(strict_types=1);
/*
* 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.
*/
require_once __DIR__.'/../../vendor/autoload.php';
use Mcp\Client;
use Mcp\Client\Transport\StdioTransport;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Content\TextResourceContents;
$client = Client::builder()
->setClientInfo('STDIO Example Client', '1.0.0')
->setInitTimeout(30)
->setRequestTimeout(60)
->build();
$transport = new StdioTransport(
command: 'php',
args: [__DIR__.'/../server/discovery-calculator/server.php'],
);
try {
echo "Connecting to MCP server...\n";
$client->connect($transport);
echo "Connected! Server info:\n";
$serverInfo = $client->getServerInfo();
echo ' Name: '.($serverInfo->name ?? 'unknown')."\n";
echo ' Version: '.($serverInfo->version ?? 'unknown')."\n\n";
echo "Available tools:\n";
$toolsResult = $client->listTools();
foreach ($toolsResult->tools as $tool) {
echo " - {$tool->name}: {$tool->description}\n";
}
echo "\n";
echo "Calling 'calculate' tool with a=5, b=3, operation='add'...\n";
$result = $client->callTool('calculate', ['a' => 5, 'b' => 3, 'operation' => 'add']);
echo 'Result: ';
foreach ($result->content as $content) {
if ($content instanceof TextContent) {
echo $content->text;
}
}
echo "\n\n";
echo "Available resources:\n";
$resourcesResult = $client->listResources();
foreach ($resourcesResult->resources as $resource) {
echo " - {$resource->uri}: {$resource->name}\n";
}
echo "\n";
echo "Reading resource 'config://calculator/settings'...\n";
$resourceContent = $client->readResource('config://calculator/settings');
foreach ($resourceContent->contents as $content) {
if ($content instanceof TextResourceContents) {
echo ' Content: '.$content->text."\n";
echo ' Mimetype: '.$content->mimeType."\n";
}
}
} catch (Throwable $e) {
echo "Error: {$e->getMessage()}\n";
echo $e->getTraceAsString()."\n";
} finally {
echo "Disconnecting...\n";
$client->disconnect();
echo "Done.\n";
}