My Symfony AI Agent Calls Its Own MCP Tools

While working on my personal garage software (the Garage App, my entry for the Symfony AI & MCP hackathon), I ended up with two kinds of agents calling the same tools. Claude Code, Claude Desktop or Goose connect to /mcp, and the app has its own agent behind /agent/turn, running in a Messenger worker. In the previous post I showed how the MCP server calls another API. This one shows how the in-app agent calls the app’s own MCP server, in-process, without HTTP or OAuth.
Why reuse the MCP tools#
Every tool of the Garage App is declared on an API Platform resource, next to its HTTP operations. For example the parts needed for a service:
#[ApiResource(
mcp: [
'get_service_parts' => new McpTool(
title: 'Service Parts',
description: 'Get everything needed for a maintenance service on a vehicle in one call: ...',
input: ServicePartsInput::class,
structuredContent: false,
processor: ServicePartsProcessor::class,
annotations: ['readOnlyHint' => true, 'openWorldHint' => true],
),
],
)]
final class ServiceParts
{
}
Full file: ServiceParts.php
The MCP server exposes the tool, API Platform does the work around it (JSON schema from ServicePartsInput, validation) and the result is handled by the processor, ServicePartsProcessor.
The in-app agent needs the same tools. With Symfony AI you add #[AsTool] on a method, but I didn’t want to write every tool twice, with two descriptions, two schemas and two validations that end up different. The description is also part of the prompt: when I improve it for Claude Code, the in-app agent should get it too.
I could just call /mcp over HTTP from the worker, but then the worker needs a token to call itself. Either the user’s token, which I removed from the queue in the previous post, or a service account. By doing this, the same PHP code would be called but it’d have a network roundtrip.
Thanks to Symfony’s MCP bundle, every server declared under mcp.servers is a service (mcp.server.garage, an Mcp\Server from the MCP PHP SDK). The /mcp route is only a Symfony controller that runs this service with an HTTP transport, and the mcp:server command runs the same service over stdio. So what if the agent’s toolbox ran it with a third transport?
An in-process transport#
The first version (5c5035c, June) used the 2025-03-26 revision of the spec, where a request needs a session opened by an initialize handshake. At that point it was hackish: each tool call sent two messages, initialize then the request, through a custom CapturingTransport, because the SDK’s InMemoryTransport discards the responses. The transport had to read them from the session queue, and delete the session afterwards so they don’t pile up.
In November (542fa5d) I moved to the 2026-07-28 revision, which is stateless: no initialize, each request carries its protocol version and client capabilities in _meta. The transport is now one request in, one response out:
final class InProcessTransport extends BaseTransport implements StatelessAwareTransportInterface
{
private ?StatelessProtocol $stateless = null;
public function __construct(private readonly array $request)
{
parent::__construct();
}
public function connectStateless(StatelessProtocol $protocol): void
{
$this->stateless = $protocol;
}
public function setHandshakeVersions(array $versions): void
{
}
public function send(string $data, array $context): void
{
throw new LogicException('The in-process transport speaks the modern era only; nothing reaches the handshake dispatcher.');
}
public function listen(): array
{
if (null === $this->stateless) {
throw new LogicException('The MCP server was built without the modern era (mcp.servers.*.protocol_versions); the in-process agent needs it.');
}
$result = $this->stateless->handleInline(json_encode($this->request, \JSON_THROW_ON_ERROR));
if ($result->isEmpty() || $result->isStream()) {
return [];
}
return json_decode($result->toJson(), true, 512, \JSON_THROW_ON_ERROR);
}
}
Full file: InProcessTransport.php
The transport only carries one JSON-RPC request to the server and brings the response back. Symfony AI doesn’t speak JSON-RPC though, it wants a toolbox.
From MCP to a Symfony AI toolbox#
McpToolbox implements Symfony AI’s ToolboxInterface and runs the same server as /mcp (mcp.server.garage). Every request goes through drive(), which adds the _meta:
protected function drive(array $request): array
{
$request['params']['_meta'] = [
RequestMeta::PROTOCOL_VERSION => ProtocolVersion::V2026_07_28->value,
RequestMeta::CLIENT_CAPABILITIES => new \stdClass(),
RequestMeta::CLIENT_INFO => ['name' => 'in-process-agent', 'version' => '0.0.0'],
];
return $this->server->run(new InProcessTransport($request));
}
getTools() sends a tools/list and turns each definition into a Symfony AI Tool, with the name, description and inputSchema given by the server:
$response = $this->drive(['jsonrpc' => '2.0', 'id' => 2, 'method' => 'tools/list', 'params' => []]);
foreach ($definitions as $definition) {
if (\in_array($definition['name'], self::EXCLUDED, true)) {
continue;
}
$tools[] = new Tool(
new ExecutionReference(self::class, 'execute'),
$definition['name'],
$definition['description'] ?? '',
$definition['inputSchema'] ?? null,
);
}
Full file: McpToolbox.php
And execute() sends a tools/call:
$response = $this->drive([
'jsonrpc' => '2.0',
'id' => 2,
'method' => 'tools/call',
'params' => [
'name' => $toolCall->getName(),
'arguments' => (object) $toolCall->getArguments(),
],
]);
The agent keeps a few #[AsTool] tools of its own, the ones that drive the dashboard (more on them below). It is configured in ai.yaml with tools: true, which gives it a toolbox with these tools. To merge it with McpToolbox, Symfony AI has a ChainToolbox that sends each call to the toolbox that listed the tool:
App\Mcp\Agent\McpToolbox:
arguments:
$server: '@mcp.server.garage'
app.toolbox.garage:
class: Symfony\AI\Agent\Toolbox\ChainToolbox
decorates: ai.toolbox.garage
arguments:
$toolboxes: ['@.inner', '@App\Mcp\Agent\McpToolbox']
The Garage App uses its own CompositeToolbox instead, because it also narrates each step on the dashboard.
What about OAuth? The OAuth on /mcp is there for external clients. The worker only runs turns that /agent/turn already authenticated, and every garage read or write is still scoped to the owner of the turn.
Excluding some tools#
The Garage App also exposes render tools (select_vehicle, toast, show_fact, focus_view…) so that Claude Code can drive the 3D dashboard. They publish to the owner’s Mercure topic. The in-app agent has its own versions as #[AsTool] in RenderIntentTools, which publish to the browser tab that asked. If the agent saw both, it would get two tools with the same name, and ChainToolbox refuses that. The MCP versions would also send the updates to the wrong topic. To fix this we declare them in McpToolbox::EXCLUDED:
private const EXCLUDED = [
'select_vehicle',
'toast',
'show_fact',
// ...
];
getTools() skips them when it reads the tools/list response.
Gotchas#
Return errors as tool results#
When a tool fails, the model must be able to read why. The Garage App follows the error handling section of the spec: every failure is a result with isError: true and a <status>: <what to do> text, never a JSON-RPC error. A JSON-RPC error tells the model nothing useful: Symfony AI turns it into a generic An error occurred while executing tool "get_service_parts"., so a 401 from upstream and an unknown plate look the same. McpToolbox passes the result as is:
if ($result['isError'] ?? false) {
return new ToolResult($toolCall, CallToolResult::fromArray($result));
}
So when the parts catalogue (bonneref.fr) rejects a request, the agent gets: upstream_rejected: the bonneref catalog rejected the request. Retrying will not help; check the arguments. And RepeatedToolFailureGuard stops the turn when the model keeps retrying the same failing call.
This is why sharing the server is worth it: errors are handled once, in the server, and Claude Code and the in-app agent read the same thing.
Don’t cache an empty tool list#
Messenger workers run for a long time, and McpToolbox caches the tool list. If tools/list fails the list is empty, and caching [] leaves the worker without MCP tools until it restarts. So getTools() only caches a non-empty list:
if ([] !== $tools) {
$this->tools = $tools;
}
Our CompositeToolbox follows the same rule for the tool names it remembers.
The Garage App always has tools, so an empty list is an error. Not caching it lets the next turn try again.
Conclusion#
If your app already has an MCP server, make its agent one more client of it, in-process. You write each tool once, and it fails the same way for everyone.
Symfony AI already lets an agent use an MCP server as a toolbox (tools: [{mcp_server: ...}]), next to the #[AsTool] tools, but the MCP bundle’s client only speaks stdio and HTTP. An in-process transport to a server declared in the same app would replace most of the code above, I’ll submit a patch for this soon.