Table of Contents

Method Register

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

Register(ITool, bool)

Registers a tool in the registry.

public void Register(ITool tool, bool overwrite = false)

Parameters

tool ITool

The tool to register. Must have a non-empty Name.

overwrite bool

When true, replaces any existing tool with the same name. When false (default), throws if a tool with the same name exists.

Examples

Example: Registering tools with overwrite control

using LMKit.Agents.Tools;

var registry = new ToolRegistry();

// First registration succeeds
registry.Register(new WeatherTool());

// Duplicate registration without overwrite throws
try
{
    registry.Register(new WeatherTool(), overwrite: false);
}
catch (InvalidOperationException ex)
{
    Console.WriteLine($"Expected error: {ex.Message}");
}

// With overwrite, the tool is replaced
registry.Register(new ImprovedWeatherTool(), overwrite: true);

Remarks

Tool names are compared using ordinal (case-sensitive) comparison. To avoid model confusion, keep tool names stable across application versions.

The tool's InputSchema must be valid JSON with an object at the root. If a type property is present, it must be "object".

Exceptions

ArgumentNullException

Thrown when tool is null.

ArgumentException

Thrown when Name is null, empty, or whitespace.

InvalidOperationException

Thrown when a tool with the same name exists and overwrite is false, or when InputSchema is invalid JSON.

Register(McpClient, bool, CancellationToken)

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

public int Register(McpClient mcpClient, bool overwrite = false, CancellationToken cancellationToken = default)

Parameters

mcpClient McpClient

An initialized MCP client to retrieve tools from.

overwrite bool

When true, replaces existing tools with the same name.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

int

The number of tools successfully registered.

Examples

Example: Registering tools from an MCP server

using LMKit.Agents.Tools;
using LMKit.Mcp.Client;

var registry = new ToolRegistry();

// Connect to an MCP server
var mcpClient = new McpClient("http://localhost:8080");
await mcpClient.ConnectAsync();

// Register all tools from the server
int count = registry.Register(mcpClient);
Console.WriteLine($"Registered {count} tools from MCP server");

Remarks

This is a blocking method that waits for the async operation to complete. Avoid calling from UI threads to prevent deadlocks. Use RegisterAsync(McpClient, bool, CancellationToken) for async contexts.

Exceptions

ArgumentNullException

Thrown when mcpClient is null.

InvalidOperationException

Thrown when a tool name conflict occurs and overwrite is false.

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

Registers filtered tools from an MCP client.

public int Register(McpClient mcpClient, Func<McpTool, bool> filter, bool overwrite = false, CancellationToken cancellationToken = default)

Parameters

mcpClient McpClient

An initialized MCP client.

filter Func<McpTool, bool>

A predicate to filter tools. Return true to include a tool, false to exclude it. Pass null to include all tools.

overwrite bool

When true, replaces existing tools with the same name.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

int

The number of tools registered after filtering.

Examples

Example: Registering only specific tools from MCP

using LMKit.Agents.Tools;
using LMKit.Mcp.Client;

var registry = new ToolRegistry();
var mcpClient = new McpClient("http://localhost:8080");

// Only register tools that start with "get_"
int count = registry.Register(
    mcpClient,
    filter: tool => tool.Name.StartsWith("get_", StringComparison.Ordinal),
    overwrite: false);

Console.WriteLine($"Registered {count} getter tools");

Exceptions

ArgumentNullException

Thrown when mcpClient is null.

Register(object, bool)

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

public int Register(object instance, bool overwrite = false)

Parameters

instance object

An object instance with annotated methods to expose as tools.

overwrite bool

When true, replaces existing tools with the same name.

Returns

int

The number of tools registered.

Examples

Example: Registering tools from an instance

using LMKit.Agents.Tools;

public class UtilityTools
{
    private readonly HttpClient _httpClient = new();

    [LMFunction("fetch_url", "Fetch content from a URL")]
    public async Task<string> FetchUrl(string url)
    {
        return await _httpClient.GetStringAsync(url);
    }

    [LMFunction("get_timestamp", "Get current UTC timestamp")]
    public string GetTimestamp()
    {
        return DateTime.UtcNow.ToString("O");
    }
}

var registry = new ToolRegistry();
var utilities = new UtilityTools();
int count = registry.Register(utilities);
Console.WriteLine($"Registered {count} utility tools");

Remarks

This method scans the instance for public instance methods annotated with LMFunctionAttribute and creates tool wrappers for each one. The tool's input schema is automatically generated from the method's parameters.

Exceptions

ArgumentNullException

Thrown when instance is null.

ArgumentException

Thrown when a discovered method has unsupported parameter types.

InvalidOperationException

Thrown when a tool name conflict occurs and overwrite is false.

Register(Type, bool)

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

public int Register(Type type, bool overwrite = false)

Parameters

type Type

A type with a public parameterless constructor and methods annotated with LMFunctionAttribute.

overwrite bool

When true, replaces existing tools with the same name.

Returns

int

The number of tools registered.

Examples

Example: Registering tools from a type

using LMKit.Agents.Tools;

var registry = new ToolRegistry();
int count = registry.Register(typeof(MathTools));
Console.WriteLine($"Registered {count} math tools");

Exceptions

ArgumentNullException

Thrown when type is null.

MissingMethodException

Thrown when the type has no public parameterless constructor.

TargetInvocationException

Thrown when the constructor throws an exception.

Register<T>(bool)

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

public int Register<T>(bool overwrite = false) where T : new()

Parameters

overwrite bool

When true, replaces existing tools with the same name.

Returns

int

The number of tools registered.

Type Parameters

T

A type with a public parameterless constructor and methods annotated with LMFunctionAttribute.

Examples

Example: Registering tools using generic type parameter

using LMKit.Agents.Tools;

var registry = new ToolRegistry();
int count = registry.Register<StringTools>();
Console.WriteLine($"Registered {count} string tools");

Register(Assembly, bool)

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

public int Register(Assembly assembly, bool overwrite = false)

Parameters

assembly Assembly

The assembly to scan for annotated methods.

overwrite bool

When true, replaces existing tools with the same name.

Returns

int

The number of tools registered.

Examples

Example: Registering all tools from an assembly

using LMKit.Agents.Tools;
using System.Reflection;

var registry = new ToolRegistry();

// Register all tools from the current assembly
var assembly = Assembly.GetExecutingAssembly();
int count = registry.Register(assembly);
Console.WriteLine($"Discovered and registered {count} tools from assembly");

Remarks

This method scans all types in the assembly for public classes with parameterless constructors that contain methods annotated with LMFunctionAttribute.

Exceptions

ArgumentNullException

Thrown when assembly is null.

Register(object, MethodInfo, bool)

Registers a tool from a specific method on an instance.

public void Register(object instance, MethodInfo method, bool overwrite = false)

Parameters

instance object

The target object instance.

method MethodInfo

A method annotated with LMFunctionAttribute.

overwrite bool

When true, replaces an existing tool with the same name.

Examples

Example: Registering a specific method as a tool

using LMKit.Agents.Tools;
using System.Reflection;

var registry = new ToolRegistry();
var service = new DataService();
var method = typeof(DataService).GetMethod("QueryDatabase");

registry.Register(service, method);

Exceptions

ArgumentNullException

Thrown when instance or method is null.

ArgumentException

Thrown when the method is not annotated or has unsupported parameter types.

Register(IEnumerable<ITool>, bool)

Registers a batch of tools.

public int Register(IEnumerable<ITool> tools, bool overwrite = false)

Parameters

tools IEnumerable<ITool>

The tools to register. Null entries and tools with empty names are skipped.

overwrite bool

When true, replaces existing tools with the same name.

Returns

int

The number of tools successfully registered.

Examples

Example: Batch registration of tools

using LMKit.Agents.Tools;

var registry = new ToolRegistry();

var tools = new ITool[]
{
    new WeatherTool(),
    new SearchTool(),
    new CalculatorTool()
};

int count = registry.Register(tools);
Console.WriteLine($"Registered {count} tools");

Exceptions

ArgumentNullException

Thrown when tools is null.

InvalidOperationException

Thrown when a tool name conflict occurs and overwrite is false.