CrestApps.Core Tool Instances
A developer writes a source (IAIToolInstanceSource) once; users create many instances (AIToolInstance) of it, each with its own name, description and settings. Every instance reaches the model as a distinct function, so the model only decides when to call it.
Guidelines
AddToolInstances(...)registers the catalog manager, the completion handler and the defaultIAIToolParameterContextResolver; withuseDefaultRegistry: true(default) it also registersToolInstanceRegistryProvider.- Register a store on the tool-instances builder (
AddYesSqlStores()orAddEntityCoreStores()); the feature does not pick one. YesSql hosts create the table withschemaBuilder.CreateAIToolInstanceIndexSchemaAsync(yesSqlStoreOptions). - An instance's
Nameis its stable lookup key and is fixed after creation. Its function name isinstance.GetFunctionName():tool_instance_plus a provider-safe version of the name, capped at 64 characters, with a deterministic hash when normalization is lossy, so it never collides with anAddCoreAIToolname. - Write a specific
Description; it is the model's main signal for choosing between instances. - Protect secrets in settings with ASP.NET Core Data Protection and never expose them as model arguments. Use
FixedorContextparameter fills for values the model must not choose.
Registration
builder.Services.AddCrestAppsCore(crestApps => crestApps
.AddAISuite(ai => ai
.AddYesSqlStores()
.AddToolInstances(toolInstances => toolInstances
.AddYesSqlStores()
.AddHttpApiRequestSource()
.AddDocumentationSearchSources()
.AddDataSourceSearchSource()
.AddSource<OrderStatusToolInstanceSource>("order-status", entry =>
{
entry.DisplayName = new LocalizedString("order-status", "Order status");
entry.Description = new LocalizedString("order-status", "Looks up orders in a configured order system.");
entry.Category = new LocalizedString("Integrations", "Integrations");
}))));
AddSource<TSource>(name, configure) registers a keyed scoped IAIToolInstanceSource under name (the value stored in each instance's Source) and records AIToolInstanceSourceEntry metadata (DisplayName, Description, Category, Parameters). An empty display name defaults to the source name.
Implement a Source
using CrestApps.Core;
using CrestApps.Core.AI.Tooling;
using Microsoft.Extensions.AI;
public sealed class OrderStatusToolInstanceSource : IAIToolInstanceSource
{
public AITool CreateTool(AIToolInstance instance)
{
ArgumentNullException.ThrowIfNull(instance);
var settings = instance.TryGet<OrderStatusToolSettings>(out var stored)
? stored
: new OrderStatusToolSettings();
var functionName = instance.GetFunctionName();
var description = string.IsNullOrWhiteSpace(instance.Description) ? functionName : instance.Description;
return new OrderStatusToolFunction(functionName, description, settings, instance);
}
}
public sealed class OrderStatusToolSettings
{
public string BaseUrl { get; set; }
public string ProtectedApiKey { get; set; }
}
OrderStatusToolFunction is the application's AIFunction; a full parameter-aware version is in tool-instance-parameters-examples.md. Save settings with instance.Put(new OrderStatusToolSettings { ... }).
Attach Instances
profile.Alter<AIToolInstanceMetadata>(metadata =>
{
metadata.ToolInstanceNames = ["orders-eu", "weather-api"];
});
The same works on a ChatInteraction. AIToolInstanceCompletionContextBuilderHandler copies the names onto AICompletionContext.ToolInstanceNames, and the registry provider materializes each one, so every orchestrator honors them.
Parameters
Parameters let whoever configures an instance declare typed, described arguments instead of an untyped bag. Each parameter (AIToolInstanceParameter) has a Name, Description, Type (String, Integer, Number, Boolean), Required, DefaultValue, AllowedValues, IsSecret, a fill and a binding:
| AIToolParameterFill | In the schema | Value from |
|---|---|---|
| Model | Yes | The model, or the default when omitted |
| Fixed | No | A value pinned on the instance |
| Context | No | An IAIToolParameterContextResolver key such as user.id, user.name, user.email, resource.id, now.utc |
Parameter support is opt-in per source. A source advertises what it can honor with AIToolInstanceSourceEntry.Parameters; a source without capabilities hides the editor and rejects saved parameters, so a parameter can never be shown to the model and then dropped.
.AddSource<OrderStatusToolInstanceSource>("order-status", entry =>
{
entry.DisplayName = new LocalizedString("order-status", "Order status");
entry.Parameters = new AIToolInstanceParameterCapabilities
{
ReservedNames = ["query"],
Bindings =
[
new AIToolParameterBindingOption("Argument")
{
DisplayName = new LocalizedString("Argument", "Call argument"),
},
new AIToolParameterBindingOption("Header")
{
DisplayName = new LocalizedString("Header", "Request header"),
AllowedFills = [AIToolParameterFill.Fixed, AIToolParameterFill.Context],
},
],
};
});
Then consume them in the function: AIToolParameterBinder.GetParameters(instance), AIToolParameterSchemaBuilder.Merge(baseSchema, parameters) for the schema (only model-filled parameters appear), and AIToolParameterBinder.Resolve(parameters, arguments, services, unprotect) at invocation, which applies precedence, coercion, allowed values and defaults and returns errors to report to the model. Declaring without consuming silently drops values; test both halves.
Register an IAIToolParameterContextResolver (SupportedKeys plus TryResolve(contextKey, services, out value)) to add context keys; resolvers are tried in registration order.
Built-in Sources
| Registration | Source name | What an instance does |
|---|---|---|
| AddHttpApiRequestSource() | http-api-request | Calls one HTTP API with stored auth (None, ApiKey, Bearer, Basic, OAuth2 with cached tokens), headers, PathTemplate and parameter bindings Query:<key>, Path, Body:<dotted.path>, Header:<name> (model fills refused for headers) |
| AddSitemapDocumentationSource() | sitemap-documentation | Crawls a site's sitemap (index, gzip, RSS/Atom, text, robots.txt discovery) and ranks pages locally |
| AddSearchIndexDocumentationSource() | search-index-documentation | Downloads a prebuilt JSON search index and ranks it locally |
| AddAlgoliaDocumentationSource() | algolia-documentation | Queries Algolia DocSearch |
| AddWebsiteSearchSource() | website-search | Queries a site's own search API live (WordPress REST by default; paths overridable) |
| AddDataSourceSearchSource() | data-source-search | Vector search over one AI data source |
HTTP API OAuth 2.0 Tokens
With AuthenticationType = OAuth2, the tool caches its tokens on the instance as HttpApiRequestTokenState (access and refresh tokens data-protected with HttpApiRequestToolConstants.DataProtectionPurpose, token type, expiry, plus the BaseUrl and TokenEndpoint they were issued for):
- A cached state is used only when its
BaseUrlandTokenEndpointmatch the instance's current settings, so an access token never goes to another host and a refresh token is never posted to another endpoint. A state saved by an older version has no addresses and is ignored once (a new token is requested). - A new token is requested with
refresh_token, elsepasswordwhenUsernameis set, elseclient_credentials. When every token request fails, the call goes out without a token; an expired cached token is never sent. - Before saving, the tool re-reads the stored instance and saves nothing when it was deleted or a token-relevant setting (
AuthenticationType,BaseUrl,TokenEndpoint,ClientId,ClientSecret,Username,Password,Scope) changed during the call; otherwise the state is written onto the stored copy, keeping concurrent edits. - Response bodies are read through
BoundedHttpContentand cut to 16,000 characters, so the tool never buffers more than it returns.
AddDocumentationSearchSources() adds all four documentation sources. DocumentationSearchOptions sets MaxResultsPerSite (5), MaxPagesPerSite (200), MaxConcurrentRequests (4), CacheDuration (1 hour) and FirstSearchWaitBudget (8 seconds): the first search on an unindexed site builds the corpus in the background and answers "still indexing" after the budget instead of blocking. Empty crawls are cached for only 5 minutes, and empty results tell the model rewording will not help.
DataSourceSearchToolSettings holds DataSourceId, RetrievalMode (DataSourceRetrievalMode.Chunk or Hierarchical), TopNDocuments, Strictness and an OData Filter. Queries use the embedding deployment the index was built with. One call may carry up to three phrases; they are embedded in one batch, searched in parallel and merged with Reciprocal Rank Fusion under shared [doc:N] citations. Hierarchical returns whole source documents and degrades to chunks when they cannot be read.
Gate Instances Per User
ToolInstanceRegistryProvider is public. Subclass it and override ShouldIncludeInstanceAsync:
using CrestApps.Core.AI.Models;
using CrestApps.Core.AI.Tooling;
using CrestApps.Core.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
public sealed class PermissionAwareToolInstanceRegistryProvider : ToolInstanceRegistryProvider
{
private readonly IAuthorizationService _authorizationService;
private readonly IUserAccessor _userAccessor;
public PermissionAwareToolInstanceRegistryProvider(
IServiceProvider serviceProvider,
ILogger<ToolInstanceRegistryProvider> logger,
IAuthorizationService authorizationService,
IUserAccessor userAccessor)
: base(serviceProvider, logger)
{
_authorizationService = authorizationService;
_userAccessor = userAccessor;
}
protected override async ValueTask<bool> ShouldIncludeInstanceAsync(
AIToolInstance instance,
AICompletionContext context,
CancellationToken cancellationToken)
{
var user = _userAccessor.User;
if (user is null)
{
return true;
}
var result = await _authorizationService.AuthorizeAsync(user, instance, "UseToolInstance");
return result.Succeeded;
}
}
ai.AddToolInstances(toolInstances => toolInstances
.AddYesSqlStores()
.AddHttpApiRequestSource(),
useDefaultRegistry: false);
builder.Services.AddScoped<IToolRegistryProvider, PermissionAwareToolInstanceRegistryProvider>();
Keep useDefaultRegistry: true only when a custom provider is additive. The provider skips unknown sources with a warning and catches exceptions from CreateTool.
Upgrading Within 2.0 Previews
- HTTP API OAuth 2.0 tokens are bound to the base URL and token endpoint they came from; every OAuth 2.0 instance requests a fresh token once after the upgrade.
- The documentation sitemap and search index sources read through size limits (
DocumentationToolConstants.MaxPageBytes,MaxSearchIndexBytes); oversized pages are skipped.
Upgrading from 1.x
ToolInstanceRegistryProvider's constructor takes(IServiceProvider, ILogger<ToolInstanceRegistryProvider>)and resolvesINamedCatalog<AIToolInstance>lazily; subclasses written against samples that passed the catalog must change their base call.- Instances that declare no parameters keep a byte-identical schema. Parameters are new and opt-in per source.
- New sources in 2.0:
AddDataSourceSearchSource()and the liveAddWebsiteSearchSource(). The sitemap source now follows sitemap indexes, gzip, feeds androbots.txt, and no longer blocks on a slow first crawl.