|
5 | 5 | namespace Mcp\Server\Defaults; |
6 | 6 |
|
7 | 7 | use Mcp\Server\Context; |
| 8 | +use Mcp\Server\Contracts\ReferenceProviderInterface; |
8 | 9 | use Mcp\Server\Contracts\ToolExecutorInterface; |
9 | | -use Mcp\Server\Elements\RegisteredTool; |
| 10 | +use Mcp\Server\Exception\ToolNotFoundException; |
| 11 | +use PhpMcp\Schema\Content\Content; |
| 12 | +use PhpMcp\Schema\Content\TextContent; |
10 | 13 | use Psr\Log\LoggerInterface; |
11 | 14 | use Psr\Log\NullLogger; |
12 | 15 |
|
13 | 16 | final readonly class ToolExecutor implements ToolExecutorInterface |
14 | 17 | { |
15 | 18 | public function __construct( |
| 19 | + private ReferenceProviderInterface $registry, |
16 | 20 | private LoggerInterface $logger = new NullLogger(), |
17 | 21 | ) {} |
18 | 22 |
|
19 | | - public function call( |
20 | | - RegisteredTool $registeredTool, |
21 | | - array $arguments, |
22 | | - Context $context, |
23 | | - ): array { |
| 23 | + public function call(string $toolName, array $arguments, Context $context): array |
| 24 | + { |
| 25 | + $tool = $this->registry->getTool($toolName); |
| 26 | + |
| 27 | + if (!$tool) { |
| 28 | + throw new ToolNotFoundException("Tool '{$toolName}' not found."); |
| 29 | + } |
| 30 | + |
24 | 31 | $this->logger->debug('Calling tool', [ |
25 | | - 'name' => $registeredTool->schema->name, |
| 32 | + 'name' => $tool->schema->name, |
26 | 33 | ]); |
27 | 34 |
|
28 | | - return $registeredTool->call($arguments, $context); |
| 35 | + $result = $tool->handler->handle($arguments, $context); |
| 36 | + |
| 37 | + return $this->formatResult($result); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Formats the result of a tool execution into an array of MCP Content items. |
| 42 | + * |
| 43 | + * - If the result is already a Content object, it's wrapped in an array. |
| 44 | + * - If the result is an array: |
| 45 | + * - If all elements are Content objects, the array is returned as is. |
| 46 | + * - If it's a mixed array (Content and non-Content items), non-Content items are |
| 47 | + * individually formatted (scalars to TextContent, others to JSON TextContent). |
| 48 | + * - If it's an array with no Content items, the entire array is JSON-encoded into a single TextContent. |
| 49 | + * - Scalars (string, int, float, bool) are wrapped in TextContent. |
| 50 | + * - null is represented as TextContent('(null)'). |
| 51 | + * - Other objects are JSON-encoded and wrapped in TextContent. |
| 52 | + * |
| 53 | + * @param mixed $toolExecutionResult The raw value returned by the tool's PHP method. |
| 54 | + * @return Content[] The content items for CallToolResult. |
| 55 | + * @throws \JsonException |
| 56 | + */ |
| 57 | + protected function formatResult(mixed $toolExecutionResult): array |
| 58 | + { |
| 59 | + if ($toolExecutionResult instanceof Content) { |
| 60 | + return [$toolExecutionResult]; |
| 61 | + } |
| 62 | + |
| 63 | + if (\is_array($toolExecutionResult)) { |
| 64 | + if (empty($toolExecutionResult)) { |
| 65 | + return [TextContent::make('[]')]; |
| 66 | + } |
| 67 | + |
| 68 | + $allAreContent = true; |
| 69 | + $hasContent = false; |
| 70 | + |
| 71 | + foreach ($toolExecutionResult as $item) { |
| 72 | + if ($item instanceof Content) { |
| 73 | + $hasContent = true; |
| 74 | + } else { |
| 75 | + $allAreContent = false; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if ($allAreContent && $hasContent) { |
| 80 | + return $toolExecutionResult; |
| 81 | + } |
| 82 | + |
| 83 | + if ($hasContent) { |
| 84 | + $result = []; |
| 85 | + foreach ($toolExecutionResult as $item) { |
| 86 | + if ($item instanceof Content) { |
| 87 | + $result[] = $item; |
| 88 | + } else { |
| 89 | + $result = \array_merge($result, $this->formatResult($item)); |
| 90 | + } |
| 91 | + } |
| 92 | + return $result; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if ($toolExecutionResult === null) { |
| 97 | + return [TextContent::make('(null)')]; |
| 98 | + } |
| 99 | + |
| 100 | + if (\is_bool($toolExecutionResult)) { |
| 101 | + return [TextContent::make($toolExecutionResult ? 'true' : 'false')]; |
| 102 | + } |
| 103 | + |
| 104 | + if (\is_scalar($toolExecutionResult)) { |
| 105 | + return [TextContent::make($toolExecutionResult)]; |
| 106 | + } |
| 107 | + |
| 108 | + $jsonResult = \json_encode( |
| 109 | + $toolExecutionResult, |
| 110 | + JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE, |
| 111 | + ); |
| 112 | + |
| 113 | + return [TextContent::make($jsonResult)]; |
29 | 114 | } |
30 | 115 | } |
0 commit comments