-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathElicitationSchema.php
More file actions
153 lines (132 loc) · 4.78 KB
/
ElicitationSchema.php
File metadata and controls
153 lines (132 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Schema\Elicitation;
use Mcp\Exception\InvalidArgumentException;
/**
* Schema wrapper for elicitation requestedSchema (JSON Schema object type).
*
* Represents an object schema with primitive property definitions and optional required fields.
*
* @author Johannes Wachter <johannes@sulu.io>
*/
final class ElicitationSchema implements \JsonSerializable
{
/**
* @param array<string, AbstractSchemaDefinition> $properties Property definitions keyed by name
* @param string[] $required Array of required property names
*/
public function __construct(
public readonly array $properties,
public readonly array $required = [],
) {
if ([] === $properties) {
throw new InvalidArgumentException('properties array must not be empty.');
}
foreach ($required as $name) {
if (!\array_key_exists($name, $properties)) {
throw new InvalidArgumentException(\sprintf('Required property "%s" is not defined in properties.', $name));
}
}
}
/**
* Create an ElicitationSchema from array data.
*
* @param array{
* type?: string,
* properties: array<string, array{type: string, title: string, ...}>,
* required?: string[],
* } $data
*/
public static function fromArray(array $data): self
{
if (isset($data['type']) && 'object' !== $data['type']) {
throw new InvalidArgumentException('ElicitationSchema type must be "object".');
}
if (!isset($data['properties']) || !\is_array($data['properties'])) {
throw new InvalidArgumentException('Missing or invalid "properties" for elicitation schema.');
}
$properties = [];
foreach ($data['properties'] as $name => $propertyData) {
if (!\is_array($propertyData)) {
throw new InvalidArgumentException(\sprintf('Property "%s" must be an array.', $name));
}
$properties[$name] = self::createSchemaDefinition($propertyData);
}
return new self(
properties: $properties,
required: $data['required'] ?? [],
);
}
/**
* Create a schema definition from array data.
*
* @param array<string, mixed> $data
*/
private static function createSchemaDefinition(array $data): AbstractSchemaDefinition
{
if (!isset($data['type']) || !\is_string($data['type'])) {
throw new InvalidArgumentException('Missing or invalid "type" for schema definition.');
}
return match ($data['type']) {
'string' => self::resolveStringType($data),
'integer', 'number' => NumberSchemaDefinition::fromArray($data),
'boolean' => BooleanSchemaDefinition::fromArray($data),
'array' => self::resolveArrayType($data),
default => throw new InvalidArgumentException(\sprintf('Unsupported type "%s". Supported types are: string, integer, number, boolean, array.', $data['type'])),
};
}
/**
* @param array<string, mixed> $data
*/
private static function resolveStringType(array $data): AbstractSchemaDefinition
{
if (isset($data['oneOf'])) {
return TitledEnumSchemaDefinition::fromArray($data);
}
if (isset($data['enum'])) {
return EnumSchemaDefinition::fromArray($data);
}
return StringSchemaDefinition::fromArray($data);
}
/**
* @param array<string, mixed> $data
*/
private static function resolveArrayType(array $data): AbstractSchemaDefinition
{
if (isset($data['items']['anyOf'])) {
return TitledMultiSelectEnumSchemaDefinition::fromArray($data);
}
if (isset($data['items']['enum'])) {
return MultiSelectEnumSchemaDefinition::fromArray($data);
}
throw new InvalidArgumentException('Array type must have "items" with either "enum" or "anyOf".');
}
/**
* @return array{
* type: string,
* properties: array<string, mixed>,
* required?: string[],
* }
*/
public function jsonSerialize(): array
{
$data = [
'type' => 'object',
'properties' => [],
];
foreach ($this->properties as $name => $property) {
$data['properties'][$name] = $property->jsonSerialize();
}
if ([] !== $this->required) {
$data['required'] = $this->required;
}
return $data;
}
}