Path: redb-route/redb.Route.Llm.Abstractions/Tools/IToolDescriptorRegistry.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/IToolDescriptorRegistry.cs
----
using System.Collections.Concurrent;
namespace redb.Route.Llm.Abstractions.Tools;
///
/// Registry of tool descriptors available to the agent engine. Resolved by name at dispatch time.
/// Descriptors registered via DSL (.AsLlmTool(...) / LlmTool.Define(...)) and
/// the [ExposeAsLlmTool] attribute all land here.
///
public interface IToolDescriptorRegistry
{
/// Adds or replaces a descriptor in the registry.
void Register(ILlmToolDescriptor descriptor);
/// Returns a descriptor by capability name, or null if not registered.
ILlmToolDescriptor? Get(string name);
/// Snapshot of all currently registered descriptors.
IReadOnlyList All();
}
/// Default in-memory .
public sealed class ToolDescriptorRegistry : IToolDescriptorRegistry
{
private readonly ConcurrentDictionary _descriptors = new(StringComparer.Ordinal);
///
///
/// The descriptor declares a caching policy on a tool that is not read-only. Every descriptor path
/// converges here — the .AsLlmTool(...) DSL, LlmTool.Define(...).Build(), the
/// [ExposeAsLlmTool] attribute and MCP discovery — so the rule is enforced once, for all of
/// them, instead of only where the route author happens to use the DSL.
///
public void Register(ILlmToolDescriptor descriptor)
{
ArgumentNullException.ThrowIfNull(descriptor);
// A cached entry answers a call without running the tool, so caching a mutating tool suppresses
// the side effect it stands for — up to the whole TTL, including legitimate repeats
// ("send that invoice again"). Only a read-only tool may declare a caching policy.
var safety = descriptor.Capability.Safety;
if (safety.Caching != ToolCachingPolicy.None && safety.SideEffect != ToolSideEffect.ReadOnly)
throw new InvalidOperationException(
$"Tool '{descriptor.Capability.Name}' declares Caching={safety.Caching} with "
+ $"SideEffect={safety.SideEffect}. Only read-only tools may be cached — a cache hit would "
+ "suppress the side effect.");
_descriptors[descriptor.Capability.Name] = descriptor;
}
///
public ILlmToolDescriptor? Get(string name) =>
_descriptors.TryGetValue(name, out var d) ? d : null;
///
public IReadOnlyList All() => [.. _descriptors.Values];
}