|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\McpBundle\Controller; |
| 13 | + |
| 14 | +use Symfony\AI\McpSdk\Server; |
| 15 | +use Symfony\AI\McpSdk\Server\Transport\Sse\Store\CachePoolStore; |
| 16 | +use Symfony\AI\McpSdk\Server\Transport\Sse\StreamTransport; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\Response; |
| 19 | +use Symfony\Component\HttpFoundation\StreamedResponse; |
| 20 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 21 | +use Symfony\Component\Uid\Uuid; |
| 22 | + |
| 23 | +final readonly class McpController |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private Server $server, |
| 27 | + private CachePoolStore $store, |
| 28 | + private UrlGeneratorInterface $urlGenerator, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + public function sse(): StreamedResponse |
| 33 | + { |
| 34 | + $id = Uuid::v4(); |
| 35 | + $endpoint = $this->urlGenerator->generate('_mcp_messages', ['id' => $id], UrlGeneratorInterface::ABSOLUTE_URL); |
| 36 | + $transport = new StreamTransport($endpoint, $this->store, $id); |
| 37 | + |
| 38 | + return new StreamedResponse(fn () => $this->server->connect($transport), headers: [ |
| 39 | + 'Content-Type' => 'text/event-stream', |
| 40 | + 'Cache-Control' => 'no-cache', |
| 41 | + 'X-Accel-Buffering' => 'no', |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + public function messages(Request $request, Uuid $id): Response |
| 46 | + { |
| 47 | + $this->store->push($id, $request->getContent()); |
| 48 | + |
| 49 | + return new Response(); |
| 50 | + } |
| 51 | +} |
0 commit comments