Skip to content

Commit 5913162

Browse files
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 <2813363+eiriktsarpalis@users.noreply.github.com>
1 parent c90afd6 commit 5913162

2 files changed

Lines changed: 8 additions & 58 deletions

File tree

src/ModelContextProtocol.Core/Server/McpServer.Methods.cs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -307,52 +307,12 @@ private static ElicitRequestParams.RequestSchema BuildRequestSchema(Type type, J
307307
foreach (JsonPropertyInfo pi in typeInfo.Properties)
308308
{
309309
var def = CreatePrimitiveSchema(pi.PropertyType, serializerOptions);
310-
311-
// Extract default value from DefaultValueAttribute if present
312-
if (pi.AttributeProvider != null)
313-
{
314-
var attrs = pi.AttributeProvider.GetCustomAttributes(typeof(System.ComponentModel.DefaultValueAttribute), false);
315-
if (attrs.Length > 0 && attrs[0] is System.ComponentModel.DefaultValueAttribute defaultValueAttr)
316-
{
317-
SetDefaultValue(def, defaultValueAttr.Value);
318-
}
319-
}
320-
321310
props[pi.Name] = def;
322311
}
323312

324313
return schema;
325314
}
326315

327-
/// <summary>
328-
/// Sets the default value on a primitive schema definition based on the value type.
329-
/// </summary>
330-
/// <param name="schema">The schema to set the default value on.</param>
331-
/// <param name="value">The default value.</param>
332-
private static void SetDefaultValue(ElicitRequestParams.PrimitiveSchemaDefinition schema, object? value)
333-
{
334-
if (value == null)
335-
{
336-
return;
337-
}
338-
339-
switch (schema)
340-
{
341-
case ElicitRequestParams.StringSchema stringSchema:
342-
stringSchema.Default = value.ToString();
343-
break;
344-
case ElicitRequestParams.NumberSchema numberSchema:
345-
numberSchema.Default = Convert.ToDouble(value);
346-
break;
347-
case ElicitRequestParams.BooleanSchema booleanSchema:
348-
booleanSchema.Default = Convert.ToBoolean(value);
349-
break;
350-
case ElicitRequestParams.EnumSchema enumSchema:
351-
enumSchema.Default = value.ToString();
352-
break;
353-
}
354-
}
355-
356316
/// <summary>
357317
/// Creates a primitive schema definition for the specified type, if supported.
358318
/// </summary>

tests/ModelContextProtocol.Tests/Protocol/ElicitationTypedTests.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -374,29 +374,19 @@ public sealed class Nested
374374
[JsonSerializable(typeof(JsonElement))]
375375
internal partial class ElicitationUnsupportedJsonContext : JsonSerializerContext;
376376

377-
public sealed class FormWithDefaults
378-
{
379-
[System.ComponentModel.DefaultValue("John Doe")]
380-
public string Name { get; set; } = "John Doe";
381-
382-
[System.ComponentModel.DefaultValue(30)]
383-
public int Age { get; set; } = 30;
384-
385-
[System.ComponentModel.DefaultValue(85.5)]
386-
public double Score { get; set; } = 85.5;
387-
388-
[System.ComponentModel.DefaultValue(true)]
389-
public bool IsActive { get; set; } = true;
390-
391-
[System.ComponentModel.DefaultValue("active")]
392-
public string Status { get; set; } = "active";
393-
}
377+
public sealed record FormWithDefaults(
378+
string Name = "John Doe",
379+
int Age = 30,
380+
double Score = 85.5,
381+
bool IsActive = true,
382+
string Status = "active"
383+
);
394384

395385
[JsonSerializable(typeof(FormWithDefaults))]
396386
[JsonSerializable(typeof(JsonElement))]
397387
internal partial class ElicitationDefaultsJsonContext : JsonSerializerContext;
398388

399-
[Fact]
389+
[Fact(Skip = "Requires AIJsonUtilities to support extracting default values from optional parameters")]
400390
public async Task Elicit_Typed_With_Defaults_Maps_To_Schema_Defaults()
401391
{
402392
await using McpClient client = await CreateMcpClientForServer(new McpClientOptions

0 commit comments

Comments
 (0)