Skip to content

[McpBundle] Wire & configure services explicitly #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions demo/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ services:
- '../src/Entity/'
- '../src/Kernel.php'

Symfony\AI\McpSdk\Capability\Tool\ToolExecutorInterface:
mcp.tool_executor:
Copy link
Contributor Author

@valtzu valtzu Jul 21, 2025

Choose a reason for hiding this comment

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

Not quite sure about this.

I'm not familiar with this MCP bundle at all – is this really how this is supposed to work? Don't we have some autoconfiguration, or explicit config within the app is always needed? 🤔

class: Symfony\AI\McpSdk\Capability\ToolChain
arguments:
- ['@App\MCP\Tools\CurrentTimeTool']

Symfony\AI\McpSdk\Capability\Tool\CollectionInterface:
alias: Symfony\AI\McpSdk\Capability\Tool\ToolExecutorInterface
mcp.tool_collection:
alias: mcp.tool_executor
51 changes: 33 additions & 18 deletions src/mcp-bundle/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,51 @@

return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()
->autowire()
->autoconfigure()
->instanceof(NotificationHandlerInterface::class)
->tag('mcp.server.notification_handler')
->instanceof(RequestHandlerInterface::class)
->tag('mcp.server.request_handler')

->set(InitializedHandler::class)
->set(InitializeHandler::class)
->args([
'$name' => param('mcp.app'),
'$version' => param('mcp.version'),
])
->set(PingHandler::class)
->set(ToolCallHandler::class)
->set(ToolListHandler::class)
->set('mcp.server.notification_handler.initialized', InitializedHandler::class)
->args([])
->tag('mcp.server.notification_handler')
->set('mcp.server.request_handler.initialize', InitializeHandler::class)
->args([
param('mcp.app'),
param('mcp.version'),
])
->tag('mcp.server.request_handler')
->set('mcp.server.request_handler.ping', PingHandler::class)
->args([])
->tag('mcp.server.request_handler')
->set('mcp.server.request_handler.tool_call', ToolCallHandler::class)
->args([
service('mcp.tool_executor'),
])
->tag('mcp.server.request_handler')
->set('mcp.server.request_handler.tool_list', ToolListHandler::class)
->args([
service('mcp.tool_collection'),
20,
])

->set('mcp.message_factory', Factory::class)
->args([])
->set('mcp.server.json_rpc', JsonRpcHandler::class)
->args([
'$messageFactory' => service('mcp.message_factory'),
'$requestHandlers' => tagged_iterator('mcp.server.request_handler'),
'$notificationHandlers' => tagged_iterator('mcp.server.notification_handler'),
service('mcp.message_factory'),
tagged_iterator('mcp.server.request_handler'),
tagged_iterator('mcp.server.notification_handler'),
service('logger')->ignoreOnInvalid(),
])
->set('mcp.server', Server::class)
->args([
'$jsonRpcHandler' => service('mcp.server.json_rpc'),
service('mcp.server.json_rpc'),
service('logger')->ignoreOnInvalid(),
])
->alias(Server::class, 'mcp.server')
->set(CachePoolStore::class)
->set('mcp.server.sse.store.cache_pool', CachePoolStore::class)
->args([
service('cache.app'),
])
;
};
13 changes: 10 additions & 3 deletions src/mcp-bundle/src/McpBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

final class McpBundle extends AbstractBundle
Expand Down Expand Up @@ -52,19 +53,25 @@ private function configureClient(array $transports, ContainerBuilder $container)

if ($transports['stdio']) {
$container->register('mcp.server.command', McpCommand::class)
->setAutowired(true)
->setArguments([
new Reference('mcp.server'),
])
->addTag('console.command');
}

if ($transports['sse']) {
$container->register('mcp.server.controller', McpController::class)
->setAutowired(true)
->setArguments([
new Reference('mcp.server'),
new Reference('mcp.server.sse.store.cache_pool'),
new Reference('router'),
])
->setPublic(true)
->addTag('controller.service_arguments');
}

$container->register('mcp.server.route_loader', RouteLoader::class)
->setArgument('$sseTransportEnabled', $transports['sse'])
->setArgument(0, $transports['sse'])
->addTag('routing.route_loader');
}
}
3 changes: 2 additions & 1 deletion src/mcp-sdk/src/Server/JsonRpcHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\AI\McpSdk\Server;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\AI\McpSdk\Exception\ExceptionInterface;
use Symfony\AI\McpSdk\Exception\HandlerNotFoundException;
use Symfony\AI\McpSdk\Exception\InvalidInputMessageException;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function __construct(
private Factory $messageFactory,
iterable $requestHandlers,
iterable $notificationHandlers,
private LoggerInterface $logger,
private LoggerInterface $logger = new NullLogger(),
) {
$this->requestHandlers = $requestHandlers instanceof \Traversable ? iterator_to_array($requestHandlers) : $requestHandlers;
$this->notificationHandlers = $notificationHandlers instanceof \Traversable ? iterator_to_array($notificationHandlers) : $notificationHandlers;
Expand Down