Files
agent-framework/dotnet/samples/GettingStarted/Agents/Agent_Step14_Middleware/README.md
T
Roger Barreto 7a8b456294 .NET: Add Agent Filtering Middleware (#478)
* WIP

* Wip

* Updated ADR

* Updated ADR

* Update files

* Address copilot comments

* Update filters from Task<T> to Task only

* Project endpoint

* Add agent ctor filter

* Other Agent Framework investigation

* Remove SK Java, no support

* Update LlamaIndex info

* Removing unrelated files

* Implementation with specialization

* Remove the specialization option as extra unecessary complexity

* Move middleware responsibility to a decorator

* Update readme

* Function invocation wip

* Add Agent Builder

* Adding comparison samples

* Reorganize Samples and Processor vs Decorator

* Remove merge files

* Address formating warnigs

* Update ADR

* Step13 README's update

* Address PR feedback

* Address PR feedback

* Remove configure await from ADR samples

* Update variables

* Address feedback

* Address Agent level tool invocation with Options.ToolsTransformer strategy

* Removing the Processor approach

* Proposal design for Middleware in CreateAIAgent extensions

* Examples clean up and consolitation

* Update middlewares to work with ApprovalREquiredFunction

* Clean-up sample

* Update override function call sample

* Drop configuration from the extensions, looks overkill

* Builder interface ..

* Revert IAIBuilder interface approach

* Cleanup sample

* Adding unit tests

* Fix UT

* Cleanup sample

* Remove unneeded dependency

* Address PR comment + Readme Samples

* Add missing comments for Program.cs Middleware

* Address mor PR comments + add client factory for OpenAI extensions

* Add OpenAI UnitTests for extensions

* Add AzureAI PersistentChatClient UT

* Addess feedback

* Add function invoking UT

* Add builder extension UT

* Address feedback + Rearange abstractions + UT fixes

* Drop context based middleware for full decorating impl

* Update unit tests

* Update UT coverage

* Removing Middelware namespace

* Add missing UT

* Remove internal ToolTransformation Property

* Adjust xmldoc

* Remove transient file

* Address merge conflict

* Add xmldoc remark for clarity

* Address comment

* Address feedback

* Update UT

---------

Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
2025-09-29 17:58:04 +00:00

1.3 KiB
Raw Blame History

Agent Middleware

This sample demonstrates how to add middleware to intercept:

  • Chat client calls (global and perrequest)
  • Agent runs (guardrails and PII filtering)
  • Function calling (logging/override)

What This Sample Shows

  1. Azure OpenAI integration via AzureOpenAIClient and AzureCliCredential
  2. Chat client middleware using ChatClientBuilder.Use(...)
  3. Agent run middleware (PII redaction and wording guardrails)
  4. Function invocation middleware (logging and overriding a tool result)
  5. Perrequest chat client middleware
  6. Perrequest function pipeline with approval
  7. Combining agentlevel and perrequest middleware

Function Invocation Middleware

Not all agents support function invocation middleware.

Attempting to use function middleware on agents that do not wrap a ChatClientAgent or derives from it will throw an InvalidOperationException.

Prerequisites

  1. Environment variables:
    • AZUREOPENAI_ENDPOINT: Your Azure OpenAI endpoint
    • AZUREOPENAI_DEPLOYMENT_NAME: Chat deployment name (optional; defaults to gpt-4o)
  2. Sign in with Azure CLI (PowerShell):
    az login
    

Running the Sample

Use PowerShell:

cd dotnet/samples/GettingStarted/Agents/Agent_Step14_Middleware
dotnet run