Skip to content

Commit f55b7dd

Browse files
committed
[MCP Bundle] autoconfigure request and notification handlers
1 parent 89c378f commit f55b7dd

File tree

4 files changed

+29
-41
lines changed

4 files changed

+29
-41
lines changed

src/mcp-bundle/src/DependencyInjection/McpPass.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ public function process(ContainerBuilder $container): void
2727
return;
2828
}
2929

30-
$definition = $container->getDefinition('mcp.server.builder');
31-
32-
$loaderReferences = $this->findAndSortTaggedServices('mcp.loader', $container);
33-
if ([] !== $loaderReferences) {
34-
$definition->addMethodCall('addLoaders', $loaderReferences);
35-
}
36-
3730
$allMcpServices = [];
3831
$mcpTags = ['mcp.tool', 'mcp.prompt', 'mcp.resource', 'mcp.resource_template'];
3932

@@ -52,6 +45,6 @@ public function process(ContainerBuilder $container): void
5245
}
5346

5447
$serviceLocatorRef = ServiceLocatorTagPass::register($container, $serviceReferences);
55-
$definition->addMethodCall('setContainer', [$serviceLocatorRef]);
48+
$container->getDefinition('mcp.server.builder')->addMethodCall('setContainer', [$serviceLocatorRef]);
5649
}
5750
}

src/mcp-bundle/src/McpBundle.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Mcp\Capability\Attribute\McpResourceTemplate;
1818
use Mcp\Capability\Attribute\McpTool;
1919
use Mcp\Capability\Registry\Loader\LoaderInterface;
20+
use Mcp\Server\Handler\Notification\NotificationHandlerInterface;
21+
use Mcp\Server\Handler\Request\RequestHandlerInterface;
2022
use Mcp\Server\Session\FileSessionStore;
2123
use Mcp\Server\Session\InMemorySessionStore;
2224
use Symfony\AI\McpBundle\Command\McpCommand;
@@ -62,6 +64,12 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
6264
$builder->registerForAutoconfiguration(LoaderInterface::class)
6365
->addTag('mcp.loader');
6466

67+
$builder->registerForAutoconfiguration(RequestHandlerInterface::class)
68+
->addTag('mcp.request_handler');
69+
70+
$builder->registerForAutoconfiguration(NotificationHandlerInterface::class)
71+
->addTag('mcp.notification_handler');
72+
6573
if ($builder->getParameter('kernel.debug')) {
6674
$traceableRegistry = (new Definition('mcp.traceable_registry'))
6775
->setClass(TraceableRegistry::class)

src/mcp-bundle/tests/DependencyInjection/McpBundleTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\AI\McpBundle\Tests\DependencyInjection;
1313

1414
use Mcp\Capability\Registry\Loader\LoaderInterface;
15+
use Mcp\Server\Handler\Notification\NotificationHandlerInterface;
16+
use Mcp\Server\Handler\Request\RequestHandlerInterface;
1517
use PHPUnit\Framework\Attributes\DataProvider;
1618
use PHPUnit\Framework\TestCase;
1719
use Symfony\AI\McpBundle\McpBundle;
@@ -359,6 +361,24 @@ public function testLoaderInterfaceAutoconfiguration()
359361
$this->assertTrue($definition->hasTag('mcp.loader'));
360362
}
361363

364+
public function testRequestHandlerInterfaceAutoconfiguration()
365+
{
366+
$container = $this->buildContainer([]);
367+
$autoconfigured = $container->getAutoconfiguredInstanceof();
368+
$this->assertArrayHasKey(RequestHandlerInterface::class, $autoconfigured);
369+
$definition = $autoconfigured[RequestHandlerInterface::class];
370+
$this->assertTrue($definition->hasTag('mcp.request_handler'));
371+
}
372+
373+
public function testNotificationHandlerInterfaceAutoconfiguration()
374+
{
375+
$container = $this->buildContainer([]);
376+
$autoconfigured = $container->getAutoconfiguredInstanceof();
377+
$this->assertArrayHasKey(NotificationHandlerInterface::class, $autoconfigured);
378+
$definition = $autoconfigured[NotificationHandlerInterface::class];
379+
$this->assertTrue($definition->hasTag('mcp.notification_handler'));
380+
}
381+
362382
private function buildContainer(array $configuration): ContainerBuilder
363383
{
364384
$container = new ContainerBuilder();

src/mcp-bundle/tests/DependencyInjection/McpPassTest.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -138,37 +138,4 @@ public function testHandlesPartialMcpServices()
138138
$this->assertInstanceOf(Reference::class, $services['tool_service']->getValues()[0]);
139139
$this->assertInstanceOf(Reference::class, $services['prompt_service']->getValues()[0]);
140140
}
141-
142-
public function testInjectsLoadersIntoBuilder()
143-
{
144-
$container = new ContainerBuilder();
145-
$container->setDefinition('mcp.server.builder', new Definition());
146-
147-
$container->setDefinition('loader_one', (new Definition())->addTag('mcp.loader'));
148-
$container->setDefinition('loader_two', (new Definition())->addTag('mcp.loader'));
149-
150-
$pass = new McpPass();
151-
$pass->process($container);
152-
153-
$builderDefinition = $container->getDefinition('mcp.server.builder');
154-
$methodCalls = $builderDefinition->getMethodCalls();
155-
156-
$addLoadersCall = null;
157-
foreach ($methodCalls as $call) {
158-
if ('addLoaders' === $call[0]) {
159-
$addLoadersCall = $call;
160-
break;
161-
}
162-
}
163-
164-
$this->assertNotNull($addLoadersCall, 'Builder should have addLoaders method call');
165-
166-
// Verify arguments are References
167-
$args = $addLoadersCall[1];
168-
$this->assertContainsOnlyInstancesOf(Reference::class, $args);
169-
170-
$ids = array_map(fn (Reference $ref) => (string) $ref, $args);
171-
$this->assertContains('loader_one', $ids);
172-
$this->assertContains('loader_two', $ids);
173-
}
174141
}

0 commit comments

Comments
 (0)