Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ModelContextProtocol.Analyzers/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static class Diagnostics
title: "MCP method must be partial to generate [Description] attributes",
messageFormat: "Method '{0}' has XML documentation that could be used to generate [Description] attributes, but the method is not declared as partial.",
category: "mcp",
defaultSeverity: DiagnosticSeverity.Warning,
defaultSeverity: DiagnosticSeverity.Info,
isEnabledByDefault: true,
description: "Methods with MCP attributes should be declared as partial to allow the source generator to emit Description attributes from XML documentation comments.");
}
17 changes: 10 additions & 7 deletions src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static void Execute(Compilation compilation, ImmutableArray<MethodToGene
List<(IMethodSymbol MethodSymbol, MethodDeclarationSyntax MethodDeclaration, XmlDocumentation? XmlDocs)> methodsToGenerate = new(methods.Length);
foreach (var methodModel in methods)
{
var xmlDocs = ExtractXmlDocumentation(methodModel.MethodSymbol, context);
var xmlDocs = ExtractXmlDocumentation(methodModel.MethodSymbol, methodModel.MethodDeclaration, context);

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

private static XmlDocumentation? ExtractXmlDocumentation(IMethodSymbol methodSymbol, SourceProductionContext context)
private static XmlDocumentation? ExtractXmlDocumentation(IMethodSymbol methodSymbol, MethodDeclarationSyntax methodDeclaration, SourceProductionContext context)
{
string? xmlDoc = methodSymbol.GetDocumentationCommentXml();
if (string.IsNullOrWhiteSpace(xmlDoc))
Expand Down Expand Up @@ -138,11 +138,14 @@ private static void Execute(Compilation compilation, ImmutableArray<MethodToGene
}
catch (System.Xml.XmlException)
{
// Emit warning for invalid XML
context.ReportDiagnostic(Diagnostic.Create(
Diagnostics.InvalidXmlDocumentation,
methodSymbol.Locations.FirstOrDefault(),
methodSymbol.Name));
// Emit warning for invalid XML only if the method is partial (as that's when it would affect generated code)
if (methodDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
{
context.ReportDiagnostic(Diagnostic.Create(
Diagnostics.InvalidXmlDocumentation,
methodSymbol.Locations.FirstOrDefault(),
methodSymbol.Name));
}
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public static string TestMethod(string input)

// Should report MCP002 diagnostic
var diagnostic = Assert.Single(result.Diagnostics, d => d.Id == "MCP002");
Assert.Equal(DiagnosticSeverity.Warning, diagnostic.Severity);
Assert.Equal(DiagnosticSeverity.Info, diagnostic.Severity);
Assert.Contains("TestMethod", diagnostic.GetMessage());
Assert.Contains("partial", diagnostic.GetMessage());
}
Expand Down Expand Up @@ -412,7 +412,7 @@ public static string TestMethod(string input)

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

[Fact]
Expand Down Expand Up @@ -441,7 +441,7 @@ public static string TestMethod(string input)

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

[Fact]
Expand Down Expand Up @@ -559,7 +559,7 @@ public static string TestMethod(string input)

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

[Fact]
Expand Down Expand Up @@ -590,7 +590,7 @@ public static string TestPrompt(string input)

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

[Fact]
Expand Down Expand Up @@ -621,7 +621,7 @@ public static string TestResource(string input)

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

[Fact]
Expand Down
Loading