For the complete documentation index, see llms.txt. This page is also available as Markdown.

Creating a Tool

Step-by-step guide to creating a custom AI tool.

This guide walks through creating a complete AI tool from start to finish.

Step 1: Define Arguments (Optional)

If your tool needs input, create an arguments record with [Description] attributes:

ContentSearchArgs.cs
using System.ComponentModel;

namespace MyProject.Tools;

/// <summary>
/// Arguments for the content search tool.
/// </summary>
public record ContentSearchArgs(
    [property: Description("The search query")] string Query,
    [property: Description("Content type to filter by (optional)")] string? ContentType = null,
    [property: Description("Maximum results (1-50, default 10)")] int MaxResults = 10);

Use records for arguments - they're immutable and provide good serialization behavior.

Step 2: Create the Tool Class

Choose the appropriate base class:

  • AIToolBase - For tools without arguments

  • AIToolBase<TArgs> - For tools with typed arguments

Step 3: Configure the AITool Attribute

The [AITool] attribute provides metadata:

Destructive Tools and Approval

IsDestructive and RequiresApproval work together to decide when a human is involved:

IsDestructive

RequiresApproval

Interactive runs (for example, Copilot Workspace)

Contextual Copilot

Non-interactive runs (for example, Automate)

false

(ignored)

Runs

Available

Runs

true

false

Runs without asking

Withheld

Denied, unless the run allows changes that don't need approval

true

true (default)

Asks the editor to approve or deny

Withheld

Denied

Set RequiresApproval = false on a destructive tool whose effect editors can undo themselves, such as saving a draft that version history can roll back. Keep approval on anything the public sees change, like publishing, or that removes content.

RequiresApproval defaults to the value of IsDestructive, so existing tools keep their current behavior. Setting it to true on a tool that isn't destructive has no effect. A tool that should be approved must also be marked destructive.

Tools that implement IAITool directly can override the RequiresApproval property instead. Its default implementation returns IsDestructive.

To make the approval message clearer, override DescribeInvocation in AIToolBase<TArgs>. It returns the sentence shown to the editor, for example "Publish 'Home', making it live." Override DescribeInvocationAsync instead when building the sentence needs a lookup, such as fetching the item's name.

Step 4: Test Your Tool

Create a test to verify the tool works:

Tool Without Arguments

For tools that don't need input:

Handling Errors

Return error information in the result rather than throwing exceptions:

Best Practices

  • Write clear descriptions - Be specific about what the tool does and what it returns so the AI knows when to use it.

  • Use descriptive argument names - Include [Description] attributes that explain the expected format and constraints.

  • Keep tools focused - Each tool should do one thing well. Prefer separate GetUser and CreateUser tools over a single ManageUsers tool.

  • Mark destructive operations - Set IsDestructive = true on tools that modify or delete data.

  • Only ask when it matters - Set RequiresApproval = false on destructive tools whose changes editors can undo, so they aren't interrupted for every draft edit.

Last updated

Was this helpful?