|
| 1 | +# ModelContextProtocol.SourceGenerators |
| 2 | + |
| 3 | +This project contains source generators for the Model Context Protocol C# SDK. |
| 4 | + |
| 5 | +## XmlToDescriptionGenerator |
| 6 | + |
| 7 | +This source generator automatically creates `Description` attributes from XML documentation comments for partial methods tagged with `[McpServerTool]`. |
| 8 | + |
| 9 | +### How it works |
| 10 | + |
| 11 | +When you write a partial method with: |
| 12 | +1. An `[McpServerTool]` attribute |
| 13 | +2. XML documentation comments (specifically a `<summary>` tag) |
| 14 | +3. NO existing `[Description]` attribute |
| 15 | + |
| 16 | +The generator will create a partial method declaration with a `[Description]` attribute containing the text from the XML `<summary>` element. |
| 17 | + |
| 18 | +### Example Usage |
| 19 | + |
| 20 | +**Your code:** |
| 21 | +```csharp |
| 22 | +using ModelContextProtocol.Server; |
| 23 | +using System.ComponentModel; |
| 24 | + |
| 25 | +[McpServerToolType] |
| 26 | +public partial class MyTools |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// This tool echoes the input message back to the user. |
| 30 | + /// </summary> |
| 31 | + [McpServerTool] |
| 32 | + public static partial string Echo(string message) |
| 33 | + { |
| 34 | + return $"Echo: {message}"; |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +**Generated code:** |
| 40 | +```csharp |
| 41 | +// <auto-generated/> |
| 42 | +#nullable enable |
| 43 | + |
| 44 | +using System.ComponentModel; |
| 45 | +using ModelContextProtocol.Server; |
| 46 | + |
| 47 | +namespace YourNamespace; |
| 48 | + |
| 49 | +public partial class MyTools |
| 50 | +{ |
| 51 | + [Description("This tool echoes the input message back to the user.")] |
| 52 | + public static partial string Echo(string message); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Benefits |
| 57 | + |
| 58 | +- **Reduces duplication**: You don't need to write both XML comments AND Description attributes |
| 59 | +- **Single source of truth**: XML comments are used for both documentation and runtime tool descriptions |
| 60 | +- **Automatic synchronization**: Changes to XML comments are automatically reflected in the Description attributes |
| 61 | + |
| 62 | +### Requirements |
| 63 | + |
| 64 | +- Your method must be marked as `partial` |
| 65 | +- Your method must have the `[McpServerTool]` attribute |
| 66 | +- Your method must have XML documentation comments with a `<summary>` tag |
| 67 | +- Your method must NOT already have a `[Description]` attribute |
| 68 | + |
| 69 | +### Notes |
| 70 | + |
| 71 | +- The generator only extracts text from the `<summary>` element |
| 72 | +- Multi-line summaries are combined into a single line |
| 73 | +- Only partial methods with implementations (method bodies) are supported |
0 commit comments