V18 / TEMPLATE

example_echo

A reusable C# tool template that validates a text argument and returns it as JSON. It is deliberately not registered by default.

Opt-in exampleIntroduced in V2V18

What it does

Copy the class, change the name and JSON schema, and implement an asynchronous handler with argument validation and cooperative cancellation.

Example arguments

JSON
{
  "text": "hello"
}

Read the implementation

This page describes the selected release. The source link points to the same exact commit; it cannot silently open a newer implementation.

Lilith/Core/Tools/ToolTemplate.cs ↗

Implementation code

Read the implementation from this version's exact source snapshot. Helper links below stay on the same release.

Lilith/Core/Tools/ToolTemplate.cs

C#
using System.Text.Json;

namespace Lilith.Core.Tools;

/// <summary>Copy and rename this class to create a tool. Not registered by default.</summary>
public sealed class ToolTemplate : ILilithTool
{
    public string Name => "example_echo";
    public string Description => "Return the supplied text. Replace this description with your tool's purpose.";
    public JsonElement Parameters => JsonSerializer.SerializeToElement(new
    {
        type = "object",
        properties = new { text = new { type = "string", description = "Text to return." } },
        required = new[] { "text" }, additionalProperties = false
    });

    public Task<string> ExecuteAsync(JsonElement arguments, CancellationToken cancellationToken = default)
    {
        cancellationToken.ThrowIfCancellationRequested();
        if (arguments.ValueKind != JsonValueKind.Object ||
            arguments.EnumerateObject().Count() != 1 ||
            !arguments.TryGetProperty("text", out var text) || text.ValueKind != JsonValueKind.String)
            throw new ArgumentException("Expected exactly one string argument named 'text'.");
        // Replace this with your implementation; await asynchronous work and honor cancellation.
        return Task.FromResult(JsonSerializer.Serialize(new { text = text.GetString() }));
    }
}