| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Client\Sampling\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Protocol\Factory\ContentBlockFactory; |
| 9 |
use WP\McpSchema\Common\Protocol\Union\ContentBlockInterface; |
| 10 |
use WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface; |
| 11 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 12 |
|
| 13 |
/** |
| 14 |
* The result of a tool use, provided by the user back to the assistant. |
| 15 |
* |
| 16 |
* @since 2025-11-25 |
| 17 |
* |
| 18 |
* @mcp-domain Client |
| 19 |
* @mcp-subdomain Sampling |
| 20 |
* @mcp-version 2025-11-25 |
| 21 |
*/ |
| 22 |
class ToolResultContent extends AbstractDataTransferObject implements SamplingMessageContentBlockInterface |
| 23 |
{ |
| 24 |
use ValidatesRequiredFields; |
| 25 |
|
| 26 |
public const TYPE = 'tool_result'; |
| 27 |
|
| 28 |
public const DISCRIMINATOR_FIELD = 'type'; |
| 29 |
public const DISCRIMINATOR_VALUE = 'tool_result'; |
| 30 |
|
| 31 |
/** |
| 32 |
* @since 2025-11-25 |
| 33 |
* |
| 34 |
* @var 'tool_result' |
| 35 |
*/ |
| 36 |
protected string $type; |
| 37 |
|
| 38 |
/** |
| 39 |
* The ID of the tool use this result corresponds to. |
| 40 |
* |
| 41 |
* This MUST match the ID from a previous ToolUseContent. |
| 42 |
* |
| 43 |
* @since 2025-11-25 |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
protected string $toolUseId; |
| 48 |
|
| 49 |
/** |
| 50 |
* The unstructured result content of the tool use. |
| 51 |
* |
| 52 |
* This has the same format as CallToolResult.content and can include text, images, |
| 53 |
* audio, resource links, and embedded resources. |
| 54 |
* |
| 55 |
* @since 2025-11-25 |
| 56 |
* |
| 57 |
* @var array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> |
| 58 |
*/ |
| 59 |
protected array $content; |
| 60 |
|
| 61 |
/** |
| 62 |
* An optional structured result object. |
| 63 |
* |
| 64 |
* If the tool defined an outputSchema, this SHOULD conform to that schema. |
| 65 |
* |
| 66 |
* @since 2025-11-25 |
| 67 |
* |
| 68 |
* @var array<string, mixed>|null |
| 69 |
*/ |
| 70 |
protected ?array $structuredContent; |
| 71 |
|
| 72 |
/** |
| 73 |
* Whether the tool use resulted in an error. |
| 74 |
* |
| 75 |
* If true, the content typically describes the error that occurred. |
| 76 |
* Default: false |
| 77 |
* |
| 78 |
* @since 2025-11-25 |
| 79 |
* |
| 80 |
* @var bool|null |
| 81 |
*/ |
| 82 |
protected ?bool $isError; |
| 83 |
|
| 84 |
/** |
| 85 |
* Optional metadata about the tool result. Clients SHOULD preserve this field when |
| 86 |
* including tool results in subsequent sampling requests to enable caching optimizations. |
| 87 |
* |
| 88 |
* See [General fields: `_meta`](/specification/2025-11-25/basic/index#meta) for notes on `_meta` usage. |
| 89 |
* |
| 90 |
* @since 2025-11-25 |
| 91 |
* |
| 92 |
* @var array<string, mixed>|null |
| 93 |
*/ |
| 94 |
protected ?array $_meta; |
| 95 |
|
| 96 |
/** |
| 97 |
* @param string $toolUseId @since 2025-11-25 |
| 98 |
* @param array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> $content @since 2025-11-25 |
| 99 |
* @param array<string, mixed>|null $structuredContent @since 2025-11-25 |
| 100 |
* @param bool|null $isError @since 2025-11-25 |
| 101 |
* @param array<string, mixed>|null $_meta @since 2025-11-25 |
| 102 |
*/ |
| 103 |
public function __construct( |
| 104 |
string $toolUseId, |
| 105 |
array $content, |
| 106 |
?array $structuredContent = null, |
| 107 |
?bool $isError = null, |
| 108 |
?array $_meta = null |
| 109 |
) { |
| 110 |
$this->type = self::TYPE; |
| 111 |
$this->toolUseId = $toolUseId; |
| 112 |
$this->content = $content; |
| 113 |
$this->structuredContent = $structuredContent; |
| 114 |
$this->isError = $isError; |
| 115 |
$this->_meta = $_meta; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Creates an instance from an array. |
| 120 |
* |
| 121 |
* @param array{ |
| 122 |
* type: 'tool_result', |
| 123 |
* toolUseId: string, |
| 124 |
* content: array<array<string, mixed>|\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface>, |
| 125 |
* structuredContent?: array<string, mixed>|null, |
| 126 |
* isError?: bool|null, |
| 127 |
* _meta?: array<string, mixed>|null |
| 128 |
* } $data |
| 129 |
* @phpstan-param array<string, mixed> $data |
| 130 |
* @return self |
| 131 |
*/ |
| 132 |
public static function fromArray(array $data): self |
| 133 |
{ |
| 134 |
self::assertRequired($data, ['toolUseId', 'content']); |
| 135 |
|
| 136 |
/** @var array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> $content */ |
| 137 |
$content = array_map( |
| 138 |
static fn($item) => is_array($item) |
| 139 |
? ContentBlockFactory::fromArray($item) |
| 140 |
: $item, |
| 141 |
self::asArray($data['content']) |
| 142 |
); |
| 143 |
|
| 144 |
return new self( |
| 145 |
self::asString($data['toolUseId']), |
| 146 |
$content, |
| 147 |
self::asArrayOrNull($data['structuredContent'] ?? null), |
| 148 |
self::asBoolOrNull($data['isError'] ?? null), |
| 149 |
self::asArrayOrNull($data['_meta'] ?? null) |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Converts the instance to an array. |
| 155 |
* |
| 156 |
* @return array<string, mixed> |
| 157 |
*/ |
| 158 |
public function toArray(): array |
| 159 |
{ |
| 160 |
$result = []; |
| 161 |
|
| 162 |
$result['type'] = $this->type; |
| 163 |
$result['toolUseId'] = $this->toolUseId; |
| 164 |
$result['content'] = array_map(static fn($item) => $item->toArray(), $this->content); |
| 165 |
if ($this->structuredContent !== null) { |
| 166 |
$result['structuredContent'] = $this->structuredContent; |
| 167 |
} |
| 168 |
if ($this->isError !== null) { |
| 169 |
$result['isError'] = $this->isError; |
| 170 |
} |
| 171 |
if ($this->_meta !== null) { |
| 172 |
$result['_meta'] = $this->_meta; |
| 173 |
} |
| 174 |
|
| 175 |
return $result; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @return 'tool_result' |
| 180 |
*/ |
| 181 |
public function getType(): string |
| 182 |
{ |
| 183 |
return $this->type; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* @return string |
| 188 |
*/ |
| 189 |
public function getToolUseId(): string |
| 190 |
{ |
| 191 |
return $this->toolUseId; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* @return array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> |
| 196 |
*/ |
| 197 |
public function getContent(): array |
| 198 |
{ |
| 199 |
return $this->content; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* @return array<string, mixed>|null |
| 204 |
*/ |
| 205 |
public function getStructuredContent(): ?array |
| 206 |
{ |
| 207 |
return $this->structuredContent; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @return bool|null |
| 212 |
*/ |
| 213 |
public function getIsError(): ?bool |
| 214 |
{ |
| 215 |
return $this->isError; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* @return array<string, mixed>|null |
| 220 |
*/ |
| 221 |
public function get_meta(): ?array |
| 222 |
{ |
| 223 |
return $this->_meta; |
| 224 |
} |
| 225 |
} |
| 226 |
|