Skip to content

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

Description

@mattbrailsford

Summary

The current MapAGUI extension method only accepts a pre-created AIAgent instance, which doesn't support scenarios where the agent needs to be resolved dynamically at request time (e.g., multi-tenant applications, agent selection by ID/alias from a database).

Current API

public static IEndpointConventionBuilder MapAGUI(
    this IEndpointRouteBuilder endpoints,
    string pattern,
    AIAgent aiAgent)  // Pre-created instance

Problem

For dynamic agent resolution scenarios like:

  • POST /agents/{agentId}/stream - resolve agent by ID from database
  • Multi-tenant applications where agent configuration varies per tenant
  • Agent marketplaces where users select from multiple agents

We cannot use MapAGUI because the agent must be resolved at request time, not at startup.

Additionally, the types needed to implement this ourselves are internal:

  • RunAgentInput - cannot use for model binding
  • AGUIServerSentEventsResult - cannot return from custom endpoint

This forces consumers to reimplement AG-UI request/response handling.

Proposed Solution

Add factory delegate overload

public static IEndpointConventionBuilder MapAGUI(
    this IEndpointRouteBuilder endpoints,
    string pattern,
    Func<HttpContext, CancellationToken, Task<AIAgent?>> agentFactory)

Usage:

endpoints.MapAGUI("/agents/{agentId}/stream", async (httpContext, ct) =>
{
    var agentId = httpContext.GetRouteValue("agentId")?.ToString();
    return await agentResolver.ResolveAsync(agentId, ct);
});

Environment

  • Package: Microsoft.Agents.AI.Hosting.AGUI.AspNetCore v1.0.0-preview.251219.1
  • Framework: .NET 10

Metadata

Metadata

Assignees

Labels

.NETUsage: [Issues, PRs], Target: .Netag-uiUsage: [Issues, PRs], Target: AG-UI protocol integration

Type

No type

Projects

Status
No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions