Skip to content

Commit 0424b0c

Browse files
Copilotstephentoub
andcommitted
Add example documentation and update README with availability info
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 721ce02 commit 0424b0c

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Source Generator Example
2+
3+
This example demonstrates how to use the XmlToDescriptionGenerator source generator.
4+
5+
## Without Source Generator (Traditional Approach)
6+
7+
```csharp
8+
using ModelContextProtocol.Server;
9+
using System.ComponentModel;
10+
11+
[McpServerToolType]
12+
public class WeatherTools
13+
{
14+
/// <summary>
15+
/// Get the current weather for a location.
16+
/// </summary>
17+
[McpServerTool]
18+
[Description("Get the current weather for a location.")] // Duplication!
19+
public static string GetWeather(string location)
20+
{
21+
return $"Weather for {location}: Sunny, 72°F";
22+
}
23+
}
24+
```
25+
26+
## With Source Generator (Recommended)
27+
28+
```csharp
29+
using ModelContextProtocol.Server;
30+
31+
[McpServerToolType]
32+
public partial class WeatherTools
33+
{
34+
/// <summary>
35+
/// Get the current weather for a location.
36+
/// </summary>
37+
[McpServerTool]
38+
public static partial string GetWeather(string location)
39+
{
40+
return $"Weather for {location}: Sunny, 72°F";
41+
}
42+
}
43+
```
44+
45+
The source generator automatically creates a partial method declaration with the `[Description]` attribute:
46+
47+
```csharp
48+
// <auto-generated/>
49+
#nullable enable
50+
51+
using System.ComponentModel;
52+
using ModelContextProtocol.Server;
53+
54+
namespace YourNamespace;
55+
56+
public partial class WeatherTools
57+
{
58+
[Description("Get the current weather for a location.")]
59+
public static partial string GetWeather(string location);
60+
}
61+
```
62+
63+
## Key Changes Required
64+
65+
1. Make the class `partial`
66+
2. Make the method `partial`
67+
3. Remove any existing `[Description]` attribute
68+
4. Keep your XML documentation comments
69+
70+
The Description attribute will be automatically generated from your XML `<summary>` element!

src/ModelContextProtocol.SourceGenerators/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public partial class MyTools
6565
- Your method must have the `[McpServerTool]` attribute
6666
- Your method must have XML documentation comments with a `<summary>` tag
6767
- Your method must NOT already have a `[Description]` attribute
68+
- Your project must reference `ModelContextProtocol.Core` directly (the source generator is distributed with this package)
69+
70+
### Availability
71+
72+
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.
6873

6974
### Notes
7075

0 commit comments

Comments
 (0)