-
Couldn't load subscription status.
- Fork 548
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 4 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
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,70 @@ | ||
| # Source Generator Example | ||
|
|
||
| This example demonstrates how to use the XmlToDescriptionGenerator source generator. | ||
|
|
||
| ## Without Source Generator (Traditional Approach) | ||
|
|
||
| ```csharp | ||
| using ModelContextProtocol.Server; | ||
| using System.ComponentModel; | ||
|
|
||
| [McpServerToolType] | ||
| public class WeatherTools | ||
| { | ||
| /// <summary> | ||
| /// Get the current weather for a location. | ||
| /// </summary> | ||
| [McpServerTool] | ||
| [Description("Get the current weather for a location.")] // Duplication! | ||
| public static string GetWeather(string location) | ||
| { | ||
| return $"Weather for {location}: Sunny, 72°F"; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## With Source Generator (Recommended) | ||
|
|
||
| ```csharp | ||
| using ModelContextProtocol.Server; | ||
|
|
||
| [McpServerToolType] | ||
| public partial class WeatherTools | ||
| { | ||
| /// <summary> | ||
| /// Get the current weather for a location. | ||
| /// </summary> | ||
| [McpServerTool] | ||
| public static partial string GetWeather(string location) | ||
| { | ||
| return $"Weather for {location}: Sunny, 72°F"; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The source generator automatically creates a partial method declaration with the `[Description]` attribute: | ||
|
|
||
| ```csharp | ||
| // <auto-generated/> | ||
| #nullable enable | ||
|
|
||
| using System.ComponentModel; | ||
| using ModelContextProtocol.Server; | ||
|
|
||
| namespace YourNamespace; | ||
|
|
||
| public partial class WeatherTools | ||
| { | ||
| [Description("Get the current weather for a location.")] | ||
| public static partial string GetWeather(string location); | ||
| } | ||
| ``` | ||
|
|
||
| ## Key Changes Required | ||
|
|
||
| 1. Make the class `partial` | ||
| 2. Make the method `partial` | ||
| 3. Remove any existing `[Description]` attribute | ||
| 4. Keep your XML documentation comments | ||
|
|
||
| The Description attribute will be automatically generated from your XML `<summary>` element! | ||
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,78 @@ | ||
| # 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]`. | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### 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
|
||
| [McpServerTool] | ||
| public static partial string Echo(string message) | ||
| { | ||
| return $"Echo: {message}"; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Generated code:** | ||
| ```csharp | ||
| // <auto-generated/> | ||
| #nullable enable | ||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| using System.ComponentModel; | ||
| using ModelContextProtocol.Server; | ||
|
|
||
| namespace YourNamespace; | ||
|
|
||
| public partial class MyTools | ||
| { | ||
| [Description("This tool echoes the input message back to the user.")] | ||
| public static partial string Echo(string message); | ||
| } | ||
| ``` | ||
|
|
||
| ### Benefits | ||
|
|
||
| - **Reduces duplication**: You don't need to write both XML comments AND Description attributes | ||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **Single source of truth**: XML comments are used for both documentation and runtime tool descriptions | ||
| - **Automatic synchronization**: Changes to XML comments are automatically reflected in the Description attributes | ||
|
|
||
| ### Requirements | ||
|
|
||
| - Your method must be marked as `partial` | ||
| - Your method must have the `[McpServerTool]` attribute | ||
| - Your method must have XML documentation comments with a `<summary>` tag | ||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - Your method must NOT already have a `[Description]` attribute | ||
| - Your project must reference `ModelContextProtocol.Core` directly (the source generator is distributed with this package) | ||
|
|
||
| ### 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. | ||
|
|
||
| ### Notes | ||
|
|
||
| - The generator only extracts text from the `<summary>` element | ||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - Multi-line summaries are combined into a single line | ||
| - Only partial methods with implementations (method bodies) are supported | ||
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.