Skip to content

Write null user-defined properties by default - #2067

Merged
halter73 merged 3 commits into
mainfrom
halter73-split-json-null-defaults
Dec 3, 2026
Merged

halter73 merged 3 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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The result serialization attribute omits required null results, producing invalid JSON-RPC response envelopes that the message reader rejects.

1 open issue
What changed in this PR

This PR changes the C# SDK’s default serialization to retain null user-defined properties, addressing the structured-output schema mismatch reported in #1915 and #690.

Changes:

  • Write user-data nulls by default while explicitly controlling null omission on SDK types.
  • Add structured-output regression tests and a protocol serialization guard.
  • Document the behavior change and null-omission opt-outs.
File Description
tests/​ModelContextProtocol.Tests/​Server/​McpServerToolTests.cs Tests required null output and opt-outs.
tests/​ModelContextProtocol.Tests/​McpJsonUtilitiesTests.cs Tests defaults and protocol null annotations.
tests/​ModelContextProtocol.Tests/​ExperimentalInternalPropertyTests.cs Excludes nonserialized message context.
src/​ModelContextProtocol.Extensions.Tasks/​Protocol/​TaskStatusNotificationParams.cs Adds explicit null omission.
src/​ModelContextProtocol.Extensions.Tasks/​Protocol/​GetTaskResult.cs Adds explicit null omission.
src/​ModelContextProtocol.Extensions.Tasks/​Protocol/​CreateTaskResult.cs Adds explicit null omission.
src/​ModelContextProtocol.Extensions.Tasks/​McpTasksJsonContext.cs Removes global null omission.
src/​ModelContextProtocol.Extensions.Skills/​Protocol/​Skill.cs Omits null metadata explicitly.
src/​ModelContextProtocol.Extensions.Skills/​Protocol/​ListSkillsResult.cs Omits null cache settings explicitly.
src/​ModelContextProtocol.Extensions.Skills/​Protocol/​GetSkillResult.cs Omits null cache settings explicitly.
src/​ModelContextProtocol.Extensions.Skills/​McpSkillsJsonContext.cs Removes global null omission.
src/​ModelContextProtocol.Extensions.Apps/​Server/​McpUiToolMeta.cs Omits null UI tool metadata.
src/​ModelContextProtocol.Extensions.Apps/​Server/​McpUiResourcePermissions.cs Omits null permissions list.
src/​ModelContextProtocol.Extensions.Apps/​Server/​McpUiResourceMeta.cs Omits null UI resource metadata.
src/​ModelContextProtocol.Extensions.Apps/​Server/​McpUiResourceCsp.cs Omits null security-policy lists.
src/​ModelContextProtocol.Extensions.Apps/​Server/​McpUiClientCapabilities.cs Omits null MIME-type list.
src/​ModelContextProtocol.Extensions.Apps/​Server/​McpAppsJsonContext.cs Removes global null omission.
src/​ModelContextProtocol.Core/​Protocol/​ToolsCapability.cs Omits null capability flag.
src/​ModelContextProtocol.Core/​Protocol/​ToolChoice.cs Omits null mode.
src/​ModelContextProtocol.Core/​Protocol/​ToolAnnotations.cs Omits null tool hints.
src/​ModelContextProtocol.Core/​Protocol/​Tool.cs Omits null tool metadata and schema.
src/​ModelContextProtocol.Core/​Protocol/​SubscriptionsListenRequestParams.cs Omits null subscription settings.
src/​ModelContextProtocol.Core/​Protocol/​ServerCapabilities.cs Omits null server capabilities.
src/​ModelContextProtocol.Core/​Protocol/​SamplingMessage.cs Omits null metadata.
src/​ModelContextProtocol.Core/​Protocol/​SamplingCapability.cs Omits null sampling capabilities.
src/​ModelContextProtocol.Core/​Protocol/​RootsCapability.cs Omits null capability flag.
src/​ModelContextProtocol.Core/​Protocol/​Root.cs Omits null root metadata.
src/​ModelContextProtocol.Core/​Protocol/​Result.cs Omits null result metadata.
src/​ModelContextProtocol.Core/​Protocol/​ResourceTemplate.cs Omits null template metadata.
src/​ModelContextProtocol.Core/​Protocol/​ResourcesCapability.cs Omits null capability flags.
src/​ModelContextProtocol.Core/​Protocol/​ResourceContents.cs Omits null content metadata.
src/​ModelContextProtocol.Core/​Protocol/​Resource.cs Omits null resource metadata.
src/​ModelContextProtocol.Core/​Protocol/​RequestParamsMetadata.cs Omits null progress token.
src/​ModelContextProtocol.Core/​Protocol/​RequestParams.cs Omits null request metadata and state.
src/​ModelContextProtocol.Core/​Protocol/​Reference.cs Adds explicit null omission.
src/​ModelContextProtocol.Core/​Protocol/​ReadResourceResult.cs Omits null cache settings.
src/​ModelContextProtocol.Core/​Protocol/​PromptsCapability.cs Omits null capability flag.
src/​ModelContextProtocol.Core/​Protocol/​PromptArgument.cs Omits null argument metadata.
src/​ModelContextProtocol.Core/​Protocol/​Prompt.cs Omits null prompt metadata.
src/​ModelContextProtocol.Core/​Protocol/​PaginatedResult.cs Omits null next cursor.
src/​ModelContextProtocol.Core/​Protocol/​PaginatedRequest.cs Omits null request cursor.
src/​ModelContextProtocol.Core/​Protocol/​NotificationParams.cs Omits null metadata.
src/​ModelContextProtocol.Core/​Protocol/​ModelPreferences.cs Omits null model preferences.
src/​ModelContextProtocol.Core/​Protocol/​ModelHint.cs Omits null model name.
src/​ModelContextProtocol.Core/​Protocol/​LoggingMessageNotificationParams.cs Omits null logger name.
src/​ModelContextProtocol.Core/​Protocol/​ListToolsResult.cs Omits null cache settings.
src/​ModelContextProtocol.Core/​Protocol/​ListResourceTemplatesResult.cs Omits null cache settings.
src/​ModelContextProtocol.Core/​Protocol/​ListResourcesResult.cs Omits null cache settings.
src/​ModelContextProtocol.Core/​Protocol/​ListPromptsResult.cs Omits null cache settings.
src/​ModelContextProtocol.Core/​Protocol/​JsonRpcResponse.cs Adds null-result omission annotation.
src/​ModelContextProtocol.Core/​Protocol/​JsonRpcRequest.cs Omits null parameters explicitly.
src/​ModelContextProtocol.Core/​Protocol/​JsonRpcNotification.cs Omits null parameters explicitly.
src/​ModelContextProtocol.Core/​Protocol/​JsonRpcMessageContext.cs Removes unused serialization annotation.
src/​ModelContextProtocol.Core/​Protocol/​JsonRpcErrorDetail.cs Omits null error data.
src/​ModelContextProtocol.Core/​Protocol/​InputRequiredResult.cs Omits null input requests and state.
src/​ModelContextProtocol.Core/​Protocol/​InitializeResult.cs Omits null instructions.
src/​ModelContextProtocol.Core/​Protocol/​Implementation.cs Omits null implementation metadata.
src/​ModelContextProtocol.Core/​Protocol/​Icon.cs Omits null icon metadata.
src/​ModelContextProtocol.Core/​Protocol/​GetPromptResult.cs Omits null description.
src/​ModelContextProtocol.Core/​Protocol/​GetPromptRequestParams.cs Omits null arguments.
src/​ModelContextProtocol.Core/​Protocol/​ElicitResult.cs Omits null content.
src/​ModelContextProtocol.Core/​Protocol/​ElicitRequestParams.cs Omits null elicitation and schema properties.
src/​ModelContextProtocol.Core/​Protocol/​DiscoverResult.cs Omits null instructions and cache settings.
src/​ModelContextProtocol.Core/​Protocol/​CreateMessageResult.cs Omits null stop reason.
src/​ModelContextProtocol.Core/​Protocol/​CreateMessageRequestParams.cs Omits null sampling options.
src/​ModelContextProtocol.Core/​Protocol/​ContentBlock.cs Omits null content metadata and options.
src/​ModelContextProtocol.Core/​Protocol/​Completion.cs Omits null completion indicators.
src/​ModelContextProtocol.Core/​Protocol/​CompleteRequestParams.cs Omits null completion context.
src/​ModelContextProtocol.Core/​Protocol/​CompleteContext.cs Omits null arguments.
src/​ModelContextProtocol.Core/​Protocol/​ClientCapabilities.cs Omits null client capabilities.
src/​ModelContextProtocol.Core/​Protocol/​CancelledNotificationParams.cs Omits null cancellation reason.
src/​ModelContextProtocol.Core/​Protocol/​CallToolResult.cs Omits null structured content and error flag.
src/​ModelContextProtocol.Core/​Protocol/​CallToolRequestParams.cs Omits null arguments.
src/​ModelContextProtocol.Core/​Protocol/​Annotations.cs Omits null content annotations.
src/​ModelContextProtocol.Core/​McpJsonUtilities.cs Removes global null omission and updates documentation.
src/​ModelContextProtocol.Core/​Authentication/​TokenResponse.cs Omits null token response fields.
src/​ModelContextProtocol.Core/​Authentication/​ProtectedResourceMetadata.cs Omits null resource metadata fields.
src/​ModelContextProtocol.Core/​Authentication/​OAuthErrorResponse.cs Omits null OAuth error fields.
src/​ModelContextProtocol.Core/​Authentication/​JwtBearerAccessTokenResponse.cs Omits null token response fields.
src/​ModelContextProtocol.Core/​Authentication/​JagTokenExchangeResponse.cs Omits null exchange response fields.
src/​ModelContextProtocol.Core/​Authentication/​DynamicClientRegistrationResponse.cs Omits null registration response fields.
src/​ModelContextProtocol.Core/​Authentication/​DynamicClientRegistrationRequest.cs Omits null registration request fields.
src/​ModelContextProtocol.Core/​Authentication/​AuthorizationServerMetadata.cs Omits null authorization metadata.
docs/​concepts/​tools/​tools.md Documents null defaults and opt-outs.

Review effort: Balanced

@halter73

halter73 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

@tarekgh Thanks, I agree the output schema should account for properties that serialization may omit. Since this belongs in M.E.AI's return-schema generation and isn't a regression from this PR, I filed dotnet/extensions#8000 to track aligning generated output schemas with the serializer contract, while keeping input-schema behavior unchanged.

Preserve nullable JSON-RPC results and task TTLs, including task status messages. Restore the ignore attribute needed when consumers source-generate message-context metadata.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@halter73
halter73 merged commit 03e3786 into main Dec 3, 2026
11 checks passed
@halter73
halter73 deleted the halter73-split-json-null-defaults branch December 3, 2026 21:48
@jeffhandley jeffhandley added the breaking-change This issue or PR introduces a breaking change label Dec 4, 2026
@jeffhandley jeffhandley mentioned this pull request Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change This issue or PR introduces a breaking change

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

4 participants