Table of Contents

Class ToolRegistry

Namespace
LMKit.Agents.Tools
Assembly
LM-Kit.NET.dll

A collection that manages tools available to agents for invocation by language models.

public sealed class ToolRegistry
Inheritance
ToolRegistry
Inherited Members
Extension Methods

Examples

Example: Basic tool registration and usage

using LMKit.Agents;
using LMKit.Agents.Tools;
using LMKit.Model;

// Create and populate a tool registry
var registry = new ToolRegistry();
registry.Register(new WeatherTool());
registry.Register(new CalculatorTool());

Console.WriteLine($"Registered {registry.Count} tools");

// Use with an agent
using var model = new LM("path/to/model.gguf");
var agent = Agent.CreateBuilder()
    .WithModel(model)
    .WithTools(registry)
    .Build();

var result = await agent.RunAsync("What's the weather in Paris?");
Console.WriteLine(result.Content);

Example: Attribute-based tool discovery

using LMKit.Agents.Tools;

// Define tools using attributes
public class MyTools
{
    [LMFunction("get_time", "Get the current time in a specified timezone")]
    public string GetTime(string timezone = "UTC")
    {
        var tz = TimeZoneInfo.FindSystemTimeZoneById(timezone);
        return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz).ToString("F");
    }

    [LMFunction("calculate", "Perform a mathematical calculation")]
    public double Calculate(double a, string operation, double b)
    {
        return operation switch
        {
            "+" => a + b,
            "-" => a - b,
            "*" => a * b,
            "/" => a / b,
            _ => throw new ArgumentException($"Unknown operation: {operation}")
        };
    }
}

// Register all annotated methods from an instance
var registry = new ToolRegistry();
var tools = new MyTools();
int count = registry.Register(tools);
Console.WriteLine($"Discovered and registered {count} tools");

Remarks

ToolRegistry provides centralized management of ITool instances that can be exposed to language models. Tools are stored by their unique Name using case-sensitive ordinal comparison.

Registration Methods
The registry supports multiple registration approaches:

  • Direct registration of ITool instances
  • Attribute-based discovery using LMFunctionAttribute
  • Bulk registration from MCP (Model Context Protocol) clients
  • Assembly scanning for annotated methods

Naming Conventions
Tool names should be stable across versions to avoid breaking model behavior. Prefer lowercase snake_case (e.g., get_weather, search_database).

Thread Safety
This class is not thread-safe for concurrent modifications. Synchronize access externally if registering or removing tools from multiple threads. Concurrent reads are safe after all registration is complete.

Properties

Count

Gets the number of tools currently registered.

PermissionPolicy

Gets or sets the permission policy that governs tool access.

Tools

Gets a read-only view of all tools currently registered.

Methods

EnsureValid(ToolCallPolicy)

Validates the registry contents and optionally a ToolCallPolicy.

EvaluatePermission(ITool)

Evaluates whether a tool is permitted by the configured PermissionPolicy.

EvaluatePermission(string)

Evaluates whether a tool is permitted by name using the configured PermissionPolicy.

Register(ITool, bool)

Registers a tool in the registry.

Register(McpClient, bool, CancellationToken)

Registers all tools from an MCP (Model Context Protocol) client.

Register(McpClient, Func<McpTool, bool>, bool, CancellationToken)

Registers filtered tools from an MCP client.

Register(IEnumerable<ITool>, bool)

Registers a batch of tools.

Register(object, bool)

Registers all tools discovered from methods annotated with LMFunctionAttribute on the specified instance.

Register(object, MethodInfo, bool)

Registers a tool from a specific method on an instance.

Register(Assembly, bool)

Registers all tools discovered from all types in the specified assembly.

Register(Type, bool)

Registers all tools discovered from a newly instantiated instance of the specified type.

RegisterAsync(McpClient, bool, CancellationToken)

Asynchronously registers all tools from an MCP client.

RegisterAsync(McpClient, Func<McpTool, bool>, bool, CancellationToken)

Asynchronously registers filtered tools from an MCP client.

Register<T>(bool)

Registers all tools discovered from a newly instantiated instance of T.

Remove(IEnumerable<ITool>)

Removes multiple tools from the registry.

Remove(string)

Removes a tool from the registry by its name.

TryGet(string, out ITool)

Attempts to retrieve a tool by its name.