Table of Contents

Class BeforeToolInvocationEventArgs

Namespace
LMKit.TextGeneration.Events
Assembly
LM-Kit.NET.dll

Raised just before a tool is invoked. Handlers may set Cancel to true to skip the invocation.

public sealed class BeforeToolInvocationEventArgs : EventArgs
Inheritance
BeforeToolInvocationEventArgs
Inherited Members

Examples

The following example shows how to subscribe to the BeforeToolInvocation event, inspect which tool the model is about to call, check the permission result, and optionally cancel the invocation.

using LMKit.Model;
using LMKit.TextGeneration;
using LMKit.TextGeneration.Events;

LM model = LM.LoadFromModelID("gemma3:4b");
var chat = new MultiTurnConversation(model);

chat.BeforeToolInvocation += (sender, e) =>
{
    Console.WriteLine($"Tool requested: {e.ToolCall.Name}");
    Console.WriteLine($"Arguments: {e.ToolCall.ArgumentsJson}");

    if (e.Tool != null)
    {
        Console.WriteLine($"Tool description: {e.Tool.Description}");
    }

    // Check the permission policy result when a policy is configured.
    if (e.PermissionResult.HasValue)
    {
        Console.WriteLine($"Permission: {e.PermissionResult.Value}");
    }

    // Cancel the invocation for a specific tool.
    if (e.ToolCall.Name == "dangerous_operation")
    {
        e.Cancel = true;
    }
};

var response = chat.Submit("Search the web for the latest AI news.");

Properties

Cancel

Set to true to cancel the invocation. When cancelled, no call to the tool is performed.

PermissionResult

Gets the permission result from the ToolPermissionPolicy, or null if no policy is configured.

Tool

Gets the tool instance that will be invoked, or null if the tool was not found in the registry.

ToolCall

The tool call the model requested.

Share