|
| 1 | +# 🧰 ToolsConfigurationBuilder Overview |
| 2 | + |
| 3 | +The **`ToolsConfigurationBuilder`** class provides a structured and extensible way to define and register **tools** that can be used by AI agents during execution. |
| 4 | +Tools act as **callable functions** — external operations the model can invoke dynamically to retrieve data, perform computations, or trigger actions. |
| 5 | + |
| 6 | +This builder simplifies tool creation by offering multiple overloads for different execution patterns (sync/async, typed/untyped, parameterized/no-parameter tools). |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +### 🧩 Namespace |
| 11 | + |
| 12 | +```csharp |
| 13 | +using System.Text.Json; |
| 14 | +using MaIN.Domain.Entities.Tools; |
| 15 | +``` |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +### 🏗️ Class Definition |
| 20 | + |
| 21 | +```csharp |
| 22 | +public class ToolsConfigurationBuilder |
| 23 | +``` |
| 24 | + |
| 25 | +A fluent builder for constructing a **`ToolsConfiguration`** object, which contains a list of **`ToolDefinition`** entries and optional configuration settings (such as tool selection behavior). |
| 26 | + |
| 27 | +Each added tool defines a function name, description, expected parameters, and an execution delegate. |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +### 🔹 Core Usage |
| 32 | + |
| 33 | +Used during agent configuration to specify tools the model can access: |
| 34 | + |
| 35 | +```csharp |
| 36 | +.WithTools(new ToolsConfigurationBuilder() |
| 37 | + .AddTool( |
| 38 | + name: "get_weather", |
| 39 | + description: "Retrieve current weather information", |
| 40 | + parameters: new { type = "object", properties = new { city = new { type = "string" } } }, |
| 41 | + execute: WeatherTools.GetWeather) |
| 42 | + .WithToolChoice("auto") |
| 43 | + .Build()) |
| 44 | +``` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### 🔧 Methods Overview |
| 49 | + |
| 50 | +#### **`AddDefaultTool(string type)`** |
| 51 | + |
| 52 | +Registers a basic tool definition with a specific type. |
| 53 | +Used when predefined tool behavior is already encapsulated elsewhere. |
| 54 | + |
| 55 | +```csharp |
| 56 | +.AddDefaultTool("system_logger") |
| 57 | +``` |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +#### **`AddTool(string name, string description, object parameters, Func<string, Task<string>> execute)`** |
| 62 | + |
| 63 | +Registers an asynchronous tool that receives raw JSON input and returns a serialized string result. |
| 64 | +This version is suitable when working directly with serialized arguments. |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +#### **`AddTool(string name, string description, object parameters, Func<string, string> execute)`** |
| 69 | + |
| 70 | +Registers a synchronous version of the above method. |
| 71 | +The provided function executes immediately and returns a plain string result. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +#### **`AddTool<TArgs>(string name, string description, object parameters, Func<TArgs, Task<object>> execute)`** |
| 76 | + |
| 77 | +Registers a **typed asynchronous tool**. |
| 78 | +The input JSON is automatically deserialized into a strongly-typed object (`TArgs`), and the result is serialized back to JSON. |
| 79 | + |
| 80 | +Example: |
| 81 | + |
| 82 | +```csharp |
| 83 | +.AddTool<CreateNoteArgs>( |
| 84 | + "create_note", |
| 85 | + "Create a new note with a title and content", |
| 86 | + new |
| 87 | + { |
| 88 | + type = "object", |
| 89 | + properties = new |
| 90 | + { |
| 91 | + title = new { type = "string", description = "Title of the note" }, |
| 92 | + content = new { type = "string", description = "Content of the note" } |
| 93 | + }, |
| 94 | + required = new[] { "title", "content" } |
| 95 | + }, |
| 96 | + NoteTools.CreateNoteAsync) |
| 97 | +``` |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +#### **`AddTool<TArgs>(string name, string description, object parameters, Func<TArgs, object> execute)`** |
| 102 | + |
| 103 | +Registers a **typed synchronous tool**, similar to the previous method but without asynchronous execution. |
| 104 | + |
| 105 | +The input is deserialized into the specified argument type (`TArgs`), the tool executes synchronously, and the result is serialized to JSON. |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +#### **`AddTool(string name, string description, Func<Task<object>> execute)`** |
| 110 | + |
| 111 | +Registers an **asynchronous parameterless tool**, ideal for actions that don’t require inputs (e.g., fetching current time or status). |
| 112 | + |
| 113 | +Example: |
| 114 | + |
| 115 | +```csharp |
| 116 | +.AddTool("get_current_time", "Get the current date and time", Tools.GetCurrentTimeAsync) |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +#### **`AddTool(string name, string description, Func<object> execute)`** |
| 122 | + |
| 123 | +Registers a **synchronous parameterless tool**. |
| 124 | +This overload is ideal for lightweight, immediate-return tools. |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +#### **`WithToolChoice(string choice)`** |
| 129 | + |
| 130 | +Specifies the **tool selection strategy** for the agent. |
| 131 | +Common values: |
| 132 | + |
| 133 | +* `"auto"` → The model decides when to use tools automatically. |
| 134 | +* `"none"` → Disables tool invocation. |
| 135 | +* `"required"` → Forces tool usage if applicable. |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +#### **`Build()`** |
| 140 | + |
| 141 | +Finalizes the configuration and returns a fully constructed **`ToolsConfiguration`** object. |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +### 🧠 Example Usage |
| 146 | + |
| 147 | +```csharp |
| 148 | +var toolsConfig = new ToolsConfigurationBuilder() |
| 149 | + .AddTool<GetWeatherArgs>( |
| 150 | + name: "get_weather", |
| 151 | + description: "Get current weather conditions for a city", |
| 152 | + parameters: new |
| 153 | + { |
| 154 | + type = "object", |
| 155 | + properties = new |
| 156 | + { |
| 157 | + city = new { type = "string", description = "City name" } |
| 158 | + }, |
| 159 | + required = new[] { "city" } |
| 160 | + }, |
| 161 | + execute: WeatherTools.GetWeatherAsync) |
| 162 | + .AddTool( |
| 163 | + name: "get_current_time", |
| 164 | + description: "Return the current local time", |
| 165 | + execute: Tools.GetCurrentTime) |
| 166 | + .WithToolChoice("auto") |
| 167 | + .Build(); |
| 168 | +``` |
| 169 | + |
| 170 | +This configuration can then be attached to a chat or agent: |
| 171 | + |
| 172 | +```csharp |
| 173 | +await AIHub.Chat() |
| 174 | + .WithModel("gpt-5-nano") |
| 175 | + .WithTools(toolsConfig) |
| 176 | + .CompleteAsync(interactive: true); |
| 177 | +``` |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +### 📘 Summary |
| 182 | + |
| 183 | +| Category | Description | |
| 184 | +| ----------------- | -------------------------------------------------------------------------------------- | |
| 185 | +| **Purpose** | Defines callable tools the model can execute during interaction | |
| 186 | +| **Supports** | Synchronous & asynchronous tools, typed & untyped execution | |
| 187 | +| **Serialization** | Automatically handles JSON deserialization for input and serialization for output | |
| 188 | +| **Integration** | Used directly with agents or chat sessions via `.WithTools()` | |
| 189 | +| **Flexibility** | Allows parameterized tools, parameterless tools, and dynamic tool selection strategies | |
| 190 | + |
| 191 | +--- |
| 192 | + |
| 193 | +The `ToolsConfigurationBuilder` provides a **clean, declarative, and extensible interface** for integrating external logic into AI-driven workflows, enabling agents to **go beyond text generation** and interact with real-world data and actions seamlessly. |
0 commit comments