forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElicitRequestParams.cs
More file actions
472 lines (413 loc) · 16.1 KB
/
ElicitRequestParams.cs
File metadata and controls
472 lines (413 loc) · 16.1 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ModelContextProtocol.Protocol;
/// <summary>
/// Represents a message issued from the server to elicit additional information from the user via the client.
/// </summary>
public sealed class ElicitRequestParams
{
/// <summary>
/// Gets or sets the message to present to the user.
/// </summary>
[JsonPropertyName("message")]
public string Message { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the requested schema.
/// </summary>
/// <remarks>
/// May be one of <see cref="StringSchema"/>, <see cref="NumberSchema"/>, <see cref="BooleanSchema"/>, or <see cref="EnumSchema"/>.
/// </remarks>
[JsonPropertyName("requestedSchema")]
[field: MaybeNull]
public RequestSchema RequestedSchema
{
get => field ??= new RequestSchema();
set => field = value;
}
/// <summary>Represents a request schema used in an elicitation request.</summary>
public class RequestSchema
{
/// <summary>Gets the type of the schema.</summary>
/// <remarks>This is always "object".</remarks>
[JsonPropertyName("type")]
public string Type => "object";
/// <summary>Gets or sets the properties of the schema.</summary>
[JsonPropertyName("properties")]
[field: MaybeNull]
public IDictionary<string, PrimitiveSchemaDefinition> Properties
{
get => field ??= new Dictionary<string, PrimitiveSchemaDefinition>();
set
{
Throw.IfNull(value);
field = value;
}
}
/// <summary>Gets or sets the required properties of the schema.</summary>
[JsonPropertyName("required")]
public IList<string>? Required { get; set; }
}
/// <summary>
/// Represents restricted subset of JSON Schema:
/// <see cref="StringSchema"/>, <see cref="NumberSchema"/>, <see cref="BooleanSchema"/>, or <see cref="EnumSchema"/>.
/// </summary>
[JsonConverter(typeof(Converter))] // TODO: This converter exists due to the lack of downlevel support for AllowOutOfOrderMetadataProperties.
public abstract class PrimitiveSchemaDefinition
{
/// <summary>Prevent external derivations.</summary>
protected private PrimitiveSchemaDefinition()
{
}
/// <summary>Gets the type of the schema.</summary>
[JsonPropertyName("type")]
public abstract string Type { get; set; }
/// <summary>Gets or sets a title for the schema.</summary>
[JsonPropertyName("title")]
public string? Title { get; set; }
/// <summary>Gets or sets a description for the schema.</summary>
[JsonPropertyName("description")]
public string? Description { get; set; }
/// <summary>
/// Provides a <see cref="JsonConverter"/> for <see cref="ResourceContents"/>.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public class Converter : JsonConverter<PrimitiveSchemaDefinition>
{
/// <inheritdoc/>
public override PrimitiveSchemaDefinition? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException();
}
string? type = null;
string? title = null;
string? description = null;
int? minLength = null;
int? maxLength = null;
string? format = null;
double? minimum = null;
double? maximum = null;
bool? defaultBool = null;
IList<string>? enumValues = null;
IList<string>? enumNames = null;
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
if (reader.TokenType != JsonTokenType.PropertyName)
{
continue;
}
string? propertyName = reader.GetString();
bool success = reader.Read();
Debug.Assert(success, "STJ must have buffered the entire object for us.");
switch (propertyName)
{
case "type":
if (reader.TokenType == JsonTokenType.String)
{
type = reader.GetString();
}
else if (reader.TokenType == JsonTokenType.StartArray)
{
var types = JsonSerializer.Deserialize(ref reader, McpJsonUtilities.JsonContext.Default.StringArray);
if (types is [var nullableType, "null"])
{
type = nullableType;
}
}
break;
case "title":
title = reader.GetString();
break;
case "description":
description = reader.GetString();
break;
case "minLength":
minLength = reader.GetInt32();
break;
case "maxLength":
maxLength = reader.GetInt32();
break;
case "format":
format = reader.GetString();
break;
case "minimum":
minimum = reader.GetDouble();
break;
case "maximum":
maximum = reader.GetDouble();
break;
case "default":
defaultBool = reader.GetBoolean();
break;
case "enum":
enumValues = JsonSerializer.Deserialize(ref reader, McpJsonUtilities.JsonContext.Default.IListString);
break;
case "enumNames":
enumNames = JsonSerializer.Deserialize(ref reader, McpJsonUtilities.JsonContext.Default.IListString);
break;
default:
break;
}
}
if (type is null)
{
throw new JsonException("The 'type' property is required.");
}
PrimitiveSchemaDefinition? psd = null;
switch (type)
{
case "string":
if (enumValues is not null)
{
psd = new EnumSchema
{
Enum = enumValues,
EnumNames = enumNames
};
}
else
{
psd = new StringSchema
{
MinLength = minLength,
MaxLength = maxLength,
Format = format,
};
}
break;
case "integer":
case "number":
psd = new NumberSchema
{
Minimum = minimum,
Maximum = maximum,
};
break;
case "boolean":
psd = new BooleanSchema
{
Default = defaultBool,
};
break;
}
if (psd is not null)
{
psd.Type = type;
psd.Title = title;
psd.Description = description;
}
return psd;
}
/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, PrimitiveSchemaDefinition value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}
writer.WriteStartObject();
writer.WriteString("type", value.Type);
if (value.Title is not null)
{
writer.WriteString("title", value.Title);
}
if (value.Description is not null)
{
writer.WriteString("description", value.Description);
}
switch (value)
{
case StringSchema stringSchema:
if (stringSchema.MinLength.HasValue)
{
writer.WriteNumber("minLength", stringSchema.MinLength.Value);
}
if (stringSchema.MaxLength.HasValue)
{
writer.WriteNumber("maxLength", stringSchema.MaxLength.Value);
}
if (stringSchema.Format is not null)
{
writer.WriteString("format", stringSchema.Format);
}
break;
case NumberSchema numberSchema:
if (numberSchema.Minimum.HasValue)
{
writer.WriteNumber("minimum", numberSchema.Minimum.Value);
}
if (numberSchema.Maximum.HasValue)
{
writer.WriteNumber("maximum", numberSchema.Maximum.Value);
}
break;
case BooleanSchema booleanSchema:
if (booleanSchema.Default.HasValue)
{
writer.WriteBoolean("default", booleanSchema.Default.Value);
}
break;
case EnumSchema enumSchema:
if (enumSchema.Enum is not null)
{
writer.WritePropertyName("enum");
JsonSerializer.Serialize(writer, enumSchema.Enum, McpJsonUtilities.JsonContext.Default.IListString);
}
if (enumSchema.EnumNames is not null)
{
writer.WritePropertyName("enumNames");
JsonSerializer.Serialize(writer, enumSchema.EnumNames, McpJsonUtilities.JsonContext.Default.IListString);
}
break;
default:
throw new JsonException($"Unexpected schema type: {value.GetType().Name}");
}
writer.WriteEndObject();
}
}
}
/// <summary>Represents a schema for a string type.</summary>
public sealed class StringSchema : PrimitiveSchemaDefinition
{
/// <inheritdoc/>
[JsonPropertyName("type")]
public override string Type
{
get => "string";
set
{
if (value is not "string")
{
throw new ArgumentException("Type must be 'string'.", nameof(value));
}
}
}
/// <summary>Gets or sets the minimum length for the string.</summary>
[JsonPropertyName("minLength")]
public int? MinLength
{
get => field;
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value), "Minimum length cannot be negative.");
}
field = value;
}
}
/// <summary>Gets or sets the maximum length for the string.</summary>
[JsonPropertyName("maxLength")]
public int? MaxLength
{
get => field;
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value), "Maximum length cannot be negative.");
}
field = value;
}
}
/// <summary>Gets or sets a specific format for the string ("email", "uri", "date", or "date-time").</summary>
[JsonPropertyName("format")]
public string? Format
{
get => field;
set
{
if (value is not (null or "email" or "uri" or "date" or "date-time"))
{
throw new ArgumentException("Format must be 'email', 'uri', 'date', or 'date-time'.", nameof(value));
}
field = value;
}
}
}
/// <summary>Represents a schema for a number or integer type.</summary>
public sealed class NumberSchema : PrimitiveSchemaDefinition
{
/// <inheritdoc/>
[field: MaybeNull]
public override string Type
{
get => field ??= "number";
set
{
if (value is not ("number" or "integer"))
{
throw new ArgumentException("Type must be 'number' or 'integer'.", nameof(value));
}
field = value;
}
}
/// <summary>Gets or sets the minimum allowed value.</summary>
[JsonPropertyName("minimum")]
public double? Minimum { get; set; }
/// <summary>Gets or sets the maximum allowed value.</summary>
[JsonPropertyName("maximum")]
public double? Maximum { get; set; }
}
/// <summary>Represents a schema for a Boolean type.</summary>
public sealed class BooleanSchema : PrimitiveSchemaDefinition
{
/// <inheritdoc/>
[JsonPropertyName("type")]
public override string Type
{
get => "boolean";
set
{
if (value is not "boolean")
{
throw new ArgumentException("Type must be 'boolean'.", nameof(value));
}
}
}
/// <summary>Gets or sets the default value for the Boolean.</summary>
[JsonPropertyName("default")]
public bool? Default { get; set; }
}
/// <summary>Represents a schema for an enum type.</summary>
public sealed class EnumSchema : PrimitiveSchemaDefinition
{
/// <inheritdoc/>
[JsonPropertyName("type")]
public override string Type
{
get => "string";
set
{
if (value is not "string")
{
throw new ArgumentException("Type must be 'string'.", nameof(value));
}
}
}
/// <summary>Gets or sets the list of allowed string values for the enum.</summary>
[JsonPropertyName("enum")]
[field: MaybeNull]
public IList<string> Enum
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}
/// <summary>Gets or sets optional display names corresponding to the enum values.</summary>
[JsonPropertyName("enumNames")]
public IList<string>? EnumNames { get; set; }
}
}