Skip to content

Tool Harnesses ​

A tool harness groups related C# capabilities and adds them to an agent's definition with one call.

Use a harness when the agent should see a small tool surface that belongs together, such as support tools, file tools, account tools, or project tools.

Add Program.cs ​

csharp
using HPD.Agent;
using HPD.Agent.Providers.OpenAI;
using Microsoft.Extensions.AI;

var support = new AgentConfig()
    .WithOpenAI(model: "gpt-5-mini")
    .WithInstructions("Use the support tools when they help answer the user.")
    .WithToolHarness<SupportToolHarness>();

await using var runtime = await new AgentRuntimeBuilder()
    .AddAgent("support", support)
    .BuildAsync();

var result = await runtime.RunAsync("Look up ticket 4815 and summarize the next action.");
Console.WriteLine(result.Text);

public class SupportToolHarness
{
    [AIFunction(Name = "lookup_ticket")]
    [AIDescription("Looks up a support ticket by id.")]
    public string LookupTicket(
        [AIDescription("The support ticket id.")] string ticketId)
        => $"Ticket {ticketId}: customer cannot sign in after changing email.";

    [AIFunction(Name = "suggest_next_action")]
    [AIDescription("Suggests the next support action for a ticket summary.")]
    public string SuggestNextAction(
        [AIDescription("The ticket summary.")] string summary)
        => "Verify the new email address, reset active sessions, and ask the customer to sign in again.";
}

Run it:

bash
dotnet run

What Happens ​

SupportToolHarness is the unit of registration.

WithToolHarness<SupportToolHarness>() adds the harness to the AgentConfig and exposes all generated functions from it. It writes the harness's exact activation identity into the definition, which is how BuildAsync() and stored definitions name the harness.

Each [AIFunction] method becomes a tool the model can call. The method name, descriptions, and parameter descriptions shape how the model chooses and fills the tool call.

Use WithTool<T>("function_name") when the agent should see one function. Use WithToolHarness<T>() when the agent should see the whole harness.

A harness has exactly one public constructor. This first example uses the implicit parameterless constructor. A harness that needs settings accepts IHarnessConfig<T>; a harness that needs services accepts those services as normal constructor parameters.

If the application supplies a pre-created harness with WithToolHarness<T>(instance), a null instance and a second instance of the same harness fail immediately, and so does a harness with no generated registration. The instance is a process-local part of the definition: every agent the runtime builds from it uses that instance, and it is never serialized or stored.

csharp
public sealed class SupportToolHarness(
    IHarnessConfig<SupportSettings> configuration,
    ITicketClient tickets)
{
}

Configure it with .Configure(...) and .UseDependency(...), or load settings from JSON/YAML. See Configure A Tool Harness when the simple harness grows.

Keep The First Harness Small ​

Start with two or three functions that clearly belong together. A smaller tool surface usually gives the model a cleaner choice.

When the harness grows, split it by workflow or audience. For example, expose SupportToolHarness to support agents and BillingToolHarness to billing agents instead of one large general harness.

Next ​

Next: return to the primary path with Multi-Turn Sessions.

Go deeper: for selective registration, configured harnesses, collapsed tool containers, permissions, subagents, and source generation behavior, see Author A Tool Harness and Tools, Functions, And Harnesses.

Built for production .NET agent applications.