Path: redb-route/redb.Route.Llm.Abstractions/Tools/LlmTool.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm.Abstractions/Tools/LlmTool.cs
----
using redb.Route.Llm.Abstractions.Tools;
namespace redb.Route.Llm.Tools;
///
/// Homeless static builder for . Use when the
/// tool descriptor is authored outside of an
/// (e.g. registered directly in DI via services.AddLlmTool(LlmTool.Define(...).Build())),
/// or when the same descriptor needs to be shared across multiple routes.
///
/// Pairs with the fluent route-level DSL .AsLlmTool(name) (see
/// ) and the attribute-driven
/// form. All three styles produce the
/// same contract.
///
///
///
/// var tool = LlmTool.Define("get_order")
/// .EndpointUri("direct:order-lookup")
/// .Description("Returns order details by id.")
/// .Input("""{"type":"object","properties":{"orderId":{"type":"string"}},"required":["orderId"]}""")
/// .SideEffect(ToolSideEffect.ReadOnly)
/// .Cost(ToolCostClass.Cheap)
/// .Build();
///
/// services.AddLlmTool(tool);
///
///
///
public static class LlmTool
{
/// Starts a fluent definition for a tool descriptor with the given name.
/// Tool name exposed to the model (must be unique per registry).
public static LlmToolStaticBuilder Define(string name)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
return new LlmToolStaticBuilder(name);
}
}
///
/// Fluent builder produced by . Call
/// once all metadata has been set to materialise an
/// .
///
public sealed class LlmToolStaticBuilder
{
private readonly string _name;
private string? _endpointUri;
private string _description = string.Empty;
private string _inputSchema = RouteToolBridge.DefaultInputSchema;
private ToolSideEffect _sideEffect = ToolSideEffect.ReadOnly;
private ToolCachingPolicy _caching = ToolCachingPolicy.None;
private ToolCostClass _cost = ToolCostClass.Cheap;
private bool _requiresApproval;
private List? _requiredClaims;
internal LlmToolStaticBuilder(string name)
{
_name = name;
}
/// Sets the target redb.Route endpoint URI (e.g. direct:order-lookup).
public LlmToolStaticBuilder EndpointUri(string endpointUri)
{
ArgumentException.ThrowIfNullOrWhiteSpace(endpointUri);
_endpointUri = endpointUri;
return this;
}
/// Sets the human-readable description shown to the model.
public LlmToolStaticBuilder Description(string description)
{
ArgumentException.ThrowIfNullOrWhiteSpace(description);
_description = description;
return this;
}
/// Sets the JSON Schema describing the tool's input arguments.
public LlmToolStaticBuilder Input(string jsonSchema)
{
ArgumentException.ThrowIfNullOrWhiteSpace(jsonSchema);
_inputSchema = jsonSchema;
return this;
}
/// Declares the tool's side-effect class.
public LlmToolStaticBuilder SideEffect(ToolSideEffect sideEffect)
{
_sideEffect = sideEffect;
return this;
}
/// Declares the caching policy.
public LlmToolStaticBuilder Caching(ToolCachingPolicy caching)
{
_caching = caching;
return this;
}
/// Declares the cost class \u2014 drives budget enforcement.
public LlmToolStaticBuilder Cost(ToolCostClass cost)
{
_cost = cost;
return this;
}
/// Marks the tool as requiring explicit approval before each call.
public LlmToolStaticBuilder RequiresApproval(bool requiresApproval = true)
{
_requiresApproval = requiresApproval;
return this;
}
///
/// Declares a claim the calling principal must carry for the tool to fire.
/// Enforced — the engine verifies the requirement against the registered
/// IToolClaimsSource (the caller's principal by default) and refuses the call when it cannot;
/// see .
///
public LlmToolStaticBuilder RequireClaim(string claim)
{
ArgumentException.ThrowIfNullOrWhiteSpace(claim);
(_requiredClaims ??= []).Add(claim);
return this;
}
///
/// Materialises the descriptor. Throws if was
/// not set \u2014 the bridge has nothing to dispatch to without a target URI.
///
public ILlmToolDescriptor Build()
{
if (string.IsNullOrWhiteSpace(_endpointUri))
throw new InvalidOperationException(
$"LlmTool.Define('{_name}'): .EndpointUri(uri) must be called before .Build().");
var capability = new LlmToolCapability
{
Name = _name,
Description = _description,
InputSchema = _inputSchema,
Safety = new LlmToolSafety
{
SideEffect = _sideEffect,
Caching = _caching,
Cost = _cost,
RequiresApproval = _requiresApproval,
RequiredClaims = _requiredClaims is { Count: > 0 } cs ? [.. cs] : []
}
};
return new RouteToolBridge(capability, _endpointUri);
}
}