Skip to content

.NET: fix: pass IServiceProvider to ChatClientAgent in AddAIAgent overloads - #4483

Closed
Max Montes Soza (max-montes) wants to merge 3 commits into
microsoft:mainfrom
max-montes:fix/hosting-service-provider-passthrough
Closed

.NET: fix: pass IServiceProvider to ChatClientAgent in AddAIAgent overloads#4483
Max Montes Soza (max-montes) wants to merge 3 commits into
microsoft:mainfrom
max-montes:fix/hosting-service-provider-passthrough

Conversation

@max-montes

Copy link
Copy Markdown
Contributor

Motivation and Context

All four AddAIAgent overloads in AgentHostingServiceCollectionExtensions were creating ChatClientAgent without forwarding the IServiceProvider. This meant tools registered via dependency injection could not resolve their dependencies at invocation time.

Description

Pass services: sp to each ChatClientAgent constructor call so that the FunctionInvokingChatClient middleware receives the application's service provider and can resolve tool dependencies correctly.

Fixes#4453

Contribution Checklist

  • [ X] The code builds clean without any errors or warnings
  • [ X] The PR follows the Contribution Guidelines
  • [ X] All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

CopilotAI review requested due to automatic review settings March 4, 2026 22:12
@markwallace-microsoftMark Wallace (markwallace-microsoft) added the .NET Usage: [Issues, PRs], Target: .Net label Mar 4, 2026
@github-actionsgithub-actionsBot changed the title fix: pass IServiceProvider to ChatClientAgent in AddAIAgent overloads.NET: fix: pass IServiceProvider to ChatClientAgent in AddAIAgent overloadsMar 4, 2026

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug (#4453) where AddAIAgent overloads in AgentHostingServiceCollectionExtensions were not forwarding the IServiceProvider to ChatClientAgent, causing tools registered via dependency injection to fail to resolve their dependencies at invocation time.

Changes:

  • Pass services: sp to each of the four ChatClientAgent constructor calls in AddAIAgent overloads so the FunctionInvokingChatClient middleware receives the application's service provider.
  • Add four regression tests (one per overload) and a MockChatClient helper class to verify that the FunctionInvokingChatClient is present in the chat client pipeline.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

FileDescription
dotnet/src/Microsoft.Agents.AI.Hosting/AgentHostingServiceCollectionExtensions.csAdds services: sp to all four ChatClientAgent constructor calls to forward the DI container's IServiceProvider.
dotnet/tests/Microsoft.Agents.AI.Hosting.UnitTests/AgentHostingServiceCollectionExtensionsTests.csAdds four regression tests and a MockChatClient class to verify the service provider is passed through each AddAIAgent overload.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

All four AddAIAgent overloads in AgentHostingServiceCollectionExtensions
were creating ChatClientAgent without forwarding the IServiceProvider.
This meant tools registered via dependency injection could not resolve
their dependencies at invocation time.
Pass services: sp to each ChatClientAgent constructor call so that the
FunctionInvokingChatClient middleware receives the application's service
provider and can resolve tool dependencies correctly.
Fixesmicrosoft#4453
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@westey-m

Copy link
Copy Markdown
Contributor

Closing in favour of #7737 with some test improvements.

auto-merge was automatically disabled August 18, 2026 17:50

Pull request was closed

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NETUsage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Bug]: Microsoft.Agents.AI.Hosting Add AddAIAgent Tool call is not adding the service provider to the agent.

5 participants

@max-montes@westey-m@SergeyMenshykh@markwallace-microsoft