Skip to content

.NET: Add MapAGUI overload that resolves the agent per request via a factory#6251

Open
darthmolen wants to merge 3 commits into
microsoft:mainfrom
darthmolen:feature/mapagui-factory-delegate
Open

.NET: Add MapAGUI overload that resolves the agent per request via a factory#6251
darthmolen wants to merge 3 commits into
microsoft:mainfrom
darthmolen:feature/mapagui-factory-delegate

Conversation

@darthmolen
Copy link
Copy Markdown

Summary

Adds a MapAGUI overload that resolves the agent per request via a factory delegate, instead of capturing a single AIAgent at startup from the root service provider.

public static IEndpointConventionBuilder MapAGUI(
    this IEndpointRouteBuilder endpoints,
    string agentName,
    [StringSyntax("route")] string pattern,
    Func<IServiceProvider, string, AIAgent> createAgentDelegate)

Motivation

The existing MapAGUI overloads resolve one keyed AIAgent once at map time (endpoints.ServiceProvider.GetRequiredKeyedService<AIAgent>(name)). Hosts that must build the agent per request — for per-request authentication, scoped tool/MCP sessions, or conversation-scoped configuration — have no supported entry point and must work around it (e.g. a singleton facade AIAgent that re-resolves a real agent on each run).

This overload invokes the factory once per request with the request's IServiceProvider (HttpContext.RequestServices) and the agent name, so request-scoped services resolve correctly. It mirrors the existing AddAIAgent(name, Func<IServiceProvider, string, AIAgent>) delegate shape.

Details

  • The keyed AgentSessionStore lookup and the ThreadId trust model are unchanged; the store is resolved per request from HttpContext.RequestServices using agentName as the key.
  • The shared request-handling body is factored into a private HandleRunAsync reused by both the instance and factory overloads (no behavior change to the existing path).
  • Argument null-checks for endpoints, agentName, and createAgentDelegate; the factory returning null throws a clear InvalidOperationException.

Tests

Adds unit tests: the overload maps an endpoint; it defers resolution (factory not invoked and no keyed AIAgent resolved at map time, unlike the startup-capture overloads); and a null-factory guard. All 38 tests in Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTests pass on net8.0/net9.0/net10.0.

Closes #2988
Related to #5209

The existing MapAGUI overloads resolve a single AIAgent at startup from
the root service provider. Hosts that build agents per request -- for
per-request authentication, scoped tool/MCP sessions, or conversation-
scoped configuration -- cannot use them. This adds:

    MapAGUI(this IEndpointRouteBuilder endpoints, string agentName,
            string pattern, Func<IServiceProvider, string, AIAgent> createAgentDelegate)

The factory is invoked once per request with the request's
IServiceProvider (HttpContext.RequestServices) and the agent name, so
scoped services resolve correctly within the request. The keyed
AgentSessionStore lookup and the ThreadId trust model are unchanged --
the store is resolved per request from HttpContext.RequestServices using
agentName as the key. The shared request-handling body is factored into
a private HandleRunAsync helper reused by both overloads.

Addresses microsoft#2988.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Steven Molen <steven.molen@csat.com>
Copilot AI review requested due to automatic review settings June 1, 2026 21:05
@moonbox3 moonbox3 added the .NET label Jun 1, 2026
@github-actions github-actions Bot changed the title Add MapAGUI overload that resolves the agent per request via a factory .NET: Add MapAGUI overload that resolves the agent per request via a factory Jun 1, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a new MapAGUI overload that creates an AIAgent per request via a factory delegate, and refactors the shared request-handling logic into a helper to reduce duplication.

Changes:

  • Added MapAGUI overload accepting Func<IServiceProvider, string, AIAgent> for per-request agent resolution.
  • Refactored endpoint handler body into HandleRunAsync and reused it from existing overload(s).
  • Added unit tests for the new overload (mapping, deferral behavior, and null delegate guard).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTests/AGUIEndpointRouteBuilderExtensionsTests.cs Adds new unit tests targeting the new factory-delegate MapAGUI overload and its mapping-time behavior.
dotnet/src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore/AGUIEndpointRouteBuilderExtensions.cs Introduces the per-request factory-delegate overload and extracts shared request processing into HandleRunAsync.

Covers the per-request factory invocation (the factory is invoked once
per request rather than captured at startup) and the factory-returns-null
failure path (a clear InvalidOperationException naming the agent). The
latter addresses review feedback that the null-return branch was uncovered.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Steven Molen <steven.molen@csat.com>
@darthmolen
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@hansmbakker
Copy link
Copy Markdown

hansmbakker commented Jun 2, 2026

This would be helpful! I had to add IServiceScopeFactory scopeFactory to my tools to be able to use scopes with Entity Framework in them, and if I understand this PR correctly that workaround would become redundant then.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: Support dynamic agent resolution in AG-UI endpoints (MapAGUI with factory delegate)

5 participants