Method Register
Register(ITool, bool)
Registers a tool in the registry.
public void Register(ITool tool, bool overwrite = false)
Parameters
toolIToolThe tool to register. Must have a non-empty Name.
overwriteboolWhen
true, replaces any existing tool with the same name. Whenfalse(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
toolisnull.- ArgumentException
Thrown when Name is null, empty, or whitespace.
- InvalidOperationException
Thrown when a tool with the same name exists and
overwriteisfalse, 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
mcpClientMcpClientAn initialized MCP client to retrieve tools from.
overwriteboolWhen
true, replaces existing tools with the same name.cancellationTokenCancellationTokenA 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
mcpClientisnull.- InvalidOperationException
Thrown when a tool name conflict occurs and
overwriteisfalse.
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
mcpClientMcpClientAn initialized MCP client.
filterFunc<McpTool, bool>A predicate to filter tools. Return
trueto include a tool,falseto exclude it. Passnullto include all tools.overwriteboolWhen
true, replaces existing tools with the same name.cancellationTokenCancellationTokenA 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
mcpClientisnull.
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
instanceobjectAn object instance with annotated methods to expose as tools.
overwriteboolWhen
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
instanceisnull.- ArgumentException
Thrown when a discovered method has unsupported parameter types.
- InvalidOperationException
Thrown when a tool name conflict occurs and
overwriteisfalse.
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
typeTypeA type with a public parameterless constructor and methods annotated with LMFunctionAttribute.
overwriteboolWhen
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
typeisnull.- 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
overwriteboolWhen
true, replaces existing tools with the same name.
Returns
- int
The number of tools registered.
Type Parameters
TA 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
assemblyAssemblyThe assembly to scan for annotated methods.
overwriteboolWhen
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
assemblyisnull.
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
instanceobjectThe target object instance.
methodMethodInfoA method annotated with LMFunctionAttribute.
overwriteboolWhen
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
instanceormethodisnull.- 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
toolsIEnumerable<ITool>The tools to register. Null entries and tools with empty names are skipped.
overwriteboolWhen
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
toolsisnull.- InvalidOperationException
Thrown when a tool name conflict occurs and
overwriteisfalse.