Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/mcp-bundle/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
->call('setEventDispatcher', [service('event_dispatcher')])
->call('setRegistry', [service('mcp.registry')])
->call('setSession', [service('mcp.session.store')])
->call('addRequestHandlers', [tagged_iterator('mcp.request_handler')])
->call('addNotificationHandlers', [tagged_iterator('mcp.notification_handler')])
->call('addLoaders', [tagged_iterator('mcp.loader')])
->call('setDiscovery', [param('kernel.project_dir'), param('mcp.discovery.scan_dirs'), param('mcp.discovery.exclude_dirs')])

->set('mcp.server', Server::class)
Expand Down
9 changes: 1 addition & 8 deletions src/mcp-bundle/src/DependencyInjection/McpPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ public function process(ContainerBuilder $container): void
return;
}

$definition = $container->getDefinition('mcp.server.builder');

$loaderReferences = $this->findAndSortTaggedServices('mcp.loader', $container);
if ([] !== $loaderReferences) {
$definition->addMethodCall('addLoaders', $loaderReferences);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted since modelcontextprotocol/php-sdk#183 makes things easier

$allMcpServices = [];
$mcpTags = ['mcp.tool', 'mcp.prompt', 'mcp.resource', 'mcp.resource_template'];

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

$serviceLocatorRef = ServiceLocatorTagPass::register($container, $serviceReferences);
$definition->addMethodCall('setContainer', [$serviceLocatorRef]);
$container->getDefinition('mcp.server.builder')->addMethodCall('setContainer', [$serviceLocatorRef]);
}
}
8 changes: 8 additions & 0 deletions src/mcp-bundle/src/McpBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Mcp\Capability\Attribute\McpResourceTemplate;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Capability\Registry\Loader\LoaderInterface;
use Mcp\Server\Handler\Notification\NotificationHandlerInterface;
use Mcp\Server\Handler\Request\RequestHandlerInterface;
use Mcp\Server\Session\FileSessionStore;
use Mcp\Server\Session\InMemorySessionStore;
use Symfony\AI\McpBundle\Command\McpCommand;
Expand Down Expand Up @@ -62,6 +64,12 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
$builder->registerForAutoconfiguration(LoaderInterface::class)
->addTag('mcp.loader');

$builder->registerForAutoconfiguration(RequestHandlerInterface::class)
->addTag('mcp.request_handler');

$builder->registerForAutoconfiguration(NotificationHandlerInterface::class)
->addTag('mcp.notification_handler');

if ($builder->getParameter('kernel.debug')) {
$traceableRegistry = (new Definition('mcp.traceable_registry'))
->setClass(TraceableRegistry::class)
Expand Down
60 changes: 59 additions & 1 deletion src/mcp-bundle/tests/DependencyInjection/McpBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
namespace Symfony\AI\McpBundle\Tests\DependencyInjection;

use Mcp\Capability\Registry\Loader\LoaderInterface;
use Mcp\Server\Handler\Notification\NotificationHandlerInterface;
use Mcp\Server\Handler\Request\RequestHandlerInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\AI\McpBundle\McpBundle;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class McpBundleTest extends TestCase
Expand Down Expand Up @@ -150,13 +153,50 @@ public function testServerServices()
$methodCalls = $builderDefinition->getMethodCalls();

$hasEventDispatcherCall = false;
$hasRequestHandlers = false;
$hasNotificationHandlers = false;
$hasLoaders = false;

foreach ($methodCalls as $call) {
if ('setEventDispatcher' === $call[0]) {
$hasEventDispatcherCall = true;
break;
}

if ('addRequestHandlers' === $call[0]) {
$argument = $call[1][0];
if (
$argument instanceof TaggedIteratorArgument
&& 'mcp.request_handler' === $argument->getTag()
) {
$hasRequestHandlers = true;
}
}

if ('addNotificationHandlers' === $call[0]) {
$argument = $call[1][0];
if (
$argument instanceof TaggedIteratorArgument
&& 'mcp.notification_handler' === $argument->getTag()
) {
$hasNotificationHandlers = true;
}
}

if ('addLoaders' === $call[0]) {
$argument = $call[1][0];
if (
$argument instanceof TaggedIteratorArgument
&& 'mcp.loader' === $argument->getTag()
) {
$hasLoaders = true;
}
}
}

$this->assertTrue($hasEventDispatcherCall, 'ServerBuilder should have setEventDispatcher method call');
$this->assertTrue($hasRequestHandlers, 'ServerBuilder should have addRequestHandlers with mcp.request_handler tag');
$this->assertTrue($hasNotificationHandlers, 'ServerBuilder should have addNotificationHandlers with mcp.notification_handler tag');
$this->assertTrue($hasLoaders, 'ServerBuilder should have addLoaders with mcp.loader tag');
}

public function testMcpToolAttributeAutoconfiguration()
Expand Down Expand Up @@ -359,6 +399,24 @@ public function testLoaderInterfaceAutoconfiguration()
$this->assertTrue($definition->hasTag('mcp.loader'));
}

public function testRequestHandlerInterfaceAutoconfiguration()
{
$container = $this->buildContainer([]);
$autoconfigured = $container->getAutoconfiguredInstanceof();
$this->assertArrayHasKey(RequestHandlerInterface::class, $autoconfigured);
$definition = $autoconfigured[RequestHandlerInterface::class];
$this->assertTrue($definition->hasTag('mcp.request_handler'));
}

public function testNotificationHandlerInterfaceAutoconfiguration()
{
$container = $this->buildContainer([]);
$autoconfigured = $container->getAutoconfiguredInstanceof();
$this->assertArrayHasKey(NotificationHandlerInterface::class, $autoconfigured);
$definition = $autoconfigured[NotificationHandlerInterface::class];
$this->assertTrue($definition->hasTag('mcp.notification_handler'));
}

private function buildContainer(array $configuration): ContainerBuilder
{
$container = new ContainerBuilder();
Expand Down
33 changes: 0 additions & 33 deletions src/mcp-bundle/tests/DependencyInjection/McpPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,37 +138,4 @@ public function testHandlesPartialMcpServices()
$this->assertInstanceOf(Reference::class, $services['tool_service']->getValues()[0]);
$this->assertInstanceOf(Reference::class, $services['prompt_service']->getValues()[0]);
}

public function testInjectsLoadersIntoBuilder()
{
$container = new ContainerBuilder();
$container->setDefinition('mcp.server.builder', new Definition());

$container->setDefinition('loader_one', (new Definition())->addTag('mcp.loader'));
$container->setDefinition('loader_two', (new Definition())->addTag('mcp.loader'));

$pass = new McpPass();
$pass->process($container);

$builderDefinition = $container->getDefinition('mcp.server.builder');
$methodCalls = $builderDefinition->getMethodCalls();

$addLoadersCall = null;
foreach ($methodCalls as $call) {
if ('addLoaders' === $call[0]) {
$addLoadersCall = $call;
break;
}
}

$this->assertNotNull($addLoadersCall, 'Builder should have addLoaders method call');

// Verify arguments are References
$args = $addLoadersCall[1];
$this->assertContainsOnlyInstancesOf(Reference::class, $args);

$ids = array_map(fn (Reference $ref) => (string) $ref, $args);
$this->assertContains('loader_one', $ids);
$this->assertContains('loader_two', $ids);
}
}