Generate Model Context Protocol (MCP) servers from OpenAPI specifications for AI integration with Claude Code and other tools.

Installation

dotnet add package RestClient.Net.McpGenerator

Prerequisites

First, generate the REST client using the OpenAPI Generator:

dotnet run --project RestClient.Net.OpenApiGenerator.Cli -- \
  -u api.yaml -o Generated -n YourApi.Generated

CLI Usage

dotnet run --project RestClient.Net.McpGenerator.Cli -- \
  --openapi-url api.yaml \
  --output-file Generated/McpTools.g.cs \
  --namespace YourApi.Mcp \
  --server-name YourApi \
  --ext-namespace YourApi.Generated \
  --tags "Search,Resources"

CLI Options

Option Description
--openapi-url Path to OpenAPI specification
--output-file Output file for generated MCP tools
--namespace C# namespace for MCP server
--server-name Name of the MCP server
--ext-namespace Namespace of generated REST client
--tags OpenAPI tags to include (comma-separated)

Generated Code

The generator creates MCP tool definitions that wrap the HttpClient extensions:

[McpServerToolType]
public static partial class McpTools
{
    [McpServerTool(Name = "get_user")]
    [Description("Get user by ID")]
    public static async Task<string> GetUser(
        [Description("User ID")] string id,
        HttpClient httpClient,
        CancellationToken ct)
    {
        var result = await httpClient.GetUserById(id, ct);
        return result switch
        {
            OkUser(var user) => JsonSerializer.Serialize(user),
            ErrorUser(var error) => $"Error: {error}"
        };
    }
}

Claude Code Integration

Add to your Claude Code configuration:

{
  "mcpServers": {
    "yourapi": {
      "command": "dotnet",
      "args": ["run", "--project", "YourApi.McpServer"]
    }
  }
}

Tool Naming

OpenAPI operations are converted to MCP tool names:

OpenAPI MCP Tool
GET /users/{id} get_user
POST /users create_user
PUT /users/{id} update_user
DELETE /users/{id} delete_user

See Also