From 3dc8e1e025d540249fdf047594120618d1d73447 Mon Sep 17 00:00:00 2001
From: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
Date: Tue, 30 Sep 2025 09:40:03 +0100
Subject: [PATCH] .NET: Fix DI for ChatClientAgent (#973)
* fix di for chat client agent so ai funcitons dependencies can be resolved from di container
* merge with latest main
---
.../Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs | 11 +++++++----
.../ChatClient/ChatClientExtensions.cs | 5 +++--
2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
index 5f714be24f..cc5ddf31c3 100644
--- a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
+++ b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
@@ -35,7 +35,8 @@ public sealed class ChatClientAgent : AIAgent
/// Optional description for the agent.
/// Optional list of tools that the agent can use during invocation.
/// Optional logger factory to use for logging.
- public ChatClientAgent(IChatClient chatClient, string? instructions = null, string? name = null, string? description = null, IList? tools = null, ILoggerFactory? loggerFactory = null)
+ /// An optional to use for resolving services required by the instances being invoked.
+ public ChatClientAgent(IChatClient chatClient, string? instructions = null, string? name = null, string? description = null, IList? tools = null, ILoggerFactory? loggerFactory = null, IServiceProvider? functionInvocationServices = null)
: this(
chatClient,
new ChatClientAgentOptions
@@ -48,7 +49,8 @@ public sealed class ChatClientAgent : AIAgent
Tools = tools,
}
},
- loggerFactory)
+ loggerFactory,
+ functionInvocationServices)
{
}
@@ -58,7 +60,8 @@ public sealed class ChatClientAgent : AIAgent
/// The chat client to use for invoking the agent.
/// Full set of options to configure the agent.
/// Optional logger factory to use for logging.
- public ChatClientAgent(IChatClient chatClient, ChatClientAgentOptions? options, ILoggerFactory? loggerFactory = null)
+ /// An optional to use for resolving services required by the instances being invoked.
+ public ChatClientAgent(IChatClient chatClient, ChatClientAgentOptions? options, ILoggerFactory? loggerFactory = null, IServiceProvider? functionInvocationServices = null)
{
_ = Throw.IfNull(chatClient);
@@ -71,7 +74,7 @@ public sealed class ChatClientAgent : AIAgent
this._chatClientType = chatClient.GetType();
// If the user has not opted out of using our default decorators, we wrap the chat client.
- this.ChatClient = options?.UseProvidedChatClientAsIs is true ? chatClient : chatClient.WithDefaultAgentMiddleware(options);
+ this.ChatClient = options?.UseProvidedChatClientAsIs is true ? chatClient : chatClient.WithDefaultAgentMiddleware(options, functionInvocationServices);
this._logger = (loggerFactory ?? chatClient.GetService() ?? NullLoggerFactory.Instance).CreateLogger();
}
diff --git a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientExtensions.cs b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientExtensions.cs
index 80d3347d1f..67cd7c0d54 100644
--- a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientExtensions.cs
+++ b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientExtensions.cs
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
+using System;
using System.Diagnostics;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
@@ -10,7 +11,7 @@ namespace Microsoft.Extensions.AI;
internal static class ChatClientExtensions
{
- internal static IChatClient WithDefaultAgentMiddleware(this IChatClient chatClient, ChatClientAgentOptions? options)
+ internal static IChatClient WithDefaultAgentMiddleware(this IChatClient chatClient, ChatClientAgentOptions? options, IServiceProvider? functionInvocationServices = null)
{
var chatBuilder = chatClient.AsBuilder();
@@ -24,7 +25,7 @@ internal static class ChatClientExtensions
});
}
- var agentChatClient = chatBuilder.Build();
+ var agentChatClient = chatBuilder.Build(functionInvocationServices);
if (options?.ChatOptions?.Tools is { Count: > 0 })
{