Skip to content

Conversation

@MackinnonBuck
Copy link
Collaborator

Summary

Prevent experimental API types (Tasks, ToolExecution) from leaking into user code via the System.Text.Json source generator, eliminating spurious MCPEXP001 diagnostics for users who don't use experimental features.

Description

When users define their own JsonSerializerContext that includes MCP protocol types (e.g., Tool, ServerCapabilities), the System.Text.Json source generator follows property types transitively and generates code that references experimental types. This causes MCPEXP001 errors in user projects even when they aren't using experimental features like Tasks.

Approach: Object Backing Field Pattern

For each stable type that exposes an experimental property, the public typed property is now marked with [JsonIgnore] and backed by an internal object? field annotated with [JsonInclude], [JsonPropertyName], and a new [JsonConverter(typeof(ExperimentalJsonConverter<T>))]. Because the source generator sees object? rather than the experimental type, it no longer walks the experimental type graph, so no MCPEXP001 diagnostic is emitted in consuming projects.

The ExperimentalJsonConverter<T> delegates serialization to McpJsonUtilities.DefaultOptions, which already contains source-generated contracts for all experimental types. This preserves NativeAOT compatibility and ensures the experimental properties continue to round-trip correctly over the wire.

A DiagnosticSuppressor was considered but is not necessary since the backing field pattern fully hides experimental types from the source generator (and it wouldn't help anyway because DiagnosticSuppressors cannot suppress error-severity diagnostics).

Other changes

  • Added [Experimental(MCPEXP001)] to the 6 experimental properties that were missing it (only Tool.Execution had it previously). Happy to undo this change if the omission of the [Experimental] attribute was deliberate.
  • Added 11 tests covering round-trip serialization, null handling, JSON property names, and WriteIndented propagation.

Fixes #1255

[JsonPropertyName("task")]
public McpTaskMetadata? Task { get; set; }
[JsonConverter(typeof(ExperimentalJsonConverter<McpTaskMetadata>))]
internal object? _task;
Copy link
Member

Choose a reason for hiding this comment

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

Today, JsonInclude against inaccessible members only works with reflection but not the source generator. We'd want to mark this field as public and additionally EditorBrowsable.Never. Because McpTaskMetadata is itself marked as experimental this will likely cause experimental warnings to resurface on account of it producing new ExperimentalJsonConverter<McpTaskMetadata>() expressions.


namespace ModelContextProtocol.Tests;

public static class ExperimentalJsonConverterTests
Copy link
Member

@eiriktsarpalis eiriktsarpalis Feb 11, 2026

Choose a reason for hiding this comment

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

ModelContextProtocol.Tests currently suppresses experimental warnings. Do we need any testing checking that applying the source generator on MCP types doesn't trigger experimental warnings? I foresee that this could easily regress in the future without us noticing.

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.

Experimental APIs force users to suppress diagnostics even when they are not used

2 participants