| 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 |
* @var string|null |
| 25 |
*/ |
| 26 |
protected ?string $schema; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var 'object' |
| 30 |
*/ |
| 31 |
protected string $type; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var array<string, object>|null |
| 35 |
*/ |
| 36 |
protected ?array $properties; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var array<string>|null |
| 40 |
*/ |
| 41 |
protected ?array $required; |
| 42 |
|
| 43 |
/** |
| 44 |
* @param string|null $schema |
| 45 |
* @param array<string, object>|null $properties |
| 46 |
* @param array<string>|null $required |
| 47 |
*/ |
| 48 |
public function __construct( |
| 49 |
?string $schema = null, |
| 50 |
?array $properties = null, |
| 51 |
?array $required = null |
| 52 |
) { |
| 53 |
$this->type = self::TYPE; |
| 54 |
$this->schema = $schema; |
| 55 |
$this->properties = $properties; |
| 56 |
$this->required = $required; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Creates an instance from an array. |
| 61 |
* |
| 62 |
* @param array{ |
| 63 |
* '$schema'?: string|null, |
| 64 |
* type: 'object', |
| 65 |
* properties?: array<string, object>|null, |
| 66 |
* required?: array<string>|null |
| 67 |
* } $data |
| 68 |
* @phpstan-param array<string, mixed> $data |
| 69 |
* @return self |
| 70 |
*/ |
| 71 |
public static function fromArray(array $data): self |
| 72 |
{ |
| 73 |
return new self( |
| 74 |
self::asStringOrNull($data['$schema'] ?? null), |
| 75 |
self::asObjectMapOrNull($data['properties'] ?? null), |
| 76 |
self::asStringArrayOrNull($data['required'] ?? null) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Converts the instance to an array. |
| 82 |
* |
| 83 |
* @return array<string, mixed> |
| 84 |
*/ |
| 85 |
public function toArray(): array |
| 86 |
{ |
| 87 |
$result = []; |
| 88 |
|
| 89 |
if ($this->schema !== null) { |
| 90 |
$result['$schema'] = $this->schema; |
| 91 |
} |
| 92 |
$result['type'] = $this->type; |
| 93 |
$result['properties'] = !empty($this->properties) |
| 94 |
? $this->properties |
| 95 |
: new \stdClass(); |
| 96 |
if ($this->required !== null) { |
| 97 |
$result['required'] = $this->required; |
| 98 |
} |
| 99 |
|
| 100 |
return $result; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return string|null |
| 105 |
*/ |
| 106 |
public function getSchema(): ?string |
| 107 |
{ |
| 108 |
return $this->schema; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @return 'object' |
| 113 |
*/ |
| 114 |
public function getType(): string |
| 115 |
{ |
| 116 |
return $this->type; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @return array<string, object>|null |
| 121 |
*/ |
| 122 |
public function getProperties(): ?array |
| 123 |
{ |
| 124 |
return $this->properties; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @return array<string>|null |
| 129 |
*/ |
| 130 |
public function getRequired(): ?array |
| 131 |
{ |
| 132 |
return $this->required; |
| 133 |
} |
| 134 |
} |
| 135 |
|