Agent Framework には、 エージェント間 (A2A) プロトコルを介して AI エージェントを公開するホスティング パッケージが用意されています。 ホストされると、A2A 準拠のクライアントは、クライアントが構築されたフレームワークやテクノロジに関係なく、エージェントを検出して通信できます。
NuGet パッケージ:
-
Microsoft.Agents.AI.Hosting.A2A.AspNetCore - ASP.NET A2A プロトコル バインディングのコア エンドポイント マッピング。 このパッケージには、
Microsoft.Agents.AI.Hosting.A2Aが推移的に含まれています。 - Microsoft.Agents.AI.Hosting.A2A - AI エージェントを A2A プロトコルにブリッジするためのコア ホスティング ロジック (サーバー登録、要求処理、セッション管理)。
作業の開始
ASP.NET Core ホスティング パッケージをインストールします (コア パッケージが自動的にプルされます)。
dotnet add package Microsoft.Agents.AI.Hosting.A2A.AspNetCore --prerelease
dotnet add package A2A.AspNetCore --prerelease
dotnet add package Azure.AI.Projects --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.Foundry --prerelease
次の例は、A2A 経由で 1 つのエージェントをホストする最小 ASP.NET Core アプリケーションを示しています。 AI プロバイダーとして Microsoft Foundry を使用します。その他のオプションについては、 プロバイダーを 参照してください。
using A2A;
using A2A.AspNetCore;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
string endpoint = builder.Configuration["AZURE_AI_PROJECT_ENDPOINT"]
?? throw new InvalidOperationException("AZURE_AI_PROJECT_ENDPOINT is not set.");
string model = builder.Configuration["AZURE_AI_MODEL"] ?? "gpt-4o-mini";
// 1. Create and register the "weather-agent" agent in the DI container.
builder.Services.AddKeyedSingleton<AIAgent>("weather-agent", (sp, _) =>
{
return new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential())
.AsAIAgent(
model: model,
instructions: "You are a helpful weather assistant.",
name: "weather-agent");
});
// 2. Register the A2A server for the "weather-agent" agent.
builder.AddA2AServer("weather-agent");
var app = builder.Build();
// 3. Map A2A protocol endpoints for the "weather-agent" agent.
app.MapA2AHttpJson("weather-agent", "/a2a/weather-agent");
// 4. Serve a minimal agent card for the "weather-agent" agent discovery.
app.MapWellKnownAgentCard(new AgentCard
{
Name = "WeatherAgent",
Description = "A helpful weather assistant.",
SupportedInterfaces =
[
new AgentInterface
{
Url = "http://localhost:5000/a2a/weather-agent",
ProtocolBinding = ProtocolBindingNames.HttpJson,
ProtocolVersion = "1.0",
}
]
});
app.Run();
エージェントは A2A HTTP+JSON プロトコル バインディングを介して /a2a/weather-agent で到達可能になり、そのエージェント カードは /.well-known/agent.jsonで検出できるようになりました。 A2A 準拠のクライアントは、このエージェントを検出して通信できます。
プロトコル バインド
A2A プロトコルは、2 つのトランスポート バインディングを定義します。 どちらもサポートされています。
| Binding | メソッド | 説明 |
|---|---|---|
| HTTP + JSON | MapA2AHttpJson |
ストリーミング用の標準 HTTP 要求と Server-Sent イベント。 |
| JSON-RPC | MapA2AJsonRpc |
JSON-RPC 2.0 を HTTP 経由で使用します。 |
クライアントが優先トランスポートを選択できるように、両方のバインディングを同時にマップできます。 必要に応じて、異なるパスを使用できます。
app.MapA2AHttpJson("weather-agent", "/a2a/weather-agent"); // HTTP+JSON
app.MapA2AJsonRpc("weather-agent", "/a2a/weather-agent"); // JSON-RPC
エージェント カード
エージェント カード は、クライアントが要求を送信する前にその機能を検出して理解できるように、エージェントのメタデータ (名前、説明、バージョン、サポートされているインターフェイス) を記述します。 [ はじめに ] セクションには、最小限のエージェント カードが表示されます。 運用環境で使用する場合は、全部の情報が設定されたカードを指定します。
using A2A;
using A2A.AspNetCore;
app.MapWellKnownAgentCard(new AgentCard
{
Name = "WeatherAgent",
Description = "A helpful weather assistant.",
Version = "1.0",
DefaultInputModes = ["text"],
DefaultOutputModes = ["text"],
SupportedInterfaces =
[
new AgentInterface
{
Url = "http://localhost:5000/a2a/weather-agent",
ProtocolBinding = ProtocolBindingNames.HttpJson,
ProtocolVersion = "1.0",
}
]
});
Note
MapWellKnownAgentCard は、エージェント フレームワーク ホスティング パッケージではなく、A2A SDK パッケージ (A2A.AspNetCore) によって提供されます。
ヒント
ホストごとに提供できるエージェント カードは 1 つだけであるため、既知のパスを介して検出できるエージェントは 1 つだけです。 他のエージェントには、引き続き URL で直接アクセスできます。 その他のオプションについては、「 エージェント検出 」を参照してください。
AddA2AServer の動作のしくみ
AddA2AServer メソッドは、キー付きA2AServerシングルトンを依存関係挿入コンテナーに登録します。 サーバーが構築されると、いくつかの内部コンポーネントが解決または作成されます。
| コンポーネント | Default | 目的 |
|---|---|---|
IAgentHandler |
A2AAgentHandler |
受信した A2A 要求を AIAgent に中継します。 メッセージを変換し、エージェントを実行し、応答を A2A メッセージとして返します。 |
AgentSessionStore |
InMemoryAgentSessionStore |
エージェントが同じ contextIdを持つ複数の要求間でコンテキストを維持できるように、会話セッションを格納します。 |
ITaskStore |
InMemoryTaskStore |
実行時間の長い A2A 操作のタスクの状態を追跡します。 |
AgentRunMode |
DisallowBackground |
エージェントが即時メッセージではなくバックグラウンド応答 (A2A タスク) を返すことができるかどうかを制御します。 |
Warning
既定の InMemoryAgentSessionStore と InMemoryTaskStore は開発専用です。 アプリケーションの再起動時に状態が失われ、複数のインスタンス間で共有されません。 運用環境のデプロイの場合は、永続的な実装を登録します。
既定値のオーバーライド
これらのコンポーネントは、 AddA2AServerを呼び出す前に DI コンテナーにキー付きサービスを登録することで置き換えることができます。 サーバーは、キーとしてエージェント名を使用してキー付きサービスを解決します。
カスタム セッション ストア - 永続的な会話ストレージの場合:
builder.Services.AddKeyedSingleton<AgentSessionStore>("weather-agent", new MyDurableSessionStore());
builder.AddA2AServer("weather-agent");
カスタム タスク ストア - 永続的なタスク追跡用:
builder.Services.AddKeyedSingleton<ITaskStore>("weather-agent", new MyDurableTaskStore());
builder.AddA2AServer("weather-agent");
カスタム エージェント ハンドラー - 要求処理を完全に制御します。 キー付き IAgentHandler が登録されると、既定の A2AAgentHandler が完全に置き換えられます。
builder.Services.AddKeyedSingleton<IAgentHandler>("weather-agent", new MyCustomHandler());
builder.AddA2AServer("weather-agent");
エージェント実行モード - A2AServerRegistrationOptionsを使用して構成します。
builder.AddA2AServer("weather-agent", options =>
{
options.AgentRunMode = AgentRunMode.DisallowBackground;
});
複数のエージェント
1 つのアプリケーションで複数のエージェントをホストできます。 各エージェントは、独自の A2A サーバーとエンドポイントを取得します。
// Register agents in DI.
builder.Services.AddKeyedSingleton<AIAgent>("weather-agent", (sp, _) =>
{
return new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential())
.AsAIAgent(model: model, instructions: "You are a helpful weather assistant.", name: "weather-agent");
});
builder.Services.AddKeyedSingleton<AIAgent>("scientist", (sp, _) =>
{
return new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential())
.AsAIAgent(model: model, instructions: "You are a scientist.", name: "scientist");
});
// Register A2A servers.
builder.AddA2AServer("weather-agent");
builder.AddA2AServer("scientist");
var app = builder.Build();
// Map endpoints.
app.MapA2AHttpJson("weather-agent", "/a2a/weather-agent");
app.MapA2AHttpJson("scientist", "/a2a/scientist");
app.Run();
この例では、どちらのエージェントにもエージェント カードがないため、クライアントはエンドポイント URL を直接認識する必要があります。
MapWellKnownAgentCardを使用してエージェント カードの検出を追加できますが、ホストごとにアドバタイズできるエージェントは 1 つだけです。エージェント カードを参照してください。
バックグラウンド応答
Note
A2A でホストされるエージェントでは、バックグラウンド応答はまだサポートされていません。
AgentRunModeの既定値は DisallowBackground です。つまり、すべての応答が即時 A2A メッセージとして返されます。