Files
agent-framework/dotnet/samples/GettingStarted/Agents/Agent_Step14_Middleware
T
SergeyMenshykh bd167ee476 .NET: AI Agent with plugin (#1009)
* add sample to demonstrate plugins for ai agent

* Update dotnet/samples/GettingStarted/Agents/Agent_Step15_Plugins/Program.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/samples/GettingStarted/Agents/Agent_Step15_Plugins/Program.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/samples/GettingStarted/Agents/Agent_Step15_Plugins/Program.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* remove lang version property

* address review comments

* rename functionInvocationServices to services

* restore the solution file corrupted during the merge with lates main

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
bd167ee476 · 2025-09-30 15:10:03 +00:00
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:
    • AZURE_OPENAI_ENDPOINT: Your Azure OpenAI endpoint
    • AZURE_OPENAI_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