Skip to content

Commit ed42cb5

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

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: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,55 @@
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+
])
3434
->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)
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+
])
4548

4649
->set('mcp.message_factory', Factory::class)
50+
->args([])
4751
->set('mcp.server.json_rpc', JsonRpcHandler::class)
4852
->args([
49-
'$messageFactory' => service('mcp.message_factory'),
50-
'$requestHandlers' => tagged_iterator('mcp.server.request_handler'),
51-
'$notificationHandlers' => tagged_iterator('mcp.server.notification_handler'),
53+
service('mcp.message_factory'),
54+
tagged_iterator('mcp.server.request_handler'),
55+
tagged_iterator('mcp.server.notification_handler'),
56+
service('logger')->ignoreOnInvalid(),
5257
])
5358
->set('mcp.server', Server::class)
5459
->args([
55-
'$jsonRpcHandler' => service('mcp.server.json_rpc'),
60+
service('mcp.server.json_rpc'),
61+
service('logger')->ignoreOnInvalid(),
5662
])
5763
->alias(Server::class, 'mcp.server')
58-
->set(CachePoolStore::class)
64+
->set('mcp.server.sse.store.cache_pool', CachePoolStore::class)
65+
->args([
66+
service('cache.app'),
67+
])
5968
;
6069
};

src/mcp-bundle/src/McpBundle.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
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;
20+
use Symfony\Component\DependencyInjection\ChildDefinition;
1821
use Symfony\Component\DependencyInjection\ContainerBuilder;
1922
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
23+
use Symfony\Component\DependencyInjection\Reference;
2024
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
2125

2226
final class McpBundle extends AbstractBundle
@@ -50,21 +54,32 @@ private function configureClient(array $transports, ContainerBuilder $container)
5054
return;
5155
}
5256

57+
$container->registerForAutoconfiguration(NotificationHandlerInterface::class)
58+
->addTag('mcp.server.notification_handler');
59+
$container->registerForAutoconfiguration(RequestHandlerInterface::class)
60+
->addTag('mcp.server.request_handler');
61+
5362
if ($transports['stdio']) {
5463
$container->register('mcp.server.command', McpCommand::class)
55-
->setAutowired(true)
64+
->setArguments([
65+
new Reference('mcp.server'),
66+
])
5667
->addTag('console.command');
5768
}
5869

5970
if ($transports['sse']) {
6071
$container->register('mcp.server.controller', McpController::class)
61-
->setAutowired(true)
72+
->setArguments([
73+
new Reference('mcp.server'),
74+
new Reference('mcp.server.sse.store.cache_pool'),
75+
new Reference('router'),
76+
])
6277
->setPublic(true)
6378
->addTag('controller.service_arguments');
6479
}
6580

6681
$container->register('mcp.server.route_loader', RouteLoader::class)
67-
->setArgument('$sseTransportEnabled', $transports['sse'])
82+
->setArgument(0, $transports['sse'])
6883
->addTag('routing.route_loader');
6984
}
7085
}

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)