ameliabooking
/
vendor
/
wordpress
/
php-mcp-schema
/
src
/
Server
/
Tools
/
DTO
/
CallToolResult.php
CallToolRequest.php
1 month ago
CallToolRequestParams.php
1 month ago
CallToolResult.php
1 month ago
ListToolsRequest.php
1 month ago
ListToolsResult.php
1 month ago
Tool.php
1 month ago
ToolAnnotations.php
1 month ago
ToolExecution.php
1 month ago
ToolInputSchema.php
1 month ago
ToolListChangedNotification.php
1 month ago
ToolOutputSchema.php
1 month ago
CallToolResult.php
159 lines
| 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 | * A list of content objects that represent the unstructured result of the tool call. |
| 29 | * |
| 30 | * @since 2024-11-05 |
| 31 | * |
| 32 | * @var array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> |
| 33 | */ |
| 34 | protected array $content; |
| 35 | |
| 36 | /** |
| 37 | * An optional JSON object that represents the structured result of the tool call. |
| 38 | * |
| 39 | * @since 2025-06-18 |
| 40 | * |
| 41 | * @var array<string, mixed>|null |
| 42 | */ |
| 43 | protected ?array $structuredContent; |
| 44 | |
| 45 | /** |
| 46 | * Whether the tool call ended in an error. |
| 47 | * |
| 48 | * If not set, this is assumed to be false (the call was successful). |
| 49 | * |
| 50 | * Any errors that originate from the tool SHOULD be reported inside the result |
| 51 | * object, with `isError` set to true, _not_ as an MCP protocol-level error |
| 52 | * response. Otherwise, the LLM would not be able to see that an error occurred |
| 53 | * and self-correct. |
| 54 | * |
| 55 | * However, any errors in _finding_ the tool, an error indicating that the |
| 56 | * server does not support tool calls, or any other exceptional conditions, |
| 57 | * should be reported as an MCP error response. |
| 58 | * |
| 59 | * @since 2024-11-05 |
| 60 | * |
| 61 | * @var bool|null |
| 62 | */ |
| 63 | protected ?bool $isError; |
| 64 | |
| 65 | /** |
| 66 | * @param array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> $content @since 2024-11-05 |
| 67 | * @param array<string, mixed>|null $_meta @since 2024-11-05 |
| 68 | * @param array<string, mixed>|null $structuredContent @since 2025-06-18 |
| 69 | * @param bool|null $isError @since 2024-11-05 |
| 70 | */ |
| 71 | public function __construct( |
| 72 | array $content, |
| 73 | ?array $_meta = null, |
| 74 | ?array $structuredContent = null, |
| 75 | ?bool $isError = null |
| 76 | ) { |
| 77 | parent::__construct($_meta); |
| 78 | $this->content = $content; |
| 79 | $this->structuredContent = $structuredContent; |
| 80 | $this->isError = $isError; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Creates an instance from an array. |
| 85 | * |
| 86 | * @param array{ |
| 87 | * _meta?: array<string, mixed>|null, |
| 88 | * content: array<array<string, mixed>|\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface>, |
| 89 | * structuredContent?: array<string, mixed>|null, |
| 90 | * isError?: bool|null |
| 91 | * } $data |
| 92 | * @phpstan-param array<string, mixed> $data |
| 93 | * @return self |
| 94 | */ |
| 95 | public static function fromArray(array $data): self |
| 96 | { |
| 97 | self::assertRequired($data, ['content']); |
| 98 | |
| 99 | /** @var array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> $content */ |
| 100 | $content = array_map( |
| 101 | static fn($item) => is_array($item) |
| 102 | ? ContentBlockFactory::fromArray($item) |
| 103 | : $item, |
| 104 | self::asArray($data['content']) |
| 105 | ); |
| 106 | |
| 107 | return new self( |
| 108 | $content, |
| 109 | self::asArrayOrNull($data['_meta'] ?? null), |
| 110 | self::asArrayOrNull($data['structuredContent'] ?? null), |
| 111 | self::asBoolOrNull($data['isError'] ?? null) |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Converts the instance to an array. |
| 117 | * |
| 118 | * @return array<string, mixed> |
| 119 | */ |
| 120 | public function toArray(): array |
| 121 | { |
| 122 | $result = parent::toArray(); |
| 123 | |
| 124 | $result['content'] = array_map(static fn($item) => $item->toArray(), $this->content); |
| 125 | if ($this->structuredContent !== null) { |
| 126 | $result['structuredContent'] = $this->structuredContent; |
| 127 | } |
| 128 | if ($this->isError !== null) { |
| 129 | $result['isError'] = $this->isError; |
| 130 | } |
| 131 | |
| 132 | return $result; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return array<\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface> |
| 137 | */ |
| 138 | public function getContent(): array |
| 139 | { |
| 140 | return $this->content; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return array<string, mixed>|null |
| 145 | */ |
| 146 | public function getStructuredContent(): ?array |
| 147 | { |
| 148 | return $this->structuredContent; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @return bool|null |
| 153 | */ |
| 154 | public function getIsError(): ?bool |
| 155 | { |
| 156 | return $this->isError; |
| 157 | } |
| 158 | } |
| 159 |