Skip to content

Commit d6cb1e2

Browse files
committed
[McpBundle] Wire & configure services explicitly
1 parent d022fa1 commit d6cb1e2

File tree

4 files changed

+54
-29
lines changed

4 files changed

+54
-29
lines changed

demo/config/services.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ services:
1919
- '../src/Entity/'
2020
- '../src/Kernel.php'
2121

22-
Symfony\AI\McpSdk\Capability\Tool\ToolExecutorInterface:
22+
mcp.tool_executor:
2323
class: Symfony\AI\McpSdk\Capability\ToolChain
2424
arguments:
2525
- ['@App\MCP\Tools\CurrentTimeTool']
2626

27-
Symfony\AI\McpSdk\Capability\Tool\CollectionInterface:
28-
alias: Symfony\AI\McpSdk\Capability\Tool\ToolExecutorInterface
27+
mcp.tool_collection:
28+
alias: mcp.tool_executor

src/mcp-bundle/config/services.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,56 @@
1515
use Symfony\AI\McpSdk\Server;
1616
use Symfony\AI\McpSdk\Server\JsonRpcHandler;
1717
use Symfony\AI\McpSdk\Server\NotificationHandler\InitializedHandler;
18-
use Symfony\AI\McpSdk\Server\NotificationHandlerInterface;
1918
use Symfony\AI\McpSdk\Server\RequestHandler\InitializeHandler;
2019
use Symfony\AI\McpSdk\Server\RequestHandler\PingHandler;
2120
use Symfony\AI\McpSdk\Server\RequestHandler\ToolCallHandler;
2221
use Symfony\AI\McpSdk\Server\RequestHandler\ToolListHandler;
23-
use Symfony\AI\McpSdk\Server\RequestHandlerInterface;
2422
use Symfony\AI\McpSdk\Server\Transport\Sse\Store\CachePoolStore;
2523

2624
return static function (ContainerConfigurator $container): void {
2725
$container->services()
28-
->defaults()
29-
->autowire()
30-
->autoconfigure()
31-
->instanceof(NotificationHandlerInterface::class)
26+
->set('mcp.server.notification_handler.initialized', InitializedHandler::class)
27+
->args([])
3228
->tag('mcp.server.notification_handler')
33-
->instanceof(RequestHandlerInterface::class)
29+
->set('mcp.server.request_handler.initialize', InitializeHandler::class)
30+
->args([
31+
param('mcp.app'),
32+
param('mcp.version'),
33+
])
34+
->tag('mcp.server.request_handler')
35+
->set('mcp.server.request_handler.ping', PingHandler::class)
36+
->args([])
37+
->tag('mcp.server.request_handler')
38+
->set('mcp.server.request_handler.tool_call', ToolCallHandler::class)
39+
->args([
40+
service('mcp.tool_executor'),
41+
])
42+
->tag('mcp.server.request_handler')
43+
->set('mcp.server.request_handler.tool_list', ToolListHandler::class)
44+
->args([
45+
service('mcp.tool_collection'),
46+
20,
47+
])
3448
->tag('mcp.server.request_handler')
35-
36-
->set(InitializedHandler::class)
37-
->set(InitializeHandler::class)
38-
->args([
39-
'$name' => param('mcp.app'),
40-
'$version' => param('mcp.version'),
41-
])
42-
->set(PingHandler::class)
43-
->set(ToolCallHandler::class)
44-
->set(ToolListHandler::class)
4549

4650
->set('mcp.message_factory', Factory::class)
51+
->args([])
4752
->set('mcp.server.json_rpc', JsonRpcHandler::class)
4853
->args([
49-
'$messageFactory' => service('mcp.message_factory'),
50-
'$requestHandlers' => tagged_iterator('mcp.server.request_handler'),
51-
'$notificationHandlers' => tagged_iterator('mcp.server.notification_handler'),
54+
service('mcp.message_factory'),
55+
tagged_iterator('mcp.server.request_handler'),
56+
tagged_iterator('mcp.server.notification_handler'),
57+
service('logger')->ignoreOnInvalid(),
5258
])
5359
->set('mcp.server', Server::class)
5460
->args([
55-
'$jsonRpcHandler' => service('mcp.server.json_rpc'),
61+
service('mcp.server.json_rpc'),
62+
service('logger')->ignoreOnInvalid(),
5663
])
5764
->alias(Server::class, 'mcp.server')
58-
->set(CachePoolStore::class)
65+
->set('mcp.server.sse.store.cache_pool', CachePoolStore::class)
66+
->args([
67+
service('cache.app'),
68+
])
5969
;
6070
};

src/mcp-bundle/src/McpBundle.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
use Symfony\AI\McpBundle\Command\McpCommand;
1515
use Symfony\AI\McpBundle\Controller\McpController;
1616
use Symfony\AI\McpBundle\Routing\RouteLoader;
17+
use Symfony\AI\McpSdk\Server\NotificationHandlerInterface;
18+
use Symfony\AI\McpSdk\Server\RequestHandlerInterface;
1719
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
1820
use Symfony\Component\DependencyInjection\ContainerBuilder;
1921
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
22+
use Symfony\Component\DependencyInjection\Reference;
2023
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
2124

2225
final class McpBundle extends AbstractBundle
@@ -50,21 +53,32 @@ private function configureClient(array $transports, ContainerBuilder $container)
5053
return;
5154
}
5255

56+
$container->registerForAutoconfiguration(NotificationHandlerInterface::class)
57+
->addTag('mcp.server.notification_handler');
58+
$container->registerForAutoconfiguration(RequestHandlerInterface::class)
59+
->addTag('mcp.server.request_handler');
60+
5361
if ($transports['stdio']) {
5462
$container->register('mcp.server.command', McpCommand::class)
55-
->setAutowired(true)
63+
->setArguments([
64+
new Reference('mcp.server'),
65+
])
5666
->addTag('console.command');
5767
}
5868

5969
if ($transports['sse']) {
6070
$container->register('mcp.server.controller', McpController::class)
61-
->setAutowired(true)
71+
->setArguments([
72+
new Reference('mcp.server'),
73+
new Reference('mcp.server.sse.store.cache_pool'),
74+
new Reference('router'),
75+
])
6276
->setPublic(true)
6377
->addTag('controller.service_arguments');
6478
}
6579

6680
$container->register('mcp.server.route_loader', RouteLoader::class)
67-
->setArgument('$sseTransportEnabled', $transports['sse'])
81+
->setArgument(0, $transports['sse'])
6882
->addTag('routing.route_loader');
6983
}
7084
}

src/mcp-sdk/src/Server/JsonRpcHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\AI\McpSdk\Server;
1313

1414
use Psr\Log\LoggerInterface;
15+
use Psr\Log\NullLogger;
1516
use Symfony\AI\McpSdk\Exception\ExceptionInterface;
1617
use Symfony\AI\McpSdk\Exception\HandlerNotFoundException;
1718
use Symfony\AI\McpSdk\Exception\InvalidInputMessageException;
@@ -45,7 +46,7 @@ public function __construct(
4546
private Factory $messageFactory,
4647
iterable $requestHandlers,
4748
iterable $notificationHandlers,
48-
private LoggerInterface $logger,
49+
private LoggerInterface $logger = new NullLogger(),
4950
) {
5051
$this->requestHandlers = $requestHandlers instanceof \Traversable ? iterator_to_array($requestHandlers) : $requestHandlers;
5152
$this->notificationHandlers = $notificationHandlers instanceof \Traversable ? iterator_to_array($notificationHandlers) : $notificationHandlers;

0 commit comments

Comments
 (0)