php-sdk/src/Server.php
Kyrian Obikwelu f1b471a219
[Server] Add Bidirectional Client Communication Support (#109)
* feat(server): add bidirectional client communication support

* fix(server): use correct session ID string conversion method

* fix: clean up docblocks for request handler methods in Builder

* fix: merge artifacts
2025-10-26 22:48:17 +01:00

58 lines
1.2 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.
*/
namespace Mcp;
use Mcp\Server\Builder;
use Mcp\Server\Protocol;
use Mcp\Server\Transport\TransportInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* @author Christopher Hertel <mail@christopher-hertel.de>
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
final class Server
{
public function __construct(
private readonly Protocol $protocol,
private readonly LoggerInterface $logger = new NullLogger(),
) {
}
public static function builder(): Builder
{
return new Builder();
}
/**
* @template TResult
*
* @param TransportInterface<TResult> $transport
*
* @return TResult
*/
public function run(TransportInterface $transport): mixed
{
$transport->initialize();
$this->protocol->connect($transport);
$this->logger->info('Running server...');
try {
return $transport->listen();
} finally {
$transport->close();
}
}
}