Skip to content

Commit 220575f

Browse files
Added Compiler Pass to build ToolChain
Address comments lint
1 parent f0a63bd commit 220575f

File tree

6 files changed

+71
-9
lines changed

6 files changed

+71
-9
lines changed

demo/config/services.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,3 @@ services:
1818
- '../src/DependencyInjection/'
1919
- '../src/Entity/'
2020
- '../src/Kernel.php'
21-
22-
mcp.tool_executor:
23-
class: Symfony\AI\McpSdk\Capability\ToolChain
24-
arguments:
25-
- ['@App\MCP\Tools\CurrentTimeTool']
26-
27-
mcp.tool_collection:
28-
alias: mcp.tool_executor

demo/src/MCP/Tools/CurrentTimeTool.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
use Symfony\AI\McpSdk\Capability\Tool\ToolCall;
1616
use Symfony\AI\McpSdk\Capability\Tool\ToolCallResult;
1717
use Symfony\AI\McpSdk\Capability\Tool\ToolExecutorInterface;
18+
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
1819

1920
/**
2021
* @author Tom Hart <[email protected]>
2122
*/
23+
#[AutoconfigureTag('mcp.tool')]
2224
class CurrentTimeTool implements MetadataInterface, ToolExecutorInterface
2325
{
2426
public function call(ToolCall $input): ToolCallResult

src/mcp-bundle/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CHANGELOG
22
=========
33

4+
45
0.1
56
---
67

@@ -17,4 +18,6 @@ CHANGELOG
1718
* Add `McpCommand` providing STDIO interface
1819
* Add bundle configuration for enabling/disabling transports
1920
* Add cache-based SSE message storage
20-
* Add service configuration for MCP server setup
21+
* Add service configuration for MCP server setup
22+
* Added `\Symfony\AI\McpBundle\DependencyInjection\ContainerBuilder\McpToolChainCompilerPass`
23+
to find services tagged `mcp.tool` and register them in the MCP tool chain.

src/mcp-bundle/doc/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ Configuration
6363
sse:
6464
url: 'http://localhost:8000/sse' # URL to SSE endpoint of MCP server
6565
66+
To add your tools to the MCP server, add the tag:
67+
68+
Example::
69+
70+
#[AutoconfigureTag('mcp.tool')]
71+
class CurrentTimeTool implements MetadataInterface, ToolExecutorInterface
72+
6673
.. _`Model Context Protocol`: https://modelcontextprotocol.io/
6774
.. _`symfony/mcp-sdk`: https://github.com/symfony/mcp-sdk
6875
.. _`Claude Desktop`: https://claude.ai/download
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Symfony package.
7+
*
8+
* (c) Fabien Potencier <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Symfony\AI\McpBundle\DependencyInjection\ContainerBuilder;
15+
16+
use Symfony\AI\McpSdk\Capability\Tool\CollectionInterface;
17+
use Symfony\AI\McpSdk\Capability\Tool\ToolExecutorInterface;
18+
use Symfony\AI\McpSdk\Capability\ToolChain;
19+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20+
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
21+
use Symfony\Component\DependencyInjection\ContainerBuilder;
22+
use Symfony\Component\DependencyInjection\Definition;
23+
24+
/**
25+
* @author Tom Hart <[email protected]>
26+
*/
27+
final class McpToolChainCompilerPass implements CompilerPassInterface
28+
{
29+
use PriorityTaggedServiceTrait;
30+
31+
public function process(ContainerBuilder $container): void
32+
{
33+
// If the user has already defined this service, don't override it.
34+
if ($container->hasDefinition(ToolExecutorInterface::class)) {
35+
return;
36+
}
37+
38+
$definition = new Definition(ToolChain::class);
39+
40+
$taggedServices = $this->findAndSortTaggedServices('mcp.tool', $container);
41+
42+
$definition->setArgument(0, $taggedServices);
43+
44+
$container->setDefinition(ToolExecutorInterface::class, $definition);
45+
46+
if (!$container->hasDefinition(CollectionInterface::class)) {
47+
$container->setAlias(CollectionInterface::class, ToolExecutorInterface::class);
48+
}
49+
}
50+
}

src/mcp-bundle/src/McpBundle.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\AI\McpBundle\Command\McpCommand;
1515
use Symfony\AI\McpBundle\Controller\McpController;
16+
use Symfony\AI\McpBundle\DependencyInjection\ContainerBuilder\McpToolChainCompilerPass;
1617
use Symfony\AI\McpBundle\Routing\RouteLoader;
1718
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -22,6 +23,13 @@
2223

2324
final class McpBundle extends AbstractBundle
2425
{
26+
public function build(ContainerBuilder $container): void
27+
{
28+
parent::build($container);
29+
30+
$container->addCompilerPass(new McpToolChainCompilerPass());
31+
}
32+
2533
public function configure(DefinitionConfigurator $definition): void
2634
{
2735
$definition->import('../config/options.php');

0 commit comments

Comments
 (0)