Skip to content

Commit b230b17

Browse files
Copilotstephentoub
andauthored
Adjust analyzer diagnostic severities and improve MCP001 reporting (#1038)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: stephentoub <[email protected]>
1 parent af719fc commit b230b17

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/ModelContextProtocol.Analyzers/Diagnostics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal static class Diagnostics
2525
title: "MCP method must be partial to generate [Description] attributes",
2626
messageFormat: "Method '{0}' has XML documentation that could be used to generate [Description] attributes, but the method is not declared as partial.",
2727
category: "mcp",
28-
defaultSeverity: DiagnosticSeverity.Warning,
28+
defaultSeverity: DiagnosticSeverity.Info,
2929
isEnabledByDefault: true,
3030
description: "Methods with MCP attributes should be declared as partial to allow the source generator to emit Description attributes from XML documentation comments.");
3131
}

src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private static void Execute(Compilation compilation, ImmutableArray<MethodToGene
7272
List<(IMethodSymbol MethodSymbol, MethodDeclarationSyntax MethodDeclaration, XmlDocumentation? XmlDocs)> methodsToGenerate = new(methods.Length);
7373
foreach (var methodModel in methods)
7474
{
75-
var xmlDocs = ExtractXmlDocumentation(methodModel.MethodSymbol, context);
75+
var xmlDocs = ExtractXmlDocumentation(methodModel.MethodSymbol, methodModel.MethodDeclaration, context);
7676

7777
// Generate implementation for partial methods.
7878
if (methodModel.MethodDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
@@ -97,7 +97,7 @@ private static void Execute(Compilation compilation, ImmutableArray<MethodToGene
9797
}
9898
}
9999

100-
private static XmlDocumentation? ExtractXmlDocumentation(IMethodSymbol methodSymbol, SourceProductionContext context)
100+
private static XmlDocumentation? ExtractXmlDocumentation(IMethodSymbol methodSymbol, MethodDeclarationSyntax methodDeclaration, SourceProductionContext context)
101101
{
102102
string? xmlDoc = methodSymbol.GetDocumentationCommentXml();
103103
if (string.IsNullOrWhiteSpace(xmlDoc))
@@ -138,11 +138,14 @@ private static void Execute(Compilation compilation, ImmutableArray<MethodToGene
138138
}
139139
catch (System.Xml.XmlException)
140140
{
141-
// Emit warning for invalid XML
142-
context.ReportDiagnostic(Diagnostic.Create(
143-
Diagnostics.InvalidXmlDocumentation,
144-
methodSymbol.Locations.FirstOrDefault(),
145-
methodSymbol.Name));
141+
// Emit warning for invalid XML only if the method is partial (as that's when it would affect generated code)
142+
if (methodDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
143+
{
144+
context.ReportDiagnostic(Diagnostic.Create(
145+
Diagnostics.InvalidXmlDocumentation,
146+
methodSymbol.Locations.FirstOrDefault(),
147+
methodSymbol.Name));
148+
}
146149
return null;
147150
}
148151
}

tests/ModelContextProtocol.Analyzers.Tests/XmlToDescriptionGeneratorTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public static string TestMethod(string input)
381381

382382
// Should report MCP002 diagnostic
383383
var diagnostic = Assert.Single(result.Diagnostics, d => d.Id == "MCP002");
384-
Assert.Equal(DiagnosticSeverity.Warning, diagnostic.Severity);
384+
Assert.Equal(DiagnosticSeverity.Info, diagnostic.Severity);
385385
Assert.Contains("TestMethod", diagnostic.GetMessage());
386386
Assert.Contains("partial", diagnostic.GetMessage());
387387
}
@@ -412,7 +412,7 @@ public static string TestMethod(string input)
412412

413413
// Should report MCP002 diagnostic because parameter has documentation
414414
var diagnostic = Assert.Single(result.Diagnostics, d => d.Id == "MCP002");
415-
Assert.Equal(DiagnosticSeverity.Warning, diagnostic.Severity);
415+
Assert.Equal(DiagnosticSeverity.Info, diagnostic.Severity);
416416
}
417417

418418
[Fact]
@@ -441,7 +441,7 @@ public static string TestMethod(string input)
441441

442442
// Should report MCP002 diagnostic because return has documentation
443443
var diagnostic = Assert.Single(result.Diagnostics, d => d.Id == "MCP002");
444-
Assert.Equal(DiagnosticSeverity.Warning, diagnostic.Severity);
444+
Assert.Equal(DiagnosticSeverity.Info, diagnostic.Severity);
445445
}
446446

447447
[Fact]
@@ -559,7 +559,7 @@ public static string TestMethod(string input)
559559

560560
// Should report MCP002 diagnostic because parameter description would be generated
561561
var diagnostic = Assert.Single(result.Diagnostics, d => d.Id == "MCP002");
562-
Assert.Equal(DiagnosticSeverity.Warning, diagnostic.Severity);
562+
Assert.Equal(DiagnosticSeverity.Info, diagnostic.Severity);
563563
}
564564

565565
[Fact]
@@ -590,7 +590,7 @@ public static string TestPrompt(string input)
590590

591591
// Should report MCP002 diagnostic for prompts too
592592
var diagnostic = Assert.Single(result.Diagnostics, d => d.Id == "MCP002");
593-
Assert.Equal(DiagnosticSeverity.Warning, diagnostic.Severity);
593+
Assert.Equal(DiagnosticSeverity.Info, diagnostic.Severity);
594594
}
595595

596596
[Fact]
@@ -621,7 +621,7 @@ public static string TestResource(string input)
621621

622622
// Should report MCP002 diagnostic for resources too
623623
var diagnostic = Assert.Single(result.Diagnostics, d => d.Id == "MCP002");
624-
Assert.Equal(DiagnosticSeverity.Warning, diagnostic.Severity);
624+
Assert.Equal(DiagnosticSeverity.Info, diagnostic.Severity);
625625
}
626626

627627
[Fact]

0 commit comments

Comments
 (0)