|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mcp\Schema\Elicitation; |
| 13 | + |
| 14 | +use Mcp\Exception\InvalidArgumentException; |
| 15 | + |
| 16 | +/** |
| 17 | + * Schema definition for multi-select enum fields without titles (SEP-1330). |
| 18 | + * |
| 19 | + * Produces: {"type": "array", "items": {"type": "string", "enum": [...]}} |
| 20 | + * |
| 21 | + * @see https://github.com/modelcontextprotocol/modelcontextprotocol/issues/1330 |
| 22 | + */ |
| 23 | +final class MultiSelectEnumSchemaDefinition extends AbstractSchemaDefinition |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @param string $title Human-readable title for the field |
| 27 | + * @param string[] $enum Array of allowed string values |
| 28 | + * @param string|null $description Optional description/help text |
| 29 | + * @param string[]|null $default Optional default selected values (must be subset of enum) |
| 30 | + * @param int|null $minItems Optional minimum number of selections |
| 31 | + * @param int|null $maxItems Optional maximum number of selections |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + string $title, |
| 35 | + public readonly array $enum, |
| 36 | + ?string $description = null, |
| 37 | + public readonly ?array $default = null, |
| 38 | + public readonly ?int $minItems = null, |
| 39 | + public readonly ?int $maxItems = null, |
| 40 | + ) { |
| 41 | + parent::__construct($title, $description); |
| 42 | + |
| 43 | + if ([] === $enum) { |
| 44 | + throw new InvalidArgumentException('enum array must not be empty.'); |
| 45 | + } |
| 46 | + |
| 47 | + foreach ($enum as $value) { |
| 48 | + if (!\is_string($value)) { |
| 49 | + throw new InvalidArgumentException('All enum values must be strings.'); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (null !== $minItems && $minItems < 0) { |
| 54 | + throw new InvalidArgumentException('minItems must be non-negative.'); |
| 55 | + } |
| 56 | + |
| 57 | + if (null !== $maxItems && $maxItems < 0) { |
| 58 | + throw new InvalidArgumentException('maxItems must be non-negative.'); |
| 59 | + } |
| 60 | + |
| 61 | + if (null !== $minItems && null !== $maxItems && $minItems > $maxItems) { |
| 62 | + throw new InvalidArgumentException('minItems cannot be greater than maxItems.'); |
| 63 | + } |
| 64 | + |
| 65 | + if (null !== $default) { |
| 66 | + foreach ($default as $value) { |
| 67 | + if (!\in_array($value, $enum, true)) { |
| 68 | + throw new InvalidArgumentException(\sprintf('Default value "%s" is not in the enum array.', $value)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param array{ |
| 76 | + * title: string, |
| 77 | + * items: array{type: string, enum: string[]}, |
| 78 | + * description?: string, |
| 79 | + * default?: string[], |
| 80 | + * minItems?: int, |
| 81 | + * maxItems?: int, |
| 82 | + * } $data |
| 83 | + */ |
| 84 | + public static function fromArray(array $data): self |
| 85 | + { |
| 86 | + self::validateTitle($data, 'multi-select enum'); |
| 87 | + |
| 88 | + if (!isset($data['items']['enum']) || !\is_array($data['items']['enum'])) { |
| 89 | + throw new InvalidArgumentException('Missing or invalid "items.enum" for multi-select enum schema definition.'); |
| 90 | + } |
| 91 | + |
| 92 | + return new self( |
| 93 | + title: $data['title'], |
| 94 | + enum: $data['items']['enum'], |
| 95 | + description: $data['description'] ?? null, |
| 96 | + default: $data['default'] ?? null, |
| 97 | + minItems: isset($data['minItems']) ? (int) $data['minItems'] : null, |
| 98 | + maxItems: isset($data['maxItems']) ? (int) $data['maxItems'] : null, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return array<string, mixed> |
| 104 | + */ |
| 105 | + public function jsonSerialize(): array |
| 106 | + { |
| 107 | + $data = $this->buildBaseJson('array'); |
| 108 | + $data['items'] = [ |
| 109 | + 'type' => 'string', |
| 110 | + 'enum' => $this->enum, |
| 111 | + ]; |
| 112 | + |
| 113 | + if (null !== $this->default) { |
| 114 | + $data['default'] = $this->default; |
| 115 | + } |
| 116 | + |
| 117 | + if (null !== $this->minItems) { |
| 118 | + $data['minItems'] = $this->minItems; |
| 119 | + } |
| 120 | + |
| 121 | + if (null !== $this->maxItems) { |
| 122 | + $data['maxItems'] = $this->maxItems; |
| 123 | + } |
| 124 | + |
| 125 | + return $data; |
| 126 | + } |
| 127 | +} |
0 commit comments