Uh oh!
There was an error while loading. Please reload this page.
.NET: Pass IServiceProvider to ChatClientAgent in AddAIAgent overloads - #7737
Conversation
All four AddAIAgent overloads in AgentHostingServiceCollectionExtensions created a ChatClientAgent without forwarding the IServiceProvider, so the FunctionInvokingChatClient in the agent's pipeline had no service provider and tools could not resolve their dependencies at invocation time. Fixesmicrosoft#4453 Co-authored-by: Max Montes Soza <max-montes@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes DI service resolution during hosted-agent tool invocation by forwarding the active IServiceProvider to ChatClientAgent.
Changes:
- Passes the service provider through all four built-in
AddAIAgentoverloads. - Adds end-to-end regression coverage for each overload.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
dotnet/src/Microsoft.Agents.AI.Hosting/AgentHostingServiceCollectionExtensions.cs | Forwards DI services when constructing agents. |
dotnet/tests/Microsoft.Agents.AI.Hosting.UnitTests/AgentHostingServiceCollectionExtensionsTests.cs | Verifies tools can resolve DI services. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: Findings reported
Scope: full PR (1 commit(s)): 55da434c9d7f
Model:gpt-5.6-sol
Overview
The change consistently forwards the agent factory's provider through all four convenience overloads, and the regression theory verifies the intended function-invocation path end to end. Existing keyed-tool isolation and lifetime validation remain intact. However, the default singleton registration forwards the root provider, so tools that resolve scoped dependencies can retain request- or tenant-specific state across unrelated invocations.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.
Affected areas:dotnet/src/Microsoft.Agents.AI.Hosting/AgentHostingServiceCollectionExtensions.cs
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Motivation & Context
All four
AddAIAgentoverloads inAgentHostingServiceCollectionExtensionscreated aChatClientAgentwithout forwarding theIServiceProvider. As a result, theFunctionInvokingChatClientin the agent's pipeline received no service provider, soAIFunctionArguments.Serviceswasnullat invocation time and tools registered via DI could not resolve their dependencies.This is a replication of #4483 by Max Montes Soza (@max-montes), with the unit tests reworked so they pass in CI (see below). Thanks Max Montes Soza (@max-montes) for the original fix!
Description & Review Guide
What are the major changes?
AgentHostingServiceCollectionExtensions: passservices: spto each of the fourChatClientAgentconstructor calls.AgentHostingServiceCollectionExtensionsTests: added an end-to-end regression test (a[Theory]covering all four overloads) that registers a marker service in DI, registers a tool on the agent, runs the agent against a stub chat client that requests the tool, and asserts the tool could resolve the marker service fromAIFunctionArguments.Services.What is the impact of these changes?
WithAITool/DI on hosted agents can now resolve their dependencies when invoked. No public API change.Notes on the tests (differences from .NET: fix: pass IServiceProvider to ChatClientAgent in AddAIAgent overloads #4483):
FunctionInvokingChatClient._serviceProviderprivate field, which does not exist — the four tests failed in CI.FunctionInvokingChatClient.FunctionInvocationServicesisprotected, so it can't be read directly either.Related Issue
Fixes#4453
Contribution Checklist