11// Copyright (c) Microsoft. All rights reserved.
22
3+ using System ;
34using A2A ;
45using A2A . AspNetCore ;
56using Microsoft . Agents . AI ;
7+ using Microsoft . Agents . AI . Hosting ;
68using Microsoft . Agents . AI . Hosting . A2A ;
79using Microsoft . AspNetCore . Builder ;
810using Microsoft . AspNetCore . Routing ;
@@ -23,10 +25,21 @@ public static class MicrosoftAgentAIHostingA2AEndpointRouteBuilderExtensions
2325 /// <param name="agentName">The name of the agent to use for A2A protocol integration.</param>
2426 /// <param name="path">The route group to use for A2A endpoints.</param>
2527 /// <returns>Configured <see cref="ITaskManager"/> for A2A integration.</returns>
26- public static ITaskManager MapA2A ( this IEndpointRouteBuilder endpoints , string agentName , string path )
28+ public static IEndpointConventionBuilder MapA2A ( this IEndpointRouteBuilder endpoints , string agentName , string path )
29+ => endpoints . MapA2A ( agentName , path , _ => { } ) ;
30+
31+ /// <summary>
32+ /// Attaches A2A (Agent2Agent) communication capabilities via Message processing to the specified web application.
33+ /// </summary>
34+ /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add the A2A endpoints to.</param>
35+ /// <param name="agentName">The name of the agent to use for A2A protocol integration.</param>
36+ /// <param name="path">The route group to use for A2A endpoints.</param>
37+ /// <param name="configureTaskManager">The callback to configure <see cref="ITaskManager"/>.</param>
38+ /// <returns>Configured <see cref="ITaskManager"/> for A2A integration.</returns>
39+ public static IEndpointConventionBuilder MapA2A ( this IEndpointRouteBuilder endpoints , string agentName , string path , Action < ITaskManager > configureTaskManager )
2740 {
2841 var agent = endpoints . ServiceProvider . GetRequiredKeyedService < AIAgent > ( agentName ) ;
29- return endpoints . MapA2A ( agent , path ) ;
42+ return endpoints . MapA2A ( agent , path , configureTaskManager ) ;
3043 }
3144
3245 /// <summary>
@@ -42,10 +55,27 @@ public static ITaskManager MapA2A(this IEndpointRouteBuilder endpoints, string a
4255 /// <see href="https://github.com/a2aproject/A2A/blob/main/docs/topics/agent-discovery.md#2-curated-registries-catalog-based-discovery">Curated Registries (Catalog-Based Discovery)</see>
4356 /// discovery mechanism.
4457 /// </remarks>
45- public static ITaskManager MapA2A ( this IEndpointRouteBuilder endpoints , string agentName , string path , AgentCard agentCard )
58+ public static IEndpointConventionBuilder MapA2A ( this IEndpointRouteBuilder endpoints , string agentName , string path , AgentCard agentCard )
59+ => endpoints . MapA2A ( agentName , path , agentCard , _ => { } ) ;
60+
61+ /// <summary>
62+ /// Attaches A2A (Agent2Agent) communication capabilities via Message processing to the specified web application.
63+ /// </summary>
64+ /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add the A2A endpoints to.</param>
65+ /// <param name="agentName">The name of the agent to use for A2A protocol integration.</param>
66+ /// <param name="path">The route group to use for A2A endpoints.</param>
67+ /// <param name="agentCard">Agent card info to return on query.</param>
68+ /// <param name="configureTaskManager">The callback to configure <see cref="ITaskManager"/>.</param>
69+ /// <returns>Configured <see cref="ITaskManager"/> for A2A integration.</returns>
70+ /// <remarks>
71+ /// This method can be used to access A2A agents that support the
72+ /// <see href="https://github.com/a2aproject/A2A/blob/main/docs/topics/agent-discovery.md#2-curated-registries-catalog-based-discovery">Curated Registries (Catalog-Based Discovery)</see>
73+ /// discovery mechanism.
74+ /// </remarks>
75+ public static IEndpointConventionBuilder MapA2A ( this IEndpointRouteBuilder endpoints , string agentName , string path , AgentCard agentCard , Action < ITaskManager > configureTaskManager )
4676 {
4777 var agent = endpoints . ServiceProvider . GetRequiredKeyedService < AIAgent > ( agentName ) ;
48- return endpoints . MapA2A ( agent , path , agentCard ) ;
78+ return endpoints . MapA2A ( agent , path , agentCard , configureTaskManager ) ;
4979 }
5080
5181 /// <summary>
@@ -55,11 +85,26 @@ public static ITaskManager MapA2A(this IEndpointRouteBuilder endpoints, string a
5585 /// <param name="agent">The agent to use for A2A protocol integration.</param>
5686 /// <param name="path">The route group to use for A2A endpoints.</param>
5787 /// <returns>Configured <see cref="ITaskManager"/> for A2A integration.</returns>
58- public static ITaskManager MapA2A ( this IEndpointRouteBuilder endpoints , AIAgent agent , string path )
88+ public static IEndpointConventionBuilder MapA2A ( this IEndpointRouteBuilder endpoints , AIAgent agent , string path )
89+ => endpoints . MapA2A ( agent , path , _ => { } ) ;
90+
91+ /// <summary>
92+ /// Attaches A2A (Agent2Agent) communication capabilities via Message processing to the specified web application.
93+ /// </summary>
94+ /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add the A2A endpoints to.</param>
95+ /// <param name="agent">The agent to use for A2A protocol integration.</param>
96+ /// <param name="path">The route group to use for A2A endpoints.</param>
97+ /// <param name="configureTaskManager">The callback to configure <see cref="ITaskManager"/>.</param>
98+ /// <returns>Configured <see cref="ITaskManager"/> for A2A integration.</returns>
99+ public static IEndpointConventionBuilder MapA2A ( this IEndpointRouteBuilder endpoints , AIAgent agent , string path , Action < ITaskManager > configureTaskManager )
59100 {
60101 var loggerFactory = endpoints . ServiceProvider . GetRequiredService < ILoggerFactory > ( ) ;
61- var taskManager = agent . MapA2A ( loggerFactory : loggerFactory ) ;
62- return endpoints . MapA2A ( taskManager , path ) ;
102+ var agentThreadStore = endpoints . ServiceProvider . GetKeyedService < AgentThreadStore > ( agent . Name ) ;
103+ var taskManager = agent . MapA2A ( loggerFactory : loggerFactory , agentThreadStore : agentThreadStore ) ;
104+ var endpointConventionBuilder = endpoints . MapA2A ( taskManager , path ) ;
105+
106+ configureTaskManager ( taskManager ) ;
107+ return endpointConventionBuilder ;
63108 }
64109
65110 /// <summary>
@@ -75,11 +120,33 @@ public static ITaskManager MapA2A(this IEndpointRouteBuilder endpoints, AIAgent
75120 /// <see href="https://github.com/a2aproject/A2A/blob/main/docs/topics/agent-discovery.md#2-curated-registries-catalog-based-discovery">Curated Registries (Catalog-Based Discovery)</see>
76121 /// discovery mechanism.
77122 /// </remarks>
78- public static ITaskManager MapA2A ( this IEndpointRouteBuilder endpoints , AIAgent agent , string path , AgentCard agentCard )
123+ public static IEndpointConventionBuilder MapA2A ( this IEndpointRouteBuilder endpoints , AIAgent agent , string path , AgentCard agentCard )
124+ => endpoints . MapA2A ( agent , path , agentCard , _ => { } ) ;
125+
126+ /// <summary>
127+ /// Attaches A2A (Agent2Agent) communication capabilities via Message processing to the specified web application.
128+ /// </summary>
129+ /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add the A2A endpoints to.</param>
130+ /// <param name="agent">The agent to use for A2A protocol integration.</param>
131+ /// <param name="path">The route group to use for A2A endpoints.</param>
132+ /// <param name="agentCard">Agent card info to return on query.</param>
133+ /// <param name="configureTaskManager">The callback to configure <see cref="ITaskManager"/>.</param>
134+ /// <returns>Configured <see cref="ITaskManager"/> for A2A integration.</returns>
135+ /// <remarks>
136+ /// This method can be used to access A2A agents that support the
137+ /// <see href="https://github.com/a2aproject/A2A/blob/main/docs/topics/agent-discovery.md#2-curated-registries-catalog-based-discovery">Curated Registries (Catalog-Based Discovery)</see>
138+ /// discovery mechanism.
139+ /// </remarks>
140+ public static IEndpointConventionBuilder MapA2A ( this IEndpointRouteBuilder endpoints , AIAgent agent , string path , AgentCard agentCard , Action < ITaskManager > configureTaskManager )
79141 {
80142 var loggerFactory = endpoints . ServiceProvider . GetRequiredService < ILoggerFactory > ( ) ;
81- var taskManager = agent . MapA2A ( agentCard : agentCard , loggerFactory : loggerFactory ) ;
82- return endpoints . MapA2A ( taskManager , path ) ;
143+ var agentThreadStore = endpoints . ServiceProvider . GetKeyedService < AgentThreadStore > ( agent . Name ) ;
144+ var taskManager = agent . MapA2A ( agentCard : agentCard , agentThreadStore : agentThreadStore , loggerFactory : loggerFactory ) ;
145+ var endpointConventionBuilder = endpoints . MapA2A ( taskManager , path ) ;
146+
147+ configureTaskManager ( taskManager ) ;
148+
149+ return endpointConventionBuilder ;
83150 }
84151
85152 /// <summary>
@@ -90,14 +157,12 @@ public static ITaskManager MapA2A(this IEndpointRouteBuilder endpoints, AIAgent
90157 /// <param name="taskManager">Pre-configured A2A TaskManager to use for A2A endpoints handling.</param>
91158 /// <param name="path">The route group to use for A2A endpoints.</param>
92159 /// <returns>Configured <see cref="ITaskManager"/> for A2A integration.</returns>
93- public static ITaskManager MapA2A ( this IEndpointRouteBuilder endpoints , TaskManager taskManager , string path )
160+ public static IEndpointConventionBuilder MapA2A ( this IEndpointRouteBuilder endpoints , ITaskManager taskManager , string path )
94161 {
95162 // note: current SDK version registers multiple `.well-known/agent.json` handlers here.
96163 // it makes app return HTTP 500, but will be fixed once new A2A SDK is released.
97164 // see https://github.com/microsoft/agent-framework/issues/476 for details
98165 A2ARouteBuilderExtensions . MapA2A ( endpoints , taskManager , path ) ;
99- endpoints . MapHttpA2A ( taskManager , path ) ;
100-
101- return taskManager ;
166+ return endpoints . MapHttpA2A ( taskManager , path ) ;
102167 }
103168}
0 commit comments