Method Register
Register(ITool, bool)
Registers a new ITool instance.
public void Register(ITool tool, bool overwrite = false)
Parameters
toolIToolThe tool to register. Must not be
null. Its Name must be non-empty.overwriteboolWhen
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
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.
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
mcpClientMcpClientInitialized MCP client used to retrieve tools.
overwriteboolWhen
true, replaces any existing tool with the same name; whenfalse, a duplicate registration will cause an InvalidOperationException to be thrown by Register(ITool, bool) during the underlying asynchronous operation.cancellationTokenCancellationTokenA 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
mcpClientisnull.- InvalidOperationException
Thrown if a tool with the same name already exists and
overwriteisfalse, 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
mcpClientMcpClientInitialized MCP client used to retrieve tools.
filterFunc<McpTool, bool>Optional predicate to include or exclude tools before registration.
Passnullto include all tools as returned by the MCP client.overwriteboolWhen
true, replaces any existing tool with the same name; whenfalse, a duplicate registration will cause an InvalidOperationException to be thrown by Register(ITool, bool) during the underlying asynchronous operation.cancellationTokenCancellationTokenA 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
mcpClientisnull.- InvalidOperationException
Thrown if a tool with the same name already exists and
overwriteisfalse, 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
instanceobjectAn object instance whose annotated methods should be exposed as tools.
overwriteboolWhen
true, replaces any existing tool with the same name; whenfalse, a duplicate name causes an InvalidOperationException.
Returns
- int
The number of tools successfully registered.
Exceptions
- ArgumentNullException
Thrown when
instanceisnull.- 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
typeTypeType to instantiate and scan for annotated methods.
overwriteboolWhen
true, replaces any existing tool with the same name; whenfalse, a duplicate name causes an InvalidOperationException.
Returns
- int
The number of tools successfully registered.
Exceptions
- ArgumentNullException
Thrown when
typeisnull.- MissingMethodException
Thrown when
typehas 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
overwriteboolWhen
true, replaces any existing tool with the same name; whenfalse, a duplicate name causes an InvalidOperationException.
Returns
- int
The number of tools successfully registered.
Type Parameters
TType 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
assemblyAssemblyAssembly to scan.
overwriteboolWhen
true, replaces any existing tool with the same name; whenfalse, a duplicate name causes an InvalidOperationException.
Returns
- int
The number of tools successfully registered.
Exceptions
- ArgumentNullException
Thrown when
assemblyisnull.- 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
instanceobjectTarget object instance.
methodMethodInfoA method marked with LMFunctionAttribute.
overwriteboolWhen
true, replaces any existing tool with the same name; whenfalse, a duplicate name causes an InvalidOperationException.
Exceptions
- ArgumentNullException
Thrown when
instanceormethodisnull.- ArgumentException
Thrown when
methodis 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
toolsIEnumerable<ITool>The tools to register. Items that are
nullor have an empty Name are ignored.overwriteboolWhen
true, replaces any existing tool with the same name; whenfalse, 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
toolsisnull.- InvalidOperationException
Thrown if a tool with the same name already exists and
overwriteisfalse, or if a tool’s InputSchema is invalid. These exceptions originate from Register(ITool, bool).