Skip to content

[MCP SDK] ToolCallHandler now returns structuredContent if applicable #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/mcp-sdk/src/Capability/Tool/ToolCallResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down
15 changes: 12 additions & 3 deletions src/mcp-sdk/src/Server/RequestHandler/ToolCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function __construct(
) {
}

/**
* @throws \JsonException
*/
public function createResponse(Request $message): Response|Error
{
$name = $message->params['name'];
Expand All @@ -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,
Expand All @@ -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)) {
Copy link
Preview

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The operator precedence is incorrect. The condition should be grouped as ('text' === $result->type) && (\is_array($result->result) || \is_object($result->result)) to ensure structured content is only added for text type results.

Suggested change
if ('text' === $result->type && \is_array($result->result) || \is_object($result->result)) {
if ('text' === $result->type && (\is_array($result->result) || \is_object($result->result))) {

Copilot uses AI. Check for mistakes.

$response['structuredContent'] = $result->result;
}

return new Response($message->id, $response);
}

protected function supportedMethod(): string
Expand Down