Skip to content

Commit ad86b53

Browse files
Copilotstephentoub
andauthored
Add production-ready incremental source generator for automatic Description attributes from XML comments (#899)
Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent 83098e7 commit ad86b53

File tree

9 files changed

+2080
-2
lines changed

9 files changed

+2080
-2
lines changed

Directory.Packages.props

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,19 @@
4646
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="8.14.0" />
4747
</ItemGroup>
4848

49+
<!-- Source Generator & Analyzer dependencies -->
50+
<ItemGroup>
51+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
52+
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
53+
</ItemGroup>
54+
55+
<!-- Build Infra & Packaging dependencies-->
4956
<ItemGroup>
50-
<!-- Build Infra & Packaging -->
5157
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
58+
</ItemGroup>
5259

53-
<!-- Testing dependencies -->
60+
<!-- Testing dependencies -->
61+
<ItemGroup>
5462
<PackageVersion Include="Anthropic.SDK" Version="5.8.0" />
5563
<PackageVersion Include="coverlet.collector" Version="6.0.4">
5664
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

ModelContextProtocol.slnx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@
6262
</Folder>
6363
<Folder Name="/src/">
6464
<File Path="src/Directory.Build.props" />
65+
<Project Path="src/ModelContextProtocol.Analyzers/ModelContextProtocol.Analyzers.csproj" />
6566
<Project Path="src/ModelContextProtocol.AspNetCore/ModelContextProtocol.AspNetCore.csproj" />
6667
<Project Path="src/ModelContextProtocol.Core/ModelContextProtocol.Core.csproj" />
6768
<Project Path="src/ModelContextProtocol/ModelContextProtocol.csproj" />
6869
</Folder>
6970
<Folder Name="/tests/">
71+
<Project Path="tests/ModelContextProtocol.Analyzers.Tests/ModelContextProtocol.Analyzers.Tests.csproj" />
7072
<Project Path="tests/ModelContextProtocol.AspNetCore.Tests/ModelContextProtocol.AspNetCore.Tests.csproj" />
7173
<Project Path="tests/ModelContextProtocol.TestOAuthServer/ModelContextProtocol.TestOAuthServer.csproj" />
7274
<Project Path="tests/ModelContextProtocol.Tests/ModelContextProtocol.Tests.csproj" />

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ await using McpServer server = McpServer.Create(new StdioServerTransport("MyServ
228228
await server.RunAsync();
229229
```
230230

231+
Descriptions can be added to tools, prompts, and resources in a variety of ways, including via the `[Description]` attribute from `System.ComponentModel`.
232+
This attribute may be placed on a method to provide for the tool, prompt, or resource, or on individual parameters to describe each's purpose.
233+
XML comments may also be used; if an `[McpServerTool]`, `[McpServerPrompt]`, or `[McpServerResource]`-attributed method is marked as `partial`,
234+
XML comments placed on the method will be used automatically to generate `[Description]` attributes for the method and its parameters.
235+
231236
## Acknowledgements
232237

233238
The starting point for this library was a project called [mcpdotnet](https://github.com/PederHP/mcpdotnet), initiated by [Peder Holdgaard Pedersen](https://github.com/PederHP). We are grateful for the work done by Peder and other contributors to that repository, which created a solid foundation for this library.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
using Microsoft.CodeAnalysis.Text;
5+
using System.Collections.Immutable;
6+
using System.Text;
7+
using System.Xml.Linq;
8+
9+
namespace ModelContextProtocol.Analyzers;
10+
11+
/// <summary>Provides the diagnostic descriptors used by the assembly.</summary>
12+
internal static class Diagnostics
13+
{
14+
public static DiagnosticDescriptor InvalidXmlDocumentation { get; } = new(
15+
id: "MCP001",
16+
title: "Invalid XML documentation for MCP method",
17+
messageFormat: "XML comment for method '{0}' is invalid and cannot be processed to generate [Description] attributes.",
18+
category: "mcp",
19+
defaultSeverity: DiagnosticSeverity.Warning,
20+
isEnabledByDefault: true,
21+
description: "The XML documentation comment contains invalid XML and cannot be processed to generate Description attributes.");
22+
23+
public static DiagnosticDescriptor McpMethodMustBePartial { get; } = new(
24+
id: "MCP002",
25+
title: "MCP method must be partial to generate [Description] attributes",
26+
messageFormat: "Method '{0}' has XML documentation that could be used to generate [Description] attributes, but the method is not declared as partial.",
27+
category: "mcp",
28+
defaultSeverity: DiagnosticSeverity.Warning,
29+
isEnabledByDefault: true,
30+
description: "Methods with MCP attributes should be declared as partial to allow the source generator to emit Description attributes from XML documentation comments.");
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<IsRoslynComponent>true</IsRoslynComponent>
6+
<IncludeBuildOutput>false</IncludeBuildOutput>
7+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Compile Remove="..\Common\Polyfills\**\*.cs" />
12+
<Compile Include="..\Common\Polyfills\System\Runtime\CompilerServices\IsExternalInit.cs" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
17+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)