Skip to content

Write null user-defined properties by default - #2067

Open
halter73 wants to merge 2 commits into
mainfrom
halter73-split-json-null-defaults
Open

halter73 wants to merge 2 commits into
mainfrom
halter73-split-json-null-defaults

Conversation

@halter73

@halter73 halter73 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 852b33f452755f8dd78dfd439686d8a9d55ce834

Dimension Assessment Rationale
Change confidence 8/10 ↑ Regression tests cover schema-required null output and a guard verifies nullable protocol properties are explicitly annotated.
↑ Release build has 0 warnings; 3,700 Core tests passed on .NET 10, targeted .NET Framework 4.7.2 and 754 ASP.NET Core tests passed; the Native AOT app published and ran.
↑ Full Core suite passed with reflection disabled (net9.0 build on the .NET 10 runtime), the configuration that failed CI earlier.
↓ One Core test failed because the remote Docker image lacks trigger-sampling-request; .NET 8 was not run locally and CI for this head is pending.
↓ The change intentionally changes default user-data serialization; compatibility impact is documented below.
Diagnosis confidence 9.5/10 ↑ #1915 reproduces a schema-required nullable output being omitted by the default serializer.
↑ The new regression test fails on main and passes with this change.
Evidence fit 9/10 ↑ The regression test reproduces #1915's missing structuredContent member and validates the serialized result against its output schema.
↓ No production payload from the original report was available.

Fixes #1915. Related to #690, which reported the same required-null mismatch. This is a simpler alternative to #1929.

Summary

McpJsonUtilities.DefaultOptions serializes user-defined data such as tool results, structured content and tool arguments. It now uses the standard JsonIgnoreCondition.Never default, so null-valued members are written and match JSON schemas that mark nullable members as required. This uses normal System.Text.Json behavior, without a custom repair serializer or runtime type-classification hook.

Protocol DTOs continue to omit optional null properties regardless of serializer options. The source-generated options no longer set a global null-omission default; nullable protocol properties in Core and the Skills, Tasks and Apps extensions are explicitly annotated with [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]. A guard test covers protocol types registered in the SDK's source-generated contexts and fails if a nullable property lacks null-omission metadata. The test also covers types reached through derived contracts behind custom converters.

There are no public API changes. The behavior and opt-out are documented in docs/concepts/tools/tools.md.

Compatibility

This intentionally changes default serialization for user-defined data serialized with DefaultOptions:

  • Explicit output schemas: if an output schema declares an optional, non-nullable property, it previously accepted an omitted value. It may now reject the written null.
  • Tool arguments: clients now send nested null members where they previously omitted them. A server may treat an absent value differently from an explicit null.
  • Copied options and other types: copies of DefaultOptions, and MEAI or extension types serialized through it, now write nulls by default.
  • Payload size: serialized payloads may be larger.

To omit nulls for user-defined data, apply [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] to individual properties or supply McpServerToolCreateOptions.SerializerOptions with DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull. Make sure the schema does not require omitted properties.

Note

This PR description and change were generated with GitHub Copilot.

@tarekgh

tarekgh commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

I lean toward the #2067 approach, assuming we accept the broader default change. Writing nulls through normal System.Text.Json serialization is simpler than adding a post-serialization repair layer, and aligns C# with the common default in the Python, TypeScript, Java, Go, and Rust SDKs.

One design point would make this stronger: keep the generated output schema aligned with the actual serialization contract. In particular, if [JsonIgnore(Condition = WhenWritingNull)] or SerializerOptions.DefaultIgnoreCondition = WhenWritingNull omits a nullable property, the generated schema should not require that property. Go and Rust already account for their omission annotations when generating output schemas. Otherwise, even with #2067, opting back into null omission can recreate the schema mismatch.

The tradeoff is that #2067 affects all user data serialized with DefaultOptions, including tool arguments, explicit-schema results, and other outputs. I would favor it if we can keep protocol DTO behavior unchanged and address schema alignment in #2067 or a clearly linked follow-up. If that wider behavior change is not acceptable, #1929 is the narrower fix for the reported generated-schema case.

References: Go output schema inference, Rust output schema inference, Python structured serialization, TypeScript output validation.

Write nulls for user-defined data by default while keeping null protocol properties omitted through explicit annotations. Add coverage for protocol null omission and document how to opt out.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@halter73
halter73 force-pushed the halter73-split-json-null-defaults branch from 5e65693 to eed4772 Compare December 2, 2026 00:54
@halter73
halter73 requested a review from tarekgh December 2, 2026 00:56
…ion-disabled tests

JsonRpcMessageContext is never serialized, so it no longer needs JsonIgnore
attributes. Skip the new reflection-dependent null-handling tests when
reflection-based serialization is disabled (net9.0 test target).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UseStructuredContent: with the default serializer options, structuredContent omits a null property that outputSchema requires

2 participants