diff --git a/src/mcp-sdk/src/Capability/Tool/ToolCallResult.php b/src/mcp-sdk/src/Capability/Tool/ToolCallResult.php index 2bdd2d0d..79ee4344 100644 --- a/src/mcp-sdk/src/Capability/Tool/ToolCallResult.php +++ b/src/mcp-sdk/src/Capability/Tool/ToolCallResult.php @@ -14,12 +14,12 @@ final readonly class ToolCallResult { public function __construct( - public string $result, + public mixed $result, /** * @var "text"|"image"|"audio"|"resource"|non-empty-string */ public string $type = 'text', - public string $mimeType = 'text/plan', + public string $mimeType = 'text/plain', public bool $isError = false, public ?string $uri = null, ) { diff --git a/src/mcp-sdk/src/Server/RequestHandler/ToolCallHandler.php b/src/mcp-sdk/src/Server/RequestHandler/ToolCallHandler.php index 49c16562..c56a563c 100644 --- a/src/mcp-sdk/src/Server/RequestHandler/ToolCallHandler.php +++ b/src/mcp-sdk/src/Server/RequestHandler/ToolCallHandler.php @@ -26,6 +26,9 @@ public function __construct( ) { } + /** + * @throws \JsonException + */ public function createResponse(Request $message): Response|Error { $name = $message->params['name']; @@ -40,7 +43,7 @@ public function createResponse(Request $message): Response|Error $content = match ($result->type) { 'text' => [ 'type' => 'text', - 'text' => $result->result, + 'text' => json_encode($result->result, \JSON_THROW_ON_ERROR), ], 'image', 'audio' => [ 'type' => $result->type, @@ -59,10 +62,16 @@ public function createResponse(Request $message): Response|Error default => throw new InvalidArgumentException('Unsupported tool result type: '.$result->type), }; - return new Response($message->id, [ + $response = [ 'content' => [$content], // TODO: allow multiple `ToolCallResult`s in the future 'isError' => $result->isError, - ]); + ]; + + if ('text' === $result->type && \is_array($result->result) || \is_object($result->result)) { + $response['structuredContent'] = $result->result; + } + + return new Response($message->id, $response); } protected function supportedMethod(): string