|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Tests\DependencyInjection; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\AI\McpBundle\McpBundle; |
| 18 | +use Symfony\AI\McpSdk\Capability\Tool\IdentifierInterface; |
| 19 | +use Symfony\AI\McpSdk\Server\NotificationHandlerInterface; |
| 20 | +use Symfony\AI\McpSdk\Server\RequestHandlerInterface; |
| 21 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 22 | + |
| 23 | +#[CoversClass(McpBundle::class)] |
| 24 | +class McpBundleTest extends TestCase |
| 25 | +{ |
| 26 | + public function testDefaultConfiguration() |
| 27 | + { |
| 28 | + $container = $this->buildContainer([]); |
| 29 | + |
| 30 | + $this->assertSame('app', $container->getParameter('mcp.app')); |
| 31 | + $this->assertSame('0.0.1', $container->getParameter('mcp.version')); |
| 32 | + } |
| 33 | + |
| 34 | + public function testCustomConfiguration() |
| 35 | + { |
| 36 | + $container = $this->buildContainer([ |
| 37 | + 'mcp' => [ |
| 38 | + 'app' => 'my-mcp-app', |
| 39 | + 'version' => '1.2.3', |
| 40 | + ], |
| 41 | + ]); |
| 42 | + |
| 43 | + $this->assertSame('my-mcp-app', $container->getParameter('mcp.app')); |
| 44 | + $this->assertSame('1.2.3', $container->getParameter('mcp.version')); |
| 45 | + } |
| 46 | + |
| 47 | + #[DataProvider('provideClientTransportsConfiguration')] |
| 48 | + public function testClientTransportsConfiguration(array $config, array $expectedServices) |
| 49 | + { |
| 50 | + $container = $this->buildContainer([ |
| 51 | + 'mcp' => [ |
| 52 | + 'client_transports' => $config, |
| 53 | + ], |
| 54 | + ]); |
| 55 | + |
| 56 | + foreach ($expectedServices as $serviceId => $shouldExist) { |
| 57 | + if ($shouldExist) { |
| 58 | + $this->assertTrue($container->hasDefinition($serviceId), \sprintf('Service "%s" should exist', $serviceId)); |
| 59 | + } else { |
| 60 | + $this->assertFalse($container->hasDefinition($serviceId), \sprintf('Service "%s" should not exist', $serviceId)); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static function provideClientTransportsConfiguration(): iterable |
| 66 | + { |
| 67 | + yield 'no transports enabled' => [ |
| 68 | + 'config' => [ |
| 69 | + 'stdio' => false, |
| 70 | + 'sse' => false, |
| 71 | + ], |
| 72 | + 'expectedServices' => [ |
| 73 | + 'mcp.server.command' => false, |
| 74 | + 'mcp.server.controller' => false, |
| 75 | + 'mcp.server.route_loader' => false, |
| 76 | + ], |
| 77 | + ]; |
| 78 | + |
| 79 | + yield 'stdio transport enabled' => [ |
| 80 | + 'config' => [ |
| 81 | + 'stdio' => true, |
| 82 | + 'sse' => false, |
| 83 | + ], |
| 84 | + 'expectedServices' => [ |
| 85 | + 'mcp.server.command' => true, |
| 86 | + 'mcp.server.controller' => false, |
| 87 | + 'mcp.server.route_loader' => true, |
| 88 | + ], |
| 89 | + ]; |
| 90 | + |
| 91 | + yield 'sse transport enabled' => [ |
| 92 | + 'config' => [ |
| 93 | + 'stdio' => false, |
| 94 | + 'sse' => true, |
| 95 | + ], |
| 96 | + 'expectedServices' => [ |
| 97 | + 'mcp.server.command' => false, |
| 98 | + 'mcp.server.controller' => true, |
| 99 | + 'mcp.server.route_loader' => true, |
| 100 | + ], |
| 101 | + ]; |
| 102 | + |
| 103 | + yield 'both transports enabled' => [ |
| 104 | + 'config' => [ |
| 105 | + 'stdio' => true, |
| 106 | + 'sse' => true, |
| 107 | + ], |
| 108 | + 'expectedServices' => [ |
| 109 | + 'mcp.server.command' => true, |
| 110 | + 'mcp.server.controller' => true, |
| 111 | + 'mcp.server.route_loader' => true, |
| 112 | + ], |
| 113 | + ]; |
| 114 | + } |
| 115 | + |
| 116 | + public function testToolAutoconfiguration() |
| 117 | + { |
| 118 | + $container = $this->buildContainer([]); |
| 119 | + |
| 120 | + $autoconfiguredInstances = $container->getAutoconfiguredInstanceof(); |
| 121 | + |
| 122 | + $this->assertArrayHasKey(IdentifierInterface::class, $autoconfiguredInstances); |
| 123 | + $this->assertArrayHasKey('mcp.tool', $autoconfiguredInstances[IdentifierInterface::class]->getTags()); |
| 124 | + } |
| 125 | + |
| 126 | + public function testServerAutoconfigurations() |
| 127 | + { |
| 128 | + $container = $this->buildContainer([ |
| 129 | + 'mcp' => [ |
| 130 | + 'client_transports' => [ |
| 131 | + 'stdio' => true, |
| 132 | + 'sse' => true, |
| 133 | + ], |
| 134 | + ], |
| 135 | + ]); |
| 136 | + |
| 137 | + $autoconfiguredInstances = $container->getAutoconfiguredInstanceof(); |
| 138 | + |
| 139 | + $this->assertArrayHasKey(NotificationHandlerInterface::class, $autoconfiguredInstances); |
| 140 | + $this->assertArrayHasKey(RequestHandlerInterface::class, $autoconfiguredInstances); |
| 141 | + |
| 142 | + $this->assertArrayHasKey('mcp.server.notification_handler', $autoconfiguredInstances[NotificationHandlerInterface::class]->getTags()); |
| 143 | + $this->assertArrayHasKey('mcp.server.request_handler', $autoconfiguredInstances[RequestHandlerInterface::class]->getTags()); |
| 144 | + } |
| 145 | + |
| 146 | + private function buildContainer(array $configuration): ContainerBuilder |
| 147 | + { |
| 148 | + $container = new ContainerBuilder(); |
| 149 | + $container->setParameter('kernel.debug', true); |
| 150 | + $container->setParameter('kernel.environment', 'test'); |
| 151 | + $container->setParameter('kernel.build_dir', 'public'); |
| 152 | + |
| 153 | + $extension = (new McpBundle())->getContainerExtension(); |
| 154 | + $extension->load($configuration, $container); |
| 155 | + |
| 156 | + return $container; |
| 157 | + } |
| 158 | +} |
0 commit comments