From b1c6e93a83435dbec6e6122569589f638f08d8f7 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Wed, 22 Oct 2025 15:16:03 +0200 Subject: [PATCH] [MCP Bundle] Add configurable tool discovery options --- src/mcp-bundle/config/options.php | 13 +++++ src/mcp-bundle/config/services.php | 2 +- src/mcp-bundle/src/McpBundle.php | 2 + .../DependencyInjection/McpBundleTest.php | 53 +++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/mcp-bundle/config/options.php b/src/mcp-bundle/config/options.php index 6dea5511b..223f1ef36 100644 --- a/src/mcp-bundle/config/options.php +++ b/src/mcp-bundle/config/options.php @@ -24,6 +24,19 @@ ->booleanNode('http')->defaultFalse()->end() ->end() ->end() + ->arrayNode('discovery') + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('scan_dirs') + ->scalarPrototype()->end() + ->defaultValue(['src']) + ->end() + ->arrayNode('exclude_dirs') + ->scalarPrototype()->end() + ->defaultValue([]) + ->end() + ->end() + ->end() ->arrayNode('http') ->addDefaultsIfNotSet() ->children() diff --git a/src/mcp-bundle/config/services.php b/src/mcp-bundle/config/services.php index 3da39d99d..9f836a81b 100644 --- a/src/mcp-bundle/config/services.php +++ b/src/mcp-bundle/config/services.php @@ -29,7 +29,7 @@ ->call('setLogger', [service('monolog.logger.mcp')]) ->call('setEventDispatcher', [service('event_dispatcher')]) ->call('setSession', [service('mcp.session.store')]) - ->call('setDiscovery', [param('kernel.project_dir'), ['src']]) + ->call('setDiscovery', [param('kernel.project_dir'), param('mcp.discovery.scan_dirs'), param('mcp.discovery.exclude_dirs')]) ->set('mcp.server', Server::class) ->factory([service('mcp.server.builder'), 'build']) diff --git a/src/mcp-bundle/src/McpBundle.php b/src/mcp-bundle/src/McpBundle.php index 0359b79d5..81e140c0e 100644 --- a/src/mcp-bundle/src/McpBundle.php +++ b/src/mcp-bundle/src/McpBundle.php @@ -50,6 +50,8 @@ public function loadExtension(array $config, ContainerConfigurator $container, C $builder->setParameter('mcp.version', $config['version']); $builder->setParameter('mcp.pagination_limit', $config['pagination_limit']); $builder->setParameter('mcp.instructions', $config['instructions']); + $builder->setParameter('mcp.discovery.scan_dirs', $config['discovery']['scan_dirs']); + $builder->setParameter('mcp.discovery.exclude_dirs', $config['discovery']['exclude_dirs']); $this->registerMcpAttributes($builder); diff --git a/src/mcp-bundle/tests/DependencyInjection/McpBundleTest.php b/src/mcp-bundle/tests/DependencyInjection/McpBundleTest.php index d6f162ea5..976811ba4 100644 --- a/src/mcp-bundle/tests/DependencyInjection/McpBundleTest.php +++ b/src/mcp-bundle/tests/DependencyInjection/McpBundleTest.php @@ -26,6 +26,8 @@ public function testDefaultConfiguration() $this->assertSame('0.0.1', $container->getParameter('mcp.version')); $this->assertSame(50, $container->getParameter('mcp.pagination_limit')); $this->assertNull($container->getParameter('mcp.instructions')); + $this->assertSame(['src'], $container->getParameter('mcp.discovery.scan_dirs')); + $this->assertSame([], $container->getParameter('mcp.discovery.exclude_dirs')); } public function testCustomConfiguration() @@ -296,6 +298,57 @@ public function testSessionStoreFileConfiguration() $this->assertSame(1800, $arguments[1]); // Custom TTL } + public function testDiscoveryDefaultConfiguration() + { + $container = $this->buildContainer([]); + + $this->assertSame(['src'], $container->getParameter('mcp.discovery.scan_dirs')); + $this->assertSame([], $container->getParameter('mcp.discovery.exclude_dirs')); + + // Verify the builder service uses the correct parameters + $builderDefinition = $container->getDefinition('mcp.server.builder'); + $methodCalls = $builderDefinition->getMethodCalls(); + + $setDiscoveryCall = null; + foreach ($methodCalls as $call) { + if ('setDiscovery' === $call[0]) { + $setDiscoveryCall = $call; + break; + } + } + + $this->assertNotNull($setDiscoveryCall, 'ServerBuilder should have setDiscovery method call'); + } + + public function testDiscoveryCustomConfiguration() + { + $container = $this->buildContainer([ + 'mcp' => [ + 'discovery' => [ + 'scan_dirs' => ['src', 'lib', 'modules'], + 'exclude_dirs' => ['src/DataFixtures', 'tests'], + ], + ], + ]); + + $this->assertSame(['src', 'lib', 'modules'], $container->getParameter('mcp.discovery.scan_dirs')); + $this->assertSame(['src/DataFixtures', 'tests'], $container->getParameter('mcp.discovery.exclude_dirs')); + } + + public function testDiscoveryWithExcludeDirsOnly() + { + $container = $this->buildContainer([ + 'mcp' => [ + 'discovery' => [ + 'exclude_dirs' => ['src/DataFixtures'], + ], + ], + ]); + + $this->assertSame(['src'], $container->getParameter('mcp.discovery.scan_dirs')); + $this->assertSame(['src/DataFixtures'], $container->getParameter('mcp.discovery.exclude_dirs')); + } + private function buildContainer(array $configuration): ContainerBuilder { $container = new ContainerBuilder();