Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.36% covered (warning)
86.36%
19 / 22
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
InlineRunner
86.36% covered (warning)
86.36%
19 / 22
50.00% covered (danger)
50.00%
1 / 2
6.09
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 run
85.71% covered (warning)
85.71%
18 / 21
0.00% covered (danger)
0.00%
0 / 1
5.07
1<?php
2namespace Apie\McpServer\Runner;
3
4use Apie\McpServer\Exception\StopRunnerException;
5use Apie\McpServer\Transport\InMemoryTransport;
6use Mcp\Server\InitializationOptions;
7use Mcp\Server\Server;
8use Mcp\Server\ServerRunner;
9use Mcp\Server\ServerSession;
10use Psr\Log\LoggerInterface;
11
12class InlineRunner extends ServerRunner
13{
14    /**
15     * @param array<int, JsonRpcMessage> $messages
16     */
17    public function __construct(
18        private readonly Server $server,
19        private readonly InitializationOptions $initOptions,
20        LoggerInterface $logger,
21        private readonly array $messages = []
22    ) {
23        parent::__construct($server, $initOptions, $logger);
24    }
25
26    public function run(): void
27    {
28
29        try {
30            $transport = new InMemoryTransport($this->messages);
31
32            $session = new ServerSession(
33                $transport,
34                $this->initOptions,
35                $this->logger
36            );
37
38            // Connect the server with the session
39            $this->server->setSession($session);
40
41            // Add handlers
42            $session->registerHandlers($this->server->getHandlers());
43            $session->registerNotificationHandlers($this->server->getNotificationHandlers());
44            $this->logger->info('Server started');
45            $session->start();
46
47            
48        } catch (StopRunnerException) {
49            $this->logger->info('Runner has ended');
50            return;
51        } catch (\Exception $e) {
52            $this->logger->error('Server error: ' . $e->getMessage());
53            throw $e;
54        } finally {
55            if (isset($session)) {
56                $session->stop();
57            }
58            if (isset($transport)) {
59                $transport->stop();
60            }
61        }
62    }
63}