Skip to content

Commit ad7601d

Browse files
committed
[MCP SDK] ToolCallHandler now returns structuredContent if applicable
1 parent 11a4e56 commit ad7601d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/mcp-sdk/src/Capability/Tool/ToolCallResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
final readonly class ToolCallResult
1515
{
1616
public function __construct(
17-
public string $result,
17+
public mixed $result,
1818
/**
1919
* @var "text"|"image"|"audio"|"resource"|non-empty-string
2020
*/
2121
public string $type = 'text',
22-
public string $mimeType = 'text/plan',
22+
public string $mimeType = 'text/plain',
2323
public bool $isError = false,
2424
public ?string $uri = null,
2525
) {

src/mcp-sdk/src/Server/RequestHandler/ToolCallHandler.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public function __construct(
2626
) {
2727
}
2828

29+
/**
30+
* @throws \JsonException
31+
*/
2932
public function createResponse(Request $message): Response|Error
3033
{
3134
$name = $message->params['name'];
@@ -40,7 +43,7 @@ public function createResponse(Request $message): Response|Error
4043
$content = match ($result->type) {
4144
'text' => [
4245
'type' => 'text',
43-
'text' => $result->result,
46+
'text' => json_encode($result->result, \JSON_THROW_ON_ERROR),
4447
],
4548
'image', 'audio' => [
4649
'type' => $result->type,
@@ -59,10 +62,16 @@ public function createResponse(Request $message): Response|Error
5962
default => throw new InvalidArgumentException('Unsupported tool result type: '.$result->type),
6063
};
6164

62-
return new Response($message->id, [
65+
$response = [
6366
'content' => [$content], // TODO: allow multiple `ToolCallResult`s in the future
6467
'isError' => $result->isError,
65-
]);
68+
];
69+
70+
if ('text' === $result->type && \is_array($result->result) || \is_object($result->result)) {
71+
$response['structuredContent'] = $result->result;
72+
}
73+
74+
return new Response($message->id, $response);
6675
}
6776

6877
protected function supportedMethod(): string

0 commit comments

Comments
 (0)