StructuredContent call tool result, added ability to return error from tool (#93)

This commit is contained in:
ineersa 2025-10-24 15:18:42 -04:00 • committed by GitHub
commit d347c84472
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 74 additions and 5 deletions

View file

@ -38,12 +38,14 @@ class CallToolResult implements ResultInterface
/**
* Create a new CallToolResult.
*
* @param Content[] $content The content of the tool result
* @param bool $isError Whether the tool execution resulted in an error. If not set, this is assumed to be false (the call was successful).
* @param Content[] $content The content of the tool result
* @param bool $isError Whether the tool execution resulted in an error. If not set, this is assumed to be false (the call was successful).
* @param mixed[] $structuredContent JSON content for `structuredContent`
*/
public function __construct(
public readonly array $content,
public readonly bool $isError = false,
public readonly ?array $structuredContent = null,
) {
foreach ($this->content as $item) {
if (!$item instanceof Content) {
@ -107,9 +109,15 @@ class CallToolResult implements ResultInterface
*/
public function jsonSerialize(): array
{
return [
$result = [
'content' => $this->content,
'isError' => $this->isError,
];
if ($this->structuredContent) {
$result['structuredContent'] = $this->structuredContent;
}
return $result;
}
}

View file

@ -59,14 +59,17 @@ final class CallToolHandler implements RequestHandlerInterface
}
$result = $this->referenceHandler->handle($reference, $arguments);
$formatted = $reference->formatResult($result);
if (!$result instanceof CallToolResult) {
$result = new CallToolResult($reference->formatResult($result));
}
$this->logger->debug('Tool executed successfully', [
'name' => $toolName,
'result_type' => \gettype($result),
]);
return new Response($request->getId(), new CallToolResult($formatted));
return new Response($request->getId(), $result);
} catch (ToolNotFoundException $e) {
$this->logger->error('Tool not found', ['name' => $toolName]);