Repository navigation
Preserve nulls required by structured output schemas - #1929
mikemikimike wants to merge 60 commits into
Conversation
|
Adding a data point on why this matters beyond the TypeScript 1.x client in #1915: it breaks interop with the other official SDKs' current clients, too. I hit it in a companion sample for my book. A C# server returns a positional record with nullable members (
Both clients validate Until this ships, the per-member workaround that worked for me is |
tarekgh
left a comment
There was a problem hiding this comment.
This fixes a real interoperability issue: required nullable properties must be present in structured output, and Python and TypeScript clients reject results when those properties are omitted.
I found one narrow compatibility regression with explicit output schemas, noted inline. Two related ignore-policy gaps also remain, but those predate this PR.
There was a problem hiding this comment.
🟡 Changes recommended
Globally including nulls can invalidate previously valid explicit-schema responses whose optional properties prohibit null.
1 open finding
What changed in this PR
Addresses #1915 by preserving nulls in structured tool results to match required nullable output properties.
Changes:
- Adds separate serializer options for schema-backed structured content, leaving text serialization unchanged.
- Adds record and class regression tests across two protocol versions.
| File | Description |
|---|---|
| tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs | Tests nullable output properties against their schemas. |
| src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs | Preserves nulls during schema-backed structured serialization. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Signed-off-by: mikemikimike <13286568797@163.com>
There was a problem hiding this comment.
🟡 Changes recommended
Schema traversal remains incomplete, and restoration can replace actual values with null or invalidate previously valid output.
5 open issues
Resolve references and composition branches during traversal · New Handle dictionary values via additionalProperties schemas · New Preserve omitted required members' original non-null values · New Apply prefixItems before the fallback items schema · New Recognize null acceptance in empty and const-null schemas · New
1 resolved since last review
Review effort: Balanced
|
@mikemikimike thanks for last updates. Could you please look at the copilot comments and address them? |
Signed-off-by: mikemikimike <13286568797@163.com>
There was a problem hiding this comment.
🟡 Changes recommended
The restoration logic can return incorrect values, invalidate previously valid responses, and miss required nullable properties.
6 open issues
Resolve required properties across combined schema definitions · New Restore values omitted by member-level default-ignore conditions · New Validate candidate repairs before selecting schema branches · New Support all mathematically integral JSON numbers · New Compare const and enum values structurally · New Evaluate not conditions when selecting nullable branches · New
5 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
The recovery path can bypass application redaction filters, and unresolved correctness and performance issues remain.
7 open issues
Source recovery bypasses ShouldSerialize redaction filters · New Valid responses incur unnecessary second serialization · New Nested repairs stop before restoring child properties · New Source lookup performs quadratic work for complete values · New Branch repair search misses required nullable property · New patternProperties skipped for explicitly named properties · New Fallback recovery drops property-level NumberHandling · New
6 resolved since last review
Evaluate not conditions when selecting nullable branches Compare const and enum values structurally Support all mathematically integral JSON numbers Validate candidate repairs before selecting schema branches Restore values omitted by member-level default-ignore conditions Resolve required properties across combined schema definitions
Review effort: Balanced
> [!NOTE] > AI-generated by GitHub Copilot.
|
Pushed Structured output: Recovery remains conservative for omitted values that cannot safely be reconstructed, including custom converters, ambiguous default-value equality, and non-indexable collections. For AIFunction-backed calls that need recovery, the final result is validated against the schema advertised for the negotiated protocol version after delegation. If it cannot be safely repaired or validated, the call returns a generic tool error without structured content. Explicit Recovery plans are limited per dispatch to 4 MiB and 128 identities. Accounting uses the original serialized size, including whitespace, and schema evaluation has work limits without fetching unresolved external references. Stdio shutdown: Closing stdin starts graceful shutdown while stdout continues to drain. Disposal and stderr waits are coordinated and bounded, including when a stderr callback blocks. To prevent unbounded buffering, shutdown retains at most 8 MiB or 4,096 complete messages, with a 1,048,576-character line limit. Once a limit is exceeded, remaining output is drained without retaining further messages, so excess shutdown notifications can be dropped. Existing CR, LF, CRLF, and final-at-EOF framing is preserved. Validation: The build and full non-manual suite passed: 18,464 passed and 849 skipped. Focused transport and recovery tests passed on .NET 8, 9, 10, and .NET Framework 4.7.2; formatting checks passed. |
There was a problem hiding this comment.
🟡 Changes recommended
A critical buffering issue can indefinitely stall a complete JSON-RPC response when the server flushes exactly 1,024 ASCII bytes and waits for the next request, requiring a read-strategy fix and an exact-buffer-size regression test.
1 open issue
2 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because removing a tool’s schema after invocation can bypass output validation and return results that violate the schema advertised to clients.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
Active issues can reject valid tool results by matching unrelated schemas and can trigger premature validation when replacement request facades reset nesting depth.
2 open issues
1 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed to weigh unpublished concerns about the no-fetch guarantee for CLR tool outputs and the framing test’s potentially flaky two-second timeout.
No open issues 🎉
2 resolved since last review
Review effort: Balanced
Copilot-Session: b91be0f5-23d2-433f-a110-5001e3f607b8
Copilot-Session: b91be0f5-23d2-433f-a110-5001e3f607b8
There was a problem hiding this comment.
🔵 Needs a closer look
Cross-cutting schema-validation and platform-specific shutdown changes warrant human architectural review, especially because the central recovery implementation’s diff is absent and builds and tests were not run.
No open issues 🎉
Review effort: Balanced
Proposal: scope this PR back to #1915 and track the broader work separatelyWhere the PR is todayThe original problem in #1915 is narrow. With Through several review rounds, the PR grew well beyond that fix. It now changes 26 files (about +14.8K / -0.5K lines, roughly 4.7K of production code) and includes:
Decisions taken along the way
Why we should not merge this scope as isEach of these decisions is reasonable on its own, but together they are a significant design change that has not had a dedicated discussion:
At the same time, #1915 is a real interoperability bug that users hit with the TypeScript and Python clients today. We do not want the broader design questions to keep that fix blocked. Proposed scope for this PRFix only the case reported in #1915:
Why this scope is a good middle ground
Follow-up issues to open
We will preserve the current branch head so none of the existing work is lost, and link it from the follow-up issues. We will scope this PR to the focused fix above, then track the remaining work in follow-up issues so those design questions can be discussed separately. |
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because serialization repair can insert null for intentionally omitted, non-null source-generated readonly fields when field and property ignore settings differ.
No open issues 🎉
1 resolved since last review
Review effort: Balanced


Fixes #1915
Issue: #1915
When structured tool output has an output schema and serializer options ignore null values, required nullable properties are omitted from
structuredContent. Preserve explicit nulls for this serialization path so the result matches its schema while leaving ordinary text serialization unchanged.Add regression coverage for record and class outputs with null and non-null values across the current protocol and
2025-11-25.Validation passed:
dotnet testin Debug and Release across the repository test suites;dotnet format whitespace --verify-no-changes; Windows x64 NativeAOT publish and execution; Release NuGet pack; and DocFX with warnings treated as errors. The Core suites passed 2,398 tests per net8.0/net9.0/net10.0 target (4 skipped) and 2,081 tests on net472 (294 skipped); ASP.NET Core passed 635 tests per modern target (30 skipped); Analyzer passed 57 tests.