ToolResultFunctionMiddleware
NexusLabs.Foundry.MicrosoftAgentFramework.Workflows¶
NexusLabs.Foundry.MicrosoftAgentFramework.Workflows.Middleware¶
ToolResultFunctionMiddleware Class¶
MAF function-invocation middleware that intercepts [AgentFunction] return values and
exceptions, ensuring the LLM always receives a structured JSON response instead of a raw
stack trace.
public sealed class ToolResultFunctionMiddleware : NexusLabs.Foundry.MicrosoftAgentFramework.IAIAgentBuilderPlugin
Inheritance System.Object 🡒 ToolResultFunctionMiddleware
Implements NexusLabs.Foundry.MicrosoftAgentFramework.IAIAgentBuilderPlugin
Remarks¶
When an [AgentFunction] method returns a NexusLabs.Foundry.MicrosoftAgentFramework.Tools.IToolResult:
- Success — the LLM receives the NexusLabs.Foundry.MicrosoftAgentFramework.Tools.IToolResult.BoxedValue directly.
- Failure — the LLM receives { "error": { … } } (the NexusLabs.Foundry.MicrosoftAgentFramework.Tools.IToolResult.BoxedError
wrapped), and the original System.Exception is preserved on NexusLabs.Foundry.MicrosoftAgentFramework.Tools.IToolResult.Exception
for diagnostics.
When an [AgentFunction] throws an unhandled exception, the middleware catches it,
wraps it in an NexusLabs.Foundry.MicrosoftAgentFramework.Tools.ToolResult.UnhandledFailure(System.Exception,System.Func{System.Exception,NexusLabs.Foundry.MicrosoftAgentFramework.Tools.ToolError}) result, and returns a safe generic error
message to the LLM. NexusLabs.Foundry.MicrosoftAgentFramework.Tools.IToolResult.IsTransient is null in this case.
System.OperationCanceledException is intentionally not caught — it propagates so cooperative cancellation (parent timeouts, user cancels, structured-concurrency aborts) continues to function correctly. Tools that legitimately catch and translate cancellation should do so inside the tool body, not rely on this middleware.
Non-NexusLabs.Foundry.MicrosoftAgentFramework.Tools.IToolResult return values pass through unchanged.
Methods¶
ToolResultFunctionMiddleware.Configure(AIAgentBuilderPluginOptions) Method¶
Called once per agent creation to configure the builder's middleware pipeline.
Use NexusLabs.Foundry.MicrosoftAgentFramework.AIAgentBuilderPluginOptions.AgentBuilder to add
Use(...) middleware layers.
public void Configure(NexusLabs.Foundry.MicrosoftAgentFramework.AIAgentBuilderPluginOptions options);
Parameters¶
options NexusLabs.Foundry.MicrosoftAgentFramework.AIAgentBuilderPluginOptions
Provides access to the AIAgentBuilder for the agent being constructed.
Implements Configure(AIAgentBuilderPluginOptions)