A2A ホスティング

Agent Framework には、 エージェント間 (A2A) プロトコルを介して AI エージェントを公開するホスティング パッケージが用意されています。 ホストされると、A2A 準拠のクライアントは、クライアントが構築されたフレームワークやテクノロジに関係なく、エージェントを検出して通信できます。

NuGet パッケージ:

  • Microsoft.Agents.AI.Hosting.A2A.AspNetCore - A2A プロトコル バインディング用の ASP.NET Core エンドポイント マッピング。 このパッケージには、 Microsoft.Agents.AI.Hosting.A2Aが推移的に含まれています。
  • Microsoft.Agents.AI.Hosting.A2A - AI エージェントを A2A プロトコル サーバーに橋渡しするためのコア ホスティング ロジック (サーバー登録、要求処理、セッション管理)。

作業の開始

ASP.NET Core ホスティング パッケージをインストールします (コア ホスティング パッケージと A2A SDK パッケージが自動的にインストールされます)。

dotnet add package Microsoft.Agents.AI.Hosting.A2A.AspNetCore --prerelease
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["FOUNDRY_PROJECT_ENDPOINT"]
    ?? throw new InvalidOperationException("FOUNDRY_PROJECT_ENDPOINT is not set.");
string model = builder.Configuration["FOUNDRY_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-card.jsonで検出できるようになりました。 A2A 準拠のクライアントは、このエージェントを検出して通信できます。

プロトコル バインド

A2A プロトコルは、2 つのトランスポート バインディングを定義します。 どちらもサポートされています。

Binding Method 説明
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) によって提供されます。

Tip

ホストごとに提供できるエージェント カードは 1 つだけであるため、既知のパスを介して検出できるエージェントは 1 つだけです。 他のエージェントには、引き続き URL で直接アクセスできます。 その他のオプションについては、「 エージェント検出 」を参照してください。

AddA2AServer の動作のしくみ

AddA2AServer メソッドは、キー付きA2AServerシングルトンを依存関係挿入コンテナーに登録します。 サーバーが構築されると、いくつかの内部コンポーネントが解決または作成されます。

コンポーネント Default Purpose
IAgentHandler A2AAgentHandler 受信した A2A 要求を AIAgent に中継します。 メッセージを変換し、エージェントを実行し、応答を A2A メッセージとして返します。
AgentSessionStore InMemoryAgentSessionStore エージェントが同じ contextIdを持つ複数の要求間でコンテキストを維持できるように、会話セッションを格納します。

Warning

既定の InMemoryAgentSessionStore は開発専用です。 アプリケーションの再起動時に状態が失われ、複数のインスタンス間で共有されることはありません。 運用環境へのデプロイを行う場合は、永続的な実装を登録します。

既定値のオーバーライド

これらのコンポーネントは、 AddA2AServerを呼び出す前に DI コンテナーにキー付きサービスを登録することで置き換えることができます。 サーバーは、キーとしてエージェント名を使用してキー付きサービスを解決します。

カスタム セッション ストア - 永続的な会話ストレージの場合:

builder.Services.AddKeyedSingleton<AgentSessionStore>("weather-agent", new MyDurableSessionStore());

builder.AddA2AServer("weather-agent");

カスタム エージェント ハンドラー - 要求処理を完全に制御するため。 キー付き IAgentHandler が登録されると、既定の A2AAgentHandler が完全に置き換えられます。

builder.Services.AddKeyedSingleton<IAgentHandler>("weather-agent", new MyCustomHandler());

builder.AddA2AServer("weather-agent");

複数のエージェント

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 タスクを返しません。 ホストされるエージェントは、常に A2A メッセージで応答します。

タスクを返すには、デフォルト値のオーバーライド に示されているように、カスタム IAgentHandler を登録してください。 既定のハンドラーが置き換えられるため、ハンドラーはエージェントを実行し、タスクの作成、更新、再開、取り消しを行う必要があります。

次のステップ

こちらも参照ください