Skip to content

Commit c6244fb

Browse files
Add SEP-1034 support for default values in all primitive schema types
Co-authored-by: eiriktsarpalis <[email protected]>
1 parent 13c2b39 commit c6244fb

File tree

2 files changed

+376
-2
lines changed

2 files changed

+376
-2
lines changed

src/ModelContextProtocol.Core/Protocol/ElicitRequestParams.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public class Converter : JsonConverter<PrimitiveSchemaDefinition>
109109
double? minimum = null;
110110
double? maximum = null;
111111
bool? defaultBool = null;
112+
double? defaultNumber = null;
113+
string? defaultString = null;
112114
IList<string>? enumValues = null;
113115
IList<string>? enumNames = null;
114116

@@ -158,7 +160,20 @@ public class Converter : JsonConverter<PrimitiveSchemaDefinition>
158160
break;
159161

160162
case "default":
161-
defaultBool = reader.GetBoolean();
163+
// We need to handle different types for default values
164+
// Store the value based on the JSON token type
165+
if (reader.TokenType == JsonTokenType.True || reader.TokenType == JsonTokenType.False)
166+
{
167+
defaultBool = reader.GetBoolean();
168+
}
169+
else if (reader.TokenType == JsonTokenType.Number)
170+
{
171+
defaultNumber = reader.GetDouble();
172+
}
173+
else if (reader.TokenType == JsonTokenType.String)
174+
{
175+
defaultString = reader.GetString();
176+
}
162177
break;
163178

164179
case "enum":
@@ -188,7 +203,8 @@ public class Converter : JsonConverter<PrimitiveSchemaDefinition>
188203
psd = new EnumSchema
189204
{
190205
Enum = enumValues,
191-
EnumNames = enumNames
206+
EnumNames = enumNames,
207+
Default = defaultString,
192208
};
193209
}
194210
else
@@ -198,6 +214,7 @@ public class Converter : JsonConverter<PrimitiveSchemaDefinition>
198214
MinLength = minLength,
199215
MaxLength = maxLength,
200216
Format = format,
217+
Default = defaultString,
201218
};
202219
}
203220
break;
@@ -208,6 +225,7 @@ public class Converter : JsonConverter<PrimitiveSchemaDefinition>
208225
{
209226
Minimum = minimum,
210227
Maximum = maximum,
228+
Default = defaultNumber,
211229
};
212230
break;
213231

@@ -265,6 +283,10 @@ public override void Write(Utf8JsonWriter writer, PrimitiveSchemaDefinition valu
265283
{
266284
writer.WriteString("format", stringSchema.Format);
267285
}
286+
if (stringSchema.Default is not null)
287+
{
288+
writer.WriteString("default", stringSchema.Default);
289+
}
268290
break;
269291

270292
case NumberSchema numberSchema:
@@ -276,6 +298,10 @@ public override void Write(Utf8JsonWriter writer, PrimitiveSchemaDefinition valu
276298
{
277299
writer.WriteNumber("maximum", numberSchema.Maximum.Value);
278300
}
301+
if (numberSchema.Default.HasValue)
302+
{
303+
writer.WriteNumber("default", numberSchema.Default.Value);
304+
}
279305
break;
280306

281307
case BooleanSchema booleanSchema:
@@ -296,6 +322,10 @@ public override void Write(Utf8JsonWriter writer, PrimitiveSchemaDefinition valu
296322
writer.WritePropertyName("enumNames");
297323
JsonSerializer.Serialize(writer, enumSchema.EnumNames, McpJsonUtilities.JsonContext.Default.IListString);
298324
}
325+
if (enumSchema.Default is not null)
326+
{
327+
writer.WriteString("default", enumSchema.Default);
328+
}
299329
break;
300330

301331
default:
@@ -371,6 +401,10 @@ public string? Format
371401
field = value;
372402
}
373403
}
404+
405+
/// <summary>Gets or sets the default value for the string.</summary>
406+
[JsonPropertyName("default")]
407+
public string? Default { get; set; }
374408
}
375409

376410
/// <summary>Represents a schema for a number or integer type.</summary>
@@ -399,6 +433,10 @@ public override string Type
399433
/// <summary>Gets or sets the maximum allowed value.</summary>
400434
[JsonPropertyName("maximum")]
401435
public double? Maximum { get; set; }
436+
437+
/// <summary>Gets or sets the default value for the number.</summary>
438+
[JsonPropertyName("default")]
439+
public double? Default { get; set; }
402440
}
403441

404442
/// <summary>Represents a schema for a Boolean type.</summary>
@@ -456,5 +494,9 @@ public IList<string> Enum
456494
/// <summary>Gets or sets optional display names corresponding to the enum values.</summary>
457495
[JsonPropertyName("enumNames")]
458496
public IList<string>? EnumNames { get; set; }
497+
498+
/// <summary>Gets or sets the default value for the enum.</summary>
499+
[JsonPropertyName("default")]
500+
public string? Default { get; set; }
459501
}
460502
}

0 commit comments

Comments
 (0)