Class ToolRegistry
Registry for ITool instances that can be exposed to models/agents.
Tools are stored by their stable Name (case-sensitive, using Ordinal). The registry provides read-only enumeration of registered tools and name-based lookup.
public sealed class ToolRegistry
- Inheritance
-
ToolRegistry
- Inherited Members
Remarks
Responsibilities
- Enforces unique tool names with Ordinal.
- Performs minimal input-schema validation for each tool (must be a JSON object, optional
type
property should be"object"
). - Supports synchronous and asynchronous bulk registration from an McpClient with optional filtering.
- Provides validation against a ToolCallPolicy prior to model invocation.
Thread-safety
This class is not thread-safe for concurrent writers. If you need to register or remove tools from multiple
threads, synchronize calls to Register(ITool, bool), Remove(string), and MCP-based overloads externally.
Concurrent reads after construction are safe as long as no further writes occur.
Typical usage:
var registry = new ToolRegistry();
registry.Register(new WeatherTool()); // default overwrite: false
if (registry.TryGet("get_weather", out var tool))
{
var args = "{\"location\":\"Toulouse, FR\"}";
var result = await tool.InvokeAsync(args, CancellationToken.None);
}
Tool names must be unique within the registry. Attempting to register a tool with a duplicate
name will throw an InvalidOperationException unless overwrite
is set to true
.
To avoid model confusion, keep names stable across versions (e.g., get_weather
) and prefer lowercase snake_case.
Properties
- Count
Gets the total number of tools currently registered in this registry.
- Tools
Gets a read-only view of all tools currently registered in this registry.
Methods
- EnsureValid(ToolCallPolicy)
Validates the registry contents and (optionally) a ToolCallPolicy configuration before prompting a model or attempting tool invocation.
- Register(ITool, bool)
Registers a new ITool instance.
- Register(McpClient, bool, CancellationToken)
Registers all tools returned by the given McpClient using their original names.
- Register(McpClient, Func<McpTool, bool>, bool, CancellationToken)
Registers all tools returned by the given McpClient with an optional filter and overwrite behavior.
- RegisterAsync(McpClient, bool, CancellationToken)
Registers all tools returned by the given McpClient using their original names.
- RegisterAsync(McpClient, Func<McpTool, bool>, bool, CancellationToken)
Registers all tools returned by the given McpClient using their original names, with an optional filter and overwrite behavior.
- Remove(string)
Removes a previously registered tool by its exact Name.
- TryGet(string, out ITool)
Attempts to retrieve a registered tool by its Name.