CrestApps.Core Custom Tools
A code-defined tool is a Microsoft.Extensions.AI AIFunction registered under a name. The orchestrator adds it to requests through the tool registry; the model supplies all its arguments. Use tool instances (crestapps-core-tool-instances) when end users configure the tool, and agents (crestapps-core-ai-agents) when the callable thing is a whole profile.
Guidelines
- Inherit from
AIFunctionand overrideName,Description,JsonSchemaandInvokeCoreAsync. - Register with
AddCoreAITool<TTool>(name). Tools are singletons, also registered as keyedAIToolservices under the name. - A tool is a system tool by default (hidden from pickers, added by the orchestrator by purpose or context). Call
.Selectable()for tools users attach to profiles, and.Hidden()for helper tools only explicitly configured profiles or agents may use; hidden tools stay out of picker and shared MCP listings. - Catch failures and return a descriptive result the model can act on. Never return
null. - Purpose values are snake case strings (
content_generation, notContentGeneration); always use theAIToolPurposesconstants. - Resolve scoped services from
AIFunctionArguments.Services, not from the constructor (the tool is a singleton). - The deployment must declare
toolCalling, or capability enforcement removes every tool from the request. - Call
.RequireApproval()on a tool that changes or deletes data, sends a message, or is otherwise hard to undo. It then runs only after a person approves the call, and is left out of every request that cannot ask (see Tools That Need Approval).
Registration
builder.Services
.AddCoreAITool<WeatherTool>(WeatherTool.TheName)
.WithTitle("Get Weather")
.WithDescription("Returns current weather for a location.")
.WithCategory("Utilities")
.Selectable();
| Builder method | Use |
|---|---|
| .WithTitle(...) | UI title (defaults to the name) |
| .WithDescription(...) | Description (defaults to the name) |
| .WithCategory(...) | UI grouping |
| .WithPurpose(...) | Purpose tag: AIToolPurposes.DocumentProcessing (document_processing), ContentGeneration (content_generation), DataSourceSearch (data_source_search), Memory (memory) |
| .WithDependency(...) / .WithDependencies(...) | Tools added automatically when this one is selected |
| .WithoutDependency(...) / .WithoutDependencies(...) | Remove registered dependencies |
| .Selectable() | User-assignable |
| .Hidden() | Only for profiles or agents that name it |
| .RequireApproval() | A person approves each call before it runs (AIToolDefinitionEntry.RequiresApproval); .RequireApproval(false) clears it |
AddCoreAIToolServices<TTool>(name) registers the singleton and keyed service without a definition, for tools resolved only in code.
Tool Example
using System.Text.Json;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
public sealed class WeatherTool : AIFunction
{
public const string TheName = "get-weather";
private static readonly JsonElement _jsonSchema = JsonSerializer.Deserialize<JsonElement>("""
{
"type": "object",
"required": ["location"],
"properties": {
"location": { "type": "string", "description": "City name." },
"units": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature units." }
},
"additionalProperties": false
}
""");
public override string Name => TheName;
public override string Description => "Returns current weather for a location.";
public override JsonElement JsonSchema => _jsonSchema;
protected override async ValueTask<object> InvokeCoreAsync(
AIFunctionArguments arguments,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(arguments);
if (!arguments.TryGetValue("location", out var raw) || raw?.ToString() is not { Length: > 0 } location)
{
return """{"error":"A location is required."}""";
}
var weatherClient = arguments.Services.GetRequiredService<IWeatherClient>();
try
{
var forecast = await weatherClient.GetCurrentAsync(location, cancellationToken);
return JsonSerializer.Serialize(new { forecast.Temperature, forecast.Condition, Location = location });
}
catch (HttpRequestException)
{
return """{"error":"The weather service is unavailable. Try again later."}""";
}
}
}
IWeatherClient stands for the application's own service. Arguments arrive as JsonElement values from most providers, so read them with ToString() or deserialize rather than casting to string.
Dependencies
builder.Services
.AddCoreAITool<CreateOrderTool>("create_order")
.WithDescription("Creates an order from validated input.")
.WithDependency("get_order_schema")
.Selectable();
builder.Services
.AddCoreAITool<GetOrderSchemaTool>("get_order_schema")
.WithDescription("Returns the fields an order needs.")
.Hidden();
Profiles store only the top-level name. Missing dependencies are ignored, and shared or circular graphs are deduplicated.
Tools That Need Approval
builder.Services
.AddCoreAITool<DeleteArticleTool>(DeleteArticleTool.TheName)
.WithDescription("Deletes an article.")
.Selectable()
.RequireApproval();
DefaultToolMaterializerreadsToolRegistryEntry.RequiresApproval. WhenToolMaterializationOptions.SupportsToolApprovalistrue(an AG-UI chat run, whereAIChatResponseHandlersetsAICompletionContextKeys.ToolApprovalSupported), the tool is wrapped in Microsoft.Extensions.AI'sApprovalRequiredAIFunction: the run pauses with an interrupt and the chat shows Allow / Deny. Seecrestapps-core-ag-ui.- When
SupportsToolApprovalisfalse(the text-onlySendMessagestream, agents running inside a tool call, realtime sessions, the Claude orchestrator, background work, MCP and other channels), the tool is left out of the request, so the model never sees it and it never runs unasked. Each skip counts oncrestapps.core.ai.tool.deniedwithreason=approval. - A custom
IToolRegistryProvidersetsRequiresApproval = trueon its ownToolRegistryEntryvalues. - If an agent, realtime profile or background job needs the tool, either give that work to an AG-UI chat that can ask, or register the tool without
RequireApproval()if running it unasked is acceptable.
Rich Results for MCP Clients
A result that is more than text can implement IAIToolContentProvider (IReadOnlyList<AIContent> ToContents()); the MCP server maps it to content blocks (for example text plus resource links) instead of one text block. A plain string is still one text block.
Custom Registry Provider
using CrestApps.Core.AI.Models;
using CrestApps.Core.AI.Tooling;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
public sealed class FeatureFlagToolRegistryProvider : IToolRegistryProvider
{
private readonly IFeatureFlags _featureFlags;
public FeatureFlagToolRegistryProvider(IFeatureFlags featureFlags)
{
_featureFlags = featureFlags;
}
public async Task<IReadOnlyList<ToolRegistryEntry>> GetToolsAsync(
AICompletionContext context,
CancellationToken cancellationToken = default)
{
if (context is null || context.DisableTools || !await _featureFlags.IsEnabledAsync("beta-tools", cancellationToken))
{
return [];
}
return
[
new ToolRegistryEntry
{
Id = "beta:get-weather",
Name = WeatherTool.TheName,
Description = "Returns current weather for a location.",
Source = ToolRegistryEntrySource.Local,
CreateAsync = services => ValueTask.FromResult<AITool>(services.GetRequiredService<WeatherTool>()),
},
];
}
}
builder.Services.AddScoped<IToolRegistryProvider, FeatureFlagToolRegistryProvider>();
IFeatureFlags stands for the host's own service. IToolRegistry.GetAllAsync(context) concatenates every provider; the built-ins are SystemToolRegistryProvider, ProfileToolRegistryProvider, AgentToolRegistryProvider, ToolInstanceRegistryProvider, SharedStateToolRegistryProvider (update_shared_state when an AG-UI run shares state) and ClientToolRegistryProvider (an AG-UI client's frontend tools), plus McpToolRegistryProvider, A2AToolRegistryProvider and WebMcpPageToolRegistryProvider from their packages. ToolRegistryEntrySource is Local, McpServer, System or Agent.
Tool Authorization
- AI sessions: the AI Profile is the authorization boundary. Profile-selected tools, attached MCP connections and context-driven system tools run for any caller allowed to use the profile, including anonymous sessions. No per-user check runs at completion time; restrict who can edit profiles instead.
- Chat Interactions: the saved tool names are caller-controllable, so each send re-checks every selectable tool with
IAIToolAccessEvaluator.IsAuthorizedAsync(ClaimsPrincipal user, string toolName)and drops the ones the caller may not use (one warning). System, hidden and dependency tools are not checked, and anulluser (server-side work) skips the check. The default evaluator allows everything.
using System.Security.Claims;
using CrestApps.Core.AI.Tooling;
using Microsoft.AspNetCore.Authorization;
public sealed class PermissionToolAccessEvaluator : IAIToolAccessEvaluator
{
private readonly IAuthorizationService _authorizationService;
public PermissionToolAccessEvaluator(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
}
public async Task<bool> IsAuthorizedAsync(ClaimsPrincipal user, string toolName)
{
var result = await _authorizationService.AuthorizeAsync(user, toolName, "UseAITool");
return result.Succeeded;
}
}
builder.Services.AddScoped<IAIToolAccessEvaluator, PermissionToolAccessEvaluator>();
The caller comes from IUserAccessor.User (namespace CrestApps.Core.Security), which prefers a principal assigned on the current async flow and falls back to HttpContext.User. The built-in hubs assign Context.User at the start of each invocation; custom hosts that run completions outside a request or hub should assign it too.
MCP Server Exposure
An MCP server built on CrestApps.Core publishes registered tools through its own allow-list (ExposeAllTools or a list of tool names) and agents through a separate Agents/ExposeAllAgents switch. Hidden tools are not listed. Each MCP tool call runs inside an AIInvocationScope. See crestapps-core-mcp.
Upgrading Within 2.0 Previews
- A tool marked
RequireApproval()is no longer handed to the model unwrapped when the request cannot ask for an approval; it is left out. Agents, realtime profiles and background requests that relied on such a tool no longer see it.
Upgrading from 1.x
FunctionInvocationAICompletionServiceHandlerno longer filters AI session tools throughIAIToolAccessEvaluator. Keep a custom evaluator; it now governs Chat Interaction sends. It is not obsolete.IToolRegistryProvider.GetToolsAsyncreturnsTask<IReadOnlyList<ToolRegistryEntry>>. Some published samples still showValueTask<IEnumerable<AIToolMetadataEntry>>; follow the interface.- Declare
toolCallingon deployments; a 1.xChatpurpose projects totextGenerationonly, and tools are then stripped.