From 519e96ce172906db49bd35622afbb4f9f929fd6f Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Tue, 13 Oct 2026 23:15:52 +0800 Subject: [PATCH 1/9] Fix nullable structured tool results --- .../Server/AIFunctionMcpServerTool.cs | 13 +++- .../Server/McpServerToolTests.cs | 61 +++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index 82b6ceb9d..e8477854e 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.Text.Json; using System.Text.Json.Nodes; +using System.Text.Json.Serialization; using System.Text.RegularExpressions; namespace ModelContextProtocol.Server; @@ -14,6 +15,7 @@ namespace ModelContextProtocol.Server; internal sealed partial class AIFunctionMcpServerTool : McpServerTool { private readonly IReadOnlyList _metadata; + private readonly JsonSerializerOptions _structuredContentSerializerOptions; /// /// Creates an instance for a method, specified via a instance. @@ -225,6 +227,13 @@ private AIFunctionMcpServerTool(AIFunction function, Tool tool, IServiceProvider ProtocolTool = tool; _metadata = metadata; + _structuredContentSerializerOptions = tool.OutputSchema is not null && + function.JsonSerializerOptions.DefaultIgnoreCondition is JsonIgnoreCondition.WhenWritingNull + ? new JsonSerializerOptions(function.JsonSerializerOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.Never, + } + : function.JsonSerializerOptions; } /// @@ -677,7 +686,7 @@ private static void RewriteRefPointers(JsonNode? node) JsonElement jsonElement => jsonElement, JsonNode node => JsonSerializer.SerializeToElement(node, McpJsonUtilities.JsonContext.Default.JsonNode), null => null, - _ => JsonSerializer.SerializeToElement(aiFunctionResult, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object))), + _ => JsonSerializer.SerializeToElement(aiFunctionResult, _structuredContentSerializerOptions.GetTypeInfo(typeof(object))), }; // Pre-SEP-2106 clients expect the {"result": } envelope for non-object @@ -805,4 +814,4 @@ private static bool IsPrimitiveHeaderType(Type type) type == typeof(uint) || type == typeof(long); } -} \ No newline at end of file +} diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index 8fd1d9954..2a7a9af55 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -481,6 +481,58 @@ public async Task StructuredOutput_Enabled_ReturnsExpectedSchema(T value) AssertMatchesJsonSchema(tool.ProtocolTool.OutputSchema.Value, result.StructuredContent); } + [Theory] + [InlineData(null, Sep2106ProtocolVersion)] + [InlineData("note", Sep2106ProtocolVersion)] + [InlineData(null, "2025-11-25")] + [InlineData("note", "2025-11-25")] + public async Task StructuredOutput_Enabled_RequiredNullableProperties_MatchesOutputSchema(string? note, string protocolVersion) + { + JsonSerializerOptions serializerOptions = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine( + McpJsonUtilities.DefaultOptions.TypeInfoResolver, + new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new RequiredNullableOutput("42", note), + new() { Name = "tool", UseStructuredContent = true, SerializerOptions = serializerOptions }); + var request = CreateRequestContextWithProtocolVersion(protocolVersion); + + var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); + + Assert.NotNull(tool.ProtocolTool.OutputSchema); + Assert.NotNull(result.StructuredContent); + Assert.Equal(note is null ? JsonValueKind.Null : JsonValueKind.String, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(tool.ProtocolTool.OutputSchema.Value, result.StructuredContent); + } + + [Theory] + [InlineData(null, Sep2106ProtocolVersion)] + [InlineData("note", Sep2106ProtocolVersion)] + [InlineData(null, "2025-11-25")] + [InlineData("note", "2025-11-25")] + public async Task StructuredOutput_Enabled_RequiredNullablePropertiesInClass_MatchesOutputSchema(string? note, string protocolVersion) + { + JsonSerializerOptions serializerOptions = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine( + McpJsonUtilities.DefaultOptions.TypeInfoResolver, + new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new RequiredNullablePropertiesOutput { Id = "42", Note = note }, + new() { Name = "tool", UseStructuredContent = true, SerializerOptions = serializerOptions }); + var request = CreateRequestContextWithProtocolVersion(protocolVersion); + + var result = await tool.InvokeAsync(request, TestContext.Current.CancellationToken); + + Assert.NotNull(tool.ProtocolTool.OutputSchema); + Assert.NotNull(result.StructuredContent); + Assert.Equal(note is null ? JsonValueKind.Null : JsonValueKind.String, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(tool.ProtocolTool.OutputSchema.Value, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_VoidReturningTools_ReturnsExpectedSchema() { @@ -1091,6 +1143,15 @@ Instance JSON document does not match the specified schema. record Person(string Name, int Age); + private sealed record RequiredNullableOutput(string Id, string? Note); + + private sealed class RequiredNullablePropertiesOutput + { + public required string Id { get; init; } + + public required string? Note { get; init; } + } + private static JsonSerializerOptions CreateSerializerOptionsWithPerson() { JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions); From de107a5375bc8d910fbe78700778ecf0e9def962 Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Tue, 20 Oct 2026 07:33:04 +0800 Subject: [PATCH 2/9] Fix structured output for required nullable properties Signed-off-by: mikemikimike <13286568797@163.com> --- .../Server/AIFunctionMcpServerTool.cs | 118 ++++++++++++++++-- .../Server/McpServerToolTests.cs | 64 ++++++++++ 2 files changed, 172 insertions(+), 10 deletions(-) diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index e8477854e..4bef486af 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -6,7 +6,6 @@ using System.Reflection; using System.Text.Json; using System.Text.Json.Nodes; -using System.Text.Json.Serialization; using System.Text.RegularExpressions; namespace ModelContextProtocol.Server; @@ -15,7 +14,6 @@ namespace ModelContextProtocol.Server; internal sealed partial class AIFunctionMcpServerTool : McpServerTool { private readonly IReadOnlyList _metadata; - private readonly JsonSerializerOptions _structuredContentSerializerOptions; /// /// Creates an instance for a method, specified via a instance. @@ -227,13 +225,6 @@ private AIFunctionMcpServerTool(AIFunction function, Tool tool, IServiceProvider ProtocolTool = tool; _metadata = metadata; - _structuredContentSerializerOptions = tool.OutputSchema is not null && - function.JsonSerializerOptions.DefaultIgnoreCondition is JsonIgnoreCondition.WhenWritingNull - ? new JsonSerializerOptions(function.JsonSerializerOptions) - { - DefaultIgnoreCondition = JsonIgnoreCondition.Never, - } - : function.JsonSerializerOptions; } /// @@ -686,9 +677,11 @@ private static void RewriteRefPointers(JsonNode? node) JsonElement jsonElement => jsonElement, JsonNode node => JsonSerializer.SerializeToElement(node, McpJsonUtilities.JsonContext.Default.JsonNode), null => null, - _ => JsonSerializer.SerializeToElement(aiFunctionResult, _structuredContentSerializerOptions.GetTypeInfo(typeof(object))), + _ => JsonSerializer.SerializeToElement(aiFunctionResult, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object))), }; + elementResult = AddMissingRequiredNullableProperties(elementResult); + // Pre-SEP-2106 clients expect the {"result": } envelope for non-object // schemas. SEP-2106 clients see the natural shape. The classification is decided // fresh per request from the stored natural schema. @@ -707,6 +700,111 @@ private static void RewriteRefPointers(JsonNode? node) return elementResult; } + private JsonElement? AddMissingRequiredNullableProperties(JsonElement? elementResult) + { + if (elementResult is not { } result || + ProtocolTool.OutputSchema is not { } outputSchema || + result.ValueKind is not (JsonValueKind.Object or JsonValueKind.Array)) + { + return elementResult; + } + + JsonNode? resultNode = JsonSerializer.SerializeToNode(result, McpJsonUtilities.JsonContext.Default.JsonElement); + JsonNode? schemaNode = JsonSerializer.SerializeToNode(outputSchema, McpJsonUtilities.JsonContext.Default.JsonElement); + if (resultNode is null || schemaNode is null || !AddMissingRequiredNullableProperties(resultNode, schemaNode)) + { + return elementResult; + } + + return JsonSerializer.SerializeToElement(resultNode, McpJsonUtilities.JsonContext.Default.JsonNode); + } + + private static bool AddMissingRequiredNullableProperties(JsonNode resultNode, JsonNode schemaNode) + { + bool changed = false; + + if (resultNode is JsonObject resultObject && schemaNode is JsonObject schemaObject) + { + JsonArray? required = schemaObject["required"] as JsonArray; + JsonObject? properties = schemaObject["properties"] as JsonObject; + + if (properties is not null) + { + foreach (KeyValuePair property in resultObject) + { + if (property.Value is not null && properties.TryGetPropertyValue(property.Key, out JsonNode? propertySchema)) + { + changed |= AddMissingRequiredNullableProperties(property.Value, propertySchema!); + } + } + } + + if (required is not null && properties is not null) + { + foreach (JsonNode? requiredProperty in required) + { + string? propertyName = requiredProperty?.GetValue(); + if (propertyName is not null && + !resultObject.ContainsKey(propertyName) && + properties.TryGetPropertyValue(propertyName, out JsonNode? propertySchema) && + SchemaAllowsNull(propertySchema)) + { + resultObject[propertyName] = null; + changed = true; + } + } + } + } + else if (resultNode is JsonArray resultArray && + schemaNode is JsonObject { } arraySchema && + arraySchema["items"] is JsonNode itemSchema) + { + foreach (JsonNode? item in resultArray) + { + if (item is not null) + { + changed |= AddMissingRequiredNullableProperties(item, itemSchema); + } + } + } + + return changed; + } + + private static bool SchemaAllowsNull(JsonNode? schemaNode) + { + if (schemaNode is JsonValue schemaValue && schemaValue.TryGetValue(out bool allowsNull)) + { + return allowsNull; + } + + if (schemaNode is not JsonObject schemaObject) + { + return false; + } + + if (schemaObject["type"] is JsonValue typeValue && typeValue.TryGetValue(out string? type)) + { + return type == "null"; + } + + if (schemaObject["type"] is JsonArray types && + types.Any(type => type is JsonValue value && value.TryGetValue(out string? typeName) && typeName == "null")) + { + return true; + } + + foreach (string keyword in new[] { "anyOf", "oneOf" }) + { + if (schemaObject[keyword] is JsonArray alternatives && alternatives.Any(SchemaAllowsNull)) + { + return true; + } + } + + return false; + } + private static CallToolResult ConvertAIContentEnumerableToCallToolResult(IEnumerable contentItems, JsonElement? structuredContent) { List contentList = []; diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index 2a7a9af55..cc05d1b16 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -533,6 +533,53 @@ public async Task StructuredOutput_Enabled_RequiredNullablePropertiesInClass_Mat AssertMatchesJsonSchema(tool.ProtocolTool.OutputSchema.Value, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_OmitsOptionalNullProperty() + { + JsonElement outputSchema = JsonDocument.Parse("""{"type":"object","properties":{"id":{"type":"string"},"note":{"type":"string"}},"required":["id"]}""").RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutput { Id = "42", Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.False(result.StructuredContent.Value.TryGetProperty("note", out _)); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + public async Task StructuredOutput_Enabled_ExplicitSchema_EmitsRequiredNullableValueWithIgnorePolicies(string? note) + { + JsonElement outputSchema = JsonDocument.Parse("""{"type":"object","properties":{"id":{"type":"string"},"defaultedNote":{"type":["string","null"]},"ignoredNote":{"type":["string","null"]}},"required":["id","defaultedNote","ignoredNote"]}""").RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutputWithMemberIgnore { Id = "42", DefaultedNote = note, IgnoredNote = note }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + foreach (string propertyName in new[] { "defaultedNote", "ignoredNote" }) + { + JsonElement noteElement = result.StructuredContent.Value.GetProperty(propertyName); + Assert.Equal(note is null ? JsonValueKind.Null : JsonValueKind.String, noteElement.ValueKind); + Assert.Equal(note, noteElement.GetString()); + } + + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_VoidReturningTools_ReturnsExpectedSchema() { @@ -1152,6 +1199,23 @@ private sealed class RequiredNullablePropertiesOutput public required string? Note { get; init; } } + private sealed class ExplicitSchemaOutput + { + public required string Id { get; init; } + + public string? Note { get; init; } + } + + private sealed class ExplicitSchemaOutputWithMemberIgnore + { + public required string Id { get; init; } + + public string? DefaultedNote { get; init; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? IgnoredNote { get; init; } + } + private static JsonSerializerOptions CreateSerializerOptionsWithPerson() { JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions); From 55b7972abd73edb4786f6220862d8644defd0474 Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Wed, 21 Oct 2026 19:54:01 +0800 Subject: [PATCH 3/9] Preserve schema-required nullable output members Signed-off-by: mikemikimike <13286568797@163.com> --- .../Server/AIFunctionMcpServerTool.cs | 532 ++++++++++++++++-- .../Server/McpServerToolTests.cs | 152 +++++ 2 files changed, 648 insertions(+), 36 deletions(-) diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index 4bef486af..75fbd8531 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -6,6 +6,8 @@ using System.Reflection; using System.Text.Json; using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using System.Text.Json.Serialization.Metadata; using System.Text.RegularExpressions; namespace ModelContextProtocol.Server; @@ -672,15 +674,41 @@ private static void RewriteRefPointers(JsonNode? node) return null; } - JsonElement? elementResult = aiFunctionResult switch + JsonElement? elementResult; + JsonElement? elementResultIncludingDefaults; + switch (aiFunctionResult) { - JsonElement jsonElement => jsonElement, - JsonNode node => JsonSerializer.SerializeToElement(node, McpJsonUtilities.JsonContext.Default.JsonNode), - null => null, - _ => JsonSerializer.SerializeToElement(aiFunctionResult, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object))), - }; + case JsonElement jsonElement: + elementResult = elementResultIncludingDefaults = jsonElement; + break; + case JsonNode node: + elementResult = elementResultIncludingDefaults = JsonSerializer.SerializeToElement(node, McpJsonUtilities.JsonContext.Default.JsonNode); + break; + case null: + elementResult = elementResultIncludingDefaults = null; + break; + default: + JsonSerializerOptions serializerOptions = AIFunction.JsonSerializerOptions; + JsonTypeInfo typeInfo = serializerOptions.GetTypeInfo(typeof(object)); + elementResult = JsonSerializer.SerializeToElement(aiFunctionResult, typeInfo); + + if (serializerOptions.DefaultIgnoreCondition is JsonIgnoreCondition.Never) + { + elementResultIncludingDefaults = elementResult; + } + else + { + JsonSerializerOptions includeDefaultsOptions = new(serializerOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.Never, + }; + elementResultIncludingDefaults = JsonSerializer.SerializeToElement( + aiFunctionResult, includeDefaultsOptions.GetTypeInfo(typeof(object))); + } + break; + } - elementResult = AddMissingRequiredNullableProperties(elementResult); + elementResult = AddMissingRequiredNullableProperties(elementResult, elementResultIncludingDefaults); // Pre-SEP-2106 clients expect the {"result": } envelope for non-object // schemas. SEP-2106 clients see the natural shape. The classification is decided @@ -700,7 +728,7 @@ private static void RewriteRefPointers(JsonNode? node) return elementResult; } - private JsonElement? AddMissingRequiredNullableProperties(JsonElement? elementResult) + private JsonElement? AddMissingRequiredNullableProperties(JsonElement? elementResult, JsonElement? elementResultIncludingDefaults) { if (elementResult is not { } result || ProtocolTool.OutputSchema is not { } outputSchema || @@ -710,8 +738,16 @@ result.ValueKind is not (JsonValueKind.Object or JsonValueKind.Array)) } JsonNode? resultNode = JsonSerializer.SerializeToNode(result, McpJsonUtilities.JsonContext.Default.JsonElement); + JsonNode? resultNodeIncludingDefaults = elementResultIncludingDefaults is { } completeResult + ? JsonSerializer.SerializeToNode(completeResult, McpJsonUtilities.JsonContext.Default.JsonElement) + : null; JsonNode? schemaNode = JsonSerializer.SerializeToNode(outputSchema, McpJsonUtilities.JsonContext.Default.JsonElement); - if (resultNode is null || schemaNode is null || !AddMissingRequiredNullableProperties(resultNode, schemaNode)) + if (resultNode is null || schemaNode is null || !AddMissingRequiredNullableProperties( + resultNode, + resultNodeIncludingDefaults, + schemaNode, + schemaNode, + new HashSet(StringComparer.Ordinal))) { return elementResult; } @@ -719,35 +755,101 @@ result.ValueKind is not (JsonValueKind.Object or JsonValueKind.Array)) return JsonSerializer.SerializeToElement(resultNode, McpJsonUtilities.JsonContext.Default.JsonNode); } - private static bool AddMissingRequiredNullableProperties(JsonNode resultNode, JsonNode schemaNode) + private static bool AddMissingRequiredNullableProperties( + JsonNode resultNode, + JsonNode? resultNodeIncludingDefaults, + JsonNode? schemaNode, + JsonNode schemaRoot, + HashSet activeReferences) { + if (schemaNode is JsonValue schemaValue && schemaValue.TryGetValue(out _)) + { + return false; + } + + if (schemaNode is not JsonObject schemaObject) + { + return false; + } + bool changed = false; + if (schemaObject["$ref"] is JsonValue refValue && refValue.TryGetValue(out string? refText) && + refText is not null && activeReferences.Add(refText)) + { + if (TryResolveLocalReference(schemaRoot, refText, out JsonNode? referencedSchema)) + { + changed |= AddMissingRequiredNullableProperties(resultNode, resultNodeIncludingDefaults, referencedSchema, schemaRoot, activeReferences); + } + activeReferences.Remove(refText); + } - if (resultNode is JsonObject resultObject && schemaNode is JsonObject schemaObject) + if (resultNode is JsonObject resultObject) { - JsonArray? required = schemaObject["required"] as JsonArray; + JsonObject? resultObjectIncludingDefaults = resultNodeIncludingDefaults as JsonObject; JsonObject? properties = schemaObject["properties"] as JsonObject; - - if (properties is not null) + JsonObject? patternProperties = schemaObject["patternProperties"] as JsonObject; + foreach (string propertyName in resultObject.Select(static property => property.Key).ToArray()) { - foreach (KeyValuePair property in resultObject) + if (resultObject[propertyName] is not { } propertyValue) { - if (property.Value is not null && properties.TryGetPropertyValue(property.Key, out JsonNode? propertySchema)) + continue; + } + + List propertySchemas = []; + JsonNode? propertySchema = null; + bool matchedPropertySchema = properties?.TryGetPropertyValue(propertyName, out propertySchema) is true; + if (matchedPropertySchema && propertySchema is not null) + { + propertySchemas.Add(propertySchema); + } + + if (patternProperties is not null) + { + foreach (KeyValuePair patternProperty in patternProperties) { - changed |= AddMissingRequiredNullableProperties(property.Value, propertySchema!); + if (patternProperty.Value is not null && Regex.IsMatch(propertyName, patternProperty.Key) && + !propertySchemas.Contains(patternProperty.Value)) + { + propertySchemas.Add(patternProperty.Value); + matchedPropertySchema = true; + } } } + + if (!matchedPropertySchema && schemaObject["additionalProperties"] is JsonNode additionalProperties) + { + propertySchemas.Add(additionalProperties); + } + + JsonNode? completePropertyValue = null; + resultObjectIncludingDefaults?.TryGetPropertyValue(propertyName, out completePropertyValue); + foreach (JsonNode childSchema in propertySchemas) + { + changed |= AddMissingRequiredNullableProperties(propertyValue, completePropertyValue, childSchema, schemaRoot, new HashSet(StringComparer.Ordinal)); + } } - if (required is not null && properties is not null) + if (schemaObject["required"] is JsonArray required && properties is not null) { foreach (JsonNode? requiredProperty in required) { string? propertyName = requiredProperty?.GetValue(); - if (propertyName is not null && - !resultObject.ContainsKey(propertyName) && - properties.TryGetPropertyValue(propertyName, out JsonNode? propertySchema) && - SchemaAllowsNull(propertySchema)) + if (propertyName is null || resultObject.ContainsKey(propertyName) || + !properties.TryGetPropertyValue(propertyName, out JsonNode? propertySchema)) + { + continue; + } + + bool allowsNull = SchemaAllowsNull(propertySchema, schemaRoot, new HashSet(StringComparer.Ordinal)); + if (resultObjectIncludingDefaults?.TryGetPropertyValue(propertyName, out JsonNode? completePropertyValue) is true) + { + if (completePropertyValue is not null || allowsNull) + { + resultObject[propertyName] = completePropertyValue?.DeepClone(); + changed = true; + } + } + else if (allowsNull) { resultObject[propertyName] = null; changed = true; @@ -755,24 +857,289 @@ private static bool AddMissingRequiredNullableProperties(JsonNode resultNode, Js } } } - else if (resultNode is JsonArray resultArray && - schemaNode is JsonObject { } arraySchema && - arraySchema["items"] is JsonNode itemSchema) + + if (resultNode is JsonArray resultArray) + { + JsonArray? resultArrayIncludingDefaults = resultNodeIncludingDefaults as JsonArray; + JsonArray? prefixItems = schemaObject["prefixItems"] as JsonArray; + JsonNode? items = schemaObject["items"]; + for (int i = 0; i < resultArray.Count; i++) + { + JsonNode? itemSchema = prefixItems is not null && i < prefixItems.Count ? prefixItems[i] : items; + if (resultArray[i] is { } item && itemSchema is not null) + { + JsonNode? completeItem = resultArrayIncludingDefaults is not null && i < resultArrayIncludingDefaults.Count + ? resultArrayIncludingDefaults[i] + : null; + changed |= AddMissingRequiredNullableProperties(item, completeItem, itemSchema, schemaRoot, new HashSet(StringComparer.Ordinal)); + } + } + } + + if (schemaObject["allOf"] is JsonArray allOf) { - foreach (JsonNode? item in resultArray) + foreach (JsonNode? branch in allOf) { - if (item is not null) + if (branch is not null) { - changed |= AddMissingRequiredNullableProperties(item, itemSchema); + changed |= AddMissingRequiredNullableProperties(resultNode, resultNodeIncludingDefaults, branch, schemaRoot, new HashSet(StringComparer.Ordinal)); } } } + if (schemaObject["anyOf"] is JsonArray anyOf) + { + changed |= ApplyApplicableCompositionBranch(resultNode, resultNodeIncludingDefaults, anyOf, schemaRoot, exactlyOne: false); + } + + if (schemaObject["oneOf"] is JsonArray oneOf) + { + changed |= ApplyApplicableCompositionBranch(resultNode, resultNodeIncludingDefaults, oneOf, schemaRoot, exactlyOne: true); + } + return changed; } - private static bool SchemaAllowsNull(JsonNode? schemaNode) + private static bool ApplyApplicableCompositionBranch( + JsonNode resultNode, + JsonNode? resultNodeIncludingDefaults, + JsonArray branches, + JsonNode schemaRoot, + bool exactlyOne) + { + List applicableBranches = []; + foreach (JsonNode? branch in branches) + { + if (branch is not null && SchemaCouldMatch(resultNode, branch, schemaRoot, new HashSet(StringComparer.Ordinal))) + { + applicableBranches.Add(branch); + } + } + + if (applicableBranches.Count == 0 || (exactlyOne && applicableBranches.Count != 1)) + { + return false; + } + + IEnumerable branchesToApply = exactlyOne + ? applicableBranches + : applicableBranches.OrderBy(branch => CountMissingNullableRequirements(resultNode, branch, schemaRoot, new HashSet(StringComparer.Ordinal))).Take(1); + bool changed = false; + foreach (JsonNode branch in branchesToApply) + { + changed |= AddMissingRequiredNullableProperties(resultNode, resultNodeIncludingDefaults, branch, schemaRoot, new HashSet(StringComparer.Ordinal)); + } + + return changed; + } + + private static int CountMissingNullableRequirements(JsonNode resultNode, JsonNode schemaNode, JsonNode schemaRoot, HashSet activeReferences) + { + if (schemaNode is not JsonObject schemaObject) + { + return 0; + } + + int count = 0; + if (resultNode is JsonObject resultObject && schemaObject["required"] is JsonArray required && schemaObject["properties"] is JsonObject properties) + { + foreach (JsonNode? requiredProperty in required) + { + string? propertyName = requiredProperty?.GetValue(); + if (propertyName is not null && !resultObject.ContainsKey(propertyName) && + properties.TryGetPropertyValue(propertyName, out JsonNode? propertySchema) && + SchemaAllowsNull(propertySchema, schemaRoot, new HashSet(StringComparer.Ordinal))) + { + count++; + } + } + } + + if (schemaObject["$ref"] is JsonValue refValue && refValue.TryGetValue(out string? refText) && + refText is not null && activeReferences.Add(refText) && TryResolveLocalReference(schemaRoot, refText, out JsonNode? referencedSchema)) + { + count += CountMissingNullableRequirements(resultNode, referencedSchema!, schemaRoot, activeReferences); + activeReferences.Remove(refText); + } + + if (schemaObject["allOf"] is JsonArray allOf) + { + foreach (JsonNode? branch in allOf) + { + if (branch is not null) + { + count += CountMissingNullableRequirements(resultNode, branch, schemaRoot, new HashSet(StringComparer.Ordinal)); + } + } + } + + return count; + } + + private static bool SchemaCouldMatch(JsonNode? instance, JsonNode? schemaNode, JsonNode schemaRoot, HashSet activeReferences) + { + if (schemaNode is JsonValue booleanSchema && booleanSchema.TryGetValue(out bool isAllowed)) + { + return isAllowed; + } + + if (schemaNode is not JsonObject schemaObject) + { + return true; + } + + if (schemaObject["$ref"] is JsonValue refValue && refValue.TryGetValue(out string? refText) && + refText is not null && activeReferences.Add(refText)) + { + if (!TryResolveLocalReference(schemaRoot, refText, out JsonNode? referencedSchema) || + !SchemaCouldMatch(instance, referencedSchema, schemaRoot, activeReferences)) + { + activeReferences.Remove(refText); + return false; + } + activeReferences.Remove(refText); + } + + if (schemaObject["type"] is JsonValue typeValue && typeValue.TryGetValue(out string? type) && + !ValueMatchesType(instance, type)) + { + return false; + } + + if (schemaObject["type"] is JsonArray types && !types.Any(typeNode => + typeNode is JsonValue typeName && typeName.TryGetValue(out string? name) && ValueMatchesType(instance, name))) + { + return false; + } + + if (schemaObject.TryGetPropertyValue("const", out JsonNode? constValue) && !JsonNodesEqual(instance, constValue)) + { + return false; + } + + if (schemaObject["enum"] is JsonArray enumValues && !enumValues.Any(value => JsonNodesEqual(instance, value))) + { + return false; + } + + if (instance is JsonObject instanceObject) + { + if (schemaObject["required"] is JsonArray required && schemaObject["properties"] is JsonObject properties) + { + foreach (JsonNode? requiredProperty in required) + { + string? propertyName = requiredProperty?.GetValue(); + if (propertyName is not null && !instanceObject.ContainsKey(propertyName) && + (!properties.TryGetPropertyValue(propertyName, out JsonNode? propertySchema) || + !SchemaAllowsNull(propertySchema, schemaRoot, new HashSet(StringComparer.Ordinal)))) + { + return false; + } + } + } + + JsonObject? propertiesForInstance = schemaObject["properties"] as JsonObject; + JsonObject? patternProperties = schemaObject["patternProperties"] as JsonObject; + foreach (KeyValuePair property in instanceObject) + { + bool matched = false; + if (propertiesForInstance?.TryGetPropertyValue(property.Key, out JsonNode? propertySchema) is true) + { + matched = true; + if (!SchemaCouldMatch(property.Value, propertySchema, schemaRoot, new HashSet(StringComparer.Ordinal))) + { + return false; + } + } + + if (patternProperties is not null) + { + foreach (KeyValuePair patternProperty in patternProperties) + { + if (Regex.IsMatch(property.Key, patternProperty.Key)) + { + matched = true; + if (!SchemaCouldMatch(property.Value, patternProperty.Value, schemaRoot, new HashSet(StringComparer.Ordinal))) + { + return false; + } + } + } + } + + if (!matched && schemaObject["additionalProperties"] is JsonValue additionalBoolean && + additionalBoolean.TryGetValue(out bool allowsAdditionalProperties) && !allowsAdditionalProperties) + { + return false; + } + } + } + + if (instance is JsonArray instanceArray) + { + JsonArray? prefixItems = schemaObject["prefixItems"] as JsonArray; + JsonNode? items = schemaObject["items"]; + for (int i = 0; i < instanceArray.Count; i++) + { + JsonNode? itemSchema = prefixItems is not null && i < prefixItems.Count ? prefixItems[i] : items; + if (itemSchema is not null && !SchemaCouldMatch(instanceArray[i], itemSchema, schemaRoot, new HashSet(StringComparer.Ordinal))) + { + return false; + } + } + } + + if (schemaObject["allOf"] is JsonArray allOf && + allOf.Any(branch => !SchemaCouldMatch(instance, branch, schemaRoot, new HashSet(StringComparer.Ordinal)))) + { + return false; + } + + if (schemaObject["anyOf"] is JsonArray anyOf && + !anyOf.Any(branch => SchemaCouldMatch(instance, branch, schemaRoot, new HashSet(StringComparer.Ordinal)))) + { + return false; + } + + if (schemaObject["oneOf"] is JsonArray oneOf && + oneOf.Count(branch => SchemaCouldMatch(instance, branch, schemaRoot, new HashSet(StringComparer.Ordinal))) != 1) + { + return false; + } + + return true; + } + + private static bool ValueMatchesType(JsonNode? value, string? type) => type switch { + "null" => value is null, + "object" => value is JsonObject, + "array" => value is JsonArray, + "string" => value is JsonValue jsonValue && jsonValue.TryGetValue(out _), + "boolean" => value is JsonValue jsonValue && jsonValue.TryGetValue(out _), + "integer" => value is JsonValue jsonValue && jsonValue.TryGetValue(out JsonElement element) && element.ValueKind == JsonValueKind.Number && element.TryGetInt64(out _), + "number" => value is JsonValue jsonValue && jsonValue.TryGetValue(out JsonElement element) && element.ValueKind == JsonValueKind.Number, + _ => true, + }; + + private static bool JsonNodesEqual(JsonNode? left, JsonNode? right) + { + if (left is null || right is null) + { + return left is null && right is null; + } + + return JsonSerializer.SerializeToElement(left, McpJsonUtilities.JsonContext.Default.JsonNode).GetRawText() == + JsonSerializer.SerializeToElement(right, McpJsonUtilities.JsonContext.Default.JsonNode).GetRawText(); + } + + private static bool SchemaAllowsNull(JsonNode? schemaNode, JsonNode schemaRoot, HashSet activeReferences) + { + if (schemaNode is null) + { + return true; + } + if (schemaNode is JsonValue schemaValue && schemaValue.TryGetValue(out bool allowsNull)) { return allowsNull; @@ -783,26 +1150,119 @@ private static bool SchemaAllowsNull(JsonNode? schemaNode) return false; } - if (schemaObject["type"] is JsonValue typeValue && typeValue.TryGetValue(out string? type)) + if (schemaObject["$ref"] is JsonValue refValue && refValue.TryGetValue(out string? refText) && refText is not null) { - return type == "null"; + if (!activeReferences.Add(refText)) + { + return true; + } + + bool referenceAllowsNull = TryResolveLocalReference(schemaRoot, refText, out JsonNode? referencedSchema) && + SchemaAllowsNull(referencedSchema, schemaRoot, activeReferences); + activeReferences.Remove(refText); + if (!referenceAllowsNull) + { + return false; + } + } + + if (schemaObject["type"] is JsonValue typeValue && typeValue.TryGetValue(out string? type) && type != "null") + { + return false; } if (schemaObject["type"] is JsonArray types && - types.Any(type => type is JsonValue value && value.TryGetValue(out string? typeName) && typeName == "null")) + !types.Any(typeNode => typeNode is JsonValue value && value.TryGetValue(out string? name) && name == "null")) + { + return false; + } + + if (schemaObject.TryGetPropertyValue("const", out JsonNode? constValue) && constValue is not null) + { + return false; + } + + if (schemaObject["enum"] is JsonArray enumValues && !enumValues.Any(static value => value is null)) + { + return false; + } + + if (schemaObject["allOf"] is JsonArray allOf && + allOf.Any(branch => !SchemaAllowsNull(branch, schemaRoot, new HashSet(StringComparer.Ordinal)))) + { + return false; + } + + if (schemaObject["anyOf"] is JsonArray anyOf && + !anyOf.Any(branch => SchemaAllowsNull(branch, schemaRoot, new HashSet(StringComparer.Ordinal)))) + { + return false; + } + + if (schemaObject["oneOf"] is JsonArray oneOf && + oneOf.Count(branch => SchemaAllowsNull(branch, schemaRoot, new HashSet(StringComparer.Ordinal))) != 1) { + return false; + } + + if (schemaObject["not"] is JsonNode notSchema && SchemaAllowsNull(notSchema, schemaRoot, new HashSet(StringComparer.Ordinal))) + { + return false; + } + + if (schemaObject["if"] is JsonNode ifSchema) + { + bool ifMatchesNull = SchemaCouldMatch(null, ifSchema, schemaRoot, new HashSet(StringComparer.Ordinal)); + JsonNode? conditionalBranch = ifMatchesNull ? schemaObject["then"] : schemaObject["else"]; + if (conditionalBranch is not null && !SchemaAllowsNull(conditionalBranch, schemaRoot, new HashSet(StringComparer.Ordinal))) + { + return false; + } + } + + return true; + } + + private static bool TryResolveLocalReference(JsonNode schemaRoot, string reference, out JsonNode? resolvedSchema) + { + resolvedSchema = null; + if (!reference.StartsWith('#')) + { + return false; + } + + string pointer = Uri.UnescapeDataString(reference[1..]); + if (pointer.Length == 0) + { + resolvedSchema = schemaRoot; return true; } - foreach (string keyword in new[] { "anyOf", "oneOf" }) + if (!pointer.StartsWith('/')) { - if (schemaObject[keyword] is JsonArray alternatives && alternatives.Any(SchemaAllowsNull)) + return false; + } + + JsonNode? current = schemaRoot; + foreach (string rawSegment in pointer[1..].Split('/')) + { + string segment = rawSegment.Replace("~1", "/", StringComparison.Ordinal).Replace("~0", "~", StringComparison.Ordinal); + if (current is JsonObject currentObject && currentObject.TryGetPropertyValue(segment, out JsonNode? child)) { - return true; + current = child; + } + else if (current is JsonArray currentArray && int.TryParse(segment, out int index) && index >= 0 && index < currentArray.Count) + { + current = currentArray[index]; + } + else + { + return false; } } - return false; + resolvedSchema = current; + return true; } private static CallToolResult ConvertAIContentEnumerableToCallToolResult(IEnumerable contentItems, JsonElement? structuredContent) diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index cc05d1b16..21496ce93 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -580,6 +580,151 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_EmitsRequiredNullableV AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_PreservesRequiredDefaultValue() + { + JsonElement outputSchema = JsonDocument.Parse("""{"type":"object","properties":{"id":{"type":"string"},"count":{"type":["integer","null"]}},"required":["id","count"]}""").RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutputWithDefaultValue { Id = "42", Count = 0 }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(0, result.StructuredContent.Value.GetProperty("count").GetInt32()); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_TraversesDictionaryValuesAndReferences() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "$defs": { + "entry": { + "type": "object", + "properties": { + "id": { "type": "string" }, + "note": { "type": ["string", "null"] } + }, + "required": ["id", "note"] + } + }, + "type": "object", + "additionalProperties": { "$ref": "#/$defs/entry" } + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new Dictionary { ["entry"] = new() { Id = "42", Note = null } }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("entry").GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_TraversesAllOfRequirements() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "type": "object", + "properties": { "id": { "type": "string" } }, + "allOf": [ + { + "properties": { "note": { "type": ["string", "null"] } }, + "required": ["note"] + } + ], + "required": ["id"] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutput { Id = "42", Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_UsesPrefixItemInsteadOfTrailingItemsSchema() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "type": "array", + "prefixItems": [ + { + "type": "object", + "properties": { "id": { "type": "string" } }, + "required": ["id"], + "additionalProperties": false + } + ], + "items": { + "type": "object", + "properties": { + "id": { "type": "string" }, + "note": { "type": ["string", "null"] } + }, + "required": ["id", "note"] + } + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new[] { new ExplicitSchemaOutput { Id = "42", Note = null } }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.False(result.StructuredContent.Value[0].TryGetProperty("note", out _)); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Theory] + [InlineData("{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"},\"note\":{}},\"required\":[\"id\",\"note\"]}")] + [InlineData("{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"},\"note\":{\"const\":null}},\"required\":[\"id\",\"note\"]}")] + public async Task StructuredOutput_Enabled_ExplicitSchema_RecognizesPermissiveNullSchemas(string schema) + { + JsonElement outputSchema = JsonDocument.Parse(schema).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutput { Id = "42", Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_VoidReturningTools_ReturnsExpectedSchema() { @@ -1216,6 +1361,13 @@ private sealed class ExplicitSchemaOutputWithMemberIgnore public string? IgnoredNote { get; init; } } + private sealed class ExplicitSchemaOutputWithDefaultValue + { + public required string Id { get; init; } + + public int Count { get; init; } + } + private static JsonSerializerOptions CreateSerializerOptionsWithPerson() { JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions); From f1a39f86ccd4b6abdfba7ab3121444b211794b91 Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Thu, 22 Oct 2026 15:31:42 +0800 Subject: [PATCH 4/9] Fix structured output required member restoration --- .../ModelContextProtocol.Core.csproj | 1 + .../Server/AIFunctionMcpServerTool.cs | 872 ++++++++++++------ .../Server/McpServerToolTests.cs | 318 +++++++ 3 files changed, 899 insertions(+), 292 deletions(-) diff --git a/src/ModelContextProtocol.Core/ModelContextProtocol.Core.csproj b/src/ModelContextProtocol.Core/ModelContextProtocol.Core.csproj index 3fbef0377..20bacdddc 100644 --- a/src/ModelContextProtocol.Core/ModelContextProtocol.Core.csproj +++ b/src/ModelContextProtocol.Core/ModelContextProtocol.Core.csproj @@ -62,6 +62,7 @@ + diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index 75fbd8531..dc710d015 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -1,7 +1,10 @@ using Microsoft.Extensions.AI; +using Json.Schema; using Microsoft.Extensions.DependencyInjection; using ModelContextProtocol.Protocol; +using System.Collections; using System.ComponentModel; +using System.Globalization; using System.Diagnostics; using System.Reflection; using System.Text.Json; @@ -16,6 +19,7 @@ namespace ModelContextProtocol.Server; internal sealed partial class AIFunctionMcpServerTool : McpServerTool { private readonly IReadOnlyList _metadata; + private readonly Lazy _outputSchema; /// /// Creates an instance for a method, specified via a instance. @@ -225,6 +229,7 @@ private AIFunctionMcpServerTool(AIFunction function, Tool tool, IServiceProvider AIFunction = function; ProtocolTool = tool; + _outputSchema = new Lazy(BuildOutputSchema, LazyThreadSafetyMode.ExecutionAndPublication); _metadata = metadata; } @@ -708,7 +713,7 @@ private static void RewriteRefPointers(JsonNode? node) break; } - elementResult = AddMissingRequiredNullableProperties(elementResult, elementResultIncludingDefaults); + elementResult = AddMissingRequiredNullableProperties(elementResult, elementResultIncludingDefaults, aiFunctionResult); // Pre-SEP-2106 clients expect the {"result": } envelope for non-object // schemas. SEP-2106 clients see the natural shape. The classification is decided @@ -728,7 +733,10 @@ private static void RewriteRefPointers(JsonNode? node) return elementResult; } - private JsonElement? AddMissingRequiredNullableProperties(JsonElement? elementResult, JsonElement? elementResultIncludingDefaults) + private JsonElement? AddMissingRequiredNullableProperties( + JsonElement? elementResult, + JsonElement? elementResultIncludingDefaults, + object? originalResult) { if (elementResult is not { } result || ProtocolTool.OutputSchema is not { } outputSchema || @@ -742,491 +750,771 @@ result.ValueKind is not (JsonValueKind.Object or JsonValueKind.Array)) ? JsonSerializer.SerializeToNode(completeResult, McpJsonUtilities.JsonContext.Default.JsonElement) : null; JsonNode? schemaNode = JsonSerializer.SerializeToNode(outputSchema, McpJsonUtilities.JsonContext.Default.JsonElement); - if (resultNode is null || schemaNode is null || !AddMissingRequiredNullableProperties( - resultNode, - resultNodeIncludingDefaults, - schemaNode, - schemaNode, - new HashSet(StringComparer.Ordinal))) + JsonSchema? schema = _outputSchema.Value; + if (resultNode is null || schemaNode is null || schema is null) { return elementResult; } - return JsonSerializer.SerializeToElement(resultNode, McpJsonUtilities.JsonContext.Default.JsonNode); + JsonNode? repairedResult = RepairStructuredResult( + resultNode, + resultNodeIncludingDefaults, + schemaNode, + schema, + originalResult, + AIFunction.JsonSerializerOptions); + + return repairedResult is null + ? elementResult + : JsonSerializer.SerializeToElement(repairedResult, McpJsonUtilities.JsonContext.Default.JsonNode); } - private static bool AddMissingRequiredNullableProperties( + /// + /// Restores omitted required properties only when the complete result validates against the + /// published schema. The serializer can omit a value because of either the global ignore + /// policy or a member-level . Candidate values are therefore + /// read from the source object when the second serialization snapshot does not contain them. + /// + private static JsonNode? RepairStructuredResult( JsonNode resultNode, JsonNode? resultNodeIncludingDefaults, - JsonNode? schemaNode, - JsonNode schemaRoot, - HashSet activeReferences) + JsonNode schemaNode, + JsonSchema schema, + object? originalResult, + JsonSerializerOptions serializerOptions) { - if (schemaNode is JsonValue schemaValue && schemaValue.TryGetValue(out _)) + // An explicit schema may deliberately omit optional nulls. Preserve that valid output + // before looking for missing required members. + if (IsSchemaValid(schema, resultNode)) + { + return null; + } + + JsonNode workingResult = resultNode.DeepClone(); + for (int pass = 0; pass < 32; pass++) + { + Dictionary contexts = new(StringComparer.Ordinal); + CollectSchemaContexts( + workingResult, + schemaNode, + schemaNode, + string.Empty, + contexts, + new HashSet(StringComparer.Ordinal)); + + List candidates = CreateRepairCandidates( + workingResult, + resultNodeIncludingDefaults, + originalResult, + serializerOptions, + contexts); + + if (candidates.Count == 0 || !TryFindValidCandidateSet(schema, workingResult, candidates, out JsonNode? repaired)) + { + return null; + } + + if (repaired is null) + { + return null; + } + + workingResult = repaired; + if (IsSchemaValid(schema, workingResult)) + { + return workingResult; + } + } + + return null; + } + + private static bool TryBuildJsonSchema(JsonNode schemaNode, out JsonSchema? schema) + { + try + { + JsonElement schemaElement = JsonSerializer.SerializeToElement(schemaNode, McpJsonUtilities.JsonContext.Default.JsonNode); + schema = JsonSchema.Build(schemaElement); + return true; + } + catch (Exception ex) when (ex is JsonException or InvalidOperationException or ArgumentException) { + schema = null; return false; } + } - if (schemaNode is not JsonObject schemaObject) + private JsonSchema? BuildOutputSchema() + { + if (ProtocolTool.OutputSchema is not { } outputSchema) + { + return null; + } + + JsonNode? schemaNode = JsonSerializer.SerializeToNode(outputSchema, McpJsonUtilities.JsonContext.Default.JsonElement); + return schemaNode is not null && TryBuildJsonSchema(schemaNode, out JsonSchema? schema) + ? schema + : null; + } + + private static bool IsSchemaValid(JsonSchema schema, JsonNode instance) + { + try + { + JsonElement instanceElement = JsonSerializer.SerializeToElement(instance, McpJsonUtilities.JsonContext.Default.JsonNode); + return schema.Evaluate(instanceElement).IsValid; + } + catch (Exception ex) when (ex is JsonException or InvalidOperationException or ArgumentException) { return false; } + } - bool changed = false; - if (schemaObject["$ref"] is JsonValue refValue && refValue.TryGetValue(out string? refText) && - refText is not null && activeReferences.Add(refText)) + private static List CreateRepairCandidates( + JsonNode resultNode, + JsonNode? resultNodeIncludingDefaults, + object? originalResult, + JsonSerializerOptions serializerOptions, + Dictionary contexts) + { + List candidates = []; + HashSet seen = new(StringComparer.Ordinal); + + foreach (KeyValuePair contextEntry in contexts) { - if (TryResolveLocalReference(schemaRoot, refText, out JsonNode? referencedSchema)) + if (!TryGetNodeAtPath(resultNode, contextEntry.Key, out JsonNode? parentNode) || parentNode is not JsonObject parentObject) { - changed |= AddMissingRequiredNullableProperties(resultNode, resultNodeIncludingDefaults, referencedSchema, schemaRoot, activeReferences); + continue; } - activeReferences.Remove(refText); - } - if (resultNode is JsonObject resultObject) - { - JsonObject? resultObjectIncludingDefaults = resultNodeIncludingDefaults as JsonObject; - JsonObject? properties = schemaObject["properties"] as JsonObject; - JsonObject? patternProperties = schemaObject["patternProperties"] as JsonObject; - foreach (string propertyName in resultObject.Select(static property => property.Key).ToArray()) + foreach (string propertyName in contextEntry.Value.Required) { - if (resultObject[propertyName] is not { } propertyValue) + if (parentObject.ContainsKey(propertyName)) { continue; } - List propertySchemas = []; - JsonNode? propertySchema = null; - bool matchedPropertySchema = properties?.TryGetPropertyValue(propertyName, out propertySchema) is true; - if (matchedPropertySchema && propertySchema is not null) + string candidateKey = contextEntry.Key + "\u0000" + propertyName; + if (!seen.Add(candidateKey)) { - propertySchemas.Add(propertySchema); + continue; } - if (patternProperties is not null) + bool hasCompleteValue = TryGetNodeAtPath( + resultNodeIncludingDefaults, + AppendJsonPointer(contextEntry.Key, propertyName), + out JsonNode? completeValue); + + bool hasSourceValue = TryGetSourceValue( + originalResult, + contextEntry.Key, + propertyName, + serializerOptions, + out JsonNode? sourceValue); + + if (hasCompleteValue) { - foreach (KeyValuePair patternProperty in patternProperties) - { - if (patternProperty.Value is not null && Regex.IsMatch(propertyName, patternProperty.Key) && - !propertySchemas.Contains(patternProperty.Value)) - { - propertySchemas.Add(patternProperty.Value); - matchedPropertySchema = true; - } - } + candidates.Add(new RepairCandidate(contextEntry.Key, propertyName, completeValue?.DeepClone())); } - - if (!matchedPropertySchema && schemaObject["additionalProperties"] is JsonNode additionalProperties) + else if (hasSourceValue) { - propertySchemas.Add(additionalProperties); + candidates.Add(new RepairCandidate(contextEntry.Key, propertyName, sourceValue)); } - - JsonNode? completePropertyValue = null; - resultObjectIncludingDefaults?.TryGetPropertyValue(propertyName, out completePropertyValue); - foreach (JsonNode childSchema in propertySchemas) + else { - changed |= AddMissingRequiredNullableProperties(propertyValue, completePropertyValue, childSchema, schemaRoot, new HashSet(StringComparer.Ordinal)); + candidates.Add(new RepairCandidate(contextEntry.Key, propertyName, null)); } } + } - if (schemaObject["required"] is JsonArray required && properties is not null) - { - foreach (JsonNode? requiredProperty in required) - { - string? propertyName = requiredProperty?.GetValue(); - if (propertyName is null || resultObject.ContainsKey(propertyName) || - !properties.TryGetPropertyValue(propertyName, out JsonNode? propertySchema)) - { - continue; - } + return candidates; + } - bool allowsNull = SchemaAllowsNull(propertySchema, schemaRoot, new HashSet(StringComparer.Ordinal)); - if (resultObjectIncludingDefaults?.TryGetPropertyValue(propertyName, out JsonNode? completePropertyValue) is true) - { - if (completePropertyValue is not null || allowsNull) - { - resultObject[propertyName] = completePropertyValue?.DeepClone(); - changed = true; - } - } - else if (allowsNull) - { - resultObject[propertyName] = null; - changed = true; - } - } - } + private static bool TryFindValidCandidateSet( + JsonSchema schema, + JsonNode originalResult, + IReadOnlyList candidates, + out JsonNode? repairedResult) + { + repairedResult = null; + if (candidates.Count == 0) + { + return false; } - if (resultNode is JsonArray resultArray) + // Prefer larger repairs, but cap schema evaluations for unusually broad unions so a + // single tool call cannot trigger an exponential search. + const int maxCandidateSetEvaluations = 256; + bool[] included = Enumerable.Repeat(true, candidates.Count).ToArray(); + int evaluations = 0; + JsonNode? matchingResult = null; + + bool TryCurrentSet() { - JsonArray? resultArrayIncludingDefaults = resultNodeIncludingDefaults as JsonArray; - JsonArray? prefixItems = schemaObject["prefixItems"] as JsonArray; - JsonNode? items = schemaObject["items"]; - for (int i = 0; i < resultArray.Count; i++) + if (evaluations >= maxCandidateSetEvaluations) { - JsonNode? itemSchema = prefixItems is not null && i < prefixItems.Count ? prefixItems[i] : items; - if (resultArray[i] is { } item && itemSchema is not null) - { - JsonNode? completeItem = resultArrayIncludingDefaults is not null && i < resultArrayIncludingDefaults.Count - ? resultArrayIncludingDefaults[i] - : null; - changed |= AddMissingRequiredNullableProperties(item, completeItem, itemSchema, schemaRoot, new HashSet(StringComparer.Ordinal)); - } + return false; } - } - if (schemaObject["allOf"] is JsonArray allOf) - { - foreach (JsonNode? branch in allOf) + evaluations++; + JsonNode candidateResult = originalResult.DeepClone(); + bool hasIncludedCandidate = false; + for (int i = 0; i < candidates.Count; i++) { - if (branch is not null) + if (included[i]) { - changed |= AddMissingRequiredNullableProperties(resultNode, resultNodeIncludingDefaults, branch, schemaRoot, new HashSet(StringComparer.Ordinal)); + ApplyRepairCandidate(candidateResult, candidates[i]); + hasIncludedCandidate = true; } } - } - if (schemaObject["anyOf"] is JsonArray anyOf) - { - changed |= ApplyApplicableCompositionBranch(resultNode, resultNodeIncludingDefaults, anyOf, schemaRoot, exactlyOne: false); - } + if (!hasIncludedCandidate || !IsSchemaValid(schema, candidateResult)) + { + return false; + } - if (schemaObject["oneOf"] is JsonArray oneOf) - { - changed |= ApplyApplicableCompositionBranch(resultNode, resultNodeIncludingDefaults, oneOf, schemaRoot, exactlyOne: true); + matchingResult = candidateResult; + return true; } - return changed; - } - - private static bool ApplyApplicableCompositionBranch( - JsonNode resultNode, - JsonNode? resultNodeIncludingDefaults, - JsonArray branches, - JsonNode schemaRoot, - bool exactlyOne) - { - List applicableBranches = []; - foreach (JsonNode? branch in branches) + bool SearchExclusions(int nextIndex, int remaining) { - if (branch is not null && SchemaCouldMatch(resultNode, branch, schemaRoot, new HashSet(StringComparer.Ordinal))) + if (remaining == 0) { - applicableBranches.Add(branch); + return TryCurrentSet(); + } + + for (int i = nextIndex; i <= candidates.Count - remaining; i++) + { + included[i] = false; + if (SearchExclusions(i + 1, remaining - 1)) + { + return true; + } + + included[i] = true; + if (evaluations >= maxCandidateSetEvaluations) + { + return false; + } } - } - if (applicableBranches.Count == 0 || (exactlyOne && applicableBranches.Count != 1)) - { return false; } - IEnumerable branchesToApply = exactlyOne - ? applicableBranches - : applicableBranches.OrderBy(branch => CountMissingNullableRequirements(resultNode, branch, schemaRoot, new HashSet(StringComparer.Ordinal))).Take(1); - bool changed = false; - foreach (JsonNode branch in branchesToApply) + if (TryCurrentSet()) { - changed |= AddMissingRequiredNullableProperties(resultNode, resultNodeIncludingDefaults, branch, schemaRoot, new HashSet(StringComparer.Ordinal)); + repairedResult = matchingResult; + return true; } - return changed; - } - - private static int CountMissingNullableRequirements(JsonNode resultNode, JsonNode schemaNode, JsonNode schemaRoot, HashSet activeReferences) - { - if (schemaNode is not JsonObject schemaObject) + for (int excludedCount = 1; excludedCount < candidates.Count; excludedCount++) { - return 0; - } + if (SearchExclusions(0, excludedCount)) + { + repairedResult = matchingResult; + return true; + } - int count = 0; - if (resultNode is JsonObject resultObject && schemaObject["required"] is JsonArray required && schemaObject["properties"] is JsonObject properties) - { - foreach (JsonNode? requiredProperty in required) + if (evaluations >= maxCandidateSetEvaluations) { - string? propertyName = requiredProperty?.GetValue(); - if (propertyName is not null && !resultObject.ContainsKey(propertyName) && - properties.TryGetPropertyValue(propertyName, out JsonNode? propertySchema) && - SchemaAllowsNull(propertySchema, schemaRoot, new HashSet(StringComparer.Ordinal))) - { - count++; - } + break; } } - if (schemaObject["$ref"] is JsonValue refValue && refValue.TryGetValue(out string? refText) && - refText is not null && activeReferences.Add(refText) && TryResolveLocalReference(schemaRoot, refText, out JsonNode? referencedSchema)) - { - count += CountMissingNullableRequirements(resultNode, referencedSchema!, schemaRoot, activeReferences); - activeReferences.Remove(refText); - } + return false; + } - if (schemaObject["allOf"] is JsonArray allOf) + private static void ApplyRepairCandidate(JsonNode resultNode, RepairCandidate candidate) + { + if (TryGetNodeAtPath(resultNode, candidate.ParentPath, out JsonNode? parentNode) && parentNode is JsonObject parentObject) { - foreach (JsonNode? branch in allOf) - { - if (branch is not null) - { - count += CountMissingNullableRequirements(resultNode, branch, schemaRoot, new HashSet(StringComparer.Ordinal)); - } - } + parentObject[candidate.PropertyName] = candidate.Value?.DeepClone(); } + } - return count; + private sealed class SchemaObjectContext + { + internal HashSet Required { get; } = new(StringComparer.Ordinal); } - private static bool SchemaCouldMatch(JsonNode? instance, JsonNode? schemaNode, JsonNode schemaRoot, HashSet activeReferences) + private sealed record RepairCandidate(string ParentPath, string PropertyName, JsonNode? Value); + + private static void CollectSchemaContexts( + JsonNode? instanceNode, + JsonNode? schemaNode, + JsonNode schemaRoot, + string instancePath, + Dictionary contexts, + HashSet activeReferences) { - if (schemaNode is JsonValue booleanSchema && booleanSchema.TryGetValue(out bool isAllowed)) + if (schemaNode is JsonValue schemaValue && schemaValue.TryGetValue(out _)) { - return isAllowed; + return; } if (schemaNode is not JsonObject schemaObject) { - return true; + return; } - if (schemaObject["$ref"] is JsonValue refValue && refValue.TryGetValue(out string? refText) && - refText is not null && activeReferences.Add(refText)) + if (schemaObject["$ref"] is JsonValue refValue && + refValue.TryGetValue(out string? refText) && + refText is not null && + activeReferences.Add(refText + "|" + instancePath)) { - if (!TryResolveLocalReference(schemaRoot, refText, out JsonNode? referencedSchema) || - !SchemaCouldMatch(instance, referencedSchema, schemaRoot, activeReferences)) + if (TryResolveLocalReference(schemaRoot, refText, out JsonNode? referencedSchema)) { - activeReferences.Remove(refText); - return false; + CollectSchemaContexts(instanceNode, referencedSchema, schemaRoot, instancePath, contexts, activeReferences); } - activeReferences.Remove(refText); - } - if (schemaObject["type"] is JsonValue typeValue && typeValue.TryGetValue(out string? type) && - !ValueMatchesType(instance, type)) - { - return false; + activeReferences.Remove(refText + "|" + instancePath); } - if (schemaObject["type"] is JsonArray types && !types.Any(typeNode => - typeNode is JsonValue typeName && typeName.TryGetValue(out string? name) && ValueMatchesType(instance, name))) + SchemaObjectContext context = GetOrCreateSchemaContext(contexts, instancePath); + if (schemaObject["required"] is JsonArray required) { - return false; - } - - if (schemaObject.TryGetPropertyValue("const", out JsonNode? constValue) && !JsonNodesEqual(instance, constValue)) - { - return false; - } - - if (schemaObject["enum"] is JsonArray enumValues && !enumValues.Any(value => JsonNodesEqual(instance, value))) - { - return false; + foreach (JsonNode? requiredProperty in required) + { + if (requiredProperty?.GetValue() is string propertyName) + { + context.Required.Add(propertyName); + } + } } - if (instance is JsonObject instanceObject) + if (schemaObject["properties"] is JsonObject properties) { - if (schemaObject["required"] is JsonArray required && schemaObject["properties"] is JsonObject properties) + foreach (KeyValuePair property in properties) { - foreach (JsonNode? requiredProperty in required) + if (property.Value is null) { - string? propertyName = requiredProperty?.GetValue(); - if (propertyName is not null && !instanceObject.ContainsKey(propertyName) && - (!properties.TryGetPropertyValue(propertyName, out JsonNode? propertySchema) || - !SchemaAllowsNull(propertySchema, schemaRoot, new HashSet(StringComparer.Ordinal)))) - { - return false; - } + continue; + } + + if (instanceNode is not JsonObject instanceObject || + !instanceObject.TryGetPropertyValue(property.Key, out JsonNode? propertyInstance) || + propertyInstance is null) + { + continue; } + + CollectSchemaContexts( + propertyInstance, + property.Value, + schemaRoot, + AppendJsonPointer(instancePath, property.Key), + contexts, + new HashSet(activeReferences, StringComparer.Ordinal)); } + } - JsonObject? propertiesForInstance = schemaObject["properties"] as JsonObject; + if (instanceNode is JsonObject objectInstance) + { JsonObject? patternProperties = schemaObject["patternProperties"] as JsonObject; - foreach (KeyValuePair property in instanceObject) + JsonNode? additionalProperties = schemaObject["additionalProperties"]; + foreach (KeyValuePair property in objectInstance) { - bool matched = false; - if (propertiesForInstance?.TryGetPropertyValue(property.Key, out JsonNode? propertySchema) is true) + if (schemaObject["properties"] is JsonObject explicitProperties && explicitProperties.ContainsKey(property.Key)) { - matched = true; - if (!SchemaCouldMatch(property.Value, propertySchema, schemaRoot, new HashSet(StringComparer.Ordinal))) - { - return false; - } + continue; } + bool matchedPattern = false; if (patternProperties is not null) { foreach (KeyValuePair patternProperty in patternProperties) { - if (Regex.IsMatch(property.Key, patternProperty.Key)) + if (patternProperty.Value is not null && Regex.IsMatch(property.Key, patternProperty.Key)) { - matched = true; - if (!SchemaCouldMatch(property.Value, patternProperty.Value, schemaRoot, new HashSet(StringComparer.Ordinal))) - { - return false; - } + matchedPattern = true; + CollectSchemaContexts( + property.Value, + patternProperty.Value, + schemaRoot, + AppendJsonPointer(instancePath, property.Key), + contexts, + new HashSet(activeReferences, StringComparer.Ordinal)); } } } - if (!matched && schemaObject["additionalProperties"] is JsonValue additionalBoolean && - additionalBoolean.TryGetValue(out bool allowsAdditionalProperties) && !allowsAdditionalProperties) + if (!matchedPattern && additionalProperties is not null) { - return false; + CollectSchemaContexts( + property.Value, + additionalProperties, + schemaRoot, + AppendJsonPointer(instancePath, property.Key), + contexts, + new HashSet(activeReferences, StringComparer.Ordinal)); } } } - if (instance is JsonArray instanceArray) + if (instanceNode is JsonArray arrayInstance) { JsonArray? prefixItems = schemaObject["prefixItems"] as JsonArray; JsonNode? items = schemaObject["items"]; - for (int i = 0; i < instanceArray.Count; i++) + for (int i = 0; i < arrayInstance.Count; i++) { JsonNode? itemSchema = prefixItems is not null && i < prefixItems.Count ? prefixItems[i] : items; - if (itemSchema is not null && !SchemaCouldMatch(instanceArray[i], itemSchema, schemaRoot, new HashSet(StringComparer.Ordinal))) + if (itemSchema is not null) { - return false; + CollectSchemaContexts( + arrayInstance[i], + itemSchema, + schemaRoot, + AppendJsonPointer(instancePath, i.ToString(CultureInfo.InvariantCulture)), + contexts, + new HashSet(activeReferences, StringComparer.Ordinal)); } } } - if (schemaObject["allOf"] is JsonArray allOf && - allOf.Any(branch => !SchemaCouldMatch(instance, branch, schemaRoot, new HashSet(StringComparer.Ordinal)))) + foreach (string compositionKeyword in new[] { "allOf", "anyOf", "oneOf" }) { - return false; + if (schemaObject[compositionKeyword] is not JsonArray composition) + { + continue; + } + + foreach (JsonNode? branch in composition) + { + if (branch is not null) + { + CollectSchemaContexts( + instanceNode, + branch, + schemaRoot, + instancePath, + contexts, + new HashSet(activeReferences, StringComparer.Ordinal)); + } + } } - if (schemaObject["anyOf"] is JsonArray anyOf && - !anyOf.Any(branch => SchemaCouldMatch(instance, branch, schemaRoot, new HashSet(StringComparer.Ordinal)))) + if (schemaObject["if"] is JsonNode ifSchema) { - return false; + if (schemaObject["then"] is JsonNode thenSchema) + { + CollectSchemaContexts(instanceNode, thenSchema, schemaRoot, instancePath, contexts, new HashSet(activeReferences, StringComparer.Ordinal)); + } + + if (schemaObject["else"] is JsonNode elseSchema) + { + CollectSchemaContexts(instanceNode, elseSchema, schemaRoot, instancePath, contexts, new HashSet(activeReferences, StringComparer.Ordinal)); + } } + } - if (schemaObject["oneOf"] is JsonArray oneOf && - oneOf.Count(branch => SchemaCouldMatch(instance, branch, schemaRoot, new HashSet(StringComparer.Ordinal))) != 1) + private static SchemaObjectContext GetOrCreateSchemaContext(Dictionary contexts, string instancePath) + { + if (!contexts.TryGetValue(instancePath, out SchemaObjectContext? context)) { - return false; + contexts[instancePath] = context = new SchemaObjectContext(); } - return true; + return context; } - private static bool ValueMatchesType(JsonNode? value, string? type) => type switch + private static string AppendJsonPointer(string parentPath, string segment) { - "null" => value is null, - "object" => value is JsonObject, - "array" => value is JsonArray, - "string" => value is JsonValue jsonValue && jsonValue.TryGetValue(out _), - "boolean" => value is JsonValue jsonValue && jsonValue.TryGetValue(out _), - "integer" => value is JsonValue jsonValue && jsonValue.TryGetValue(out JsonElement element) && element.ValueKind == JsonValueKind.Number && element.TryGetInt64(out _), - "number" => value is JsonValue jsonValue && jsonValue.TryGetValue(out JsonElement element) && element.ValueKind == JsonValueKind.Number, - _ => true, - }; - - private static bool JsonNodesEqual(JsonNode? left, JsonNode? right) + string escapedSegment = segment.Replace("~", "~0").Replace("/", "~1"); + return parentPath.Length == 0 ? "/" + escapedSegment : parentPath + "/" + escapedSegment; + } + + private static bool TryGetNodeAtPath(JsonNode? root, string path, out JsonNode? value) { - if (left is null || right is null) + value = root; + if (path.Length == 0) { - return left is null && right is null; + return root is not null; } - return JsonSerializer.SerializeToElement(left, McpJsonUtilities.JsonContext.Default.JsonNode).GetRawText() == - JsonSerializer.SerializeToElement(right, McpJsonUtilities.JsonContext.Default.JsonNode).GetRawText(); + if (!path.StartsWith("/", StringComparison.Ordinal)) + { + value = null; + return false; + } + + foreach (string rawSegment in path.Substring(1).Split('/')) + { + string segment = rawSegment.Replace("~1", "/").Replace("~0", "~"); + if (value is JsonObject objectValue && objectValue.TryGetPropertyValue(segment, out JsonNode? objectChild)) + { + value = objectChild; + } + else if (value is JsonArray arrayValue && int.TryParse(segment, NumberStyles.None, CultureInfo.InvariantCulture, out int index) && + index >= 0 && index < arrayValue.Count) + { + value = arrayValue[index]; + } + else + { + value = null; + return false; + } + } + + return true; } - private static bool SchemaAllowsNull(JsonNode? schemaNode, JsonNode schemaRoot, HashSet activeReferences) + private static bool TryGetSourceValue( + object? sourceRoot, + string parentPath, + string propertyName, + JsonSerializerOptions serializerOptions, + out JsonNode? value) { - if (schemaNode is null) + value = null; + if (sourceRoot is null || !TryGetSourceObjectAtPath(sourceRoot, parentPath, serializerOptions, out object? sourceParent) || sourceParent is null) { - return true; + return false; } - if (schemaNode is JsonValue schemaValue && schemaValue.TryGetValue(out bool allowsNull)) + if (!TryGetSourceMember( + sourceParent, + propertyName, + serializerOptions, + out object? sourceValue, + out Type? declaredType, + out JsonPropertyInfo? propertyInfo)) { - return allowsNull; + return false; } - if (schemaNode is not JsonObject schemaObject) + value = SerializeSourceValue(sourceValue, declaredType, serializerOptions, propertyInfo); + return sourceValue is null || value is not null; + } + + private static bool TryGetSourceObjectAtPath( + object sourceRoot, + string path, + JsonSerializerOptions serializerOptions, + out object? value) + { + value = sourceRoot; + if (path.Length == 0) { + return true; + } + + if (!path.StartsWith("/", StringComparison.Ordinal)) + { + value = null; return false; } - if (schemaObject["$ref"] is JsonValue refValue && refValue.TryGetValue(out string? refText) && refText is not null) + foreach (string rawSegment in path.Substring(1).Split('/')) { - if (!activeReferences.Add(refText)) + string segment = rawSegment.Replace("~1", "/").Replace("~0", "~"); + if (value is null) { - return true; + return false; + } + + if (value is JsonNode jsonNode) + { + if (jsonNode is JsonObject jsonObject && jsonObject.TryGetPropertyValue(segment, out JsonNode? childNode)) + { + value = childNode; + continue; + } + + if (jsonNode is JsonArray jsonArray && int.TryParse(segment, NumberStyles.None, CultureInfo.InvariantCulture, out int jsonIndex) && + jsonIndex >= 0 && jsonIndex < jsonArray.Count) + { + value = jsonArray[jsonIndex]; + continue; + } + + value = null; + return false; } - bool referenceAllowsNull = TryResolveLocalReference(schemaRoot, refText, out JsonNode? referencedSchema) && - SchemaAllowsNull(referencedSchema, schemaRoot, activeReferences); - activeReferences.Remove(refText); - if (!referenceAllowsNull) + if (value is JsonElement jsonElement) { + if (jsonElement.ValueKind == JsonValueKind.Object && jsonElement.TryGetProperty(segment, out JsonElement childElement)) + { + value = childElement; + continue; + } + + if (jsonElement.ValueKind == JsonValueKind.Array && int.TryParse(segment, NumberStyles.None, CultureInfo.InvariantCulture, out int elementIndex) && + elementIndex >= 0 && elementIndex < jsonElement.GetArrayLength()) + { + value = jsonElement[elementIndex]; + continue; + } + + value = null; return false; } + + if (value is IDictionary dictionary && dictionary.Contains(segment)) + { + value = dictionary[segment]; + continue; + } + + if (int.TryParse(segment, NumberStyles.None, CultureInfo.InvariantCulture, out int index) && value is IEnumerable enumerable) + { + int currentIndex = 0; + bool found = false; + foreach (object? item in enumerable) + { + if (currentIndex++ == index) + { + value = item; + found = true; + break; + } + } + + if (found) + { + continue; + } + } + + if (!TryGetSourceMember(value, segment, serializerOptions, out object? memberValue, out _, out _)) + { + value = null; + return false; + } + + value = memberValue; } - if (schemaObject["type"] is JsonValue typeValue && typeValue.TryGetValue(out string? type) && type != "null") + return true; + } + + private static bool TryGetSourceMember( + object source, + string propertyName, + JsonSerializerOptions serializerOptions, + out object? value, + out Type? declaredType, + out JsonPropertyInfo? propertyInfo) + { + value = null; + declaredType = null; + propertyInfo = null; + + if (source is JsonNode jsonNode) { + if (jsonNode is JsonObject jsonObject && jsonObject.TryGetPropertyValue(propertyName, out JsonNode? childNode)) + { + value = childNode; + declaredType = typeof(JsonNode); + return true; + } + return false; } - if (schemaObject["type"] is JsonArray types && - !types.Any(typeNode => typeNode is JsonValue value && value.TryGetValue(out string? name) && name == "null")) + if (source is JsonElement jsonElement && jsonElement.ValueKind == JsonValueKind.Object && jsonElement.TryGetProperty(propertyName, out JsonElement childElement)) { - return false; + value = childElement; + declaredType = typeof(JsonElement); + return true; } - if (schemaObject.TryGetPropertyValue("const", out JsonNode? constValue) && constValue is not null) + if (source is IDictionary dictionary && dictionary.Contains(propertyName)) { - return false; + value = dictionary[propertyName]; + declaredType = value?.GetType(); + return true; } - if (schemaObject["enum"] is JsonArray enumValues && !enumValues.Any(static value => value is null)) + try { - return false; + JsonTypeInfo typeInfo = serializerOptions.GetTypeInfo(source.GetType()); + foreach (JsonPropertyInfo property in typeInfo.Properties) + { + if (string.Equals(property.Name, propertyName, StringComparison.Ordinal) && property.Get is not null) + { + value = property.Get(source); + declaredType = property.PropertyType; + propertyInfo = property; + return true; + } + } + } + catch (NotSupportedException) + { + // Without serializer metadata, keep the serialized snapshot as the source of truth. } - if (schemaObject["allOf"] is JsonArray allOf && - allOf.Any(branch => !SchemaAllowsNull(branch, schemaRoot, new HashSet(StringComparer.Ordinal)))) + return false; + } + + private static JsonNode? SerializeSourceValue( + object? value, + Type? declaredType, + JsonSerializerOptions serializerOptions, + JsonPropertyInfo? propertyInfo) + { + if (value is JsonNode jsonNode) { - return false; + return jsonNode.DeepClone(); } - if (schemaObject["anyOf"] is JsonArray anyOf && - !anyOf.Any(branch => SchemaAllowsNull(branch, schemaRoot, new HashSet(StringComparer.Ordinal)))) + if (value is JsonElement jsonElement) { - return false; + return JsonSerializer.SerializeToNode(jsonElement, McpJsonUtilities.JsonContext.Default.JsonElement); } - if (schemaObject["oneOf"] is JsonArray oneOf && - oneOf.Count(branch => SchemaAllowsNull(branch, schemaRoot, new HashSet(StringComparer.Ordinal))) != 1) + Type valueType = declaredType ?? value?.GetType() ?? typeof(object); + try { - return false; + JsonSerializerOptions effectiveOptions = serializerOptions; + JsonConverter? propertyConverter = GetPropertyConverter(propertyInfo, valueType); + if (propertyConverter is not null) + { + effectiveOptions = new JsonSerializerOptions(serializerOptions); + effectiveOptions.Converters.Insert(0, propertyConverter); + } + + JsonTypeInfo typeInfo = effectiveOptions.GetTypeInfo(valueType); + JsonElement element = JsonSerializer.SerializeToElement(value, typeInfo); + return JsonSerializer.SerializeToNode(element, McpJsonUtilities.JsonContext.Default.JsonElement); + } + catch (NotSupportedException) + { + return null; } + } - if (schemaObject["not"] is JsonNode notSchema && SchemaAllowsNull(notSchema, schemaRoot, new HashSet(StringComparer.Ordinal))) + private static JsonConverter? GetPropertyConverter(JsonPropertyInfo? propertyInfo, Type propertyType) + { + if (propertyInfo is null) { - return false; + return null; } - if (schemaObject["if"] is JsonNode ifSchema) + PropertyInfo? customConverterProperty = typeof(JsonPropertyInfo).GetProperty("CustomConverter", BindingFlags.Instance | BindingFlags.Public); + if (customConverterProperty?.GetValue(propertyInfo) is JsonConverter customConverter) { - bool ifMatchesNull = SchemaCouldMatch(null, ifSchema, schemaRoot, new HashSet(StringComparer.Ordinal)); - JsonNode? conditionalBranch = ifMatchesNull ? schemaObject["then"] : schemaObject["else"]; - if (conditionalBranch is not null && !SchemaAllowsNull(conditionalBranch, schemaRoot, new HashSet(StringComparer.Ordinal))) - { - return false; - } + return customConverter; } - return true; + if (propertyInfo.AttributeProvider is ICustomAttributeProvider attributeProvider && + attributeProvider.GetCustomAttributes(typeof(JsonConverterAttribute), inherit: true) + .OfType() + .FirstOrDefault() is { } converterAttribute) + { + return converterAttribute.CreateConverter(propertyType); + } + + return null; } private static bool TryResolveLocalReference(JsonNode schemaRoot, string reference, out JsonNode? resolvedSchema) { resolvedSchema = null; - if (!reference.StartsWith('#')) + if (!reference.StartsWith("#", StringComparison.Ordinal)) { return false; } @@ -1238,7 +1526,7 @@ private static bool TryResolveLocalReference(JsonNode schemaRoot, string referen return true; } - if (!pointer.StartsWith('/')) + if (!pointer.StartsWith("/", StringComparison.Ordinal)) { return false; } @@ -1246,7 +1534,7 @@ private static bool TryResolveLocalReference(JsonNode schemaRoot, string referen JsonNode? current = schemaRoot; foreach (string rawSegment in pointer[1..].Split('/')) { - string segment = rawSegment.Replace("~1", "/", StringComparison.Ordinal).Replace("~0", "~", StringComparison.Ordinal); + string segment = rawSegment.Replace("~1", "/").Replace("~0", "~"); if (current is JsonObject currentObject && currentObject.TryGetPropertyValue(segment, out JsonNode? child)) { current = child; diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index 21496ce93..eed1447e3 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -600,6 +600,26 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_PreservesRequiredDefau AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_PreservesMemberConverterForRequiredDefault() + { + JsonElement outputSchema = JsonDocument.Parse("""{"type":"object","properties":{"id":{"type":"string"},"count":{"type":"string"}},"required":["id","count"]}""").RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutputWithMemberConverter { Id = "42", Count = 0 }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal("0", result.StructuredContent.Value.GetProperty("count").GetString()); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_TraversesDictionaryValuesAndReferences() { @@ -634,6 +654,42 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_TraversesDictionaryVal AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_TraversesRecursiveReferences() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "$defs": { + "node": { + "type": "object", + "properties": { + "id": { "type": "string" }, + "note": { "type": ["string", "null"] }, + "next": { "$ref": "#/$defs/node" } + }, + "required": ["id", "note"] + } + }, + "$ref": "#/$defs/node" + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new RecursiveStructuredOutput { Id = "root", Note = null, Next = new() { Id = "child", Note = null } }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("next").GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_TraversesAllOfRequirements() { @@ -725,6 +781,199 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_RecognizesPermissiveNu AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_CombinesSplitRequiredAndPropertySchemas() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "type": "object", + "properties": { "id": { "type": "string" }, "note": { "type": ["string", "null"] } }, + "allOf": [{ "required": ["note"] }], + "required": ["id"] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutput { Id = "42", Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsMemberLevelDefaultIgnore() + { + JsonElement outputSchema = JsonDocument.Parse("""{"type":"object","properties":{"id":{"type":"string"},"count":{"type":["integer","null"]}},"required":["id","count"]}""").RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutputWithMemberDefault { Id = "42", Count = 0 }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(0, result.StructuredContent.Value.GetProperty("count").GetInt32()); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_PreservesAlreadyValidAnyOfBranch() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "type": "object", + "anyOf": [ + { + "properties": { + "count": { "type": "integer", "minimum": 1 }, + "child": { "type": "object", "properties": { "id": { "type": "string" }, "note": { "type": ["string", "null"] } }, "required": ["id", "note"] } + }, + "required": ["count", "child"] + }, + { + "properties": { "count": { "const": 0 }, "child": { "type": "object", "properties": { "id": { "type": "string" } }, "required": ["id"], "additionalProperties": false } }, + "required": ["count", "child"] + } + ] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new AnyOfStructuredOutput { Count = 0, Child = new() { Id = "42", Note = null } }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(0, result.StructuredContent.Value.GetProperty("count").GetInt32()); + Assert.False(result.StructuredContent.Value.GetProperty("child").TryGetProperty("note", out _)); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsLargeIntegerBranch() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "anyOf": [ + { "properties": { "value": { "type": "integer" }, "note": { "type": ["string", "null"] } }, "required": ["value", "note"] }, + { "type": "null" } + ] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new LargeIntegerStructuredOutput { Value = ulong.MaxValue, Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(ulong.MaxValue, result.StructuredContent.Value.GetProperty("value").GetUInt64()); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_UsesStructuralConstEquality() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "type": "object", + "anyOf": [ + { "properties": { "tag": { "const": { "b": 2, "a": 1 } }, "note": { "type": ["string", "null"] } }, "required": ["tag", "note"] }, + { "properties": { "tag": { "const": { "different": true } } }, "required": ["tag"] } + ] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new StructuralConstOutput { Tag = new JsonObject { ["a"] = 1, ["b"] = 2 }, Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_UsesJsonNumericEquality() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "type": "object", + "anyOf": [ + { "properties": { "count": { "const": 1.0 }, "note": { "type": ["string", "null"] } }, "required": ["count", "note"] }, + { "properties": { "count": { "const": 2 } }, "required": ["count"] } + ] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new NumericConstOutput { Count = 1, Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_HandlesNotConditionalForNull() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "type": "object", + "properties": { "id": { "type": "string" }, "note": { "if": { "not": { "type": "null" } }, "then": { "type": "string" }, "else": { "type": "null" } } }, + "required": ["id", "note"] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutput { Id = "42", Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_VoidReturningTools_ReturnsExpectedSchema() { @@ -1368,6 +1617,75 @@ private sealed class ExplicitSchemaOutputWithDefaultValue public int Count { get; init; } } + private sealed class ExplicitSchemaOutputWithMemberDefault + { + public required string Id { get; init; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public int Count { get; init; } + } + + private sealed class ExplicitSchemaOutputWithMemberConverter + { + public required string Id { get; init; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + [JsonConverter(typeof(IntAsStringJsonConverter))] + public int Count { get; init; } + } + + private sealed class IntAsStringJsonConverter : JsonConverter + { + public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetInt32(); + + public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options) => + writer.WriteStringValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture)); + } + + private sealed class AnyOfStructuredOutput + { + public int Count { get; init; } + + public required AnyOfStructuredChild Child { get; init; } + } + + private sealed class AnyOfStructuredChild + { + public required string Id { get; init; } + + public string? Note { get; init; } + } + + private sealed class LargeIntegerStructuredOutput + { + public ulong Value { get; init; } + + public string? Note { get; init; } + } + + private sealed class StructuralConstOutput + { + public required JsonObject Tag { get; init; } + + public string? Note { get; init; } + } + + private sealed class NumericConstOutput + { + public int Count { get; init; } + + public string? Note { get; init; } + } + + private sealed class RecursiveStructuredOutput + { + public required string Id { get; init; } + + public string? Note { get; init; } + + public RecursiveStructuredOutput? Next { get; init; } + } + private static JsonSerializerOptions CreateSerializerOptionsWithPerson() { JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions); From c4f9d7120b2ec6bd3094047776932ca43e7cdb9b Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Fri, 23 Oct 2026 10:00:51 +0800 Subject: [PATCH 5/9] Repair structured output against explicit schemas --- .../Server/AIFunctionMcpServerTool.cs | 277 +++++++++++++---- .../Server/McpServerToolTests.cs | 292 ++++++++++++++++++ 2 files changed, 514 insertions(+), 55 deletions(-) diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index dc710d015..050dc3318 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -20,6 +20,7 @@ internal sealed partial class AIFunctionMcpServerTool : McpServerTool { private readonly IReadOnlyList _metadata; private readonly Lazy _outputSchema; + private readonly Lazy _serializerTypeInfoIncludingDefaults; /// /// Creates an instance for a method, specified via a instance. @@ -230,6 +231,14 @@ private AIFunctionMcpServerTool(AIFunction function, Tool tool, IServiceProvider AIFunction = function; ProtocolTool = tool; _outputSchema = new Lazy(BuildOutputSchema, LazyThreadSafetyMode.ExecutionAndPublication); + _serializerTypeInfoIncludingDefaults = new Lazy(() => + { + JsonSerializerOptions options = new(AIFunction.JsonSerializerOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.Never, + }; + return options.GetTypeInfo(typeof(object)); + }, LazyThreadSafetyMode.ExecutionAndPublication); _metadata = metadata; } @@ -680,40 +689,25 @@ private static void RewriteRefPointers(JsonNode? node) } JsonElement? elementResult; - JsonElement? elementResultIncludingDefaults; switch (aiFunctionResult) { case JsonElement jsonElement: - elementResult = elementResultIncludingDefaults = jsonElement; + elementResult = jsonElement; break; case JsonNode node: - elementResult = elementResultIncludingDefaults = JsonSerializer.SerializeToElement(node, McpJsonUtilities.JsonContext.Default.JsonNode); + elementResult = JsonSerializer.SerializeToElement(node, McpJsonUtilities.JsonContext.Default.JsonNode); break; case null: - elementResult = elementResultIncludingDefaults = null; + elementResult = null; break; default: JsonSerializerOptions serializerOptions = AIFunction.JsonSerializerOptions; JsonTypeInfo typeInfo = serializerOptions.GetTypeInfo(typeof(object)); elementResult = JsonSerializer.SerializeToElement(aiFunctionResult, typeInfo); - - if (serializerOptions.DefaultIgnoreCondition is JsonIgnoreCondition.Never) - { - elementResultIncludingDefaults = elementResult; - } - else - { - JsonSerializerOptions includeDefaultsOptions = new(serializerOptions) - { - DefaultIgnoreCondition = JsonIgnoreCondition.Never, - }; - elementResultIncludingDefaults = JsonSerializer.SerializeToElement( - aiFunctionResult, includeDefaultsOptions.GetTypeInfo(typeof(object))); - } break; } - elementResult = AddMissingRequiredNullableProperties(elementResult, elementResultIncludingDefaults, aiFunctionResult); + elementResult = AddMissingRequiredNullableProperties(elementResult, aiFunctionResult); // Pre-SEP-2106 clients expect the {"result": } envelope for non-object // schemas. SEP-2106 clients see the natural shape. The classification is decided @@ -735,7 +729,6 @@ private static void RewriteRefPointers(JsonNode? node) private JsonElement? AddMissingRequiredNullableProperties( JsonElement? elementResult, - JsonElement? elementResultIncludingDefaults, object? originalResult) { if (elementResult is not { } result || @@ -746,9 +739,6 @@ result.ValueKind is not (JsonValueKind.Object or JsonValueKind.Array)) } JsonNode? resultNode = JsonSerializer.SerializeToNode(result, McpJsonUtilities.JsonContext.Default.JsonElement); - JsonNode? resultNodeIncludingDefaults = elementResultIncludingDefaults is { } completeResult - ? JsonSerializer.SerializeToNode(completeResult, McpJsonUtilities.JsonContext.Default.JsonElement) - : null; JsonNode? schemaNode = JsonSerializer.SerializeToNode(outputSchema, McpJsonUtilities.JsonContext.Default.JsonElement); JsonSchema? schema = _outputSchema.Value; if (resultNode is null || schemaNode is null || schema is null) @@ -756,6 +746,21 @@ result.ValueKind is not (JsonValueKind.Object or JsonValueKind.Array)) return elementResult; } + if (IsSchemaValid(schema, resultNode)) + { + return elementResult; + } + + JsonNode? resultNodeIncludingDefaults = resultNode; + if (originalResult is not null and not JsonElement and not JsonNode && + AIFunction.JsonSerializerOptions.DefaultIgnoreCondition is not JsonIgnoreCondition.Never) + { + JsonElement completeResult = JsonSerializer.SerializeToElement( + originalResult, _serializerTypeInfoIncludingDefaults.Value); + resultNodeIncludingDefaults = JsonSerializer.SerializeToNode( + completeResult, McpJsonUtilities.JsonContext.Default.JsonElement); + } + JsonNode? repairedResult = RepairStructuredResult( resultNode, resultNodeIncludingDefaults, @@ -809,7 +814,7 @@ result.ValueKind is not (JsonValueKind.Object or JsonValueKind.Array)) serializerOptions, contexts); - if (candidates.Count == 0 || !TryFindValidCandidateSet(schema, workingResult, candidates, out JsonNode? repaired)) + if (candidates.Count == 0 || !TryFindCandidateSet(schema, workingResult, candidates, out JsonNode? repaired)) { return null; } @@ -905,32 +910,26 @@ private static List CreateRepairCandidates( AppendJsonPointer(contextEntry.Key, propertyName), out JsonNode? completeValue); - bool hasSourceValue = TryGetSourceValue( - originalResult, - contextEntry.Key, - propertyName, - serializerOptions, - out JsonNode? sourceValue); - if (hasCompleteValue) { candidates.Add(new RepairCandidate(contextEntry.Key, propertyName, completeValue?.DeepClone())); } - else if (hasSourceValue) + else if (TryGetSourceValue( + originalResult, + contextEntry.Key, + propertyName, + serializerOptions, + out JsonNode? sourceValue)) { candidates.Add(new RepairCandidate(contextEntry.Key, propertyName, sourceValue)); } - else - { - candidates.Add(new RepairCandidate(contextEntry.Key, propertyName, null)); - } } } return candidates; } - private static bool TryFindValidCandidateSet( + private static bool TryFindCandidateSet( JsonSchema schema, JsonNode originalResult, IReadOnlyList candidates, @@ -942,8 +941,8 @@ private static bool TryFindValidCandidateSet( return false; } - // Prefer larger repairs, but cap schema evaluations for unusually broad unions so a - // single tool call cannot trigger an exponential search. + // Prefer the full repair first, then small subsets so discriminated unions do not + // spend the evaluation budget excluding unrelated branch properties. const int maxCandidateSetEvaluations = 256; bool[] included = Enumerable.Repeat(true, candidates.Count).ToArray(); int evaluations = 0; @@ -977,7 +976,7 @@ bool TryCurrentSet() return true; } - bool SearchExclusions(int nextIndex, int remaining) + bool SearchInclusions(int nextIndex, int remaining) { if (remaining == 0) { @@ -986,13 +985,13 @@ bool SearchExclusions(int nextIndex, int remaining) for (int i = nextIndex; i <= candidates.Count - remaining; i++) { - included[i] = false; - if (SearchExclusions(i + 1, remaining - 1)) + included[i] = true; + if (SearchInclusions(i + 1, remaining - 1)) { return true; } - included[i] = true; + included[i] = false; if (evaluations >= maxCandidateSetEvaluations) { return false; @@ -1008,9 +1007,14 @@ bool SearchExclusions(int nextIndex, int remaining) return true; } - for (int excludedCount = 1; excludedCount < candidates.Count; excludedCount++) + for (int i = 0; i < included.Length; i++) { - if (SearchExclusions(0, excludedCount)) + included[i] = false; + } + + for (int includedCount = 1; includedCount < candidates.Count; includedCount++) + { + if (SearchInclusions(0, includedCount)) { repairedResult = matchingResult; return true; @@ -1022,7 +1026,16 @@ bool SearchExclusions(int nextIndex, int remaining) } } - return false; + // A restored parent may expose required descendants on the next pass. Only + // source-backed candidates are created, and the caller publishes a result only + // after the complete schema validates. + repairedResult = originalResult.DeepClone(); + foreach (RepairCandidate candidate in candidates) + { + ApplyRepairCandidate(repairedResult, candidate); + } + + return true; } private static void ApplyRepairCandidate(JsonNode resultNode, RepairCandidate candidate) @@ -1115,10 +1128,8 @@ refText is not null && JsonNode? additionalProperties = schemaObject["additionalProperties"]; foreach (KeyValuePair property in objectInstance) { - if (schemaObject["properties"] is JsonObject explicitProperties && explicitProperties.ContainsKey(property.Key)) - { - continue; - } + bool isExplicitProperty = schemaObject["properties"] is JsonObject explicitProperties && + explicitProperties.ContainsKey(property.Key); bool matchedPattern = false; if (patternProperties is not null) @@ -1139,7 +1150,7 @@ refText is not null && } } - if (!matchedPattern && additionalProperties is not null) + if (!isExplicitProperty && !matchedPattern && additionalProperties is not null) { CollectSchemaContexts( property.Value, @@ -1172,6 +1183,24 @@ refText is not null && } } + if (instanceNode is JsonObject dependencyInstance && + schemaObject["dependentSchemas"] is JsonObject dependentSchemas) + { + foreach (KeyValuePair dependentSchema in dependentSchemas) + { + if (dependentSchema.Value is not null && dependencyInstance.ContainsKey(dependentSchema.Key)) + { + CollectSchemaContexts( + instanceNode, + dependentSchema.Value, + schemaRoot, + instancePath, + contexts, + new HashSet(activeReferences, StringComparer.Ordinal)); + } + } + } + foreach (string compositionKeyword in new[] { "allOf", "anyOf", "oneOf" }) { if (schemaObject[compositionKeyword] is not JsonArray composition) @@ -1284,10 +1313,84 @@ private static bool TryGetSourceValue( return false; } + if (!WasOmittedBySupportedIgnoreCondition( + sourceValue, propertyInfo, serializerOptions)) + { + return false; + } + value = SerializeSourceValue(sourceValue, declaredType, serializerOptions, propertyInfo); return sourceValue is null || value is not null; } + private static bool WasOmittedBySupportedIgnoreCondition( + object? value, + JsonPropertyInfo? propertyInfo, + JsonSerializerOptions serializerOptions) + { + JsonIgnoreAttribute? ignoreAttribute = propertyInfo?.AttributeProvider is ICustomAttributeProvider attributeProvider + ? attributeProvider.GetCustomAttributes(typeof(JsonIgnoreAttribute), inherit: true) + .OfType() + .FirstOrDefault() + : null; + + bool omittedByMemberIgnore = ignoreAttribute?.Condition switch + { + JsonIgnoreCondition.WhenWritingNull => value is null, + JsonIgnoreCondition.WhenWritingDefault => + propertyInfo is not null && IsDefaultValue(value, propertyInfo.PropertyType), + _ => false, + }; + + if (propertyInfo?.ShouldSerialize is not null && !omittedByMemberIgnore) + { + // A caller-provided predicate may enforce redaction. Its result must not be + // overridden by recovering a value that the member ignore rule would serialize. + return false; + } + + JsonIgnoreCondition ignoreCondition = ignoreAttribute?.Condition ?? serializerOptions.DefaultIgnoreCondition; + return ignoreCondition switch + { + JsonIgnoreCondition.WhenWritingNull => value is null, + // Global default-ignore omissions are present in the full serialization with + // DefaultIgnoreCondition.Never. Only member-level ignores need source recovery. + JsonIgnoreCondition.WhenWritingDefault => + ignoreAttribute is not null && + propertyInfo is not null && + IsDefaultValue(value, propertyInfo.PropertyType), + _ => false, + }; + } + + private static bool IsDefaultValue(object? value, Type valueType) + { + if (value is null) + { + return true; + } + + Type effectiveType = Nullable.GetUnderlyingType(valueType) ?? valueType; + return effectiveType.IsValueType && value.Equals(CreateDefaultValue(effectiveType)); + } + + [System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage( + "Trimming", + "IL2067:Type argument does not satisfy DynamicallyAccessedMemberTypes requirements", + Justification = "This helper is called only for value types; zero-initializing them does not invoke constructors.")] + [System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage( + "Trimming", + "IL2072:Type argument does not satisfy DynamicallyAccessedMemberTypes requirements", + Justification = "This helper is called only for value types; zero-initializing them does not invoke constructors.")] + private static object CreateDefaultValue(Type valueType) + { +#if NETSTANDARD2_0 + return System.Runtime.Serialization.FormatterServices.GetUninitializedObject(valueType); +#else + return System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(valueType); +#endif + } + private static bool TryGetSourceObjectAtPath( object sourceRoot, string path, @@ -1352,9 +1455,9 @@ private static bool TryGetSourceObjectAtPath( return false; } - if (value is IDictionary dictionary && dictionary.Contains(segment)) + if (value is IDictionary dictionary && TryGetDictionaryValue(dictionary, segment, serializerOptions, out object? dictionaryValue)) { - value = dictionary[segment]; + value = dictionaryValue; continue; } @@ -1390,6 +1493,62 @@ private static bool TryGetSourceObjectAtPath( return true; } + private static bool TryGetDictionaryValue( + IDictionary dictionary, + string propertyName, + JsonSerializerOptions serializerOptions, + out object? value) + { + value = null; + JsonTypeInfo dictionaryTypeInfo; + try + { + dictionaryTypeInfo = serializerOptions.GetTypeInfo(dictionary.GetType()); + } + catch (NotSupportedException) + { + return false; + } + + if (dictionaryTypeInfo.Kind != JsonTypeInfoKind.Dictionary || + dictionaryTypeInfo.KeyType is null || + dictionaryTypeInfo.ElementType is not { } valueType || + dictionaryTypeInfo.CreateObject?.Invoke() is not IDictionary singleEntry) + { + return false; + } + + object? placeholderValue = valueType.IsValueType + ? CreateDefaultValue(valueType) + : null; + + foreach (DictionaryEntry entry in dictionary) + { + if (entry.Key is null) + { + continue; + } + + try + { + singleEntry.Clear(); + singleEntry.Add(entry.Key, placeholderValue); + JsonElement serialized = JsonSerializer.SerializeToElement(singleEntry, dictionaryTypeInfo); + if (serialized.ValueKind == JsonValueKind.Object && serialized.TryGetProperty(propertyName, out _)) + { + value = entry.Value; + return true; + } + } + catch (Exception exception) when (exception is ArgumentException or NotSupportedException or InvalidOperationException) + { + return false; + } + } + + return false; + } + private static bool TryGetSourceMember( object source, string propertyName, @@ -1471,10 +1630,18 @@ private static bool TryGetSourceMember( { JsonSerializerOptions effectiveOptions = serializerOptions; JsonConverter? propertyConverter = GetPropertyConverter(propertyInfo, valueType); - if (propertyConverter is not null) + if (propertyConverter is not null || propertyInfo?.NumberHandling is not null) { effectiveOptions = new JsonSerializerOptions(serializerOptions); - effectiveOptions.Converters.Insert(0, propertyConverter); + if (propertyConverter is not null) + { + effectiveOptions.Converters.Insert(0, propertyConverter); + } + + if (propertyInfo?.NumberHandling is { } numberHandling) + { + effectiveOptions.NumberHandling = numberHandling; + } } JsonTypeInfo typeInfo = effectiveOptions.GetTypeInfo(valueType); diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index eed1447e3..ff79b7c04 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -974,6 +974,207 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_HandlesNotConditionalF AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_DoesNotRestoreCallerFilteredProperty() + { + JsonElement outputSchema = JsonDocument.Parse("""{"type":"object","properties":{"secret":{"type":"string"}},"required":["secret"]}""").RootElement; + DefaultJsonTypeInfoResolver resolver = new(); + resolver.Modifiers.Add(typeInfo => + { + if (typeInfo.Type == typeof(ExplicitSchemaFilteredOutput)) + { + typeInfo.Properties.Single(property => property.Name == "secret").ShouldSerialize = (_, _) => false; + } + }); + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(resolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaFilteredOutput { Secret = "do-not-publish" }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.False(result.StructuredContent.Value.TryGetProperty("secret", out _)); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_DoesNotSerializeAlreadyValidOutputAgain() + { + int getterCalls = 0; + JsonElement outputSchema = JsonDocument.Parse("""{"type":"object","properties":{"id":{"type":"string"}},"required":["id"]}""").RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new GetterCountedStructuredOutput(() => getterCalls++), + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal("42", result.StructuredContent.Value.GetProperty("id").GetString()); + // The result is serialized once for structured content and once for text content. + Assert.Equal(2, getterCalls); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsNestedDefaultValueInPasses() + { + JsonElement outputSchema = JsonDocument.Parse(""" + {"type":"object","properties":{"child":{"type":"object","properties":{"note":{"type":["string","null"]}},"required":["note"]}},"required":["child"]} + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new NestedDefaultedStructuredOutput { Child = default }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("child").GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsRequiredPropertyFromDependentSchema() + { + JsonElement outputSchema = JsonDocument.Parse(""" + {"type":"object","properties":{"id":{"type":"string"},"note":{"type":["string","null"]}},"dependentSchemas":{"id":{"required":["note"]}}} + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutput { Id = "42", Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_TraversesMatchingPatternForExplicitProperty() + { + JsonElement outputSchema = JsonDocument.Parse(""" + {"type":"object","properties":{"child":{"type":"object","properties":{"id":{"type":"string"}},"required":["id"]}},"patternProperties":{"^child$":{"type":"object","properties":{"note":{"type":["string","null"]}},"required":["note"]}}} + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaPatternChildOutput { Child = new() { Id = "42", Note = null } }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("child").GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_SelectsAValidBranchFromBroadUnion() + { + string branchSchemas = string.Join(",", Enumerable.Range(0, 10).Select(index => + $$$"""{"type":"object","properties":{"tag":{"const":"{{{index}}}"},"note{{{index}}}":{"type":["string","null"]}},"required":["tag","note{{{index}}}"],"additionalProperties":false}""")); + using JsonDocument schemaDocument = JsonDocument.Parse($$"""{"type":"object","oneOf":[{{branchSchemas}}]}"""); + JsonElement outputSchema = schemaDocument.RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new TenBranchStructuredOutput { Tag = "0" }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note0").ValueKind); + Assert.False(result.StructuredContent.Value.TryGetProperty("note1", out _)); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_DoesNotInventOneOfProperty() + { + JsonElement outputSchema = JsonDocument.Parse(""" + {"type":"object","oneOf":[{"properties":{"note":{"type":["string","null"]}},"required":["note"]},{"properties":{"other":{"type":["string","null"]}},"required":["other"]}]} + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOneOfSourceOnlyOutput { Id = "42", Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + Assert.False(result.StructuredContent.Value.TryGetProperty("other", out _)); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RecoversIntegerDictionaryKey() + { + JsonElement outputSchema = JsonDocument.Parse(""" + {"type":"object","properties":{"items":{"type":"object","properties":{"42":{"type":"object","properties":{"count":{"type":"integer"}},"required":["count"]}},"required":["42"]}},"required":["items"]} + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new IntegerKeyDictionaryOutput { Items = new() { [42] = new() { Count = 0 } } }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(0, result.StructuredContent.Value.GetProperty("items").GetProperty("42").GetProperty("count").GetInt32()); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_PreservesPropertyNumberHandlingOnRecovery() + { + JsonElement outputSchema = JsonDocument.Parse("""{"type":"object","properties":{"count":{"type":"string"}},"required":["count"]}""").RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutputWithNumberHandling { Count = 0 }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal("0", result.StructuredContent.Value.GetProperty("count").GetString()); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_VoidReturningTools_ReturnsExpectedSchema() { @@ -1677,6 +1878,97 @@ private sealed class NumericConstOutput public string? Note { get; init; } } + private sealed class ExplicitSchemaFilteredOutput + { + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public required string Secret { get; init; } + } + + private sealed class GetterCountedStructuredOutput(Action onGet) + { + public string Id + { + get + { + onGet(); + return "42"; + } + } + } + + private struct NestedDefaultedStructuredChild + { + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? Note { get; init; } + } + + private sealed class NestedDefaultedStructuredOutput + { + public NestedDefaultedStructuredChild Child { get; init; } + } + + private sealed class ExplicitSchemaPatternChildOutput + { + public required ExplicitSchemaPatternChild Child { get; init; } + } + + private sealed class ExplicitSchemaPatternChild + { + public required string Id { get; init; } + + public string? Note { get; init; } + } + + private sealed class TenBranchStructuredOutput + { + public required string Tag { get; init; } + + public string? Note0 { get; init; } + + public string? Note1 { get; init; } + + public string? Note2 { get; init; } + + public string? Note3 { get; init; } + + public string? Note4 { get; init; } + + public string? Note5 { get; init; } + + public string? Note6 { get; init; } + + public string? Note7 { get; init; } + + public string? Note8 { get; init; } + + public string? Note9 { get; init; } + } + + private sealed class ExplicitSchemaOneOfSourceOnlyOutput + { + public required string Id { get; init; } + + public string? Note { get; init; } + } + + private sealed class IntegerKeyDictionaryOutput + { + public required Dictionary Items { get; init; } + } + + private sealed class IntegerKeyDictionaryChild + { + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public int Count { get; init; } + } + + private sealed class ExplicitSchemaOutputWithNumberHandling + { + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + [JsonNumberHandling(JsonNumberHandling.WriteAsString)] + public int Count { get; init; } + } + private sealed class RecursiveStructuredOutput { public required string Id { get; init; } From 0170ae672019cd5ea2c27b423765620e57f4fcd6 Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Fri, 23 Oct 2026 10:26:03 +0800 Subject: [PATCH 6/9] Add regression for structured output source lookups > [!NOTE] > AI-generated by GitHub Copilot. --- .../Server/McpServerToolTests.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index ff79b7c04..acb7505c9 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -1022,6 +1022,33 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_DoesNotSerializeAlread Assert.Equal(2, getterCalls); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_DoesNotLookUpValuesPresentInCompleteSnapshot() + { + const int itemCount = 100; + JsonElement outputSchema = JsonDocument.Parse(""" + {"type":"object","properties":{"items":{"type":"array","items":{"type":"object","properties":{"id":{"type":"string"},"note":{"type":["string","null"]}},"required":["id","note"]}}},"required":["items"]} + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + GetterCountedCollectionStructuredOutput output = new( + Enumerable.Range(0, itemCount).Select(index => new ExplicitSchemaCollectionItem { Id = index.ToString() }).ToList()); + McpServerTool tool = McpServerTool.Create( + () => output, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(itemCount, result.StructuredContent.Value.GetProperty("items").GetArrayLength()); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + // Structured output, text output, and the complete-value snapshot each access the collection once. + Assert.Equal(3, output.ItemsGetterCalls); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsNestedDefaultValueInPasses() { @@ -1896,6 +1923,27 @@ public string Id } } + private sealed class GetterCountedCollectionStructuredOutput(List items) + { + public int ItemsGetterCalls { get; private set; } + + public List Items + { + get + { + ItemsGetterCalls++; + return items; + } + } + } + + private sealed class ExplicitSchemaCollectionItem + { + public required string Id { get; init; } + + public string? Note { get; init; } + } + private struct NestedDefaultedStructuredChild { [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] From dfed8d35951e9d8dc56d8144bbdbfccf5874d34e Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Mon, 26 Oct 2026 09:23:10 +0800 Subject: [PATCH 7/9] Fix structured content schema recovery --- .../Server/AIFunctionMcpServerTool.cs | 335 ++++++++++-------- .../Server/McpServerToolTests.cs | 139 ++++++++ 2 files changed, 323 insertions(+), 151 deletions(-) diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index 050dc3318..078223c9f 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -755,10 +755,19 @@ result.ValueKind is not (JsonValueKind.Object or JsonValueKind.Array)) if (originalResult is not null and not JsonElement and not JsonNode && AIFunction.JsonSerializerOptions.DefaultIgnoreCondition is not JsonIgnoreCondition.Never) { - JsonElement completeResult = JsonSerializer.SerializeToElement( - originalResult, _serializerTypeInfoIncludingDefaults.Value); - resultNodeIncludingDefaults = JsonSerializer.SerializeToNode( - completeResult, McpJsonUtilities.JsonContext.Default.JsonElement); + try + { + JsonElement completeResult = JsonSerializer.SerializeToElement( + originalResult, _serializerTypeInfoIncludingDefaults.Value); + resultNodeIncludingDefaults = JsonSerializer.SerializeToNode( + completeResult, McpJsonUtilities.JsonContext.Default.JsonElement); + } + catch (Exception exception) when (exception is JsonException or NotSupportedException or InvalidOperationException) + { + // A value omitted from the actual result may fail when the ignore policy is + // relaxed. Required members can still be recovered individually from metadata. + resultNodeIncludingDefaults = null; + } } JsonNode? repairedResult = RepairStructuredResult( @@ -795,39 +804,145 @@ result.ValueKind is not (JsonValueKind.Object or JsonValueKind.Array)) return null; } - JsonNode workingResult = resultNode.DeepClone(); - for (int pass = 0; pass < 32; pass++) + return TryRepairStructuredResult( + resultNode, + resultNodeIncludingDefaults, + schemaNode, + schema, + originalResult, + serializerOptions, + depth: 0, + new RepairSearchState()); + } + + private static JsonNode? TryRepairStructuredResult( + JsonNode resultNode, + JsonNode? resultNodeIncludingDefaults, + JsonNode schemaNode, + JsonSchema schema, + object? originalResult, + JsonSerializerOptions serializerOptions, + int depth, + RepairSearchState searchState) + { + if (IsSchemaValid(schema, resultNode)) + { + return resultNode.DeepClone(); + } + + const int maxRepairDepth = 32; + const int maxCandidateSetEvaluations = 256; + if (depth >= maxRepairDepth || searchState.CandidateSetEvaluations >= maxCandidateSetEvaluations || + !searchState.VisitedStates.Add(resultNode.ToJsonString())) { - Dictionary contexts = new(StringComparer.Ordinal); - CollectSchemaContexts( - workingResult, - schemaNode, - schemaNode, - string.Empty, - contexts, - new HashSet(StringComparer.Ordinal)); + return null; + } + + Dictionary contexts = new(StringComparer.Ordinal); + CollectSchemaContexts( + resultNode, + schemaNode, + schemaNode, + string.Empty, + contexts, + new HashSet(StringComparer.Ordinal)); + + List candidates = CreateRepairCandidates( + resultNode, + resultNodeIncludingDefaults, + originalResult, + serializerOptions, + contexts); + if (candidates.Count == 0) + { + return null; + } + + bool[] included = Enumerable.Repeat(true, candidates.Count).ToArray(); + string originalJson = resultNode.ToJsonString(); + JsonNode? repairedResult = null; + + bool TryCurrentSet() + { + if (searchState.CandidateSetEvaluations >= maxCandidateSetEvaluations) + { + return false; + } + + searchState.CandidateSetEvaluations++; + JsonNode candidateResult = resultNode.DeepClone(); + bool hasIncludedCandidate = false; + for (int i = 0; i < candidates.Count; i++) + { + if (included[i]) + { + ApplyRepairCandidate(candidateResult, candidates[i]); + hasIncludedCandidate = true; + } + } - List candidates = CreateRepairCandidates( - workingResult, + if (!hasIncludedCandidate || string.Equals(candidateResult.ToJsonString(), originalJson, StringComparison.Ordinal)) + { + return false; + } + + repairedResult = TryRepairStructuredResult( + candidateResult, resultNodeIncludingDefaults, + schemaNode, + schema, originalResult, serializerOptions, - contexts); + depth + 1, + searchState); + return repairedResult is not null; + } + + bool SearchInclusions(int nextIndex, int remaining) + { + if (remaining == 0) + { + return TryCurrentSet(); + } - if (candidates.Count == 0 || !TryFindCandidateSet(schema, workingResult, candidates, out JsonNode? repaired)) + for (int i = nextIndex; i <= candidates.Count - remaining; i++) { - return null; + included[i] = true; + if (SearchInclusions(i + 1, remaining - 1)) + { + return true; + } + + included[i] = false; + if (searchState.CandidateSetEvaluations >= maxCandidateSetEvaluations) + { + return false; + } } - if (repaired is null) + return false; + } + + if (TryCurrentSet()) + { + return repairedResult; + } + + for (int i = 0; i < included.Length; i++) + { + included[i] = false; + } + + for (int includedCount = 1; includedCount < candidates.Count; includedCount++) + { + if (SearchInclusions(0, includedCount)) { - return null; + return repairedResult; } - workingResult = repaired; - if (IsSchemaValid(schema, workingResult)) + if (searchState.CandidateSetEvaluations >= maxCandidateSetEvaluations) { - return workingResult; + break; } } @@ -929,115 +1044,6 @@ private static List CreateRepairCandidates( return candidates; } - private static bool TryFindCandidateSet( - JsonSchema schema, - JsonNode originalResult, - IReadOnlyList candidates, - out JsonNode? repairedResult) - { - repairedResult = null; - if (candidates.Count == 0) - { - return false; - } - - // Prefer the full repair first, then small subsets so discriminated unions do not - // spend the evaluation budget excluding unrelated branch properties. - const int maxCandidateSetEvaluations = 256; - bool[] included = Enumerable.Repeat(true, candidates.Count).ToArray(); - int evaluations = 0; - JsonNode? matchingResult = null; - - bool TryCurrentSet() - { - if (evaluations >= maxCandidateSetEvaluations) - { - return false; - } - - evaluations++; - JsonNode candidateResult = originalResult.DeepClone(); - bool hasIncludedCandidate = false; - for (int i = 0; i < candidates.Count; i++) - { - if (included[i]) - { - ApplyRepairCandidate(candidateResult, candidates[i]); - hasIncludedCandidate = true; - } - } - - if (!hasIncludedCandidate || !IsSchemaValid(schema, candidateResult)) - { - return false; - } - - matchingResult = candidateResult; - return true; - } - - bool SearchInclusions(int nextIndex, int remaining) - { - if (remaining == 0) - { - return TryCurrentSet(); - } - - for (int i = nextIndex; i <= candidates.Count - remaining; i++) - { - included[i] = true; - if (SearchInclusions(i + 1, remaining - 1)) - { - return true; - } - - included[i] = false; - if (evaluations >= maxCandidateSetEvaluations) - { - return false; - } - } - - return false; - } - - if (TryCurrentSet()) - { - repairedResult = matchingResult; - return true; - } - - for (int i = 0; i < included.Length; i++) - { - included[i] = false; - } - - for (int includedCount = 1; includedCount < candidates.Count; includedCount++) - { - if (SearchInclusions(0, includedCount)) - { - repairedResult = matchingResult; - return true; - } - - if (evaluations >= maxCandidateSetEvaluations) - { - break; - } - } - - // A restored parent may expose required descendants on the next pass. Only - // source-backed candidates are created, and the caller publishes a result only - // after the complete schema validates. - repairedResult = originalResult.DeepClone(); - foreach (RepairCandidate candidate in candidates) - { - ApplyRepairCandidate(repairedResult, candidate); - } - - return true; - } - private static void ApplyRepairCandidate(JsonNode resultNode, RepairCandidate candidate) { if (TryGetNodeAtPath(resultNode, candidate.ParentPath, out JsonNode? parentNode) && parentNode is JsonObject parentObject) @@ -1053,6 +1059,13 @@ private sealed class SchemaObjectContext private sealed record RepairCandidate(string ParentPath, string PropertyName, JsonNode? Value); + private sealed class RepairSearchState + { + internal int CandidateSetEvaluations { get; set; } + + internal HashSet VisitedStates { get; } = new(StringComparer.Ordinal); + } + private static void CollectSchemaContexts( JsonNode? instanceNode, JsonNode? schemaNode, @@ -1184,11 +1197,29 @@ refText is not null && } if (instanceNode is JsonObject dependencyInstance && + schemaObject["dependentRequired"] is JsonObject dependentRequired) + { + foreach (KeyValuePair dependency in dependentRequired) + { + if (dependencyInstance.ContainsKey(dependency.Key) && dependency.Value is JsonArray requiredProperties) + { + foreach (JsonNode? requiredProperty in requiredProperties) + { + if (requiredProperty?.GetValue() is string propertyName) + { + context.Required.Add(propertyName); + } + } + } + } + } + + if (instanceNode is JsonObject dependencyInstanceForSchemas && schemaObject["dependentSchemas"] is JsonObject dependentSchemas) { foreach (KeyValuePair dependentSchema in dependentSchemas) { - if (dependentSchema.Value is not null && dependencyInstance.ContainsKey(dependentSchema.Key)) + if (dependentSchema.Value is not null && dependencyInstanceForSchemas.ContainsKey(dependentSchema.Key)) { CollectSchemaContexts( instanceNode, @@ -1314,7 +1345,7 @@ private static bool TryGetSourceValue( } if (!WasOmittedBySupportedIgnoreCondition( - sourceValue, propertyInfo, serializerOptions)) + sourceParent, sourceValue, propertyInfo, serializerOptions)) { return false; } @@ -1324,6 +1355,7 @@ private static bool TryGetSourceValue( } private static bool WasOmittedBySupportedIgnoreCondition( + object sourceParent, object? value, JsonPropertyInfo? propertyInfo, JsonSerializerOptions serializerOptions) @@ -1334,18 +1366,10 @@ private static bool WasOmittedBySupportedIgnoreCondition( .FirstOrDefault() : null; - bool omittedByMemberIgnore = ignoreAttribute?.Condition switch + if (propertyInfo?.ShouldSerialize is { } shouldSerialize && + !IsJsonIgnoreConditionPredicate(shouldSerialize) && + !shouldSerialize(sourceParent, value)) { - JsonIgnoreCondition.WhenWritingNull => value is null, - JsonIgnoreCondition.WhenWritingDefault => - propertyInfo is not null && IsDefaultValue(value, propertyInfo.PropertyType), - _ => false, - }; - - if (propertyInfo?.ShouldSerialize is not null && !omittedByMemberIgnore) - { - // A caller-provided predicate may enforce redaction. Its result must not be - // overridden by recovering a value that the member ignore rule would serialize. return false; } @@ -1353,16 +1377,24 @@ private static bool WasOmittedBySupportedIgnoreCondition( return ignoreCondition switch { JsonIgnoreCondition.WhenWritingNull => value is null, - // Global default-ignore omissions are present in the full serialization with - // DefaultIgnoreCondition.Never. Only member-level ignores need source recovery. JsonIgnoreCondition.WhenWritingDefault => - ignoreAttribute is not null && propertyInfo is not null && IsDefaultValue(value, propertyInfo.PropertyType), _ => false, }; } + // System.Text.Json uses this generated delegate for JsonIgnore conditions; evaluate those from metadata instead. + private static bool IsJsonIgnoreConditionPredicate(Delegate predicate) + { + Type? generatedClosureType = predicate.Method.DeclaringType; + Type? propertyInfoType = generatedClosureType?.DeclaringType; + + return predicate.Method.Name.StartsWith("b__", StringComparison.Ordinal) && + propertyInfoType?.Assembly == typeof(JsonPropertyInfo).Assembly && + string.Equals(propertyInfoType.Name, "JsonPropertyInfo`1", StringComparison.Ordinal); + } + private static bool IsDefaultValue(object? value, Type valueType) { if (value is null) @@ -1370,8 +1402,9 @@ private static bool IsDefaultValue(object? value, Type valueType) return true; } - Type effectiveType = Nullable.GetUnderlyingType(valueType) ?? valueType; - return effectiveType.IsValueType && value.Equals(CreateDefaultValue(effectiveType)); + return Nullable.GetUnderlyingType(valueType) is null && + valueType.IsValueType && + value.Equals(CreateDefaultValue(valueType)); } [System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage( diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index acb7505c9..15008f654 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -620,6 +620,30 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_PreservesMemberConvert AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RecoversRequiredNullWhenCompleteSnapshotFails() + { + JsonElement outputSchema = JsonDocument.Parse(""" + {"type":"object","properties":{"note":{"type":["string","null"]},"status":{"type":"string"}},"required":["note"]} + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + options.Converters.Add(new JsonStringEnumConverter(allowIntegerValues: false)); + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutputWithUndefinedEnumDefault { Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + Assert.False(result.StructuredContent.Value.TryGetProperty("status", out _)); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_TraversesDictionaryValuesAndReferences() { @@ -1000,6 +1024,37 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_DoesNotRestoreCallerFi Assert.False(result.StructuredContent.Value.TryGetProperty("secret", out _)); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RespectsCallerFilterForMemberDefault() + { + JsonElement outputSchema = JsonDocument.Parse("""{"type":"object","properties":{"count":{"type":"integer"},"nullableCount":{"type":"integer"}},"required":["count","nullableCount"]}""").RootElement; + DefaultJsonTypeInfoResolver resolver = new(); + resolver.Modifiers.Add(typeInfo => + { + if (typeInfo.Type == typeof(ExplicitSchemaFilteredDefaultOutput)) + { + foreach (JsonPropertyInfo property in typeInfo.Properties) + { + property.ShouldSerialize = (_, _) => false; + } + } + }); + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + TypeInfoResolver = JsonTypeInfoResolver.Combine(resolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaFilteredDefaultOutput(), + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.False(result.StructuredContent.Value.TryGetProperty("count", out _)); + Assert.False(result.StructuredContent.Value.TryGetProperty("nullableCount", out _)); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_DoesNotSerializeAlreadyValidOutputAgain() { @@ -1093,6 +1148,28 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsRequiredPropert AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsRequiredPropertyFromDependentRequired() + { + JsonElement outputSchema = JsonDocument.Parse(""" + {"type":"object","properties":{"id":{"type":"string"},"note":{"type":["string","null"]}},"required":["id"],"dependentRequired":{"id":["note"]}} + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutput { Id = "42", Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_TraversesMatchingPatternForExplicitProperty() { @@ -1162,6 +1239,34 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_DoesNotInventOneOfProp AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsNestedOneOfWithoutMixingBranches() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "oneOf": [ + {"type":"object","properties":{"first":{"type":"object","properties":{"note":{"type":["string","null"]}},"required":["note"],"additionalProperties":false}},"required":["first"],"additionalProperties":false}, + {"type":"object","properties":{"second":{"type":"object","properties":{"note":{"type":["string","null"]}},"required":["note"],"additionalProperties":false}},"required":["second"],"additionalProperties":false} + ] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new NestedOneOfRecoveryOutput(), + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("first").GetProperty("note").ValueKind); + Assert.False(result.StructuredContent.Value.TryGetProperty("second", out _)); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_RecoversIntegerDictionaryKey() { @@ -1862,6 +1967,18 @@ private sealed class ExplicitSchemaOutputWithMemberConverter public int Count { get; init; } } + private sealed class ExplicitSchemaOutputWithUndefinedEnumDefault + { + public string? Note { get; init; } + + public ExplicitSchemaStatus Status { get; init; } + } + + private enum ExplicitSchemaStatus + { + Ready = 1, + } + private sealed class IntAsStringJsonConverter : JsonConverter { public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetInt32(); @@ -1911,6 +2028,15 @@ private sealed class ExplicitSchemaFilteredOutput public required string Secret { get; init; } } + private sealed class ExplicitSchemaFilteredDefaultOutput + { + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public int Count { get; init; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public int? NullableCount { get; init; } = 0; + } + private sealed class GetterCountedStructuredOutput(Action onGet) { public string Id @@ -1950,6 +2076,19 @@ private struct NestedDefaultedStructuredChild public string? Note { get; init; } } + private struct NestedOneOfRecoveryChild + { + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? Note { get; init; } + } + + private sealed class NestedOneOfRecoveryOutput + { + public NestedOneOfRecoveryChild First { get; init; } + + public NestedOneOfRecoveryChild Second { get; init; } + } + private sealed class NestedDefaultedStructuredOutput { public NestedDefaultedStructuredChild Child { get; init; } From f75698860951cad8fb4f947620b65c1870c10fbf Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Mon, 26 Oct 2026 11:25:35 +0800 Subject: [PATCH 8/9] Test structured output schema edge cases --- .../Server/McpServerToolTests.cs | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index 15008f654..0bc0e3955 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -831,6 +831,33 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_CombinesSplitRequiredA AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_CombinesReferenceRequirementsWithSiblingProperties() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "$defs": { "requiresNote": { "type": "object", "required": ["note"] } }, + "$ref": "#/$defs/requiresNote", + "type": "object", + "properties": { "id": { "type": "string" }, "note": { "type": ["string", "null"] } }, + "required": ["id"] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new ExplicitSchemaOutput { Id = "42", Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsMemberLevelDefaultIgnore() { @@ -917,6 +944,36 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsLargeIntegerBra AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsMathematicallyIntegralDecimal() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "anyOf": [ + { "properties": { "value": { "type": "integer" }, "note": { "type": ["string", "null"] } }, "required": ["value", "note"] }, + { "type": "null" } + ] + } + """).RootElement; + using JsonDocument integerDocument = JsonDocument.Parse("1.0"); + JsonElement integerValue = integerDocument.RootElement.Clone(); + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new MathematicalIntegerStructuredOutput { Value = integerValue, Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(1, result.StructuredContent.Value.GetProperty("value").GetDouble()); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_UsesStructuralConstEquality() { @@ -945,6 +1002,34 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_UsesStructuralConstEqu AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_UsesStructuralEnumEquality() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "type": "object", + "anyOf": [ + { "properties": { "tag": { "enum": [{ "b": 2, "a": 1 }] }, "note": { "type": ["string", "null"] } }, "required": ["tag", "note"] }, + { "properties": { "tag": { "enum": [{ "different": true }] } }, "required": ["tag"] } + ] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new StructuralConstOutput { Tag = new JsonObject { ["a"] = 1, ["b"] = 2 }, Note = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("note").ValueKind); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_UsesJsonNumericEquality() { @@ -2008,6 +2093,13 @@ private sealed class LargeIntegerStructuredOutput public string? Note { get; init; } } + private sealed class MathematicalIntegerStructuredOutput + { + public JsonElement Value { get; init; } + + public string? Note { get; init; } + } + private sealed class StructuralConstOutput { public required JsonObject Tag { get; init; } From e294aeb6ac692d4efabb5340047d417df097aa41 Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Mon, 26 Oct 2026 14:16:24 +0800 Subject: [PATCH 9/9] Cover branch-isolated structured output recovery --- .../Server/McpServerToolTests.cs | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index 0bc0e3955..25afa3536 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -1352,6 +1352,54 @@ public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsNestedOneOfWith AssertMatchesJsonSchema(outputSchema, result.StructuredContent); } + [Fact] + public async Task StructuredOutput_Enabled_ExplicitSchema_RepairsMatchingOneOfBranchWithoutOtherCandidates() + { + JsonElement outputSchema = JsonDocument.Parse(""" + { + "type": "object", + "oneOf": [ + { + "properties": { + "tag": { "const": "0" }, + "child": { + "type": "object", + "properties": { "note": { "type": ["string", "null"] } }, + "required": ["note"] + } + }, + "required": ["tag", "child"], + "additionalProperties": false + }, + { + "properties": { + "tag": { "const": "1" }, + "other": { "type": ["string", "null"] } + }, + "required": ["tag", "other"], + "additionalProperties": false + } + ] + } + """).RootElement; + JsonSerializerOptions options = new(McpJsonUtilities.DefaultOptions) + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + TypeInfoResolver = JsonTypeInfoResolver.Combine(McpJsonUtilities.DefaultOptions.TypeInfoResolver, new DefaultJsonTypeInfoResolver()), + }; + McpServerTool tool = McpServerTool.Create( + () => new BranchSpecificOneOfRecoveryOutput { Tag = "0", Child = default, Other = null }, + new() { Name = "tool", UseStructuredContent = true, OutputSchema = outputSchema, SerializerOptions = options }); + + var result = await tool.InvokeAsync(CreateRequestContextWithProtocolVersion(Sep2106ProtocolVersion), TestContext.Current.CancellationToken); + + Assert.NotNull(result.StructuredContent); + Assert.Equal("0", result.StructuredContent.Value.GetProperty("tag").GetString()); + Assert.Equal(JsonValueKind.Null, result.StructuredContent.Value.GetProperty("child").GetProperty("note").ValueKind); + Assert.False(result.StructuredContent.Value.TryGetProperty("other", out _)); + AssertMatchesJsonSchema(outputSchema, result.StructuredContent); + } + [Fact] public async Task StructuredOutput_Enabled_ExplicitSchema_RecoversIntegerDictionaryKey() { @@ -2181,6 +2229,21 @@ private sealed class NestedOneOfRecoveryOutput public NestedOneOfRecoveryChild Second { get; init; } } + private sealed class BranchSpecificOneOfRecoveryOutput + { + public required string Tag { get; init; } + + public BranchSpecificOneOfRecoveryChild Child { get; init; } + + public string? Other { get; init; } + } + + private struct BranchSpecificOneOfRecoveryChild + { + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? Note { get; init; } + } + private sealed class NestedDefaultedStructuredOutput { public NestedDefaultedStructuredChild Child { get; init; }