Skip to content

Commit a5b8d2b

Browse files
Copilotstephentoub
andcommitted
Address final review feedback: simplify attribute checks, use newline separator for summary/remarks, remove type visibility modifiers
Co-authored-by: stephentoub <[email protected]>
1 parent a7e54c7 commit a5b8d2b

File tree

2 files changed

+13
-43
lines changed

2 files changed

+13
-43
lines changed

src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,9 @@ private static void Execute(Compilation compilation, ImmutableArray<MethodToGene
7171
var mcpServerResourceAttribute = compilation.GetTypeByMetadataName("ModelContextProtocol.Server.McpServerResourceAttribute");
7272
var descriptionAttribute = compilation.GetTypeByMetadataName("System.ComponentModel.DescriptionAttribute");
7373

74-
if (descriptionAttribute is null)
74+
if (descriptionAttribute is null || mcpServerToolAttribute is null || mcpServerPromptAttribute is null || mcpServerResourceAttribute is null)
7575
{
76-
// Description attribute is required - can't generate without it
77-
return;
78-
}
79-
80-
if (mcpServerToolAttribute is null && mcpServerPromptAttribute is null && mcpServerResourceAttribute is null)
81-
{
82-
// No MCP attributes found - nothing to generate
76+
// Required attributes not found - can't generate
8377
return;
8478
}
8579

@@ -97,9 +91,9 @@ private static void Execute(Compilation compilation, ImmutableArray<MethodToGene
9791

9892
// Check if method has any MCP attribute with symbol comparison
9993
var hasMcpAttribute =
100-
(mcpServerToolAttribute is not null && HasAttribute(methodSymbol, mcpServerToolAttribute)) ||
101-
(mcpServerPromptAttribute is not null && HasAttribute(methodSymbol, mcpServerPromptAttribute)) ||
102-
(mcpServerResourceAttribute is not null && HasAttribute(methodSymbol, mcpServerResourceAttribute));
94+
HasAttribute(methodSymbol, mcpServerToolAttribute) ||
95+
HasAttribute(methodSymbol, mcpServerPromptAttribute) ||
96+
HasAttribute(methodSymbol, mcpServerResourceAttribute);
10397

10498
if (!hasMcpAttribute)
10599
{
@@ -182,7 +176,7 @@ private static bool NeedsGeneration(IMethodSymbol methodSymbol, XmlDocumentation
182176
? summary
183177
: string.IsNullOrWhiteSpace(summary)
184178
? remarks
185-
: $"{summary} {remarks}";
179+
: $"{summary}\n{remarks}";
186180

187181
var paramDocs = new Dictionary<string, string>(StringComparer.Ordinal);
188182
foreach (var paramElement in memberElement.Elements("param"))
@@ -320,41 +314,17 @@ private static void AppendNestedTypeDeclarations(StringBuilder sb, INamedTypeSym
320314
string typeKeyword;
321315
if (typeDecl is RecordDeclarationSyntax rds)
322316
{
323-
var classOrStruct = rds.ClassOrStructKeyword.IsKind(SyntaxKind.None)
324-
? "class"
325-
: rds.ClassOrStructKeyword.ValueText;
326-
typeKeyword = $"{typeDecl.Keyword.ValueText} {classOrStruct}";
317+
var classOrStruct = rds.ClassOrStructKeyword.ValueText;
318+
typeKeyword = string.IsNullOrEmpty(classOrStruct)
319+
? $"{typeDecl.Keyword.ValueText} class"
320+
: $"{typeDecl.Keyword.ValueText} {classOrStruct}";
327321
}
328322
else
329323
{
330-
typeKeyword = type.TypeKind switch
331-
{
332-
TypeKind.Class => "class",
333-
TypeKind.Struct => "struct",
334-
TypeKind.Interface => "interface",
335-
_ => "class"
336-
};
337-
}
338-
339-
// Build modifiers
340-
var modifiers = new List<string>();
341-
if (type.DeclaredAccessibility == Accessibility.Public)
342-
{
343-
modifiers.Add("public");
344-
}
345-
else if (type.DeclaredAccessibility == Accessibility.Internal)
346-
{
347-
modifiers.Add("internal");
324+
typeKeyword = typeDecl?.Keyword.ValueText ?? "class";
348325
}
349326

350-
if (type.IsStatic)
351-
{
352-
modifiers.Add("static");
353-
}
354-
355-
modifiers.Add("partial");
356-
357-
sb.AppendLine($"{typeIndent}{string.Join(" ", modifiers)} {typeKeyword} {type.Name}");
327+
sb.AppendLine($"{typeIndent}partial {typeKeyword} {type.Name}");
358328
sb.AppendLine($"{typeIndent}{{");
359329

360330
indentLevel++;

tests/ModelContextProtocol.SourceGenerators.Tests/XmlToDescriptionGeneratorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static partial string TestMethod(string input)
7171

7272
Assert.True(result.Success);
7373
var generatedSource = result.GeneratedSources[0].SourceText.ToString();
74-
Assert.Contains("[Description(\"Test tool summary Additional remarks\")]", generatedSource);
74+
Assert.Contains("[Description(\"Test tool summary\\nAdditional remarks\")]", generatedSource);
7575
}
7676

7777
[Fact]

0 commit comments

Comments
 (0)