Skip to content

Commit 3397227

Browse files
committed
Add a source generator for creating [Description]s from XML comments on tools/prompts/resources
1 parent 90a2d34 commit 3397227

File tree

9 files changed

+1242
-2
lines changed

9 files changed

+1242
-2
lines changed

Directory.Packages.props

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,19 @@
4848
<PackageVersion Include="System.Net.ServerSentEvents" Version="$(System10Version)" />
4949
</ItemGroup>
5050

51+
<!-- Source Generator & Analyzer dependencies -->
5152
<ItemGroup>
53+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
54+
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
55+
</ItemGroup>
5256

53-
<!-- Build Infra & Packaging -->
57+
<!-- Build Infra & Packaging dependencies-->
58+
<ItemGroup>
5459
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
60+
</ItemGroup>
5561

56-
<!-- Testing dependencies -->
62+
<!-- Testing dependencies -->
63+
<ItemGroup>
5764
<PackageVersion Include="Anthropic.SDK" Version="5.6.0" />
5865
<PackageVersion Include="coverlet.collector" Version="6.0.4">
5966
<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
@@ -225,6 +225,11 @@ await using McpServer server = McpServer.Create(new StdioServerTransport("MyServ
225225
await server.RunAsync();
226226
```
227227

228+
Descriptions can be added to tools, prompts, and resources in a variety of ways, including via the `[Description]` attribute from `System.ComponentModel`.
229+
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.
230+
XML comments may also be used; if an `[McpServerTool]`, `[McpServerPrompt]`, or `[McpServerResource]`-attributed method is marked as `partial`,
231+
XML comments placed on the method will be used automatically to generate `[Description]` attributes for the method and its parameters.
232+
228233
## Acknowledgements
229234

230235
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
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)