Skip to content

Commit 721ce02

Browse files
Copilotstephentoub
andcommitted
Complete source generator implementation with README and fix analyzer loading
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 031a51a commit 721ce02

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

src/ModelContextProtocol.Core/ModelContextProtocol.Core.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,17 @@
5454
ReferenceOutputAssembly="false" />
5555
</ItemGroup>
5656

57+
<!-- Direct analyzer reference for build-time source generation -->
58+
<ItemGroup>
59+
<Analyzer Include="$(ArtifactsDir)bin\ModelContextProtocol.SourceGenerators\netstandard2.0\ModelContextProtocol.SourceGenerators.dll" />
60+
</ItemGroup>
61+
62+
<!-- Package the analyzer DLL into the NuGet package -->
63+
<ItemGroup>
64+
<None Include="$(ArtifactsDir)bin\ModelContextProtocol.SourceGenerators\netstandard2.0\ModelContextProtocol.SourceGenerators.dll"
65+
Pack="true"
66+
PackagePath="analyzers/dotnet/cs"
67+
Visible="false" />
68+
</ItemGroup>
69+
5770
</Project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)