OpenAPI Tools
Use HPD-Agent.OpenApi to turn operations from an OpenAPI document into model-callable agent tools. Imported operations participate in the same tool visibility, permissions, retry, and response projection system as native tools.
This package consumes an external API contract. To publish HPD Agent's own HTTP API as OpenAPI 3.1, use ASP.NET OpenAPI Contracts.
The package targets .NET 10 and .NET 11.
Install
dotnet add package HPD-Agent.OpenApi --version 0.9.7
dotnet add package HPD-Agent.Providers.OpenAI --version 0.9.7HPD-Agent.OpenApi brings in HPD-Agent.Framework. Install a provider package or configure an IChatClient directly.
Load A Local Spec
using HPD.Agent;
using HPD.Agent.OpenApi;
var specPath = Path.Combine(AppContext.BaseDirectory, "openapi.json");
var todoAgent = new AgentConfig { Name = "todo-agent" }
.WithChatClient(chatClient)
.WithOpenApi("todo", specPath, config =>
{
config.ServerUrlOverride = new Uri("https://api.example.com/");
config.OperationsToExclude = ["deleteTodo"];
config.RequiresPermission = true;
});
await using var runtime = await new AgentRuntimeBuilder()
.AddAgent("todo-agent", todoAgent)
.BuildAsync();
var sessionId = await runtime.CreateSessionAsync();
var result = await runtime.RunAsync("List the open TODOs.", sessionId);WithOpenApi(...) adds an OpenAPI capability source to the agent's definition. The source is a process-local part of the definition: every agent the runtime builds from it loads the operations, and the definition cannot be written to the agent store. The operations load when the runtime builds the agent, the first time it runs.
The package supports JSON and YAML OpenAPI documents. Exactly one of SpecPath or SpecUri must be configured.
An operation named listTodos from source todo becomes todo_listTodos.
Load A Remote Spec
var config = new OpenApiConfig
{
SpecUri = new Uri("https://api.example.com/openapi.json"),
ServerUrlOverride = new Uri("https://api.example.com/")
};
agentConfig
.WithChatClient(chatClient)
.WithOpenApi("example", config);HPD creates and owns an HttpClient when one is not supplied. Supply your own client for custom handlers, proxies, certificates, or application-owned lifetime:
var config = new OpenApiConfig
{
SpecPath = "openapi.json",
HttpClient = applicationOwnedHttpClient
};An application-provided HttpClient is not disposed by HPD.
Authentication
AuthCallback runs before each request. Resolve secrets inside the callback so rotation and refresh do not require rebuilding the runtime:
var config = new OpenApiConfig
{
SpecUri = new Uri("https://api.example.com/openapi.json"),
AuthCallback = async (request, cancellationToken) =>
{
var token = await secrets.RequireAsync(
"example:ApiToken",
"Example API",
cancellationToken: cancellationToken);
request.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue(
"Bearer",
token);
}
};Use ErrorDetector when an API reports application errors through HTTP 200:
config.ErrorDetector = (response, body) =>
{
if (body is not null &&
body.Contains("\"ok\":false", StringComparison.OrdinalIgnoreCase))
{
return new HPD.OpenApi.Core.OpenApiErrorResponse
{
StatusCode = (int)response.StatusCode,
Body = body
};
}
return null;
};Request Bodies And Schemas
By default, body properties are flattened into tool arguments. Set EnableDynamicPayload = false to expose the request body as one JSON string:
config.EnableDynamicPayload = false;Set EnablePayloadNamespacing = true when flattened nested payload names need parent-property prefixes to avoid collisions.
SchemaTransformOptions applies AIJsonUtilities.TransformSchema after OpenAPI schema conversion:
config.SchemaTransformOptions = new AIJsonSchemaTransformOptions
{
DisallowAdditionalProperties = true
};Operation Selection
Choose one filtering mechanism per source:
config.OperationsToExclude = ["deleteTodo", "replaceTodo"];or:
config.OperationSelectionPredicate = operation =>
!string.Equals(operation.Method, "DELETE", StringComparison.OrdinalIgnoreCase);OperationsToExclude and OperationSelectionPredicate are mutually exclusive. A source that produces zero operations fails building the agent, on its first run, with:
OpenApiSourceProducedNoOperationsThe failure reports the source name, remaining operation count, exclusions, and whether a predicate is configured. If an empty source is intentional:
config.AllowEmptyCatalog = true;A malformed operation is skipped without preventing the remaining operations from loading. HPD emits a structured warning with the source name, operation ID, HTTP method, path, and reason. Full exception detail goes through the configured ILogger.
Permissions And Invocation
OpenAPI tools do not require permission by default. Enable permission for an entire source when operations mutate state or reach sensitive systems:
config.RequiresPermission = true;The generated function receives the normal HPD function permission contract and can be authorized or denied through standard permission middleware.
Retryable HTTP failures (401, 408, 429, and 5xx) enter the function retry path. OpenAPI functions automatically use HPD's OpenAPI error handler for status categorization and Retry-After delays. Client errors such as 400, 404, and 422 are returned as structured model-facing errors so the model can correct its arguments.
Response Optimization
Reduce the API payload returned to the model:
config.ResponseOptimization = new ResponseOptimizationConfig
{
DataField = "result.items",
FieldsToInclude = ["id", "name", "status"],
MaxLength = 8_000
};Transforms run in this order:
- extract
DataField; - include
FieldsToIncludeor excludeFieldsToExclude; - truncate to
MaxLength.
The cross-field transformation is applied to arrays of objects as well as single objects. The default response limit is 4,000 characters.
Collapsing
Set CollapseWithinToolHarness = true to expose operations behind an OpenApi_{source} activation container. Expanding the container reveals the operation tools.
config.CollapseWithinToolHarness = true;Without collapsing, imported operations are top-level tools or flat members of their owning ToolHarness.
Harness-Owned OpenAPI Sources
A generated ToolHarness can contribute an OpenAPI source:
using HPD.Agent;
using HPD.Agent.OpenApi;
using HPD.Agent.Secrets;
public sealed class ExternalTools(ISecretResolver secrets)
{
[OpenApi(Prefix = "todo")]
[RequiresPermission]
public OpenApiConfig TodoApi() => new()
{
SpecPath = "openapi.json",
AuthCallback = async (request, cancellationToken) =>
{
var token = await secrets.RequireAsync(
"todo:ApiToken",
"Todo API",
cancellationToken: cancellationToken);
request.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue(
"Bearer",
token);
}
};
}The attributed method must be parameterless and return OpenApiConfig. Constructor injection supplies dependencies to the harness instance.
Native AOT And Public API
The package is Native AOT-compatible. Use source-generated composition and the AgentConfig.WithOpenApi(...) or [OpenApi] registration paths.
Application code should depend on WithOpenApi, OpenApiConfig, and ResponseOptimizationConfig. Specification parsing, operation loading, function construction, response formatting, and error classification are internal.
Troubleshooting
- No tools: inspect the
OpenApiSourceProducedNoOperationsfailure,OperationsToExclude,OperationSelectionPredicate, and operation IDs in the source document. - Missing server URL: set
ServerUrlOverrideor provide an absolute server URL in the OpenAPI document. - Authentication changes require a new runtime: move secret resolution into
AuthCallback. - A successful HTTP response contains an application error: configure
ErrorDetector. - Body arguments are too dynamic: set
EnableDynamicPayload = falseand pass a JSONpayloadstring, or define a tighter OpenAPI schema.