-
Notifications
You must be signed in to change notification settings - Fork 585
Add CS1066 suppressor for MCP server methods with optional parameters #1110
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
Merged
+429
−20
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a4f6bc8
Initial plan
Copilot 00d647a
Add CS1066 suppressor for MCP server methods with optional parameters
Copilot 009399b
Address code review feedback for CS1066 suppressor
Copilot aad7546
Address review feedback: factor out attribute constants and match on …
Copilot db0ba50
Update generator tests to validate CS1066 warnings are suppressed
Copilot 846a93c
Update generator tests to validate CS1066 warnings are suppressed
Copilot f811455
Update generator tests to fail on any unexpected diagnostic
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.Threading; | ||
|
|
||
| namespace ModelContextProtocol.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// Suppresses CS1066 warnings for MCP server methods that have optional parameters. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// CS1066 is issued when a partial method's implementing declaration has default parameter values. | ||
| /// For partial methods, only the defining declaration's defaults are used by callers, | ||
| /// making the implementing declaration's defaults redundant. | ||
| /// </para> | ||
| /// <para> | ||
| /// However, for MCP tool, prompt, and resource methods, users often want to specify default values | ||
| /// in their implementing declaration for documentation purposes. The XmlToDescriptionGenerator | ||
| /// automatically copies these defaults to the generated defining declaration, making them functional. | ||
| /// </para> | ||
| /// <para> | ||
| /// This suppressor suppresses CS1066 for methods marked with [McpServerTool], [McpServerPrompt], | ||
| /// or [McpServerResource] attributes, allowing users to specify defaults in their code without warnings. | ||
| /// </para> | ||
| /// </remarks> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class CS1066Suppressor : DiagnosticSuppressor | ||
| { | ||
| private static readonly SuppressionDescriptor McpToolSuppression = new( | ||
| id: "MCP_CS1066_TOOL", | ||
| suppressedDiagnosticId: "CS1066", | ||
| justification: "Default values on MCP tool method implementing declarations are copied to the generated defining declaration by the source generator."); | ||
|
|
||
| private static readonly SuppressionDescriptor McpPromptSuppression = new( | ||
| id: "MCP_CS1066_PROMPT", | ||
| suppressedDiagnosticId: "CS1066", | ||
| justification: "Default values on MCP prompt method implementing declarations are copied to the generated defining declaration by the source generator."); | ||
|
|
||
| private static readonly SuppressionDescriptor McpResourceSuppression = new( | ||
| id: "MCP_CS1066_RESOURCE", | ||
| suppressedDiagnosticId: "CS1066", | ||
| justification: "Default values on MCP resource method implementing declarations are copied to the generated defining declaration by the source generator."); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => | ||
| ImmutableArray.Create(McpToolSuppression, McpPromptSuppression, McpResourceSuppression); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void ReportSuppressions(SuppressionAnalysisContext context) | ||
| { | ||
| // Cache semantic models and attribute symbols per syntax tree/compilation to avoid redundant calls | ||
| Dictionary<SyntaxTree, SemanticModel>? semanticModelCache = null; | ||
| INamedTypeSymbol? mcpToolAttribute = null; | ||
| INamedTypeSymbol? mcpPromptAttribute = null; | ||
| INamedTypeSymbol? mcpResourceAttribute = null; | ||
| bool attributesResolved = false; | ||
|
|
||
| foreach (Diagnostic diagnostic in context.ReportedDiagnostics) | ||
| { | ||
| Location? location = diagnostic.Location; | ||
| SyntaxTree? tree = location.SourceTree; | ||
| if (tree is null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| SyntaxNode root = tree.GetRoot(context.CancellationToken); | ||
| SyntaxNode? node = root.FindNode(location.SourceSpan); | ||
|
|
||
| // Find the containing method declaration | ||
| MethodDeclarationSyntax? method = node.FirstAncestorOrSelf<MethodDeclarationSyntax>(); | ||
| if (method is null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Get or cache the semantic model for this tree | ||
| semanticModelCache ??= new Dictionary<SyntaxTree, SemanticModel>(); | ||
| if (!semanticModelCache.TryGetValue(tree, out SemanticModel? semanticModel)) | ||
| { | ||
| semanticModel = context.GetSemanticModel(tree); | ||
| semanticModelCache[tree] = semanticModel; | ||
| } | ||
|
|
||
| // Resolve attribute symbols once per compilation | ||
| if (!attributesResolved) | ||
| { | ||
| mcpToolAttribute = semanticModel.Compilation.GetTypeByMetadataName(McpAttributeNames.McpServerToolAttribute); | ||
| mcpPromptAttribute = semanticModel.Compilation.GetTypeByMetadataName(McpAttributeNames.McpServerPromptAttribute); | ||
| mcpResourceAttribute = semanticModel.Compilation.GetTypeByMetadataName(McpAttributeNames.McpServerResourceAttribute); | ||
| attributesResolved = true; | ||
| } | ||
|
|
||
| // Check for MCP attributes | ||
| SuppressionDescriptor? suppression = GetSuppressionForMethod(method, semanticModel, mcpToolAttribute, mcpPromptAttribute, mcpResourceAttribute, context.CancellationToken); | ||
| if (suppression is not null) | ||
| { | ||
| context.ReportSuppression(Suppression.Create(suppression, diagnostic)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static SuppressionDescriptor? GetSuppressionForMethod( | ||
| MethodDeclarationSyntax method, | ||
| SemanticModel semanticModel, | ||
| INamedTypeSymbol? mcpToolAttribute, | ||
| INamedTypeSymbol? mcpPromptAttribute, | ||
| INamedTypeSymbol? mcpResourceAttribute, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| IMethodSymbol? methodSymbol = semanticModel.GetDeclaredSymbol(method, cancellationToken); | ||
|
|
||
| if (methodSymbol is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| foreach (AttributeData attribute in methodSymbol.GetAttributes()) | ||
| { | ||
| INamedTypeSymbol? attributeClass = attribute.AttributeClass; | ||
| if (attributeClass is null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (mcpToolAttribute is not null && SymbolEqualityComparer.Default.Equals(attributeClass, mcpToolAttribute)) | ||
| { | ||
| return McpToolSuppression; | ||
| } | ||
|
|
||
| if (mcpPromptAttribute is not null && SymbolEqualityComparer.Default.Equals(attributeClass, mcpPromptAttribute)) | ||
| { | ||
| return McpPromptSuppression; | ||
| } | ||
|
|
||
| if (mcpResourceAttribute is not null && SymbolEqualityComparer.Default.Equals(attributeClass, mcpResourceAttribute)) | ||
| { | ||
| return McpResourceSuppression; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
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,12 @@ | ||
| namespace ModelContextProtocol.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// Contains the fully qualified metadata names for MCP server attributes. | ||
| /// </summary> | ||
| internal static class McpAttributeNames | ||
| { | ||
| public const string McpServerToolAttribute = "ModelContextProtocol.Server.McpServerToolAttribute"; | ||
| public const string McpServerPromptAttribute = "ModelContextProtocol.Server.McpServerPromptAttribute"; | ||
| public const string McpServerResourceAttribute = "ModelContextProtocol.Server.McpServerResourceAttribute"; | ||
| public const string DescriptionAttribute = "System.ComponentModel.DescriptionAttribute"; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.