Repository navigation
Expand file tree
/
Copy pathServer.php
More file actions
77 lines (66 loc) · 2.35 KB
/
Copy pathServer.php
File metadata and controls
77 lines (66 loc) · 2.35 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?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\Server;
use Mcp\Schema\Enum\ProtocolVersion;
use Mcp\Server\Stateless\StatelessProtocol;
use Mcp\Server\Transport\StatelessAwareTransportInterface;
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
{
/**
* @param StatelessProtocol|null $statelessProtocol the modern-era (SEP-2575) dispatcher, absent on a
* server that serves the handshake era alone
* @param non-empty-list<ProtocolVersion>|null $handshakeVersions revisions `initialize` negotiates, all of them by default
*/
public function __construct(
private readonly Protocol $protocol,
private readonly LoggerInterface $logger = new NullLogger(),
private readonly ?StatelessProtocol $statelessProtocol = null,
private readonly ?array $handshakeVersions = null,
) {
}
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);
// The eras share the transport, not the dispatcher: a transport that
// can tell them apart takes both and picks per request. One that
// cannot carries the handshake era alone.
if ($transport instanceof StatelessAwareTransportInterface) {
$transport->setHandshakeVersions($this->handshakeVersions ?? ProtocolVersion::handshakeVersions());
if (null !== $this->statelessProtocol) {
$transport->connectStateless($this->statelessProtocol);
}
}
$this->logger->info('Running server...');
try {
return $transport->listen();
} finally {
$transport->close();
}
}
}