Hlibz.UseCases.Mcp 2.0.0-preview.4

This is a prerelease version of Hlibz.UseCases.Mcp.
dotnet add package Hlibz.UseCases.Mcp --version 2.0.0-preview.4
                    
NuGet\Install-Package Hlibz.UseCases.Mcp -Version 2.0.0-preview.4
                    
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="Hlibz.UseCases.Mcp" Version="2.0.0-preview.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hlibz.UseCases.Mcp" Version="2.0.0-preview.4" />
                    
Directory.Packages.props
<PackageReference Include="Hlibz.UseCases.Mcp" />
                    
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 Hlibz.UseCases.Mcp --version 2.0.0-preview.4
                    
#r "nuget: Hlibz.UseCases.Mcp, 2.0.0-preview.4"
                    
#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 Hlibz.UseCases.Mcp@2.0.0-preview.4
                    
#: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=Hlibz.UseCases.Mcp&version=2.0.0-preview.4&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Hlibz.UseCases.Mcp&version=2.0.0-preview.4&prerelease
                    
Install as a Cake Tool

Hlibz.UseCases.Mcp

Serves Hlibz.UseCases use cases as MCP tools (the MCP C# SDK, ModelContextProtocol), with schemas that say what the use cases check and errors that come from the use cases themselves.

dotnet add package Hlibz.UseCases.Mcp
builder.Services.AddUseCases([typeof(RenameCityCommand).Assembly]);

builder.Services
    .AddMcpServer()
    .WithHttpTransport()
    .WithUseCaseTools([typeof(CityTools).Assembly], tools =>
    {
        tools.SerializerOptions = json;                           // your API's JSON
        tools.ParameterNamingPolicy = JsonNamingPolicy.SnakeCaseLower; // city_id, not cityId
    })
    .WithToolErrors(errors => errors                              // Hlibz.Results.Mcp
        .Guide<CityNotFound>("No city has id {city_id}. Call list_cities to find it.")
        .Guide<ValidationFailed>("Fix these arguments and call the tool again: {errors}"));

[McpServerToolType]
public static class CityTools
{
    [McpServerTool(Name = "rename_city", UseStructuredContent = true)]
    [Description("Renames a city.")]
    public static async Task<CityDto> RenameAsync(
        IRenameCityCommandHandler handler, CityId cityId, string newName, CancellationToken ct) =>
        (await handler.HandleAsync(new RenameCityCommand(cityId, newName), ct)).ValueOrThrow();
}

WithUseCaseTools registers every [McpServerTool] method of the [McpServerToolType] classes in the assemblies, as WithToolsFromAssembly would, with what that can't take:

  • Your JSON options for arguments and results, and optionally a naming policy for the tools' own parameters. The SDK names them as C# does (cityId) and applies your policy only inside them; with ParameterNamingPolicy the input schema says city_id and each call's arguments are named back before they bind.
  • Schemas described as Hlibz.OpenApi describes your HTTP API, in the same words: strongly-typed IDs as the value they wrap (a uuid string, keeping the description; by default a struct whose only public property is Value, or say which with IdValueTypeOf), XML <summary>s as descriptions (Hlibz.XmlDocs.AI), and every IDescribedRule and [RuleSummary] as JSON Schema keywords plus a sentence after the summary, on types, properties, positional record parameters and the tool's own parameters. DataAnnotations' own attributes ([MaxLength], [Range]) are written by Microsoft.Extensions.AI.
  • Each tool's errors from its use case: the first parameter that is a use case handler's interface names the use case, and IUseCaseCatalog.ErrorsOf gives its errors (its error set, ValidationFailed, and the ambient errors your filters declare, such as AddConcurrencyConflictFilter's ConcurrencyConflict). Hlibz.Results.Mcp lists them in the tool's _meta with their guidance, and refuses to start while one has none, so no tool declares its own. Add an IToolErrorSource of your own for errors that don't come from a use case (a rate limit you run per tool, say).
  • Arguments checked before they bind: a missing argument, or one that can't be read as its parameter's type (text for a Guid, an int, a bool or an enum, null for a value type, a bad value inside a record), fails the call with ValidationFailed, keyed by the argument's published name (city_id, command.city_id) with what its schema expects ("Expected a string in the uuid format."). The SDK would throw while binding instead, and the model would get a generic error it can't act on. So every tool that takes arguments lists ValidationFailed, even one that runs no use case, and needs its guidance.

TransformInputSchema makes a last change to each input schema (leaving out a value no use case accepts, say). Every tool needs a Name, which its errors are listed under. A tool returning an object still opts into structured content itself (UseStructuredContent = true): the attribute decides that, whatever the options say.

Targets .NET 11. Works with the MCP C# SDK 2.4.0 and later.

A family of packages

One of the packages I build to set up new .NET APIs the way I want them: Hlibz.Results, Hlibz.UseCases, Hlibz.MinimalApi.Endpoints, Hlibz.OpenApi and Hlibz.Paging. Each works on its own, and they work best together, as in Hlibz.Stack. New minor and major versions come often, a new major whenever .NET gives me a better way to do something. Breaking changes only ever come in a major and are listed first in its release notes. You're welcome to use the packages, and to open an issue when something is wrong or missing.

Full documentation and samples: github.com/hlib-zinchenko/Hlibz.UseCases.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.0-preview.4 48 12/1/2026
2.0.0-preview.3 80 11/29/2026