| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Server\Tools\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\Protocol\DTO\Result; |
| 8 |
use WP\McpSchema\Common\Protocol\Factory\ContentBlockFactory; |
| 9 |
use WP\McpSchema\Common\Protocol\Union\ContentBlockInterface; |
| 10 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 11 |
use WP\McpSchema\Server\Lifecycle\Union\ServerResultInterface; |
| 12 |
|
| 13 |
/** |
| 14 |
* The server's response to a tool call. |
| 15 |
* |
| 16 |
* @since 2024-11-05 |
| 17 |
* @last-updated 2025-11-25 (modified property: content) |
| 18 |
* |
| 19 |
* @mcp-domain Server |
| 20 |
* @mcp-subdomain Tools |
| 21 |
* @mcp-version 2025-11-25 |
| 22 |
*/ |
| 23 |
class CallToolResult extends Result implements ServerResultInterface |
| 24 |
{ |
| 25 |
use ValidatesRequiredFields; |
| 26 |
|
| 27 |
/** |
| 28 |
* Wire keys this class models. Anything else is kept in $additionalProperties. |
| 29 |
* |
| 30 |
* @var array<int, string> |
| 31 |
*/ |
| 32 |
private const KNOWN_KEYS = ['_meta', 'content', 'structuredContent', 'isError']; |
| 33 |
|
| 34 |
/** |
| 35 |
* A list of content objects that represent the unstructured result of the tool call. |
| 36 |
* |
| 37 |
* @since 2024-11-05 |
| 38 |
* |
| 39 |
* @var array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> |
| 40 |
*/ |
| 41 |
protected array $content; |
| 42 |
|
| 43 |
/** |
| 44 |
* An optional JSON object that represents the structured result of the tool call. |
| 45 |
* |
| 46 |
* @since 2025-06-18 |
| 47 |
* |
| 48 |
* @var array<string, mixed>|null |
| 49 |
*/ |
| 50 |
protected ?array $structuredContent; |
| 51 |
|
| 52 |
/** |
| 53 |
* Whether the tool call ended in an error. |
| 54 |
* |
| 55 |
* If not set, this is assumed to be false (the call was successful). |
| 56 |
* |
| 57 |
* Any errors that originate from the tool SHOULD be reported inside the result |
| 58 |
* object, with `isError` set to true, _not_ as an MCP protocol-level error |
| 59 |
* response. Otherwise, the LLM would not be able to see that an error occurred |
| 60 |
* and self-correct. |
| 61 |
* |
| 62 |
* However, any errors in _finding_ the tool, an error indicating that the |
| 63 |
* server does not support tool calls, or any other exceptional conditions, |
| 64 |
* should be reported as an MCP error response. |
| 65 |
* |
| 66 |
* @since 2024-11-05 |
| 67 |
* |
| 68 |
* @var bool|null |
| 69 |
*/ |
| 70 |
protected ?bool $isError; |
| 71 |
|
| 72 |
/** |
| 73 |
* @param array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> $content @since 2024-11-05 |
| 74 |
* @param array<string, mixed>|null $_meta @since 2024-11-05 |
| 75 |
* @param array<string, mixed>|null $structuredContent @since 2025-06-18 |
| 76 |
* @param bool|null $isError @since 2024-11-05 |
| 77 |
* @param array<string, mixed>|null $additionalProperties |
| 78 |
*/ |
| 79 |
public function __construct( |
| 80 |
array $content, |
| 81 |
?array $_meta = null, |
| 82 |
?array $structuredContent = null, |
| 83 |
?bool $isError = null, |
| 84 |
?array $additionalProperties = null |
| 85 |
) { |
| 86 |
parent::__construct($_meta, $additionalProperties); |
| 87 |
$this->content = $content; |
| 88 |
$this->structuredContent = $structuredContent; |
| 89 |
$this->isError = $isError; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Creates an instance from an array. |
| 94 |
* |
| 95 |
* @param array{ |
| 96 |
* _meta?: array<string, mixed>|null, |
| 97 |
* content: array<array<string, mixed>|\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface>, |
| 98 |
* structuredContent?: array<string, mixed>|null, |
| 99 |
* isError?: bool|null |
| 100 |
* } $data |
| 101 |
* @phpstan-param array<string, mixed> $data |
| 102 |
* @return self |
| 103 |
*/ |
| 104 |
public static function fromArray(array $data): self |
| 105 |
{ |
| 106 |
self::assertRequired($data, ['content']); |
| 107 |
|
| 108 |
/** @var array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> $content */ |
| 109 |
$content = array_map( |
| 110 |
static fn($item) => is_array($item) |
| 111 |
? ContentBlockFactory::fromArray($item) |
| 112 |
: $item, |
| 113 |
self::asArray($data['content']) |
| 114 |
); |
| 115 |
|
| 116 |
return new self( |
| 117 |
$content, |
| 118 |
self::asArrayOrNull($data['_meta'] ?? null), |
| 119 |
self::asArrayOrNull($data['structuredContent'] ?? null), |
| 120 |
self::asBoolOrNull($data['isError'] ?? null), |
| 121 |
self::additionalFields($data, self::KNOWN_KEYS) |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Converts the instance to an array. |
| 127 |
* |
| 128 |
* @return array<string, mixed> |
| 129 |
*/ |
| 130 |
public function toArray(): array |
| 131 |
{ |
| 132 |
$result = parent::toArray(); |
| 133 |
|
| 134 |
$result['content'] = array_map(static fn($item) => $item->toArray(), $this->content); |
| 135 |
if ($this->structuredContent !== null) { |
| 136 |
$result['structuredContent'] = $this->structuredContent; |
| 137 |
} |
| 138 |
if ($this->isError !== null) { |
| 139 |
$result['isError'] = $this->isError; |
| 140 |
} |
| 141 |
|
| 142 |
return $result; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @return array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> |
| 147 |
*/ |
| 148 |
public function getContent(): array |
| 149 |
{ |
| 150 |
return $this->content; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @return array<string, mixed>|null |
| 155 |
*/ |
| 156 |
public function getStructuredContent(): ?array |
| 157 |
{ |
| 158 |
return $this->structuredContent; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @return bool|null |
| 163 |
*/ |
| 164 |
public function getIsError(): ?bool |
| 165 |
{ |
| 166 |
return $this->isError; |
| 167 |
} |
| 168 |
} |
| 169 |
|