Skip to content

Commit b1c22fe

Browse files
Added Compiler Pass to build ToolChain
1 parent d022fa1 commit b1c22fe

File tree

6 files changed

+90
-10
lines changed

6 files changed

+90
-10
lines changed

demo/composer.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55
"license": "MIT",
66
"minimum-stability": "dev",
77
"prefer-stable": true,
8+
"repositories": [
9+
{
10+
"type": "path",
11+
"url": "../src/mcp-bundle"
12+
},
13+
{
14+
"type": "path",
15+
"url": "../src/ai-bundle"
16+
},
17+
{
18+
"type": "path",
19+
"url": "../src/mcp-sdk"
20+
}
21+
],
822
"require": {
923
"php": ">=8.4",
1024
"ext-ctype": "*",
@@ -14,7 +28,7 @@
1428
"mrmysql/youtube-transcript": "^0.0.5",
1529
"php-http/discovery": "^1.20",
1630
"runtime/frankenphp-symfony": "^0.2.0",
17-
"symfony/ai-bundle": "@dev",
31+
"symfony/ai-bundle": "dev-main",
1832
"symfony/asset": "7.3.*",
1933
"symfony/asset-mapper": "7.3.*",
2034
"symfony/clock": "7.3.*",
@@ -25,7 +39,8 @@
2539
"symfony/flex": "^2.5",
2640
"symfony/framework-bundle": "7.3.*",
2741
"symfony/http-client": "7.3.*",
28-
"symfony/mcp-bundle": "@dev",
42+
"symfony/mcp-sdk": "dev-main",
43+
"symfony/mcp-bundle": "dev-main",
2944
"symfony/monolog-bundle": "^3.10",
3045
"symfony/runtime": "7.3.*",
3146
"symfony/twig-bundle": "7.3.*",

demo/config/services.yaml

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

22-
Symfony\AI\McpSdk\Capability\Tool\ToolExecutorInterface:
23-
class: Symfony\AI\McpSdk\Capability\ToolChain
24-
arguments:
25-
- ['@App\MCP\Tools\CurrentTimeTool']
26-
27-
Symfony\AI\McpSdk\Capability\Tool\CollectionInterface:
28-
alias: Symfony\AI\McpSdk\Capability\Tool\ToolExecutorInterface
22+
App\MCP\Tools\CurrentTimeTool:
23+
tags:
24+
- { name: 'mcp_tool' }

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ 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+
.. code-block:: yaml
69+
70+
App\MCP\Tools\MyCustomTool:
71+
tags:
72+
- { name: 'mcp_tool' }
73+
6674
.. _`Model Context Protocol`: https://modelcontextprotocol.io/
6775
.. _`symfony/mcp-sdk`: https://github.com/symfony/mcp-sdk
6876
.. _`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+
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;
@@ -21,6 +22,13 @@
2122

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

0 commit comments

Comments
 (0)