forked from modelcontextprotocol/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.php
More file actions
60 lines (50 loc) · 1.57 KB
/
Server.php
File metadata and controls
60 lines (50 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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\Handler\JsonRpcHandler;
use Mcp\Server\Transport\TransportInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Uid\Uuid;
/**
* @author Christopher Hertel <mail@christopher-hertel.de>
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
final class Server
{
public function __construct(
private readonly JsonRpcHandler $jsonRpcHandler,
private readonly LoggerInterface $logger = new NullLogger(),
) {
}
public static function builder(): Builder
{
return new Builder();
}
public function connect(TransportInterface $transport): void
{
$transport->initialize();
$this->logger->info('Transport initialized.', [
'transport' => $transport::class,
]);
$transport->onMessage(function (string $message, ?Uuid $sessionId) use ($transport) {
foreach ($this->jsonRpcHandler->process($message, $sessionId) as [$response, $context]) {
if (null === $response) {
continue;
}
$transport->send($response, $context);
}
});
$transport->onSessionEnd(function (Uuid $sessionId) {
$this->jsonRpcHandler->destroySession($sessionId);
});
}
}