| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Server; |
| 15: | |
| 16: | use Amp\DeferredFuture; |
| 17: | use Nexus\Mcp\Core\Dispatch\MessageDispatcherInterface; |
| 18: | use Nexus\Mcp\Core\Schema\RequestId; |
| 19: | use Nexus\Mcp\Core\Transport\CancellableTransportInterface; |
| 20: | use Nexus\Mcp\Core\Transport\ReceiveContext; |
| 21: | use Nexus\Mcp\Core\Transport\TransportInterface; |
| 22: | use Nexus\Mcp\Server\Subscription\SubscriptionStoreInterface; |
| 23: | use Psr\Log\LoggerInterface; |
| 24: | use Psr\Log\NullLogger; |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | final readonly class Server |
| 31: | { |
| 32: | public function __construct( |
| 33: | private MessageDispatcherInterface $dispatcher, |
| 34: | private LoggerInterface $logger = new NullLogger(), |
| 35: | private ?SubscriptionStoreInterface $subscriptions = null, |
| 36: | ) { |
| 37: | } |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | public function run(TransportInterface $transport): void |
| 45: | { |
| 46: | $this->logger->info('Starting MCP server.'); |
| 47: | |
| 48: | $deferred = new DeferredFuture(); |
| 49: | |
| 50: | $this->attachDispatchListeners($transport); |
| 51: | |
| 52: | $transport->onClose(static function () use ($deferred): void { |
| 53: | |
| 54: | |
| 55: | if ($deferred->isComplete()) { |
| 56: | return; |
| 57: | } |
| 58: | |
| 59: | $deferred->complete(); |
| 60: | }); |
| 61: | |
| 62: | $transport->start(); |
| 63: | $deferred->getFuture()->await(); |
| 64: | |
| 65: | $this->logger->info('MCP server stopped.'); |
| 66: | } |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | public function listen(TransportInterface $transport): void |
| 74: | { |
| 75: | $this->attachDispatchListeners($transport); |
| 76: | |
| 77: | $transport->start(); |
| 78: | } |
| 79: | |
| 80: | private function attachDispatchListeners(TransportInterface $transport): void |
| 81: | { |
| 82: | $transport->onMessage(function (array $envelope, ReceiveContext $context) use ($transport): void { |
| 83: | $this->dispatcher->dispatch($envelope, $transport, $context); |
| 84: | }); |
| 85: | $transport->onError(function (\Throwable $e): void { |
| 86: | $this->logger->error('Transport error.', ['exception' => $e]); |
| 87: | }); |
| 88: | $transport->onDrain(function (): void { |
| 89: | |
| 90: | |
| 91: | $this->subscriptions?->closeAll(); |
| 92: | $this->dispatcher->flushPending(); |
| 93: | }); |
| 94: | |
| 95: | if ($transport instanceof CancellableTransportInterface) { |
| 96: | $transport->onCancel(function (RequestId $id): void { |
| 97: | $this->dispatcher->cancelRequest($id); |
| 98: | }); |
| 99: | } |
| 100: | } |
| 101: | } |
| 102: | |