-
Couldn't load subscription status.
- Fork 551
Add production-ready incremental source generator for automatic Description attributes from XML comments #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
17
commits into
main
Choose a base branch
from
copilot/scaffold-analyzer-source-generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,375
−0
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
416e1b9
Initial plan
Copilot 031a51a
Add source generator project and initial implementation
Copilot 721ce02
Complete source generator implementation with README and fix analyzer…
Copilot 0424b0c
Add example documentation and update README with availability info
Copilot 4f96860
Convert to incremental generator, add parameter/return docs support, …
Copilot 5ade3c3
Apply ASP.NET Core OpenAPI generator patterns: struct for caching, ca…
Copilot c7898da
Add tests for ASP.NET Core OpenAPI generator improvements: string esc…
Copilot 3cf7601
Address all review feedback: single file generation, nullable disable…
Copilot a7e54c7
Fix nested type indentation calculation
Copilot a5b8d2b
Address final review feedback: simplify attribute checks, use newline…
Copilot e04d9dd
Fix CI build failures by removing direct Analyzer reference that expe…
Copilot fe179cf
Remove Benefits section from README per review feedback
Copilot efc0626
Merge branch 'main' into copilot/scaffold-analyzer-source-generator
stephentoub 288e1fe
Fix CI build failures by disabling parallel builds for Core project t…
Copilot 7376e58
Fix packaging path for analyzer DLL - remove incorrect $(Configuratio…
Copilot 4c01569
Fix analyzer packaging - use $(Configuration) in path as SourceGenera…
Copilot c337fd2
Fix analyzer reference and packaging - use SetTargetFramework to ensu…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/ModelContextProtocol.SourceGenerators/ModelContextProtocol.SourceGenerators.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <Nullable>enable</Nullable> | ||
| <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
| <IsRoslynComponent>true</IsRoslynComponent> | ||
| <IsPackable>false</IsPackable> | ||
| <IncludeBuildOutput>false</IncludeBuildOutput> | ||
| <GenerateDocumentationFile>false</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
|
|
||
| <!-- Remove auto-included polyfills from Directory.Build.props --> | ||
| <ItemGroup> | ||
| <Compile Remove="..\Common\Polyfills\**\*.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" /> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # ModelContextProtocol.SourceGenerators | ||
|
|
||
| This project contains source generators for the Model Context Protocol C# SDK. | ||
|
|
||
| ## XmlToDescriptionGenerator | ||
|
|
||
| This source generator automatically creates `Description` attributes from XML documentation comments for partial methods tagged with `[McpServerTool]`. | ||
|
|
||
| ### How it works | ||
|
|
||
| When you write a partial method with: | ||
| 1. An `[McpServerTool]` attribute | ||
| 2. XML documentation comments (specifically a `<summary>` tag) | ||
| 3. NO existing `[Description]` attribute | ||
|
|
||
| The generator will create a partial method declaration with a `[Description]` attribute containing the text from the XML `<summary>` element. | ||
|
|
||
| ### Example Usage | ||
|
|
||
| **Your code:** | ||
| ```csharp | ||
| using ModelContextProtocol.Server; | ||
| using System.ComponentModel; | ||
|
|
||
| [McpServerToolType] | ||
| public partial class MyTools | ||
| { | ||
| /// <summary> | ||
| /// This tool echoes the input message back to the user. | ||
| /// </summary> | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <param name="message">The message to echo</param> | ||
| /// <returns>The echoed message</returns> | ||
| [McpServerTool] | ||
| public static partial string Echo(string message) | ||
| { | ||
| return $"Echo: {message}"; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Generated code:** | ||
| ```csharp | ||
| // <auto-generated/> | ||
| #nullable disable | ||
|
|
||
| using System.ComponentModel; | ||
| using ModelContextProtocol.Server; | ||
|
|
||
| namespace YourNamespace | ||
| { | ||
| public partial class MyTools | ||
| { | ||
| [Description("This tool echoes the input message back to the user.")] | ||
| [return: Description("The echoed message")] | ||
| public static partial string Echo([Description("The message to echo")] string message); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Requirements | ||
|
|
||
| - Your method must be marked as `partial` | ||
| - Your method must have the `[McpServerTool]`, `[McpServerPrompt]`, or `[McpServerResource]` attribute | ||
| - Your method must have XML documentation comments (e.g., `<summary>`, `<param>`, or `<returns>` tags) | ||
| - Your project must reference `ModelContextProtocol.Core` directly (the source generator is distributed with this package) | ||
|
|
||
| The generator will only add `[Description]` attributes for elements that don't already have them. | ||
|
|
||
| ### Availability | ||
|
|
||
| The source generator is distributed as part of the `ModelContextProtocol.Core` NuGet package and will be automatically available when you reference that package in your project. | ||
|
|
||
| ### Advanced Features | ||
|
|
||
| The generator intelligently fills in missing Description attributes: | ||
|
|
||
| - **Method Description**: Combines `<summary>` and `<remarks>` XML elements | ||
| - **Parameter Descriptions**: Extracts from `<param>` XML elements (only for parameters without existing Description attributes) | ||
| - **Return Description**: Extracts from `<returns>` XML element (only if not already present) | ||
|
|
||
| The generator only generates attributes that are missing, so you can mix XML documentation with explicit Description attributes as needed. | ||
|
|
||
| ### Implementation Details | ||
|
|
||
| - **Incremental Generator**: Uses `IIncrementalGenerator` for optimal build performance | ||
| - **Symbol-based Detection**: Compares attribute symbols using `SymbolEqualityComparer` for reliability | ||
| - **Efficient Filtering**: Syntax-only predicate filters candidates before expensive semantic analysis | ||
| - **Cancellation Support**: Respects cancellation tokens throughout generation pipeline | ||
| - **String Escaping**: Properly escapes special characters (quotes, backslashes, newlines, tabs) | ||
| - **Error Handling**: Gracefully handles invalid XML in documentation comments | ||
|
|
||
| ### Notes | ||
|
|
||
| - Multi-line XML comments are combined into single-line descriptions | ||
| - Only partial methods with implementations (method bodies) are supported | ||
| - Generic types are handled with proper arity in generated file names | ||
| - All generated code is marked with `<auto-generated/>` and `#nullable enable` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.