---
title: Custom Extensions
---
Adding your own capability to AI Chat means writing a `ChatExtension`. It's the same API the built-in extensions use - routes, tools, filters, providers, UI and background workers all register through the `ExtensionContext` passed to `Install`.
```csharp
services.AddPlugin(new ChatFeature {
Extensions = {
new BookingToolsExtension(),
},
});
```
## A minimal extension
```csharp
public class BookingToolsExtension() : ChatExtension("booking_tools")
{
IDbConnectionFactory dbFactory = null!;
public override void Install(ExtensionContext ctx)
{
// Install() runs after the App's services are available
dbFactory = ctx.Feature.Services.GetRequiredService();
// routes are prefixed with /ext/booking_tools automatically
ctx.AddGet("summary", async req => await GetSummaryAsync(req));
// register a tool the Chat UI's tool loop can call
ctx.RegisterTool("bookings");
}
}
```
The constructor argument is the extension **name**, which becomes:
- its `/ext/{name}` route prefix
- the default group for tools it registers
- the key used in `DisableExtensions`
- the folder its static UI assets are served from
Any public properties you add are its configuration, set where the extension is added and readable from `Install` - see [Configuring your own extensions](/chat/configuration#configuring-your-own-extensions).
## Registering tools
### From a ServiceStack Command
The most concise option: the Command's request type generates the tool's JSON Schema, and calls run through `CommandsFeature` so they're executed, validated, retried and logged like any other command.
```csharp
[Description("Summarise this hotel's room bookings: how many, what they're worth, "
+ "and a breakdown by room type")]
[Tool("the user asks about occupancy or booking revenue",
Name = "booking_summary", Safety = ToolSafety.ReadOnly)]
public class BookingSummaryCommand(IDbConnectionFactory dbFactory)
: AsyncCommandWithResult
{
protected override async Task RunAsync(
BookingSummary request, CancellationToken token)
{
using var db = await dbFactory.OpenAsync(token: token);
// ...
}
}
```
```csharp
ctx.RegisterTool("bookings");
ctx.RegisterTool(typeof(BookingSummaryCommand), "bookings"); // runtime type
```
Document the request type **for the Model**, not for a developer - `[Description]` on each property becomes that argument's schema description, and an enum property becomes its allowed values:
```csharp
public class BookingSummary
{
[Description("Only count bookings starting on or after this date")]
public DateTime? From { get; set; }
[Description("Only count bookings of this room type")]
public RoomType? Type { get; set; }
}
```
### From a raw function definition
When you need full control of the OpenAI function schema:
```csharp
ctx.RegisterTool(new JsonObject {
["type"] = "function",
["function"] = new JsonObject {
["name"] = "booking_summary",
["description"] = "Summarise this hotel's room bookings: how many, what they're "
+ "worth, and a breakdown by room type. Use for questions about occupancy or "
+ "revenue instead of querying bookings one by one.",
["parameters"] = new JsonObject {
["type"] = "object",
["properties"] = new JsonObject {
["from"] = new JsonObject {
["type"] = "string",
["description"] = "Only count bookings starting on or after this "
+ "ISO-8601 date, e.g. '2026-01-01'. Omit for all bookings.",
},
["room_type"] = new JsonObject {
["type"] = "string",
["enum"] = new JsonArray("Single", "Double", "Queen", "Twin", "Suite"),
["description"] = "Only count bookings of this room type",
},
},
},
},
}, BookingSummaryAsync, group: "bookings", safety: ToolSafety.ReadOnly);
```
Tool handlers receive the arguments the Model chose plus the context it's running in:
```csharp
async Task