php-sdk/examples/server/bootstrap.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

95 lines
2.5 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.
*/
use Http\Discovery\Psr17Factory;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Mcp\Capability\Registry\Container;
use Mcp\Server\Transport\StdioTransport;
use Mcp\Server\Transport\StreamableHttpTransport;
use Mcp\Server\Transport\TransportInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
set_exception_handler(static function (Throwable $t): never {
logger()->critical('Uncaught exception: '.$t->getMessage(), ['exception' => $t]);
exit(1);
});
/**
* @return TransportInterface<int>|TransportInterface<ResponseInterface>
*/
function transport(): TransportInterface
{
if ('cli' === \PHP_SAPI) {
return new StdioTransport(logger: logger());
}
return new StreamableHttpTransport(
(new Psr17Factory())->createServerRequestFromGlobals(),
logger: logger(),
);
}
function shutdown(ResponseInterface|int $result): never
{
if ('cli' === \PHP_SAPI) {
exit($result);
}
(new SapiEmitter())->emit($result);
exit(0);
}
function logger(): LoggerInterface
{
return new class extends AbstractLogger {
public function log($level, string|Stringable $message, array $context = []): void
{
$debug = $_SERVER['DEBUG'] ?? false;
if (!$debug && 'debug' === $level) {
return;
}
$exception = $context['exception'] ?? null;
unset($context['exception']);
$logMessage = sprintf(
"[%s] %s %s\n",
strtoupper($level),
$message,
([] === $context || !$debug) ? '' : json_encode($context),
);
if ($exception instanceof Throwable) {
$logMessage .= sprintf('> %s', $exception->getMessage())."\n";
}
if (($_SERVER['FILE_LOG'] ?? false) || !defined('STDERR')) {
file_put_contents('dev.log', $logMessage, \FILE_APPEND);
} else {
fwrite(\STDERR, $logMessage);
}
}
};
}
function container(): Container
{
$container = new Container();
$container->set(LoggerInterface::class, logger());
return $container;
}