This repository was archived by the owner on Nov 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathToolsCallMethodHandler.php
More file actions
157 lines (131 loc) · 5.77 KB
/
Copy pathToolsCallMethodHandler.php
File metadata and controls
157 lines (131 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace Ecourty\McpServerBundle\MethodHandler;
use Ecourty\McpServerBundle\Attribute\AsMethodHandler;
use Ecourty\McpServerBundle\Contract\MethodHandlerInterface;
use Ecourty\McpServerBundle\Event\ToolCallEvent;
use Ecourty\McpServerBundle\Event\ToolCallExceptionEvent;
use Ecourty\McpServerBundle\Event\ToolResultEvent;
use Ecourty\McpServerBundle\Exception\ToolCallException;
use Ecourty\McpServerBundle\Exception\ToolNotFoundException;
use Ecourty\McpServerBundle\HttpFoundation\JsonRpcRequest;
use Ecourty\McpServerBundle\IO\ToolResult;
use Ecourty\McpServerBundle\Service\InputSanitizer;
use Ecourty\McpServerBundle\Service\ToolRegistry;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Handles the 'tools/call' method in the MCP server.
*
* This method is used to invoke a tool with the specified name and arguments.
* It validates the input against the tool's input schema and returns the tool's response.
*
* @see https://modelcontextprotocol.io/specification/2025-03-26/server/tools#calling-tools
*/
#[AsMethodHandler(methodName: 'tools/call')]
class ToolsCallMethodHandler implements MethodHandlerInterface
{
public function __construct(
private readonly ToolRegistry $toolRegistry,
private readonly SerializerInterface $serializer,
private readonly ValidatorInterface $validator,
private readonly InputSanitizer $inputSanitizer,
private readonly ?EventDispatcherInterface $eventDispatcher = null,
) {
}
public function handle(JsonRpcRequest $request): array
{
$toolName = $request->params['name'] ?? null;
if ($toolName === null) {
throw new \InvalidArgumentException('Tool name is required.');
}
$tool = $this->toolRegistry->getTool($toolName);
$toolDefinition = $this->toolRegistry->getToolDefinition($toolName);
if ($tool === null) {
throw new ToolNotFoundException(\sprintf('Tool "%s" not found.', $request->params['name']));
}
if ($toolDefinition === null) {
throw new \InvalidArgumentException(\sprintf(
'Tool "%s" does not have a definition.',
$toolName,
));
}
try {
$arguments = $request->params['arguments'] ?? [];
$sanitizedInput = $this->inputSanitizer->sanitize($arguments);
$jsonPayload = json_encode($sanitizedInput, \JSON_THROW_ON_ERROR);
$inputSchemaClass = $toolDefinition->inputSchemaClass;
$inputModel = $inputSchemaClass === null
? null
: $this->serializer->deserialize(
data: $jsonPayload,
type: $inputSchemaClass,
format: 'json',
);
if ($inputSchemaClass !== null && $inputModel instanceof $inputSchemaClass === false) {
throw new \InvalidArgumentException(\sprintf(
'Deserialized result is not an instance of "%s".',
$inputSchemaClass,
));
}
$violations = $inputModel === null
? new ConstraintViolationList()
: $this->validator->validate($inputModel);
if ($violations->count() > 0) {
return $this->buildValidationErrorResponse(iterator_to_array($violations));
}
if (method_exists($tool, '__invoke') === false) {
throw new \LogicException(\sprintf('Tool "%s" does not implement __invoke method.', $toolName));
}
if ($this->eventDispatcher !== null) {
$event = new ToolCallEvent(toolName: $toolName, payload: $inputModel);
$this->eventDispatcher->dispatch($event);
}
$toolResult = $inputModel === null
? $tool->__invoke()
: $tool->__invoke($inputModel);
if ($toolResult instanceof ToolResult === false) {
throw new \LogicException(\sprintf(
'Tool "%s" did not return an instance of %s.',
$toolName,
ToolResult::class,
));
}
if ($this->eventDispatcher !== null) {
$event = new ToolResultEvent(toolName: $toolName, payload: $inputModel, result: $toolResult);
$this->eventDispatcher->dispatch($event);
}
} catch (\Throwable $exception) {
if ($this->eventDispatcher !== null) {
$event = new ToolCallExceptionEvent(toolName: $toolName, payload: $inputModel ?? null, exception: $exception);
$this->eventDispatcher->dispatch($event);
}
throw new ToolCallException(
message: 'An error occurred while handling a tool call.',
code: Response::HTTP_INTERNAL_SERVER_ERROR,
previous: $exception,
);
}
return $toolResult->toArray();
}
private function buildValidationErrorResponse(array $errors): array
{
return [
'isError' => true,
'content' => array_map(function (ConstraintViolationInterface $violation) {
return [
'type' => 'text',
'text' => \sprintf(
'Validation error: %s - %s',
$violation->getPropertyPath(),
$violation->getMessage(),
),
];
}, $errors),
];
}
}