Configure A Tool Harness
A configured tool harness receives an immutable, typed settings contract through IConfig<T>. The source generator emits the binding, schema, validation path, JSON codecs, and per-run patch contract from that type.
Use configuration when the whole harness needs behavior settings. Keep executable dependencies in ordinary constructor parameters; see Inject Harness Dependencies.
Declare Settings
using HPD.Agent;
public record SearchConfig : IConfigValidator
{
[RunOverridable]
public int MaxResults { get; init; } = 10;
public string Endpoint { get; init; } = "https://search.example";
public void Validate()
{
if (MaxResults < 1)
throw new AgentRunConfigurationException(
"SearchSettingsInvalid",
"config.maxResults",
"MaxResults must be positive.");
}
}
public class SearchHarness(
IConfig<SearchConfig> configuration)
{
[AIFunction]
public string Search(string query)
{
var settings = configuration.AgentDefaults;
return $"{query}:{settings.MaxResults}:{settings.Endpoint}";
}
}The public parameterless constructor supplies the library defaults. Settings properties are init-only and use the supported immutable shapes described below.
[RunOverridable] opts a property into the per-run patch contract. Properties without it remain construction-only.
IConfigValidator is optional. It handles semantic rules that the generated type and shape checks cannot express. Throw AgentRunConfigurationException for a stable error code and property path.
Configure In C#
assistant.WithToolHarness<SearchHarness>(options => options
.Configure(new SearchConfig
{
MaxResults = 20,
Endpoint = "https://search.internal"
}));.Configure(...) writes a complete construction setting onto the harness reference in the definition, through the harness's generated contract. It replaces a setting the definition already holds for the same harness owner. Because the setting is JSON on the definition, it is kept when the definition is serialized or stored.
Configure From YAML
The harness is named by its exact activation identity (Assembly:Namespace:Name):
toolHarnesses:
- name: MyApp:MyApp.Tools:SearchHarness
config:
maxResults: 20
endpoint: https://search.internalThe equivalent JSON is:
{
"toolHarnesses": [
{
"name": "MyApp:MyApp.Tools:SearchHarness",
"config": {
"maxResults": 20,
"endpoint": "https://search.internal"
}
}
]
}String values are not coerced into numbers or booleans. Duplicate and unknown properties fail before the effective configuration is published.
Precedence
Library defaults
→ AgentConfig document
→ explicit WithToolHarness<T>(options => options.Configure(...))
→ per-run overrideDocument settings patch the library defaults. An explicit complete .Configure(...) value replaces the document value. Run settings are partial patches; see Configure A Harness Per Run.
Supported Settings Shape
A settings contract supports:
- strings;
- booleans, integer and floating-point numbers, and
decimal; - enums;
TimeSpan,DateTime,DateTimeOffset, andGuid;JsonElement;- nested immutable record classes;
ImmutableArray<T>andImmutableDictionary<string, T>.
Settings must have a public parameterless constructor and public init-only properties. Required properties, arrays, mutable collections, and arbitrary object graphs are rejected.
The generator must run in the harness library. A referenced harness package must contain its generated immutable-contract metadata; older packages must be rebuilt.
Read The Effective Configuration
configuration.AgentDefaults is the captured construction setting.
Tools should use configuration.For(context) when the harness has run-overridable fields:
[AIFunction]
public Task<string> Search(
string query,
FunctionExecutionContext context)
{
var settings = configuration.For(context);
return SearchAsync(query, settings.MaxResults, settings.Endpoint);
}Resolve the snapshot once. Do not repeatedly read configuration after detaching background work.