OpenCliToMcp.Generator
1.0.1
dotnet add package OpenCliToMcp.Generator --version 1.0.1
NuGet\Install-Package OpenCliToMcp.Generator -Version 1.0.1
<PackageReference Include="OpenCliToMcp.Generator" Version="1.0.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="OpenCliToMcp.Generator" Version="1.0.1" />
<PackageReference Include="OpenCliToMcp.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add OpenCliToMcp.Generator --version 1.0.1
#r "nuget: OpenCliToMcp.Generator, 1.0.1"
#:package OpenCliToMcp.Generator@1.0.1
#addin nuget:?package=OpenCliToMcp.Generator&version=1.0.1
#tool nuget:?package=OpenCliToMcp.Generator&version=1.0.1
OpenCliToMcp.Generator
C# source generator that automatically generates MCP (Model Context Protocol) server code from OpenCLI tool definitions.
Overview
OpenCliToMcp.Generator is a Roslyn source generator that simplifies the creation of MCP tools by automatically generating the necessary boilerplate code at compile time. It reads OpenCLI JSON definitions and generates strongly-typed C# code for MCP tool implementations.
Installation
dotnet add package OpenCliToMcp.Generator
Usage
- Add the package reference to your project
- Create an OpenCLI JSON definition file (e.g.,
mytool.opencli.json
) - Add the
[OpenCliTool]
attribute to your MCP tool class - The generator will create the implementation at compile time
Method 1: File in Project Directory
Place the OpenCLI JSON file in your project directory and reference it by filename:
[OpenCliTool("git-tool.opencli.json")]
public partial class GitToolMcp
{
// Generated code
}
Method 2: Embedded Resource
Include the JSON file as an AdditionalFiles
item in your .csproj
:
<ItemGroup>
<AdditionalFiles Include="git-tool.opencli.json" />
</ItemGroup>
Then reference it in your code:
[OpenCliTool("git-tool.opencli.json")]
public partial class GitToolMcp
{
// Generated code
}
This method is useful when:
- You want explicit control over which files are included
- The JSON file is in a different directory
- You're creating a NuGet package and want to ensure the file is included
Example OpenCLI Definition
{
"name": "git-tool",
"description": "Git operations tool",
"commands": [
{
"name": "status",
"description": "Show the working tree status",
"parameters": [
{
"name": "short",
"description": "Give the output in short format",
"type": "boolean"
}
]
}
]
}
Example Usage
using OpenCliToMcp.Core;
[OpenCliTool("git-tool.opencli.json")]
public partial class GitToolMcp
{
// The generator will create:
// - Constructor with ICliExecutor dependency
// - ToolDefinition property
// - CallToolAsync method implementation
// - Strongly-typed parameter classes
}
Generated Code
The generator creates:
- MCP tool implementation with proper method signatures
- Parameter validation and parsing
- Type-safe parameter classes
- Integration with OpenCliToMcp.Core for CLI execution
Features
- Compile-time Generation: No runtime overhead
- Type Safety: Strongly-typed parameter classes
- Validation: Built-in parameter validation
- IntelliSense Support: Full IDE support for generated code
- Diagnostics: Helpful error messages during compilation
Analyzer Diagnostics
Code | Description |
---|---|
OCLI001 | OpenCLI file not found |
OCLI002 | Invalid OpenCLI JSON format |
OCLI003 | Missing required property |
OCLI004 | Invalid parameter type |
Requirements
- .NET 6.0 or later (for the target project)
- C# 9.0 or later
- OpenCliToMcp.Core package
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.1 | 42 | 8/4/2025 |