-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathXmlToDescriptionGenerator.cs
More file actions
544 lines (471 loc) · 21.5 KB
/
XmlToDescriptionGenerator.cs
File metadata and controls
544 lines (471 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System.CodeDom.Compiler;
using System.Collections.Immutable;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ModelContextProtocol.Analyzers;
/// <summary>
/// Source generator that creates [Description] attributes from XML comments
/// for partial methods tagged with MCP attributes.
/// </summary>
[Generator]
public sealed class XmlToDescriptionGenerator : IIncrementalGenerator
{
private const string GeneratedFileName = "ModelContextProtocol.Descriptions.g.cs";
/// <summary>
/// A display format that produces fully-qualified type names with "global::" prefix
/// and includes nullability annotations.
/// </summary>
private static readonly SymbolDisplayFormat s_fullyQualifiedFormatWithNullability =
SymbolDisplayFormat.FullyQualifiedFormat.AddMiscellaneousOptions(
SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier);
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// Extract method information for all MCP tools, prompts, and resources.
// The transform extracts all necessary data upfront so the output doesn't depend on the compilation.
var allMethods = CreateProviderForAttribute(context, McpAttributeNames.McpServerToolAttribute).Collect()
.Combine(CreateProviderForAttribute(context, McpAttributeNames.McpServerPromptAttribute).Collect())
.Combine(CreateProviderForAttribute(context, McpAttributeNames.McpServerResourceAttribute).Collect())
.Select(static (tuple, _) =>
{
var ((tools, prompts), resources) = tuple;
return new EquatableArray<MethodToGenerate>(tools.Concat(prompts).Concat(resources));
});
// Report diagnostics for all methods.
context.RegisterSourceOutput(
allMethods,
static (spc, methods) =>
{
foreach (var method in methods)
{
foreach (var diagnostic in method.Diagnostics)
{
spc.ReportDiagnostic(CreateDiagnostic(diagnostic));
}
}
});
// Generate source code only for methods that need generation.
context.RegisterSourceOutput(
allMethods.Select(static (methods, _) => new EquatableArray<MethodToGenerate>(methods.Where(m => m.NeedsGeneration))),
static (spc, methods) =>
{
if (methods.Length > 0)
{
spc.AddSource(GeneratedFileName, SourceText.From(GenerateSourceFile(methods), Encoding.UTF8));
}
});
}
private static Diagnostic CreateDiagnostic(DiagnosticInfo info) =>
Diagnostic.Create(info.Id switch
{
"MCP001" => Diagnostics.InvalidXmlDocumentation,
"MCP002" => Diagnostics.McpMethodMustBePartial,
_ => throw new InvalidOperationException($"Unknown diagnostic ID: {info.Id}")
}, info.Location?.ToLocation(), info.MessageArgs);
private static IncrementalValuesProvider<MethodToGenerate> CreateProviderForAttribute(
IncrementalGeneratorInitializationContext context,
string attributeMetadataName) =>
context.SyntaxProvider.ForAttributeWithMetadataName(
attributeMetadataName,
static (node, _) => node is MethodDeclarationSyntax,
static (ctx, _) => ExtractMethodInfo((MethodDeclarationSyntax)ctx.TargetNode, (IMethodSymbol)ctx.TargetSymbol, ctx.SemanticModel.Compilation));
private static MethodToGenerate ExtractMethodInfo(
MethodDeclarationSyntax methodDeclaration,
IMethodSymbol methodSymbol,
Compilation compilation)
{
bool isPartial = methodDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword);
var descriptionAttribute = compilation.GetTypeByMetadataName(McpAttributeNames.DescriptionAttribute);
// Try to extract XML documentation
var (xmlDocs, hasInvalidXml) = TryExtractXmlDocumentation(methodSymbol);
// For non-partial methods, check if we should report a diagnostic
if (!isPartial)
{
// Report invalid XML diagnostic only if the method would have generated content
if (hasInvalidXml)
{
// We can't know if it would have been generatable, so skip for non-partial
return MethodToGenerate.Empty;
}
// Check if this non-partial method has generatable content - if so, report diagnostic
if (xmlDocs is not null && descriptionAttribute is not null &&
HasGeneratableContent(xmlDocs, methodSymbol, descriptionAttribute))
{
return MethodToGenerate.CreateDiagnosticOnly(
DiagnosticInfo.Create("MCP002", methodDeclaration.Identifier.GetLocation(), methodSymbol.Name));
}
return MethodToGenerate.Empty;
}
// For partial methods with invalid XML, report diagnostic but still generate partial declaration.
EquatableArray<DiagnosticInfo> diagnostics = hasInvalidXml ?
new EquatableArray<DiagnosticInfo>(ImmutableArray.Create(DiagnosticInfo.Create("MCP001", methodSymbol.Locations.FirstOrDefault(), methodSymbol.Name))) :
default;
bool needsMethodDescription = xmlDocs is not null &&
!string.IsNullOrWhiteSpace(xmlDocs.MethodDescription) &&
(descriptionAttribute is null || !HasAttribute(methodSymbol, descriptionAttribute));
bool needsReturnDescription = xmlDocs is not null &&
!string.IsNullOrWhiteSpace(xmlDocs.Returns) &&
(descriptionAttribute is null ||
methodSymbol.GetReturnTypeAttributes().All(attr => !SymbolEqualityComparer.Default.Equals(attr.AttributeClass, descriptionAttribute)));
// Extract method info for partial methods
var modifiers = methodDeclaration.Modifiers
.Where(m => !m.IsKind(SyntaxKind.AsyncKeyword))
.Select(m => m.Text);
string modifiersStr = string.Join(" ", modifiers);
string returnType = methodSymbol.ReturnType.ToDisplayString(s_fullyQualifiedFormatWithNullability);
string methodName = methodSymbol.Name;
// Extract parameters
var parameterSyntaxList = methodDeclaration.ParameterList.Parameters;
ParameterInfo[] parameters = new ParameterInfo[methodSymbol.Parameters.Length];
for (int i = 0; i < methodSymbol.Parameters.Length; i++)
{
var param = methodSymbol.Parameters[i];
var paramSyntax = i < parameterSyntaxList.Count ? parameterSyntaxList[i] : null;
parameters[i] = new ParameterInfo(
ParameterType: param.Type.ToDisplayString(s_fullyQualifiedFormatWithNullability),
Name: param.Name,
HasDescriptionAttribute: descriptionAttribute is not null && HasAttribute(param, descriptionAttribute),
XmlDescription: xmlDocs?.Parameters.TryGetValue(param.Name, out var pd) == true && !string.IsNullOrWhiteSpace(pd) ? pd : null,
DefaultValue: paramSyntax?.Default?.ToFullString().Trim());
}
return new MethodToGenerate(
NeedsGeneration: true,
TypeInfo: ExtractTypeInfo(methodSymbol.ContainingType),
Modifiers: modifiersStr,
ReturnType: returnType,
MethodName: methodName,
Parameters: new EquatableArray<ParameterInfo>(parameters),
MethodDescription: needsMethodDescription ? xmlDocs?.MethodDescription : null,
ReturnDescription: needsReturnDescription ? xmlDocs?.Returns : null,
Diagnostics: diagnostics);
}
/// <summary>Checks if XML documentation would generate any Description attributes for a method.</summary>
private static bool HasGeneratableContent(XmlDocumentation xmlDocs, IMethodSymbol methodSymbol, INamedTypeSymbol descriptionAttribute)
{
if (!string.IsNullOrWhiteSpace(xmlDocs.MethodDescription) && !HasAttribute(methodSymbol, descriptionAttribute))
{
return true;
}
if (!string.IsNullOrWhiteSpace(xmlDocs.Returns) &&
methodSymbol.GetReturnTypeAttributes().All(attr => !SymbolEqualityComparer.Default.Equals(attr.AttributeClass, descriptionAttribute)))
{
return true;
}
foreach (var param in methodSymbol.Parameters)
{
if (!HasAttribute(param, descriptionAttribute) &&
xmlDocs.Parameters.TryGetValue(param.Name, out var paramDoc) &&
!string.IsNullOrWhiteSpace(paramDoc))
{
return true;
}
}
return false;
}
private static TypeInfo ExtractTypeInfo(INamedTypeSymbol? typeSymbol)
{
if (typeSymbol is null)
{
return new TypeInfo(string.Empty, default);
}
// Build list of nested types from innermost to outermost
var typesBuilder = ImmutableArray.CreateBuilder<TypeDeclarationInfo>();
for (var current = typeSymbol; current is not null; current = current.ContainingType)
{
var typeDecl = current.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() as TypeDeclarationSyntax;
string typeKeyword;
if (typeDecl is RecordDeclarationSyntax rds)
{
string classOrStruct = rds.ClassOrStructKeyword.ValueText;
if (string.IsNullOrEmpty(classOrStruct))
{
classOrStruct = "class";
}
typeKeyword = $"{typeDecl.Keyword.ValueText} {classOrStruct}";
}
else
{
typeKeyword = typeDecl?.Keyword.ValueText ?? "class";
}
typesBuilder.Add(new TypeDeclarationInfo(current.Name, typeKeyword));
}
// Reverse to get outermost first
typesBuilder.Reverse();
string ns = typeSymbol.ContainingNamespace.IsGlobalNamespace ? "" : typeSymbol.ContainingNamespace.ToDisplayString();
return new TypeInfo(ns, new EquatableArray<TypeDeclarationInfo>(typesBuilder.ToImmutable()));
}
private static (XmlDocumentation? Docs, bool HasInvalidXml) TryExtractXmlDocumentation(IMethodSymbol methodSymbol)
{
string? xmlDoc = methodSymbol.GetDocumentationCommentXml();
if (string.IsNullOrWhiteSpace(xmlDoc))
{
return (null, false);
}
try
{
if (XDocument.Parse(xmlDoc).Element("member") is not { } memberElement)
{
return (null, false);
}
var summary = CleanXmlDocText(memberElement.Element("summary")?.Value);
var remarks = CleanXmlDocText(memberElement.Element("remarks")?.Value);
var returns = CleanXmlDocText(memberElement.Element("returns")?.Value);
// Combine summary and remarks for method description.
var methodDescription =
string.IsNullOrWhiteSpace(remarks) ? summary :
string.IsNullOrWhiteSpace(summary) ? remarks :
$"{summary}\n{remarks}";
Dictionary<string, string> paramDocs = new(StringComparer.Ordinal);
foreach (var paramElement in memberElement.Elements("param"))
{
var name = paramElement.Attribute("name")?.Value;
var value = CleanXmlDocText(paramElement.Value);
if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(value))
{
paramDocs[name!] = value;
}
}
// Return documentation even if empty - we'll still generate the partial implementation
return (new XmlDocumentation(methodDescription ?? string.Empty, returns ?? string.Empty, paramDocs), false);
}
catch (XmlException)
{
return (null, true);
}
}
private static string CleanXmlDocText(string? text)
{
if (string.IsNullOrWhiteSpace(text))
{
return string.Empty;
}
// Remove leading/trailing whitespace and normalize line breaks
var lines = text!.Split('\n')
.Select(line => line.Trim())
.Where(line => !string.IsNullOrEmpty(line));
return string.Join(" ", lines).Trim();
}
private static string GenerateSourceFile(EquatableArray<MethodToGenerate> methods)
{
StringWriter sw = new();
IndentedTextWriter writer = new(sw);
writer.WriteLine("// <auto-generated/>");
writer.WriteLine($"// ModelContextProtocol.Analyzers {typeof(XmlToDescriptionGenerator).Assembly.GetName().Version}");
writer.WriteLine();
writer.WriteLine("#pragma warning disable");
writer.WriteLine();
writer.WriteLine("using System.ComponentModel;");
writer.WriteLine("using ModelContextProtocol.Server;");
writer.WriteLine();
// Group methods by namespace and containing type
var groupedMethods = methods.GroupBy(m => m.TypeInfo.Namespace);
bool firstNamespace = true;
foreach (var namespaceGroup in groupedMethods)
{
if (!firstNamespace)
{
writer.WriteLine();
}
firstNamespace = false;
// Check if this is the global namespace
bool isGlobalNamespace = string.IsNullOrEmpty(namespaceGroup.Key);
if (!isGlobalNamespace)
{
writer.WriteLine($"namespace {namespaceGroup.Key}");
writer.WriteLine("{");
writer.Indent++;
}
// Group by containing type within namespace (using structural equality for TypeInfo)
bool isFirstTypeInNamespace = true;
foreach (var typeGroup in namespaceGroup.GroupBy(m => m.TypeInfo))
{
if (!isFirstTypeInNamespace)
{
writer.WriteLine();
}
isFirstTypeInNamespace = false;
// Write out the type, which could include parent types.
AppendNestedTypeDeclarations(writer, typeGroup.Key, typeGroup);
}
if (!isGlobalNamespace)
{
writer.Indent--;
writer.WriteLine("}");
}
}
return sw.ToString();
}
private static void AppendNestedTypeDeclarations(
IndentedTextWriter writer,
TypeInfo typeInfo,
IEnumerable<MethodToGenerate> methods)
{
// Generate type declarations from outermost to innermost
int nestingCount = typeInfo.Types.Length;
foreach (var type in typeInfo.Types)
{
writer.WriteLine($"partial {type.TypeKeyword} {type.Name}");
writer.WriteLine("{");
writer.Indent++;
}
// Generate methods for this type.
bool firstMethodInType = true;
foreach (var method in methods)
{
AppendMethodDeclaration(writer, method, firstMethodInType);
firstMethodInType = false;
}
// Close all type declarations.
for (int i = 0; i < nestingCount; i++)
{
writer.Indent--;
writer.WriteLine("}");
}
}
private static void AppendMethodDeclaration(
IndentedTextWriter writer,
MethodToGenerate method,
bool firstMethodInType)
{
if (!firstMethodInType)
{
writer.WriteLine();
}
// Add the Description attribute for method if needed
if (!string.IsNullOrWhiteSpace(method.MethodDescription))
{
writer.WriteLine($"[Description(\"{EscapeString(method.MethodDescription!)}\")]");
}
// Add return: Description attribute if needed
if (!string.IsNullOrWhiteSpace(method.ReturnDescription))
{
writer.WriteLine($"[return: Description(\"{EscapeString(method.ReturnDescription!)}\")]");
}
// Write method signature
writer.Write(method.Modifiers);
writer.Write(' ');
writer.Write(method.ReturnType);
writer.Write(' ');
writer.Write(method.MethodName);
// Add parameters with their Description attributes.
writer.Write("(");
for (int i = 0; i < method.Parameters.Length; i++)
{
var param = method.Parameters[i];
if (i > 0)
{
writer.Write(", ");
}
if (!param.HasDescriptionAttribute && !string.IsNullOrWhiteSpace(param.XmlDescription))
{
writer.Write($"[Description(\"{EscapeString(param.XmlDescription!)}\")] ");
}
writer.Write(param.ParameterType);
writer.Write(' ');
writer.Write(param.Name);
// Preserve default parameter values
if (!string.IsNullOrEmpty(param.DefaultValue))
{
writer.Write(' ');
writer.Write(param.DefaultValue);
}
}
writer.WriteLine(");");
}
/// <summary>Checks if a symbol has a specific attribute applied.</summary>
private static bool HasAttribute(ISymbol symbol, INamedTypeSymbol attributeType)
{
foreach (var attr in symbol.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, attributeType))
{
return true;
}
}
return false;
}
/// <summary>Escape special characters for C# string literals.</summary>
private static string EscapeString(string text) =>
string.IsNullOrEmpty(text) ? text :
text.Replace("\\", "\\\\")
.Replace("\"", "\\\"")
.Replace("\r", "\\r")
.Replace("\n", "\\n")
.Replace("\t", "\\t");
// Cache-friendly data structures - these hold only primitive data, no symbols or syntax
/// <summary>Represents a method that may need Description attributes generated.</summary>
private readonly record struct MethodToGenerate(
bool NeedsGeneration,
TypeInfo TypeInfo,
string Modifiers,
string ReturnType,
string MethodName,
EquatableArray<ParameterInfo> Parameters,
string? MethodDescription,
string? ReturnDescription,
EquatableArray<DiagnosticInfo> Diagnostics) : IEquatable<MethodToGenerate>
{
public static MethodToGenerate Empty => new(
NeedsGeneration: false,
TypeInfo: default,
Modifiers: string.Empty,
ReturnType: string.Empty,
MethodName: string.Empty,
Parameters: default,
MethodDescription: null,
ReturnDescription: null,
Diagnostics: default);
public static MethodToGenerate CreateDiagnosticOnly(DiagnosticInfo diagnostic) => new(
NeedsGeneration: false,
TypeInfo: default,
Modifiers: string.Empty,
ReturnType: string.Empty,
MethodName: string.Empty,
Parameters: default,
MethodDescription: null,
ReturnDescription: null,
Diagnostics: new([diagnostic]));
}
/// <summary>Holds information about a method parameter.</summary>
private readonly record struct ParameterInfo(
string ParameterType,
string Name,
bool HasDescriptionAttribute,
string? XmlDescription,
string? DefaultValue);
/// <summary>Holds information about a type containing MCP methods.</summary>
private readonly record struct TypeInfo(
string Namespace,
EquatableArray<TypeDeclarationInfo> Types);
/// <summary>Holds information about a type declaration.</summary>
private readonly record struct TypeDeclarationInfo(
string Name,
string TypeKeyword);
/// <summary>Holds serializable location information for incremental generator caching.</summary>
/// <remarks>
/// Roslyn <see cref="Location"/> objects cannot be stored in incremental generator cached data
/// because they contain references to syntax trees from specific compilations. Storing them
/// causes issues when the generator returns cached data with locations from earlier compilations.
/// </remarks>
private readonly record struct LocationInfo(string FilePath, TextSpan TextSpan, LinePositionSpan LineSpan)
{
public static LocationInfo? FromLocation(Location? location) =>
location is null || !location.IsInSource ? null :
new LocationInfo(location.SourceTree?.FilePath ?? "", location.SourceSpan, location.GetLineSpan().Span);
public Location ToLocation() =>
Location.Create(FilePath, TextSpan, LineSpan);
}
/// <summary>Holds diagnostic information to be reported.</summary>
private readonly record struct DiagnosticInfo(string Id, LocationInfo? Location, string MethodName)
{
public static DiagnosticInfo Create(string id, Location? location, string methodName) =>
new(id, LocationInfo.FromLocation(location), methodName);
public object?[] MessageArgs => [MethodName];
}
/// <summary>Holds extracted XML documentation for a method (used only during extraction, not cached).</summary>
private sealed record XmlDocumentation(string MethodDescription, string Returns, Dictionary<string, string> Parameters);
}