|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ecourty\McpServerBundle\Command; |
| 6 | + |
| 7 | +use Ecourty\McpServerBundle\Controller\EntrypointController; |
| 8 | +use Ecourty\McpServerBundle\EventListener\ExceptionListener; |
| 9 | +use Ecourty\McpServerBundle\HttpFoundation\JsonRpcRequest; |
| 10 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 11 | +use Symfony\Component\Console\Command\Command; |
| 12 | +use Symfony\Component\Console\Input\InputArgument; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 17 | +use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
| 18 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 19 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 20 | +use Symfony\Component\Serializer\SerializerInterface; |
| 21 | + |
| 22 | +#[AsCommand( |
| 23 | + name: 'mcp:stdio', |
| 24 | + description: 'MCP server STDIO entrypoint', |
| 25 | +)] |
| 26 | +class EntrypointCommand extends Command |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private readonly SerializerInterface $serializer, |
| 30 | + private readonly EntrypointController $entrypointController, |
| 31 | + private readonly ExceptionListener $exceptionListener, |
| 32 | + private readonly KernelInterface $kernel, |
| 33 | + private readonly RequestStack $requestStack, |
| 34 | + ) { |
| 35 | + parent::__construct(); |
| 36 | + } |
| 37 | + |
| 38 | + protected function configure(): void |
| 39 | + { |
| 40 | + $this->addArgument('jsonrpc_request', InputArgument::REQUIRED, 'JSON-RPC request'); |
| 41 | + } |
| 42 | + |
| 43 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 44 | + { |
| 45 | + $jsonRpcRequestString = (string) $input->getArgument('jsonrpc_request'); |
| 46 | + if (empty($jsonRpcRequestString) === true) { |
| 47 | + throw new \UnexpectedValueException('Missing request argument'); |
| 48 | + } |
| 49 | + |
| 50 | + $jsonRpcRequest = $this->serializer->deserialize($jsonRpcRequestString, JsonRpcRequest::class, 'json'); |
| 51 | + $request = new Request(content: $jsonRpcRequestString); |
| 52 | + |
| 53 | + try { |
| 54 | + $response = $this->entrypointController->__invoke($jsonRpcRequest, $request); |
| 55 | + } catch (\Throwable $exception) { |
| 56 | + $this->requestStack->push($request); |
| 57 | + $exceptionEvent = new ExceptionEvent( |
| 58 | + kernel: $this->kernel, |
| 59 | + request: $request, |
| 60 | + requestType: HttpKernelInterface::MAIN_REQUEST, |
| 61 | + e: $exception, |
| 62 | + ); |
| 63 | + $this->exceptionListener->onKernelException($exceptionEvent); |
| 64 | + |
| 65 | + $response = $exceptionEvent->getResponse(); |
| 66 | + } |
| 67 | + |
| 68 | + if ($response === null) { |
| 69 | + throw new \RuntimeException('No response received'); |
| 70 | + } |
| 71 | + |
| 72 | + $output->write((string) $response->getContent()); |
| 73 | + |
| 74 | + return self::SUCCESS; |
| 75 | + } |
| 76 | +} |
0 commit comments