convertkit
/
vendor
/
wordpress
/
php-mcp-schema
/
src
/
Server
/
Tools
/
DTO
/
ToolOutputSchema.php
ToolOutputSchema.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.3, at vendor/wordpress/php-mcp-schema/src/Server/Tools/DTO/ToolOutputSchema.php
| 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 | * An optional JSON Schema object defining the structure of the tool's output returned in |
| 12 | * the structuredContent field of a CallToolResult. |
| 13 | * |
| 14 | * Defaults to JSON Schema 2020-12 when no explicit $schema is provided. |
| 15 | * Currently restricted to type: "object" at the root level. |
| 16 | * |
| 17 | * @mcp-domain Server |
| 18 | * @mcp-subdomain Tools |
| 19 | * @mcp-version 2025-11-25 |
| 20 | */ |
| 21 | class ToolOutputSchema extends AbstractDataTransferObject |
| 22 | { |
| 23 | use ValidatesRequiredFields; |
| 24 | |
| 25 | public const TYPE = 'object'; |
| 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 = ['$schema', 'type', 'properties', 'required']; |
| 33 | |
| 34 | /** |
| 35 | * @var string|null |
| 36 | */ |
| 37 | protected ?string $schema; |
| 38 | |
| 39 | /** |
| 40 | * @var 'object' |
| 41 | */ |
| 42 | protected string $type; |
| 43 | |
| 44 | /** |
| 45 | * @var array<string, object>|null |
| 46 | */ |
| 47 | protected ?array $properties; |
| 48 | |
| 49 | /** |
| 50 | * @var array<string>|null |
| 51 | */ |
| 52 | protected ?array $required; |
| 53 | |
| 54 | /** |
| 55 | * Keys carried on the wire that this type does not model. Preserved verbatim so unrecognized fields survive a round trip. |
| 56 | * |
| 57 | * @var array<string, mixed>|null |
| 58 | */ |
| 59 | protected ?array $additionalProperties; |
| 60 | |
| 61 | /** |
| 62 | * @param string|null $schema |
| 63 | * @param array<string, object>|null $properties |
| 64 | * @param array<string>|null $required |
| 65 | * @param array<string, mixed>|null $additionalProperties |
| 66 | */ |
| 67 | public function __construct( |
| 68 | ?string $schema = null, |
| 69 | ?array $properties = null, |
| 70 | ?array $required = null, |
| 71 | ?array $additionalProperties = null |
| 72 | ) { |
| 73 | $this->type = self::TYPE; |
| 74 | $this->schema = $schema; |
| 75 | $this->properties = $properties; |
| 76 | $this->required = $required; |
| 77 | $this->additionalProperties = $additionalProperties; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates an instance from an array. |
| 82 | * |
| 83 | * @param array{ |
| 84 | * '$schema'?: string|null, |
| 85 | * type: 'object', |
| 86 | * properties?: array<string, object>|null, |
| 87 | * required?: array<string>|null |
| 88 | * } $data |
| 89 | * @phpstan-param array<string, mixed> $data |
| 90 | * @return self |
| 91 | */ |
| 92 | public static function fromArray(array $data): self |
| 93 | { |
| 94 | return new self( |
| 95 | self::asStringOrNull($data['$schema'] ?? null), |
| 96 | self::asObjectMapOrNull($data['properties'] ?? null), |
| 97 | self::asStringArrayOrNull($data['required'] ?? null), |
| 98 | self::additionalFields($data, self::KNOWN_KEYS) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Converts the instance to an array. |
| 104 | * |
| 105 | * @return array<string, mixed> |
| 106 | */ |
| 107 | public function toArray(): array |
| 108 | { |
| 109 | $result = []; |
| 110 | |
| 111 | if ($this->schema !== null) { |
| 112 | $result['$schema'] = $this->schema; |
| 113 | } |
| 114 | $result['type'] = $this->type; |
| 115 | $result['properties'] = !empty($this->properties) |
| 116 | ? $this->properties |
| 117 | : new \stdClass(); |
| 118 | if ($this->required !== null) { |
| 119 | $result['required'] = $this->required; |
| 120 | } |
| 121 | |
| 122 | return $result + ($this->additionalProperties ?? []); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return string|null |
| 127 | */ |
| 128 | public function getSchema(): ?string |
| 129 | { |
| 130 | return $this->schema; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @return 'object' |
| 135 | */ |
| 136 | public function getType(): string |
| 137 | { |
| 138 | return $this->type; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return array<string, object>|null |
| 143 | */ |
| 144 | public function getProperties(): ?array |
| 145 | { |
| 146 | return $this->properties; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return array<string>|null |
| 151 | */ |
| 152 | public function getRequired(): ?array |
| 153 | { |
| 154 | return $this->required; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return array<string, mixed>|null |
| 159 | */ |
| 160 | public function getAdditionalProperties(): ?array |
| 161 | { |
| 162 | return $this->additionalProperties; |
| 163 | } |
| 164 | } |
| 165 |