Skip to content

Commit 66c084c

Browse files
Add default value support for all primitive types in elicitation schemas (SEP-1034) (#924)
* Initial plan * Add SEP-1034 support for default values in all primitive schema types Co-authored-by: eiriktsarpalis <[email protected]> * Address PR feedback: convert if-else to switch and add default value mapping tests Co-authored-by: eiriktsarpalis <[email protected]> * Remove default value extraction code and linker annotations per feedback Co-authored-by: eiriktsarpalis <[email protected]> * Use literal true/false values instead of GetBoolean() call Co-authored-by: eiriktsarpalis <[email protected]> * Reinstate tests and fix CreatePrimitiveSchema to extract defaults from AttributeProvider Co-authored-by: eiriktsarpalis <[email protected]> * Remove manual default extraction and update test to use positional record Removed the manual default value extraction code from BuildRequestSchema and SetDefaultValue helper as this should be handled by AIJsonUtilities. Updated FormWithDefaults to be a positional record with optional parameters. Test is now skipped until AIJsonUtilities supports extracting defaults from optional parameters. Co-authored-by: eiriktsarpalis <[email protected]> * Use 'is not null' pattern for consistency with nullable checks Co-authored-by: eiriktsarpalis <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: eiriktsarpalis <[email protected]>
1 parent 017981f commit 66c084c

File tree

3 files changed

+468
-3
lines changed

3 files changed

+468
-3
lines changed

src/ModelContextProtocol.Core/Protocol/ElicitRequestParams.cs

Lines changed: 48 additions & 3 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,23 @@ 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+
switch (reader.TokenType)
166+
{
167+
case JsonTokenType.True:
168+
defaultBool = true;
169+
break;
170+
case JsonTokenType.False:
171+
defaultBool = false;
172+
break;
173+
case JsonTokenType.Number:
174+
defaultNumber = reader.GetDouble();
175+
break;
176+
case JsonTokenType.String:
177+
defaultString = reader.GetString();
178+
break;
179+
}
162180
break;
163181

164182
case "enum":
@@ -188,7 +206,8 @@ public class Converter : JsonConverter<PrimitiveSchemaDefinition>
188206
psd = new EnumSchema
189207
{
190208
Enum = enumValues,
191-
EnumNames = enumNames
209+
EnumNames = enumNames,
210+
Default = defaultString,
192211
};
193212
}
194213
else
@@ -198,6 +217,7 @@ public class Converter : JsonConverter<PrimitiveSchemaDefinition>
198217
MinLength = minLength,
199218
MaxLength = maxLength,
200219
Format = format,
220+
Default = defaultString,
201221
};
202222
}
203223
break;
@@ -208,6 +228,7 @@ public class Converter : JsonConverter<PrimitiveSchemaDefinition>
208228
{
209229
Minimum = minimum,
210230
Maximum = maximum,
231+
Default = defaultNumber,
211232
};
212233
break;
213234

@@ -265,6 +286,10 @@ public override void Write(Utf8JsonWriter writer, PrimitiveSchemaDefinition valu
265286
{
266287
writer.WriteString("format", stringSchema.Format);
267288
}
289+
if (stringSchema.Default is not null)
290+
{
291+
writer.WriteString("default", stringSchema.Default);
292+
}
268293
break;
269294

270295
case NumberSchema numberSchema:
@@ -276,10 +301,14 @@ public override void Write(Utf8JsonWriter writer, PrimitiveSchemaDefinition valu
276301
{
277302
writer.WriteNumber("maximum", numberSchema.Maximum.Value);
278303
}
304+
if (numberSchema.Default is not null)
305+
{
306+
writer.WriteNumber("default", numberSchema.Default.Value);
307+
}
279308
break;
280309

281310
case BooleanSchema booleanSchema:
282-
if (booleanSchema.Default.HasValue)
311+
if (booleanSchema.Default is not null)
283312
{
284313
writer.WriteBoolean("default", booleanSchema.Default.Value);
285314
}
@@ -296,6 +325,10 @@ public override void Write(Utf8JsonWriter writer, PrimitiveSchemaDefinition valu
296325
writer.WritePropertyName("enumNames");
297326
JsonSerializer.Serialize(writer, enumSchema.EnumNames, McpJsonUtilities.JsonContext.Default.IListString);
298327
}
328+
if (enumSchema.Default is not null)
329+
{
330+
writer.WriteString("default", enumSchema.Default);
331+
}
299332
break;
300333

301334
default:
@@ -371,6 +404,10 @@ public string? Format
371404
field = value;
372405
}
373406
}
407+
408+
/// <summary>Gets or sets the default value for the string.</summary>
409+
[JsonPropertyName("default")]
410+
public string? Default { get; set; }
374411
}
375412

376413
/// <summary>Represents a schema for a number or integer type.</summary>
@@ -399,6 +436,10 @@ public override string Type
399436
/// <summary>Gets or sets the maximum allowed value.</summary>
400437
[JsonPropertyName("maximum")]
401438
public double? Maximum { get; set; }
439+
440+
/// <summary>Gets or sets the default value for the number.</summary>
441+
[JsonPropertyName("default")]
442+
public double? Default { get; set; }
402443
}
403444

404445
/// <summary>Represents a schema for a Boolean type.</summary>
@@ -456,5 +497,9 @@ public IList<string> Enum
456497
/// <summary>Gets or sets optional display names corresponding to the enum values.</summary>
457498
[JsonPropertyName("enumNames")]
458499
public IList<string>? EnumNames { get; set; }
500+
501+
/// <summary>Gets or sets the default value for the enum.</summary>
502+
[JsonPropertyName("default")]
503+
public string? Default { get; set; }
459504
}
460505
}

0 commit comments

Comments
 (0)