| 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\Union\SamplingMessageContentBlockInterface; |
| 9 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 10 |
|
| 11 |
/** |
| 12 |
* A request from the assistant to call a tool. |
| 13 |
* |
| 14 |
* @since 2025-11-25 |
| 15 |
* |
| 16 |
* @mcp-domain Client |
| 17 |
* @mcp-subdomain Sampling |
| 18 |
* @mcp-version 2025-11-25 |
| 19 |
*/ |
| 20 |
class ToolUseContent extends AbstractDataTransferObject implements SamplingMessageContentBlockInterface |
| 21 |
{ |
| 22 |
use ValidatesRequiredFields; |
| 23 |
|
| 24 |
public const TYPE = 'tool_use'; |
| 25 |
|
| 26 |
public const DISCRIMINATOR_FIELD = 'type'; |
| 27 |
public const DISCRIMINATOR_VALUE = 'tool_use'; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 2025-11-25 |
| 31 |
* |
| 32 |
* @var 'tool_use' |
| 33 |
*/ |
| 34 |
protected string $type; |
| 35 |
|
| 36 |
/** |
| 37 |
* A unique identifier for this tool use. |
| 38 |
* |
| 39 |
* This ID is used to match tool results to their corresponding tool uses. |
| 40 |
* |
| 41 |
* @since 2025-11-25 |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
protected string $id; |
| 46 |
|
| 47 |
/** |
| 48 |
* The name of the tool to call. |
| 49 |
* |
| 50 |
* @since 2025-11-25 |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
protected string $name; |
| 55 |
|
| 56 |
/** |
| 57 |
* The arguments to pass to the tool, conforming to the tool's input schema. |
| 58 |
* |
| 59 |
* @since 2025-11-25 |
| 60 |
* |
| 61 |
* @var array<string, mixed> |
| 62 |
*/ |
| 63 |
protected array $input; |
| 64 |
|
| 65 |
/** |
| 66 |
* Optional metadata about the tool use. Clients SHOULD preserve this field when |
| 67 |
* including tool uses in subsequent sampling requests to enable caching optimizations. |
| 68 |
* |
| 69 |
* See [General fields: `_meta`](/specification/2025-11-25/basic/index#meta) for notes on `_meta` usage. |
| 70 |
* |
| 71 |
* @since 2025-11-25 |
| 72 |
* |
| 73 |
* @var array<string, mixed>|null |
| 74 |
*/ |
| 75 |
protected ?array $_meta; |
| 76 |
|
| 77 |
/** |
| 78 |
* @param string $id @since 2025-11-25 |
| 79 |
* @param string $name @since 2025-11-25 |
| 80 |
* @param array<string, mixed> $input @since 2025-11-25 |
| 81 |
* @param array<string, mixed>|null $_meta @since 2025-11-25 |
| 82 |
*/ |
| 83 |
public function __construct( |
| 84 |
string $id, |
| 85 |
string $name, |
| 86 |
array $input, |
| 87 |
?array $_meta = null |
| 88 |
) { |
| 89 |
$this->type = self::TYPE; |
| 90 |
$this->id = $id; |
| 91 |
$this->name = $name; |
| 92 |
$this->input = $input; |
| 93 |
$this->_meta = $_meta; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Creates an instance from an array. |
| 98 |
* |
| 99 |
* @param array{ |
| 100 |
* type: 'tool_use', |
| 101 |
* id: string, |
| 102 |
* name: string, |
| 103 |
* input: array<string, mixed>, |
| 104 |
* _meta?: array<string, mixed>|null |
| 105 |
* } $data |
| 106 |
* @phpstan-param array<string, mixed> $data |
| 107 |
* @return self |
| 108 |
*/ |
| 109 |
public static function fromArray(array $data): self |
| 110 |
{ |
| 111 |
self::assertRequired($data, ['id', 'name', 'input']); |
| 112 |
|
| 113 |
return new self( |
| 114 |
self::asString($data['id']), |
| 115 |
self::asString($data['name']), |
| 116 |
self::asArray($data['input']), |
| 117 |
self::asArrayOrNull($data['_meta'] ?? null) |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Converts the instance to an array. |
| 123 |
* |
| 124 |
* @return array<string, mixed> |
| 125 |
*/ |
| 126 |
public function toArray(): array |
| 127 |
{ |
| 128 |
$result = []; |
| 129 |
|
| 130 |
$result['type'] = $this->type; |
| 131 |
$result['id'] = $this->id; |
| 132 |
$result['name'] = $this->name; |
| 133 |
$result['input'] = $this->input; |
| 134 |
if ($this->_meta !== null) { |
| 135 |
$result['_meta'] = $this->_meta; |
| 136 |
} |
| 137 |
|
| 138 |
return $result; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @return 'tool_use' |
| 143 |
*/ |
| 144 |
public function getType(): string |
| 145 |
{ |
| 146 |
return $this->type; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* @return string |
| 151 |
*/ |
| 152 |
public function getId(): string |
| 153 |
{ |
| 154 |
return $this->id; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* @return string |
| 159 |
*/ |
| 160 |
public function getName(): string |
| 161 |
{ |
| 162 |
return $this->name; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* @return array<string, mixed> |
| 167 |
*/ |
| 168 |
public function getInput(): array |
| 169 |
{ |
| 170 |
return $this->input; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @return array<string, mixed>|null |
| 175 |
*/ |
| 176 |
public function get_meta(): ?array |
| 177 |
{ |
| 178 |
return $this->_meta; |
| 179 |
} |
| 180 |
} |
| 181 |
|