Hlibz.UseCases.Mcp
2.0.0-preview.4
dotnet add package Hlibz.UseCases.Mcp --version 2.0.0-preview.4
NuGet\Install-Package Hlibz.UseCases.Mcp -Version 2.0.0-preview.4
<PackageReference Include="Hlibz.UseCases.Mcp" Version="2.0.0-preview.4" />
<PackageVersion Include="Hlibz.UseCases.Mcp" Version="2.0.0-preview.4" />
<PackageReference Include="Hlibz.UseCases.Mcp" />
paket add Hlibz.UseCases.Mcp --version 2.0.0-preview.4
#r "nuget: Hlibz.UseCases.Mcp, 2.0.0-preview.4"
#:package Hlibz.UseCases.Mcp@2.0.0-preview.4
#addin nuget:?package=Hlibz.UseCases.Mcp&version=2.0.0-preview.4&prerelease
#tool nuget:?package=Hlibz.UseCases.Mcp&version=2.0.0-preview.4&prerelease
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; withParameterNamingPolicythe input schema sayscity_idand 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
uuidstring, keeping the description; by default a struct whose only public property isValue, or say which withIdValueTypeOf), XML<summary>s as descriptions (Hlibz.XmlDocs.AI), and everyIDescribedRuleand[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.ErrorsOfgives its errors (its error set,ValidationFailed, and the ambient errors your filters declare, such asAddConcurrencyConflictFilter'sConcurrencyConflict). Hlibz.Results.Mcp lists them in the tool's_metawith their guidance, and refuses to start while one has none, so no tool declares its own. Add anIToolErrorSourceof 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, anint, aboolor an enum,nullfor a value type, a bad value inside a record), fails the call withValidationFailed, 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 listsValidationFailed, 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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Hlibz.Results.Mcp (>= 3.0.0-preview.3)
- Hlibz.UseCases (>= 2.0.0-preview.4)
- Hlibz.XmlDocs.AI (>= 1.0.0-preview.1)
- ModelContextProtocol (>= 2.4.0)
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 |