Skip to content

AgentMiddlewareExtensions

NexusLabs.Foundry.MicrosoftAgentFramework.Workflows

NexusLabs.Foundry.MicrosoftAgentFramework.Workflows.Middleware

AgentMiddlewareExtensions Class

Extension methods that add Foundry middleware to the NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder and to MAF's Microsoft.Agents.AI.AIAgentBuilder directly.

public static class AgentMiddlewareExtensions

Inheritance System.Object 🡒 AgentMiddlewareExtensions

Methods

AgentMiddlewareExtensions.UseResilience(this AIAgentBuilder) Method

Adds a default resilience pipeline to an Microsoft.Agents.AI.AIAgentBuilder directly, for agents not managed by Foundry.

public static Microsoft.Agents.AI.AIAgentBuilder UseResilience(this Microsoft.Agents.AI.AIAgentBuilder builder);

Parameters

builder Microsoft.Agents.AI.AIAgentBuilder

Returns

Microsoft.Agents.AI.AIAgentBuilder

AgentMiddlewareExtensions.UseResilience(this AIAgentBuilder, ResiliencePipeline<AgentResponse>) Method

Adds a custom resilience pipeline to an Microsoft.Agents.AI.AIAgentBuilder directly, for agents not managed by Foundry.

public static Microsoft.Agents.AI.AIAgentBuilder UseResilience(this Microsoft.Agents.AI.AIAgentBuilder builder, Polly.ResiliencePipeline<Microsoft.Agents.AI.AgentResponse> pipeline);

Parameters

builder Microsoft.Agents.AI.AIAgentBuilder

pipeline Polly.ResiliencePipeline<Microsoft.Agents.AI.AgentResponse>

Returns

Microsoft.Agents.AI.AIAgentBuilder

AgentMiddlewareExtensions.UseToolResultMiddleware(this AIAgentBuilder) Method

Adds ToolResultFunctionMiddleware to an Microsoft.Agents.AI.AIAgentBuilder directly, for agents not managed by Foundry.

public static Microsoft.Agents.AI.AIAgentBuilder UseToolResultMiddleware(this Microsoft.Agents.AI.AIAgentBuilder builder);

Parameters

builder Microsoft.Agents.AI.AIAgentBuilder

Returns

Microsoft.Agents.AI.AIAgentBuilder

AgentMiddlewareExtensions.UsingResilience(this AgentFrameworkBuilder) Method

Wraps every agent created by the factory with a default resilience pipeline: 2 retries with exponential back-off and a 120-second timeout.

public static NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder UsingResilience(this NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder syringe);

Parameters

syringe NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder

Returns

NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder

Remarks

Per-agent settings can be overridden by applying NexusLabs.Foundry.MicrosoftAgentFramework.AgentResilienceAttribute to the agent class.

For a custom pipeline use the UsingResilience(this AgentFrameworkBuilder, Action<ResiliencePipelineBuilder<AgentResponse>>) overload.

AgentMiddlewareExtensions.UsingResilience(this AgentFrameworkBuilder, Action<ResiliencePipelineBuilder<AgentResponse>>) Method

Wraps every agent created by the factory with a custom resilience pipeline.

public static NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder UsingResilience(this NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder syringe, System.Action<Polly.ResiliencePipelineBuilder<Microsoft.Agents.AI.AgentResponse>> configure);

Parameters

syringe NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder

The syringe to configure.

configure System.Action<Polly.ResiliencePipelineBuilder<Microsoft.Agents.AI.AgentResponse>>

Callback to configure the Polly.ResiliencePipelineBuilder<>.

Returns

NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder

AgentMiddlewareExtensions.UsingToolResultMiddleware(this AgentFrameworkBuilder) Method

Adds ToolResultFunctionMiddleware to every agent created by the factory.

public static NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder UsingToolResultMiddleware(this NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder syringe);

Parameters

syringe NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder

Returns

NexusLabs.Foundry.MicrosoftAgentFramework.AgentFrameworkBuilder

Remarks

This ensures that all [AgentFunction] methods return a safe, structured JSON payload to the LLM — even when they throw an unhandled exception.