-
Notifications
You must be signed in to change notification settings - Fork 628
Hide experimental properties from the JSON source generator to avoid MCPEXP001 diagnostics #1260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using System.Collections.Immutable; | ||
|
|
||
| namespace ModelContextProtocol.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// Suppresses MCPEXP001 diagnostics in source-generated code. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The MCP SDK uses <c>object?</c> backing fields with <c>[JsonConverter(typeof(ExperimentalJsonConverter<T>))]</c> | ||
| /// to handle serialization of experimental types. When consumers define their own <c>JsonSerializerContext</c>, | ||
| /// the System.Text.Json source generator emits code referencing these converters with experimental type arguments, | ||
| /// which triggers MCPEXP001 diagnostics in the generated code. | ||
| /// </para> | ||
| /// <para> | ||
| /// This suppressor suppresses MCPEXP001 only in source-generated files (identified by <c>.g.cs</c> file extension), | ||
| /// so that hand-written user code that directly references experimental types still produces the diagnostic. | ||
| /// </para> | ||
| /// </remarks> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class MCPEXP001Suppressor : DiagnosticSuppressor | ||
| { | ||
| private static readonly SuppressionDescriptor SuppressInGeneratedCode = new( | ||
| id: "MCP_MCPEXP001_GENERATED", | ||
| suppressedDiagnosticId: "MCPEXP001", | ||
| justification: "MCPEXP001 is suppressed in source-generated code because the experimental type reference originates from the MCP SDK's backing field infrastructure, not from user code."); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => | ||
| ImmutableArray.Create(SuppressInGeneratedCode); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void ReportSuppressions(SuppressionAnalysisContext context) | ||
| { | ||
| foreach (Diagnostic diagnostic in context.ReportedDiagnostics) | ||
| { | ||
| if (diagnostic.Id == "MCPEXP001" && IsInGeneratedCode(diagnostic)) | ||
| { | ||
| context.ReportSuppression(Suppression.Create(SuppressInGeneratedCode, diagnostic)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static bool IsInGeneratedCode(Diagnostic diagnostic) | ||
| { | ||
| string? filePath = diagnostic.Location.SourceTree?.FilePath; | ||
| return filePath is not null && filePath.EndsWith(".g.cs", StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
src/ModelContextProtocol.Core/ExperimentalJsonConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using System.ComponentModel; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using System.Text.Json.Serialization.Metadata; | ||
|
|
||
| namespace ModelContextProtocol; | ||
|
|
||
| /// <summary> | ||
| /// A JSON converter that handles serialization of experimental MCP types through <c>object?</c> backing fields. | ||
| /// </summary> | ||
| /// <typeparam name="T">The experimental type to serialize/deserialize.</typeparam> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// This converter is used on <c>object?</c> backing fields that shadow public experimental properties | ||
| /// marked with <see cref="ExperimentalAttribute"/>. By declaring the backing field | ||
| /// as <c>object?</c>, the System.Text.Json source generator does not walk the experimental type graph. | ||
| /// </para> | ||
| /// <para> | ||
| /// Serialization delegates to <see cref="McpJsonUtilities.DefaultOptions"/>, which already contains source-generated | ||
| /// contracts for all experimental types. | ||
| /// </para> | ||
| /// <para> | ||
| /// This type is not intended to be used directly. It supports the MCP infrastructure and is subject to change. | ||
| /// </para> | ||
| /// </remarks> | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public class ExperimentalJsonConverter<T> : JsonConverter<object?> where T : class | ||
| { | ||
| private static JsonTypeInfo<T> TypeInfo => (JsonTypeInfo<T>)McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(T)); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.Null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return JsonSerializer.Deserialize(ref reader, TypeInfo); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Write(Utf8JsonWriter writer, object? value, JsonSerializerOptions options) | ||
| { | ||
| if (value is null) | ||
| { | ||
| writer.WriteNullValue(); | ||
| return; | ||
| } | ||
|
|
||
| if (value is not T typed) | ||
| { | ||
| throw new JsonException($"Expected value of type '{typeof(T).Name}' but got '{value.GetType().Name}'."); | ||
| } | ||
|
|
||
| JsonSerializer.Serialize(writer, typed, TypeInfo); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend a much stronger statement about not being intended for consumption. We can take inspiration from efcore. Example: https://github.com/dotnet/efcore/blob/a471ebfd9564bc14fb188407a84a031b69d29f77/src/EFCore/Query/QueryContext.cs#L109-L116
We should also give the backing field a more egregious name to emphasize the internal aspect too.