OpenCliToMcp.Generator 1.0.1

dotnet add package OpenCliToMcp.Generator --version 1.0.1
                    
NuGet\Install-Package OpenCliToMcp.Generator -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenCliToMcp.Generator" Version="1.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenCliToMcp.Generator" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="OpenCliToMcp.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenCliToMcp.Generator --version 1.0.1
                    
#r "nuget: OpenCliToMcp.Generator, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenCliToMcp.Generator@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenCliToMcp.Generator&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=OpenCliToMcp.Generator&version=1.0.1
                    
Install as a Cake Tool

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

  1. Add the package reference to your project
  2. Create an OpenCLI JSON definition file (e.g., mytool.opencli.json)
  3. Add the [OpenCliTool] attribute to your MCP tool class
  4. 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.

There are no supported framework assets in this package.

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