| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Server\Tools\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 9 |
|
| 10 |
/** |
| 11 |
* A JSON Schema object defining the expected parameters for the tool. |
| 12 |
* |
| 13 |
* @mcp-domain Server |
| 14 |
* @mcp-subdomain Tools |
| 15 |
* @mcp-version 2025-11-25 |
| 16 |
*/ |
| 17 |
class ToolInputSchema extends AbstractDataTransferObject |
| 18 |
{ |
| 19 |
use ValidatesRequiredFields; |
| 20 |
|
| 21 |
public const TYPE = 'object'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Wire keys this class models. Anything else is kept in $additionalProperties. |
| 25 |
* |
| 26 |
* @var array<int, string> |
| 27 |
*/ |
| 28 |
private const KNOWN_KEYS = ['$schema', 'type', 'properties', 'required']; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var string|null |
| 32 |
*/ |
| 33 |
protected ?string $schema; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var 'object' |
| 37 |
*/ |
| 38 |
protected string $type; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var array<string, object>|null |
| 42 |
*/ |
| 43 |
protected ?array $properties; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var array<string>|null |
| 47 |
*/ |
| 48 |
protected ?array $required; |
| 49 |
|
| 50 |
/** |
| 51 |
* Keys carried on the wire that this type does not model. Preserved verbatim so unrecognized fields survive a round trip. |
| 52 |
* |
| 53 |
* @var array<string, mixed>|null |
| 54 |
*/ |
| 55 |
protected ?array $additionalProperties; |
| 56 |
|
| 57 |
/** |
| 58 |
* @param string|null $schema |
| 59 |
* @param array<string, object>|null $properties |
| 60 |
* @param array<string>|null $required |
| 61 |
* @param array<string, mixed>|null $additionalProperties |
| 62 |
*/ |
| 63 |
public function __construct( |
| 64 |
?string $schema = null, |
| 65 |
?array $properties = null, |
| 66 |
?array $required = null, |
| 67 |
?array $additionalProperties = null |
| 68 |
) { |
| 69 |
$this->type = self::TYPE; |
| 70 |
$this->schema = $schema; |
| 71 |
$this->properties = $properties; |
| 72 |
$this->required = $required; |
| 73 |
$this->additionalProperties = $additionalProperties; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Creates an instance from an array. |
| 78 |
* |
| 79 |
* @param array{ |
| 80 |
* '$schema'?: string|null, |
| 81 |
* type: 'object', |
| 82 |
* properties?: array<string, object>|null, |
| 83 |
* required?: array<string>|null |
| 84 |
* } $data |
| 85 |
* @phpstan-param array<string, mixed> $data |
| 86 |
* @return self |
| 87 |
*/ |
| 88 |
public static function fromArray(array $data): self |
| 89 |
{ |
| 90 |
return new self( |
| 91 |
self::asStringOrNull($data['$schema'] ?? null), |
| 92 |
self::asObjectMapOrNull($data['properties'] ?? null), |
| 93 |
self::asStringArrayOrNull($data['required'] ?? null), |
| 94 |
self::additionalFields($data, self::KNOWN_KEYS) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Converts the instance to an array. |
| 100 |
* |
| 101 |
* @return array<string, mixed> |
| 102 |
*/ |
| 103 |
public function toArray(): array |
| 104 |
{ |
| 105 |
$result = []; |
| 106 |
|
| 107 |
if ($this->schema !== null) { |
| 108 |
$result['$schema'] = $this->schema; |
| 109 |
} |
| 110 |
$result['type'] = $this->type; |
| 111 |
$result['properties'] = !empty($this->properties) |
| 112 |
? $this->properties |
| 113 |
: new \stdClass(); |
| 114 |
if ($this->required !== null) { |
| 115 |
$result['required'] = $this->required; |
| 116 |
} |
| 117 |
|
| 118 |
return $result + ($this->additionalProperties ?? []); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @return string|null |
| 123 |
*/ |
| 124 |
public function getSchema(): ?string |
| 125 |
{ |
| 126 |
return $this->schema; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @return 'object' |
| 131 |
*/ |
| 132 |
public function getType(): string |
| 133 |
{ |
| 134 |
return $this->type; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @return array<string, object>|null |
| 139 |
*/ |
| 140 |
public function getProperties(): ?array |
| 141 |
{ |
| 142 |
return $this->properties; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @return array<string>|null |
| 147 |
*/ |
| 148 |
public function getRequired(): ?array |
| 149 |
{ |
| 150 |
return $this->required; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @return array<string, mixed>|null |
| 155 |
*/ |
| 156 |
public function getAdditionalProperties(): ?array |
| 157 |
{ |
| 158 |
return $this->additionalProperties; |
| 159 |
} |
| 160 |
} |
| 161 |
|