Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RunMcpServerCommand
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
43 / 43
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\McpServer;
3
4use Apie\Core\ValueObjects\Utils;
5use Apie\McpServer\Factory\RunnerFactoryInterface;
6use Apie\McpServer\Tool\ToolFactory;
7use Apie\McpServer\Tool\ToolRunner;
8use Mcp\Server\Server;
9use Mcp\Types\CallToolRequestParams;
10use Psr\Log\LoggerInterface;
11use Symfony\Component\Console\Command\Command;
12
13class RunMcpServerCommand extends \Symfony\Component\Console\Command\Command
14{
15    public function __construct(
16        private readonly RunnerFactoryInterface $runnerFactory,
17        private readonly ToolFactory $toolFactory,
18        private readonly ToolRunner $toolRunner,
19        private readonly LoggerInterface $logger
20    ) {
21        parent::__construct('apie:mcp-server');
22    }
23
24    protected function configure()
25    {
26        $this
27            ->setDescription('Runs the MCP server to link with Apie')
28            ->setHelp('This command allows you to run the MCP server for Apie.');
29    }
30
31    protected function execute(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output): int
32    {
33        $output->writeln(json_encode([
34            "jsonrpc" => "2.0",
35            "method" => "notifications/progress",
36            "params" => [
37                "progressToken" => "apie_mcp_server_startup",
38                "progress" => 50,
39                "total" => 100,
40                "message" => "Creating MCP Server..."
41            ]
42        ]));
43        $server = new Server('apie-server', $this->logger);
44        $server->registerHandler('tools/list', function () {
45            return $this->toolFactory->createList();
46        });
47        $server->registerHandler('tools/call', function (CallToolRequestParams $params) {
48            $name = $params->name;
49            $tool = $this->toolFactory->findByName($name);
50            $result = $this->toolRunner->run($tool, Utils::toArray($params->arguments ?? []));
51            return $result;
52        });
53
54        $output->writeln(json_encode([
55            "jsonrpc" => "2.0",
56            "method" => "notifications/progress",
57            "params" => [
58                "progressToken" => "apie_mcp_server_startup",
59                "progress" => 100,
60                "total" => 100,
61                "message" => "MCP Server is running..."
62            ]
63        ]));
64        $runner = $this->runnerFactory->createRunner($server);
65        $runner->run();
66        $output->writeln(json_encode([
67            "jsonrpc" => "2.0",
68            "method" => "notifications/progress",
69            "params" => [
70                "progressToken" => "apie_mcp_server_shutdown",
71                "progress" => 100,
72                "total" => 100,
73                "message" => "MCP Server is ending..."
74            ]
75        ]));
76        return Command::SUCCESS;
77    }
78}