McpServerToolsGenerator is a Roslyn incremental source generator for C# that automatically generates static tool classes for server-side tools in the ModelContextProtocol (MCP) ecosystem.
It scans your code for classes and methods annotated with specific attributes and generates static wrapper classes and methods, making it easier to expose server-side functionality in a consistent and discoverable way.
Note: If the Mermaid diagram below does not render, view this README on GitHub web with Mermaid enabled or use a Markdown viewer/editor that supports Mermaid.
flowchart TD
A["Your Annotated Class<br/>with MCP Attributes"] --> B["Roslyn Source Generator"]
B --> C["Auto-generated Static Tool Class"]
C --> D["Use Generated Static Methods"]
- Generates static tool classes for each class annotated with
[McpServerToolNameAttribute]
. - Wraps methods annotated with
[McpServerToolTypeDescriptionAttribute]
into static methods, including descriptions. - Uses Roslyn symbol comparison for attribute detection (no hardcoded attribute names).
- Supports
netstandard2.0
for broad compatibility. - Ensures generated code is consistent and reduces boilerplate.
-
Install the NuGet package:
Add the McpServerToolGenerator.FastTrack source generator to your project using the .NET CLI:
dotnet add package McpServerToolGenerator.FastTrack
Or via the NuGet Package Manager in Visual Studio.
-
Annotate your MCP server tool classes:
using McpServerToolGenerator.FastTrack.Common; [McpServerToolName("MyClass")] public class MyClass { [McpServerToolTypeDescription("Does something")] public int DoWork(int x) => x + 1; }
-
Build your project.
The generator will automatically produce McpServerTool classes (e.g.,MyClassTools
) with static methods wrapping your annotated methods. -
Use the generated McpServerTool generated in your codebase as needed.
-
Build your project.
The generator will produce a static class (e.g.,MyClassTools
) with static methods wrapping your annotated methods:public static class MyClassTools { [McpServerTool, Description("Does something")] public static int DoWork(MyClass myClass, int x) => myClass.DoWork(x); }
-
Use the generated static methods in your codebase as needed.
- .NET Standard 2.0
- Microsoft.CodeAnalysis.CSharp (Roslyn) package
- McpServerToolGenerator.FastTrack for McpServerTool source generation
- McpServerToolGenerator.FastTrack.Common for attribute definitions
FastTrackGenerator.cs
— The main source generator implementation.Common
project — Contains attributes such asMcpServerToolNameAttribute
andMcpServerToolTypeDescriptionAttribute
.
Contributions and issues are welcome! Please open a pull request or issue on GitHub.
MIT License