This repository was archived by the owner on Nov 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEntrypointCommand.php
More file actions
76 lines (64 loc) · 2.67 KB
/
Copy pathEntrypointCommand.php
File metadata and controls
76 lines (64 loc) · 2.67 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
<?php
declare(strict_types=1);
namespace Ecourty\McpServerBundle\Command;
use Ecourty\McpServerBundle\Controller\EntrypointController;
use Ecourty\McpServerBundle\EventListener\ExceptionListener;
use Ecourty\McpServerBundle\HttpFoundation\JsonRpcRequest;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Serializer\SerializerInterface;
#[AsCommand(
name: 'mcp:stdio',
description: 'MCP server STDIO entrypoint',
)]
class EntrypointCommand extends Command
{
public function __construct(
private readonly SerializerInterface $serializer,
private readonly EntrypointController $entrypointController,
private readonly ExceptionListener $exceptionListener,
private readonly KernelInterface $kernel,
private readonly RequestStack $requestStack,
) {
parent::__construct();
}
protected function configure(): void
{
$this->addArgument('jsonrpc_request', InputArgument::REQUIRED, 'JSON-RPC request');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$jsonRpcRequestString = (string) $input->getArgument('jsonrpc_request');
if (empty($jsonRpcRequestString) === true) {
throw new \UnexpectedValueException('Missing request argument');
}
$jsonRpcRequest = $this->serializer->deserialize($jsonRpcRequestString, JsonRpcRequest::class, 'json');
$request = new Request(content: $jsonRpcRequestString);
try {
$response = $this->entrypointController->__invoke($jsonRpcRequest, $request);
} catch (\Throwable $exception) {
$this->requestStack->push($request);
$exceptionEvent = new ExceptionEvent(
kernel: $this->kernel,
request: $request,
requestType: HttpKernelInterface::MAIN_REQUEST,
e: $exception,
);
$this->exceptionListener->onKernelException($exceptionEvent);
$response = $exceptionEvent->getResponse();
}
if ($response === null) {
throw new \RuntimeException('No response received');
}
$output->write((string) $response->getContent());
return self::SUCCESS;
}
}