Skip to content

Commit ff2ecb0

Browse files
committed
Added basic StructuredContent
1 parent e1c96cf commit ff2ecb0

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/Capability/Tool/ToolCaller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function call(CallToolRequest $request): CallToolResult
7777
'result_type' => \gettype($result),
7878
]);
7979

80-
return new CallToolResult($formattedResult);
80+
return CallToolResult::fromArray($formattedResult);
8181
} catch (\Throwable $e) {
8282
$this->logger->error('Tool execution failed', [
8383
'name' => $toolName,

src/Schema/Result/CallToolResult.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ class CallToolResult implements ResultInterface
4545
/**
4646
* Create a new CallToolResult.
4747
*
48-
* @param array<TextContent|ImageContent|AudioContent|EmbeddedResource|StructuredContent> $content The content of the tool result
48+
* @param array<TextContent|ImageContent|AudioContent|EmbeddedResource> $content The content of the tool result
4949
* @param bool $isError Whether the tool execution resulted in an error. If not set, this is assumed to be false (the call was successful).
5050
*/
5151
public function __construct(
5252
public readonly array $content,
53+
public readonly ?StructuredContent $structuredContent = null,
5354
public readonly bool $isError = false,
5455
) {
5556
foreach ($this->content as $item) {
@@ -64,36 +65,37 @@ public function __construct(
6465
*
6566
* @param array<TextContent|ImageContent|AudioContent|EmbeddedResource> $content The content of the tool result
6667
*/
67-
public static function success(array $content): self
68+
public static function success(array $content, ?StructuredContent $structuredContent = null): self
6869
{
69-
return new self($content, false);
70+
return new self($content, $structuredContent, false);
7071
}
7172

7273
/**
7374
* Create a new CallToolResult with error status.
7475
*
7576
* @param array<TextContent|ImageContent|AudioContent|EmbeddedResource> $content The content of the tool result
7677
*/
77-
public static function error(array $content): self
78+
public static function error(array $content, ?StructuredContent $structuredContent = null): self
7879
{
79-
return new self($content, true);
80+
return new self($content, $structuredContent, true);
8081
}
8182

8283
/**
83-
* @param array{
84-
* content: array<TextContentData|ImageContentData|AudioContentData|EmbeddedResourceData>,
85-
* isError?: bool,
86-
* } $data
84+
* @param array<int, TextContentData|ImageContentData|AudioContentData|EmbeddedResourceData|StructuredContent> $data
8785
*/
8886
public static function fromArray(array $data): self
8987
{
90-
if (!isset($data['content']) || !\is_array($data['content'])) {
91-
throw new InvalidArgumentException('Missing or invalid "content" array in CallToolResult data.');
92-
}
93-
9488
$contents = [];
89+
$structuredContent = null;
9590

96-
foreach ($data['content'] as $item) {
91+
foreach ($data as $item) {
92+
if (!$item instanceof Content) {
93+
throw new InvalidArgumentException('Provided array must be an array of Content objects.');
94+
}
95+
if ('structured' === $item->type) {
96+
$structuredContent = $item['structured'];
97+
continue;
98+
}
9799
$contents[] = match ($item['type'] ?? null) {
98100
'text' => TextContent::fromArray($item),
99101
'image' => ImageContent::fromArray($item),
@@ -103,20 +105,27 @@ public static function fromArray(array $data): self
103105
};
104106
}
105107

106-
return new self($contents, $data['isError'] ?? false);
108+
return new self($contents, $structuredContent);
107109
}
108110

109111
/**
110112
* @return array{
111-
* content: array<TextContent|ImageContent|AudioContent|EmbeddedResource|StructuredContent>,
113+
* content: array<TextContent|ImageContent|AudioContent|EmbeddedResource>,
114+
* structuredContent?: StructuredContent,
112115
* isError: bool,
113116
* }
114117
*/
115118
public function jsonSerialize(): array
116119
{
117-
return [
120+
$result = [
118121
'content' => $this->content,
119122
'isError' => $this->isError,
120123
];
124+
125+
if ($this->structuredContent) {
126+
$result['structuredContent'] = $this->structuredContent->jsonSerialize();
127+
}
128+
129+
return $result;
121130
}
122131
}

0 commit comments

Comments
 (0)