forked from php-mcp/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegisteredTool.php
More file actions
141 lines (120 loc) · 4.4 KB
/
RegisteredTool.php
File metadata and controls
141 lines (120 loc) · 4.4 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
<?php
declare(strict_types=1);
namespace PhpMcp\Server\Elements;
use PhpMcp\Schema\Content\Content;
use PhpMcp\Schema\Content\TextContent;
use PhpMcp\Server\Context;
use Psr\Container\ContainerInterface;
use PhpMcp\Schema\Tool;
use Throwable;
class RegisteredTool extends RegisteredElement
{
public function __construct(
public readonly Tool $schema,
callable|array|string $handler,
bool $isManual = false,
) {
parent::__construct($handler, $isManual);
}
public static function make(Tool $schema, callable|array|string $handler, bool $isManual = false): self
{
return new self($schema, $handler, $isManual);
}
/**
* Calls the underlying handler for this tool.
*
* @return Content[] The content items for CallToolResult.
*/
public function call(ContainerInterface $container, array $arguments, Context $context): array
{
$result = $this->handle($container, $arguments, $context);
return $this->formatResult($result);
}
/**
* Formats the result of a tool execution into an array of MCP Content items.
*
* - If the result is already a Content object, it's wrapped in an array.
* - If the result is an array:
* - If all elements are Content objects, the array is returned as is.
* - If it's a mixed array (Content and non-Content items), non-Content items are
* individually formatted (scalars to TextContent, others to JSON TextContent).
* - If it's an array with no Content items, the entire array is JSON-encoded into a single TextContent.
* - Scalars (string, int, float, bool) are wrapped in TextContent.
* - null is represented as TextContent('(null)').
* - Other objects are JSON-encoded and wrapped in TextContent.
*
* @param mixed $toolExecutionResult The raw value returned by the tool's PHP method.
* @return Content[] The content items for CallToolResult.
* @throws JsonException if JSON encoding fails for non-Content array/object results.
*/
protected function formatResult(mixed $toolExecutionResult): array
{
if ($toolExecutionResult instanceof Content) {
return [$toolExecutionResult];
}
if (is_array($toolExecutionResult)) {
if (empty($toolExecutionResult)) {
return [TextContent::make('[]')];
}
$allAreContent = true;
$hasContent = false;
foreach ($toolExecutionResult as $item) {
if ($item instanceof Content) {
$hasContent = true;
} else {
$allAreContent = false;
}
}
if ($allAreContent && $hasContent) {
return $toolExecutionResult;
}
if ($hasContent) {
$result = [];
foreach ($toolExecutionResult as $item) {
if ($item instanceof Content) {
$result[] = $item;
} else {
$result = array_merge($result, $this->formatResult($item));
}
}
return $result;
}
}
if ($toolExecutionResult === null) {
return [TextContent::make('(null)')];
}
if (is_bool($toolExecutionResult)) {
return [TextContent::make($toolExecutionResult ? 'true' : 'false')];
}
if (is_scalar($toolExecutionResult)) {
return [TextContent::make($toolExecutionResult)];
}
$jsonResult = json_encode(
$toolExecutionResult,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE
);
return [TextContent::make($jsonResult)];
}
public function toArray(): array
{
return [
'schema' => $this->schema->toArray(),
...parent::toArray(),
];
}
public static function fromArray(array $data): self|false
{
try {
if (! isset($data['schema']) || ! isset($data['handler'])) {
return false;
}
return new self(
Tool::fromArray($data['schema']),
$data['handler'],
$data['isManual'] ?? false,
);
} catch (Throwable $e) {
return false;
}
}
}