Table of Contents

Method Register

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

Register(ITool, bool)

Registers a new ITool instance.

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

Parameters

tool ITool

The tool to register. Must not be null. Its Name must be non-empty.

overwrite bool

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

Remarks

Tool names are compared using Ordinal. To avoid model confusion, keep names stable across versions (e.g., get_weather), and prefer lowercase snake_case. The tool's InputSchema must be valid JSON whose root is an object; if present, a top-level type must equal "object" (case-insensitive).

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.

Register(McpClient, bool, CancellationToken)

Registers all tools returned by the given McpClient using their original names.

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

Parameters

mcpClient McpClient

Initialized MCP client used to retrieve tools.

overwrite bool

When true, replaces any existing tool with the same name; when false, a duplicate registration will cause an InvalidOperationException to be thrown by Register(ITool, bool) during the underlying asynchronous operation.

cancellationToken CancellationToken

A CancellationToken that is observed by the underlying asynchronous operation.

Returns

int

The number of tools successfully registered.

Examples

var registry = new ToolRegistry();
var client = new McpClient(...);
int added = registry.Register(client, overwrite: false);
Console.WriteLine($"Registered {added} tools.");

Remarks

This is a blocking wrapper over RegisterAsync(McpClient, bool, CancellationToken). It synchronously waits for completion using GetAwaiter().GetResult(). Avoid calling from a single-threaded synchronization context (e.g., UI thread) to prevent deadlocks.

Tools are registered via Register(ITool, bool), which performs minimal input schema validation and enforces name uniqueness with Ordinal.

Exceptions

ArgumentNullException

mcpClient is null.

InvalidOperationException

Thrown if a tool with the same name already exists and overwrite is false, or if any tool's InputSchema is invalid JSON or not a JSON object root. These exceptions originate from Register(ITool, bool) and the schema validator invoked therein.

See Also

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

Registers all tools returned by the given McpClient with an optional filter and overwrite behavior.

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

Parameters

mcpClient McpClient

Initialized MCP client used to retrieve tools.

filter Func<McpTool, bool>

Optional predicate to include or exclude tools before registration.
Pass null to include all tools as returned by the MCP client.

overwrite bool

When true, replaces any existing tool with the same name; when false, a duplicate registration will cause an InvalidOperationException to be thrown by Register(ITool, bool) during the underlying asynchronous operation.

cancellationToken CancellationToken

A CancellationToken that is observed by the underlying asynchronous operation.

Returns

int

The number of tools successfully registered after filtering.

Examples

var registry = new ToolRegistry();
var client = new McpClient(...);
int added = registry.Register(client, tool => tool.Name.StartsWith("get_", StringComparison.Ordinal), overwrite: true);
Console.WriteLine($"Registered {added} filtered tools.");

Remarks

This is a blocking wrapper over RegisterAsync(McpClient, Func<McpTool, bool>, bool, CancellationToken). It synchronously waits for completion using GetAwaiter().GetResult(). Avoid calling from a single-threaded synchronization context (e.g., UI thread) to prevent deadlocks.

For each tool passing filter, registration is performed via Register(ITool, bool), which validates the input schema and enforces uniqueness with Ordinal.

Exceptions

ArgumentNullException

mcpClient is null.

InvalidOperationException

Thrown if a tool with the same name already exists and overwrite is false, or if any tool's InputSchema is invalid JSON or not a JSON object root. These exceptions originate from Register(ITool, bool) and the schema validator invoked therein.

See Also

Register(object, bool)

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

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

Parameters

instance object

An object instance whose annotated methods should be exposed as tools.

overwrite bool

When true, replaces any existing tool with the same name; when false, a duplicate name causes an InvalidOperationException.

Returns

int

The number of tools successfully registered.

Exceptions

ArgumentNullException

Thrown when instance is null.

ArgumentException

Thrown when a discovered method has unsupported parameter types.

InvalidOperationException

Thrown on name conflicts or invalid generated input schemas.

Register(Type, bool)

Registers all ITools discovered on a newly created instance of the specified type. The type must have a public parameterless constructor and contain instance methods annotated with LMFunctionAttribute.

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

Parameters

type Type

Type to instantiate and scan for annotated methods.

overwrite bool

When true, replaces any existing tool with the same name; when false, a duplicate name causes an InvalidOperationException.

Returns

int

The number of tools successfully registered.

Exceptions

ArgumentNullException

Thrown when type is null.

MissingMethodException

Thrown when type has no public parameterless constructor.

TargetInvocationException

Thrown if the constructor throws.

ArgumentException

Thrown when a discovered method has unsupported parameter types.

InvalidOperationException

Thrown on name conflicts or invalid generated input schemas.

Register<T>(bool)

Registers all ITools discovered on a newly created instance of T. The type must have a public parameterless constructor and contain instance methods annotated with LMFunctionAttribute.

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

Parameters

overwrite bool

When true, replaces any existing tool with the same name; when false, a duplicate name causes an InvalidOperationException.

Returns

int

The number of tools successfully registered.

Type Parameters

T

Type to instantiate and scan for annotated methods.

Exceptions

ArgumentException

Thrown when a discovered method has unsupported parameter types.

InvalidOperationException

Thrown on name conflicts or invalid generated input schemas.

Register(Assembly, bool)

Registers all ITools discovered across all types in the given assembly that expose instance methods annotated with LMFunctionAttribute.

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

Parameters

assembly Assembly

Assembly to scan.

overwrite bool

When true, replaces any existing tool with the same name; when false, a duplicate name causes an InvalidOperationException.

Returns

int

The number of tools successfully registered.

Exceptions

ArgumentNullException

Thrown when assembly is null.

MissingMethodException

Thrown when a matched type lacks a public parameterless constructor.

TargetInvocationException

Thrown if a matched type's constructor throws.

ArgumentException

Thrown when a discovered method has unsupported parameter types.

InvalidOperationException

Thrown on name conflicts or invalid generated input schemas.

Register(object, MethodInfo, bool)

Registers a single ITool built from a specific method on the provided instance. The method must be annotated with LMFunctionAttribute.

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

Parameters

instance object

Target object instance.

method MethodInfo

A method marked with LMFunctionAttribute.

overwrite bool

When true, replaces any existing tool with the same name; when false, a duplicate name causes an InvalidOperationException.

Exceptions

ArgumentNullException

Thrown when instance or method is null.

ArgumentException

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

InvalidOperationException

Thrown on name conflicts or invalid generated input schemas.

Register(IEnumerable<ITool>, bool)

Registers a batch of ITool instances.

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

Parameters

tools IEnumerable<ITool>

The tools to register. Items that are null or have an empty Name are ignored.

overwrite bool

When true, replaces any existing tool with the same name; when false, a duplicate registration will cause an InvalidOperationException via Register(ITool, bool).

Returns

int

The number of tools successfully registered.

Remarks

Each tool is validated via Register(ITool, bool) which enforces a valid JSON input schema and uniqueness (unless overwrite is true).

Exceptions

ArgumentNullException

Thrown when tools is null.

InvalidOperationException

Thrown if a tool with the same name already exists and overwrite is false, or if a tool’s InputSchema is invalid. These exceptions originate from Register(ITool, bool).