이를 통해 애플리케이션은 A2AAgentA2A(에이전트 간) 프로토콜을 통해 노출되는 원격 에이전트에 연결할 수 있습니다. A2A 규격 엔드포인트를 표준 AIAgent으로 래핑하므로 빌드된 프레임워크 또는 기술에 관계없이 익숙한 메서드를 RunAsyncRunStreamingAsync 사용하고 원격 에이전트와 상호 작용할 수 있습니다.
Warning
A2A 에이전트에 연결하기 전에 엔드포인트 URL과 원격 에이전트를 운영하는 조직 또는 게시자를 모두 신뢰해야 합니다. 원격 에이전트의 응답을 신뢰할 수 없는 애플리케이션 입력으로 처리합니다. 코드가 부작용을 수행하거나 콘텐츠를 렌더링하기 전에 이를 검증하세요.
에이전트 프레임워크 에이전트를 A2A 서버로 노출하려면 A2A가 있는 호스트 에이전트를 참조하세요.
시작하기
프로젝트에 필요한 NuGet 패키지를 추가합니다.
dotnet add package Microsoft.Agents.AI.A2A --prerelease
에이전트 검색
원격 A2A 에이전트와 통신하기 전에 이를 검색하고 인스턴스를 AIAgent 만들어야 합니다. A2A 프로토콜은 각각 에이전트 프레임워크에서 지원하는 세 가지 검색 전략을 정의합니다.
잘 알려진 URI
A2A 에이전트는 표준화된 경로에서 https://{domain}/.well-known/agent-card.json 검색할 수 있도록 할 수 있습니다. 카드를 A2ACardResolver 가져오고 한 번의 호출로 에이전트를 만들려면 다음을 사용합니다.
using A2A;
using Microsoft.Agents.AI;
// Initialize a resolver pointing at the remote agent's host.
A2ACardResolver resolver = new(new Uri("https://a2a-agent.example.com"));
// Only connect to endpoints on the specified host.
AIAgent agent = await resolver.GetAIAgentAsync(
allowedHosts: ["a2a-agent.example.com"]);
// Use the agent.
Console.WriteLine(await agent.RunAsync("Hello!"));
참조: A2ACardResolverExtensions.
둘 다 GetAIAgentAsync 인수 AgentCard.AsAIAgent 가 allowedHosts 필요합니다. 이 목록은 확인자에서 카드를 가져오는 데 사용하는 URL이 아니라 카드에 보급된 엔드포인트 URL에 적용됩니다. 자세한 내용은 에이전트 카드 엔드포인트 유효성 검사를 참조하세요.
Tip
GetAIAgentAsync또한 프로토콜 선택에 대한 선택적 A2AClientOptions 매개 변수를 허용합니다.
카탈로그 기반 검색
엔터프라이즈 환경 또는 공용 마켓플레이스에서 에이전트 카드는 종종 중앙 레지스트리에서 관리됩니다. 이러한 레지스트리에서 이미 AgentCard 가져온 경우 다음으로 직접 AIAgent변환합니다.
using A2A;
using Microsoft.Agents.AI;
// Assume agentCard was retrieved from a registry or catalog.
AgentCard agentCard = await GetAgentCardFromRegistryAsync("travel-planner");
AIAgent agent = agentCard.AsAIAgent(
allowedHosts: ["a2a-agent.example.com"]);
Console.WriteLine(await agent.RunAsync("Plan a trip to Paris."));
직접 구성
에이전트 엔드포인트가 미리 알려진 긴밀하게 결합된 시스템 또는 개발 시나리오의 경우 직접 만들고 A2AClient 다음으로 AIAgent변환합니다.
using A2A;
using Microsoft.Agents.AI;
// Create a client pointing at the known agent endpoint.
A2AClient a2aClient = new(new Uri("https://a2a-agent.example.com"));
AIAgent agent = a2aClient.AsAIAgent();
Console.WriteLine(await agent.RunAsync("What can you help me with?"));
참조: A2AClientExtensions.
이 AsAIAgent 오버로드는 모든 IA2AClient 구현에서 작동합니다. 클라이언트에서 직접 엔드포인트를 구성하기 때문에 이 작업을 수행 allowedHosts 하지 않습니다. 오버로드 및 A2AAgent 생성자는 선택적으로 A2AAgentOptions 에이전트 IdName및 에이전트를 설정하는 옵션을 허용합니다Description.
에이전트 카드 ID 재정의
기본적으로 카드 기반 만들기는 카드 Name 와 Description.를 사용합니다. 또는 재정의에 AgentCard.AsAIAgent 전달 agentOptions 하거나 A2ACardResolver.GetAIAgentAsyncDescription재정IdName의합니다. 설정되지 않은 상태로 두는 속성은 카드의 값으로 대체됩니다.
using A2A;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.A2A;
A2ACardResolver resolver = new(new Uri("https://a2a-agent.example.com"));
AIAgent agent = await resolver.GetAIAgentAsync(
allowedHosts: ["a2a-agent.example.com"],
agentOptions: new A2AAgentOptions
{
Id = "travel-agent-id",
Name = "travel-agent",
Description = "Plans trips and suggests itineraries."
});
참조: A2AAgentOptions, A2ACardResolverExtensions.
전달하면 프로토콜 설정을 통해 전달agentOptions합니다clientOptions. 이름이 없는 agentOptions 오버로드는 동일한 매개 변수 options입니다.
에이전트 카드 엔드포인트 유효성 검사
카드에서 에이전트를 만들 때 Agent Framework는 URL이 절대 HTTPS URL이 아니거나 호스트가 허용되지 않는 인터페이스를 건너뜁니다. 원래 카드는 수정하지 않습니다. 카드가 인터페이스를 보급하고 유효성 검사를 통과하지 않으면 에이전트 생성이 throw됩니다 InvalidOperationException.
특히 HTTP 클라이언트가 자격 증명을 보낼 때 에이전트가 연락해야 하는 호스트만 전달합니다. 카드는 가져온 호스트와 다른 호스트를 보급할 수 있습니다.
allowedHosts 값 |
허용되는 내용 |
|---|---|
["a2a-agent.example.com"] |
해당 호스트의 모든 포트입니다. |
["a2a-agent.example.com:8443"] |
해당 호스트의 포트 8443만 |
null |
모든 호스트. URL 체계 및 형식 검사는 여전히 적용됩니다. |
[] |
호스트가 없습니다. |
선택적 포트와 함께 호스트 이름 또는 IP 주소를 사용합니다. 예를 들어 [::1]:8443포트를 지정할 때 IPv6 주소를 대괄호로 묶습니다. URL 및 와일드카드를 비롯한 잘못된 항목이 원인 ArgumentException입니다.
로컬 HTTP 엔드포인트에 연결
포트 5000에서 로컬 A2A 서버를 시작합니다. 카드가 일반 HTTP URL을 보급하는 경우 실험적 AllowAgentCardHostsInsecureHttp 옵션을 설정합니다. HTTP는 요청 또는 자격 증명을 암호화하지 않습니다. 배포된 엔드포인트에 HTTPS를 사용합니다.
using System;
using A2A;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.A2A;
A2ACardResolver resolver = new(new Uri("http://localhost:5000"));
#pragma warning disable MAAI001
A2AAgentOptions agentOptions = new() { AllowAgentCardHostsInsecureHttp = true };
#pragma warning restore MAAI001
AIAgent agent = await resolver.GetAIAgentAsync(
allowedHosts: ["localhost:5000"],
agentOptions: agentOptions);
Console.WriteLine(await agent.RunAsync("Hello!"));
이 옵션은 HTTPS 외에도 HTTP를 허용합니다. 호스트 및 포트 제한은 계속 적용되므로 이 예제에서는 다른 포트의 엔드포인트가 아니라 보급 http://localhost:5000/a2a/weather-agent하는 카드를 허용합니다.
참조: A2AAgentOptions, A2ACardResolverExtensions.
프로토콜 선택
A2A 에이전트는 HTTP+JSON, JSON-RPC 및 gRPC 바인딩을 노출할 수 있습니다. 기본적으로 카드 기반 만들기는 HTTP+JSON 및 JSON-RPC를 허용합니다. 엔드포인트 유효성 검사 후 SDK는 일치하는 A2AClientOptions.PreferredBindings첫 번째 나머지 인터페이스 AgentCard.SupportedInterfaces 를 선택합니다.
선택한 항목을 나열하는 바인딩으로 제한하도록 설정합니다 PreferredBindings . 생성 중에 클라이언트가 선택됩니다. 요청이 실패하면 자동 장애 조치(failover)를 사용하도록 설정하지 않습니다.
using A2A;
using Microsoft.Agents.AI;
A2ACardResolver agentCardResolver = new(
new Uri("https://a2a-agent.example.com"));
AgentCard agentCard = await agentCardResolver.GetAgentCardAsync();
// Select HTTP+JSON. Use ProtocolBindingNames.JsonRpc for JSON-RPC.
A2AClientOptions options = new()
{
PreferredBindings = [ProtocolBindingNames.HttpJson]
};
AIAgent agent = agentCard.AsAIAgent(
allowedHosts: ["a2a-agent.example.com"],
options: options);
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));
비고
선택한 프로토콜 바인딩을 지원하는 엔드포인트에서 원격 A2A 에이전트를 사용할 수 있어야 합니다.
엔드포인트 및 해당 바인딩을 이미 알고 있는 경우 카드를 건너뛰고 바인딩의 클라이언트를 직접 만듭니다. 각 클라이언트는 다음을 사용하여 변환할 AsAIAgent()수 있도록 구현합니다IA2AClient.
| Binding | Client | Package |
|---|---|---|
| JSON-RPC | A2AClient |
A2A |
| HTTP+JSON | A2AHttpJsonClient |
A2A |
| gRPC | A2AGrpcClient |
A2A.Grpc |
using A2A;
using Microsoft.Agents.AI;
A2AHttpJsonClient a2aClient = new(new Uri("https://a2a-agent.example.com"));
AIAgent agent = a2aClient.AsAIAgent();
참조: A2AClientExtensions.
gRPC
패키지에는 A2A gRPC 지원이 포함되지 않습니다. A2A SDK의 선택적 gRPC 패키지를 추가합니다.
dotnet add package A2A.Grpc --prerelease
카드 기반 검색의 경우 애플리케이션 시작 중에 gRPC 바인딩을 한 번 등록합니다. 카드는 gRPC 인터페이스를 보급해야 합니다.
using A2A;
using A2A.Grpc;
using Microsoft.Agents.AI;
A2AGrpcClientRegistration.Register();
A2ACardResolver resolver = new(new Uri("https://a2a-agent.example.com"));
AIAgent agent = await resolver.GetAIAgentAsync(
allowedHosts: ["a2a-agent.example.com"],
options: new A2AClientOptions
{
PreferredBindings = [ProtocolBindingNames.Grpc]
});
Console.WriteLine(await agent.RunAsync("Hello over gRPC!"));
참조: A2ACardResolverExtensions, A2A 샘플.
검색은 여전히 HTTP를 통해 에이전트 카드를 가져온 다음 카드에 나열된 gRPC 엔드포인트에 연결합니다.
A2AGrpcClient 직접 만들려면 등록이 필요하지 않습니다.
인증 및 HTTP 구성
HTTP 바인딩의 경우 구성한 HttpClient 매개 변수를 통해 또는 .의 AgentCard.AsAIAgent 매개 변수를 httpClient 전달합니다A2ACardResolver.GetAIAgentAsync. 에이전트에 대한 요청에 대한 인증 헤더 또는 시간 제한을 구성하는 데 사용합니다. 카드 페치 자체를 인증하려면 확인자의 HTTP 클라이언트를 별도로 구성합니다.
에이전트 요청에 대한 클라이언트를 제공하지 않는 경우 Agent Framework는 리디렉션을 따르거나 쿠키를 저장하지 않는 공유 HttpClient 를 사용하고 서버 인증서가 해지되었는지 여부를 확인합니다. 사용자 고유의 클라이언트를 제공하는 경우 동일한 설정을 AllowAutoRedirect = falseUseCookies = falseCheckCertificateRevocationList = trueHttpClientHandler사용합니다. 호스트 목록은 대상을 리디렉션하지 않고 카드의 URL을 확인합니다.
자격 증명을 전달하기 전에 보급된 엔드포인트 원본의 인증된 검색 및 유효성 검사는 A2A 인증 가이드를 참조하세요.
세션 및 후속 작업
세션을 만들고 여러 번 다시 사용하여 원격 대화를 유지합니다.
using A2A;
using Microsoft.Agents.AI;
A2ACardResolver resolver = new(new Uri("https://a2a-agent.example.com"));
AIAgent agent = await resolver.GetAIAgentAsync(
allowedHosts: ["a2a-agent.example.com"]);
AgentSession session = await agent.CreateSessionAsync();
Console.WriteLine(await agent.RunAsync("Plan a trip to Paris.", session));
Console.WriteLine(await agent.RunAsync("Make it three days.", session));
참조: A2AAgent, A2AAgentSession.
세션은 다음 프로토콜 상태를 추적하는 세션입니다 A2AAgentSession .
| Property | Purpose |
|---|---|
ContextId |
원격 대화를 식별합니다. |
TaskId |
응답에 작업이 포함된 경우 가장 최근 작업을 식별합니다. |
TaskState |
다음 메시지를 라우팅하는 데 사용되는 작업 상태를 기록합니다. |
작업 상태가면 InputRequired요청된 입력을 일반 RunAsync 으로 보내거나 RunStreamingAsync 동일한 세션을 사용하여 호출합니다. 에이전트는 나가는 메시지의 TaskId 해당 작업을 계속하도록 설정합니다. 다른 상태의 경우 이전 작업 ID를 에 보냅니다 ReferenceTaskIds.
컨텍스트, 작업 ID, 작업 상태 및 상태 모음을 유지하기 위해 세션을 SerializeSessionAsync 유지하고 복원 DeserializeSessionAsync 합니다. 알려진 컨텍스트 및 작업 ID에서 세션을 만들려면 A2AAgent.CreateSessionAsync 오버로드를 사용합니다.
Streaming
원격 에이전트가 요청을 처리할 때 업데이트를 수신하는 데 사용합니다 RunStreamingAsync .
using A2A;
using Microsoft.Agents.AI;
A2ACardResolver resolver = new(new Uri("https://a2a-agent.example.com"));
AIAgent agent = await resolver.GetAIAgentAsync(
allowedHosts: ["a2a-agent.example.com"]);
await foreach (var update in agent.RunStreamingAsync(
"Write a short story about a robot."))
{
if (!string.IsNullOrEmpty(update.Text))
{
Console.Write(update.Text);
}
}
참조: AIAgent.RunStreamingAsync.
스트리밍된 업데이트는 메시지, 작업 스냅샷, 상태 업데이트 또는 아티팩트 업데이트를 나타낼 수 있습니다.
세션은 각 업데이트 후가 아니라 스트림 열거가 완료된 후에 업데이트됩니다. 중단된 스트림에서 복구하려면 Stream 다시 연결에 표시된 대로 저장된 연속 토큰을 사용합니다.
백그라운드 응답
A2A 에이전트는 장기 실행 작업에 대한 백그라운드 응답을 지원합니다. 원격 에이전트가 또는 상태의 SubmittedWorking 작업을 반환하는 경우 응답에는 폴링 또는 스트림 다시 연결에 대한 연속 토큰이 포함됩니다.
비고
에이전트 프레임워크 .NET A2A 호스팅 패키지로 호스트되는 에이전트는 항상 메시지로 응답하므로 응답에 연속 토큰이 포함되지 않습니다. 자세한 내용은 작업 지원을 참조하세요.
작업 완료를 위한 폴링
작업 완료를 기다리지 않고 즉시 반환을 요청하도록 설정합니다 AllowBackgroundResponses . 이렇게 하면 A2A 요청의 ReturnImmediately 구성이 설정되고 명시적 세션이 필요합니다.
using A2A;
using Microsoft.Agents.AI;
A2ACardResolver resolver = new(new Uri("https://a2a-agent.example.com"));
AIAgent agent = await resolver.GetAIAgentAsync(
allowedHosts: ["a2a-agent.example.com"]);
AgentSession session = await agent.CreateSessionAsync();
// Request an immediate response when the remote agent creates a task.
AgentRunOptions options = new() { AllowBackgroundResponses = true };
// Start the initial run with a long-running task.
AgentResponse response = await agent.RunAsync(
"Analyze quantum computing applications in cryptography.",
session,
options: options);
// Poll while the task is submitted or working.
while (response.ContinuationToken is { } token)
{
// Wait before polling again.
await Task.Delay(TimeSpan.FromSeconds(2));
// Continue with the token.
response = await agent.RunAsync(
session,
options: new AgentRunOptions { ContinuationToken = token });
}
Console.WriteLine(response);
참조: AIAgent.RunAsync, AgentRunOptions.
연속 토큰 RunAsync 을 전달하면 새 실행을 시작하는 대신 기존 작업을 GetTaskAsync 검색합니다. 해당 호출에 새 메시지를 포함하지 마세요.
연속 토큰이 누락되었다고 해서 반드시 성공을 의미하는 것은 아닙니다. 입력 또는 인증을 기다리는 작업과 실패, 취소 또는 거부된 작업도 연속 토큰을 반환하지 않습니다. 애플리케이션에서 이러한 결과를 구분해야 하는 경우를 통해 response.RawRepresentation 작업 상태를 검사합니다.
스트림 다시 연결
또는 Working 상태의 Submitted 스트리밍 작업 스냅샷에는 연속 토큰이 포함됩니다. 있을 때 토큰을 저장합니다. 이후 업데이트에는 포함되지 않을 수 있습니다. 스트림이 중단되면 저장된 토큰을 사용하여 태스크의 이벤트 스트림에 다시 연결합니다.
다음 예제에서는 연속 토큰을 저장하여 중단을 시뮬레이션한 다음 해당 토큰과 다시 연결한 후 읽기를 중지합니다.
using A2A;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
A2ACardResolver resolver = new(new Uri("https://a2a-agent.example.com"));
AIAgent agent = await resolver.GetAIAgentAsync(
allowedHosts: ["a2a-agent.example.com"]);
AgentSession session = await agent.CreateSessionAsync();
ResponseContinuationToken? continuationToken = null;
await foreach (var update in agent.RunStreamingAsync(
"Analyze quantum computing applications in cryptography.",
session))
{
// Save the token for reconnection after an interruption.
if (update.ContinuationToken is { } token)
{
continuationToken = token;
// Stop reading to simulate a stream interruption.
break;
}
}
// Reconnect after the simulated interruption.
if (continuationToken is not null)
{
await foreach (var update in agent.RunStreamingAsync(
session,
options: new() { ContinuationToken = continuationToken }))
{
if (!string.IsNullOrEmpty(update.Text))
{
Console.WriteLine(update.Text);
}
}
}
참조: AIAgent.RunStreamingAsync, AgentRunOptions.
비고
다시 연결은 특정 이벤트 또는 텍스트 오프셋에서 다시 시작되지 않습니다. 필요에 따라 반복되는 작업 스냅샷 또는 아티팩트 콘텐츠를 처리합니다.
Tools
A2AAgent 는 원격 A2A 에이전트를 둘러싼 전송 수준 래퍼입니다. 원격 에이전트가 사용하는 도구는 원격 쪽에서 라이브로 사용되며 코드에 표시되지 않습니다. 에이전트 프레임워크 도구 형식(함수 도구, 코드 인터프리터, 파일 검색, 호스트/로컬 MCP 등)은 자체에 A2AAgent 구성되지 않으므로 원격 에이전트의 기능을 확장하고 원격 에이전트의 구성을 변경합니다.
다른 에이전트가 원격 에이전트를 호출하도록 하려면 .를 사용합니다 AsAIFunction(). 전체 예제는 A2A 샘플을 참조하세요.
시작하기
A2A 패키지를 설치합니다.
pip install agent-framework-a2a --pre
초기화
A2AAgent 는 원격 에이전트에 대해 미리 알고 있는 양에 따라 세 가지 방법으로 초기화할 수 있습니다.
직접 URL
엔드포인트가 알려진 개발 또는 긴밀하게 결합된 시스템의 경우:
from agent_framework.a2a import A2AAgent
async with A2AAgent(name="remote", url="https://a2a-agent.example.com") as agent:
response = await agent.run("Hello!")
print(response.messages[0].text)
URL만 제공되면 A2AAgent 내부적으로 최소 에이전트 카드를 만들고 JSON-RPC를 사용하여 연결합니다.
만드는 HTTP 클라이언트 A2AAgent 는 응답 쿠키를 유지하지 않습니다. 원격 서비스에 인증, 세션 또는 부하 분산 장치 선호도에 쿠키가 필요한 경우 통과http_client=합니다httpx.AsyncClient. 클라이언트 범위를 인증된 보안 주체 하나로 지정하고 애플리케이션에서 닫습니다.
제공된 HTTP 클라이언트는 전달할 때를 포함하여 호출자 소유로 유지됩니다 client=.
에이전트 카드
레지스트리 또는 카탈로그가 AgentCard 있는 경우 직접 전달합니다.
from agent_framework.a2a import A2AAgent
async with A2AAgent(agent_card=agent_card) as agent:
response = await agent.run("Plan a trip to Paris.")
print(response.messages[0].text)
제공 AgentCard 되면 A2AAgent 기본값 name 및 description 카드에서 가져옵니다. 카드로 전송을 협상합니다 supported_interfaces.
잘 알려진 URI(A2ACardResolver)
표준 잘 알려진 경로(A2ACardResolver)에서 원격 에이전트를 검색하려면 다음 a2a-sdk 을 사용합니다/.well-known/agent.json.
import httpx
from a2a.client import A2ACardResolver
from agent_framework.a2a import A2AAgent
async with httpx.AsyncClient(timeout=60.0) as http_client:
resolver = A2ACardResolver(httpx_client=http_client, base_url="https://a2a-agent.example.com")
agent_card = await resolver.get_agent_card()
async with A2AAgent(agent_card=agent_card) as agent:
response = await agent.run("What can you help me with?")
print(response.messages[0].text)
Streaming
원격 에이전트가 요청을 처리할 때 실시간으로 업데이트를 수신하는 데 사용합니다 stream=True .
from agent_framework.a2a import A2AAgent
async with A2AAgent(name="remote", url="https://a2a-agent.example.com") as agent:
stream = agent.run("Write a short story about a robot.", stream=True)
async for update in stream:
for content in update.contents:
if content.text:
print(content.text, end="", flush=True)
final = await stream.get_final_response()
print(f"\n({len(final.messages)} message(s))")
이진 첨부 파일
A2AAgent 는 데이터를 원시 이진 A2A 파트로 보냅니 Content 다. 0-바이트 페이로드는 스트리밍 및 비 스트리밍 실행에서 유효합니다. 미디어 형식, 데이터 URI 매개 변수, base64 인코딩 및 문자 유효성 검사는 여전히 적용됩니다.
장기 실행 작업
기본적으로 A2AAgent 원격 에이전트가 완료되기를 기다린 후 반환합니다. 장기 실행 작업의 경우 나중에 폴링하거나 구독하는 데 사용할 수 있는 연속 토큰을 표시하도록 설정합니다 background=True .
from agent_framework.a2a import A2AAgent
async with A2AAgent(name="worker", url="https://a2a-agent.example.com") as agent:
# Start a long-running task
response = await agent.run("Process this large dataset", background=True)
if response.continuation_token:
# Poll for completion later
result = await agent.poll_task(response.continuation_token)
print(result)
폴링 대신 SSE 스트림을 다시 구독할 수도 있습니다.
# Resubscribe to the task's event stream
response = await agent.run(continuation_token=response.continuation_token)
대화 ID(context_id)
A2AAgent 는 지속성 프로토콜 상태를 AgentSession.service_session_id 매핑으로 A2AServiceSessionId 저장합니다.
| Field | Type | Purpose |
|---|---|---|
context_id |
str |
A2A 대화를 식별합니다. |
task_id |
str \| None |
응답에서 만든 가장 최근의 원격 작업을 추적합니다. |
task_state |
TaskState \| None |
다음 요청이 입력 필수 작업을 계속하거나 완료된 작업을 참조할 수 있도록 최신 작업 상태를 기록합니다. |
애플리케이션이 A2A 컨텍스트를 이미 알고 있는 경우 구조화된 상태로 세션을 만듭니다.
from agent_framework import AgentSession
from agent_framework.a2a import A2AAgent, A2AServiceSessionId
async with A2AAgent(name="remote", url="https://a2a-agent.example.com") as agent:
session = AgentSession(
service_session_id=A2AServiceSessionId(
context_id="my-conversation-1",
task_id=None,
task_state=None,
)
)
# The A2A message uses context_id="my-conversation-1".
response = await agent.run("Hello!", session=session)
# A2AAgent updates task_id and task_state from the response.
response = await agent.run("Follow-up question", session=session)
첫 번째 응답에서 구조화된 매핑으로 AgentSession()A2AAgent 시작하고 채울 수도 있습니다. 일반 세션을 session.to_dict() 유지하고 A2A 컨텍스트, 작업 ID 및 작업 상태를 함께 유지하여 복원 AgentSession.from_dict(...)합니다.
작업의 TASK_STATE_INPUT_REQUIRED경우 다음 메시지는 동일한 작업을 계속하도록 설정합니다 task_id . 다른 작업 상태의 경우 원격 에이전트가 이전 결과를 구체화하거나 계속할 수 있도록 이전 작업 ID가 전송 reference_task_ids 됩니다.
선택한 실행 컨텍스트 전달
선택 function_invocation_kwargs 하거나 client_kwargs 원격 서비스에 도달해야 하는 경우 허용 목록으로 사용합니다forwarded_kwargs.
async with A2AAgent(
name="remote",
url="https://a2a-agent.example.com",
forwarded_kwargs=["tenant_id", "trace"],
) as agent:
response = await agent.run(
"Create the report.",
function_invocation_kwargs={"tenant_id": "contoso", "secret": "not-forwarded"},
client_kwargs={"trace": {"id": "abc"}},
)
나열된 키만 원본 매핑에 따라 그룹화된 아래에 agent_framework전송 SendMessageRequest.metadata 됩니다. 기본값은 실행 kwargs를 보내지 않으며 메타데이터는 HTTP 헤더 또는 메시지 기록에 추가되지 않습니다.
수신 서비스는 독립적인 허용 목록을 사용합니다.
from agent_framework.a2a import A2AExecutor
executor = A2AExecutor(
local_agent,
accepted_function_invocation_kwargs=["tenant_id"],
accepted_client_kwargs=["trace"],
)
서비스는 기본적으로 아무것도 허용하지 않습니다. 값은 클라이언트가 해당 키를 전달하고 서비스에서 일치하는 원본 매핑에서 동일한 키를 수락하는 경우에만 로컬 에이전트에 도달합니다. 전달된 메타데이터를 인증 또는 권한 부여가 아닌 요청 컨텍스트로 처리합니다.
전달된 값은 JSON과 호환되어야 합니다. 매핑 키는 문자열이어야 하고, 숫자는 유한해야 하며, 정수는 포함과 사이 -(2**53)2**53여야 합니다.
Protobuf Struct 는 숫자를 double로 저장하므로 정수는 부동 소수점 값으로 도착합니다. 잘못된 선택한 값은 요청을 보내기 전에 발생 AgentInvalidRequestException 합니다.
Authentication
A2A 보안 엔드포인트에 AuthInterceptor를 사용합니다.
from a2a.client.auth.interceptor import AuthInterceptor
from agent_framework.a2a import A2AAgent
class BearerAuth(AuthInterceptor):
def __init__(self, token: str):
self.token = token
async def intercept(self, request):
request.headers["Authorization"] = f"Bearer {self.token}"
return request
async with A2AAgent(
name="secure-agent",
url="https://secure-a2a-agent.example.com",
auth_interceptor=BearerAuth("your-token"),
) as agent:
response = await agent.run("Hello!")
시간 제한 구성
A2AAgent 은(는) 시간(초)을 int 허용합니다 float . 이 값은 연결, 읽기, 쓰기 및 풀 시간 제한 구성 요소에 적용됩니다. 개체를 httpx.Timeout 전달하여 이러한 값을 별도로 구성합니다.
import httpx
from agent_framework.a2a import A2AAgent
# Simple timeout (applies to all components)
async with A2AAgent(name="remote", url="https://example.com", timeout=120) as agent:
...
# Fine-grained timeout
async with A2AAgent(
name="remote",
url="https://example.com",
timeout=httpx.Timeout(connect=10.0, read=120.0, write=10.0, pool=5.0),
) as agent:
...
시간 제한을 지정하지 않으면 기본값은 10대 연결, 60년대 읽기, 10s 쓰기, 5s 풀입니다.
Tools
A2AAgent 는 원격 A2A 에이전트를 둘러싼 전송 수준 래퍼입니다. 원격 에이전트가 사용하는 도구는 원격 쪽에서 라이브로 사용되며 코드에 표시되지 않습니다. 에이전트 프레임워크 도구 형식(함수 도구, 코드 인터프리터, 파일 검색, 호스트/로컬 MCP 등)은 자체에 A2AAgent 구성되지 않으므로 원격 에이전트의 기능을 확장하고 원격 에이전트의 구성을 변경합니다.
Foundry 에이전트가 A2A 에이전트를 도구로 호출하도록 하려면 <
Go는 패키지를 통해 원격 A2A 에이전트를 provider/a2aprovider 지원합니다.
에이전트 프레임워크 및 A2A 패키지를 설치합니다.
go get github.com/microsoft/agent-framework-go
go get github.com/a2aproject/a2a-go/v2
원격 A2A 에이전트에 연결
원격 에이전트 카드를 해석하여 A2A 클라이언트를 생성하고, 이 클라이언트를 표준 Agent Framework 에이전트로 래핑합니다.
import (
"context"
"github.com/a2aproject/a2a-go/v2/a2aclient"
"github.com/a2aproject/a2a-go/v2/a2aclient/agentcard"
"github.com/microsoft/agent-framework-go/agent"
"github.com/microsoft/agent-framework-go/provider/a2aprovider"
)
ctx := context.Background()
card, err := agentcard.DefaultResolver.Resolve(ctx, "http://localhost:5000")
if err != nil {
panic(err)
}
client, err := a2aclient.NewFromCard(ctx, card)
if err != nil {
panic(err)
}
a := a2aprovider.NewAgent(
client,
a2aprovider.AgentConfig{
Config: agent.Config{
Name: card.Name,
Description: card.Description,
},
},
)
resp, err := a.RunText(ctx, "Hello!").Collect()
공급자는 에이전트 프레임워크 세션에 A2A context_id 및 작업 ID를 저장하므로 후속 메시지는 대화 연속성을 유지할 수 있습니다.
프로토콜 선택
원격 에이전트가 여러 전송 바인딩을 보급하는 경우 A2A 클라이언트를 만들 때 기본 전송을 구성합니다.
client, err := a2aclient.NewFromCard(
ctx,
card,
a2aclient.WithConfig(a2aclient.Config{
PreferredTransports: []a2a.TransportProtocol{a2a.TransportProtocolHTTPJSON},
}),
)
JSON-RPC를 선호하는 경우 사용합니다 a2a.TransportProtocolJSONRPC .
장기 실행 작업
A2A 작업은 에이전트 프레임워크 연속 토큰을 통해 표시됩니다. 명시적 세션과 agent.AllowBackgroundResponses(true)을 사용해 실행을 시작한 다음, 새 메시지 없이 continuation token을 사용하여 Run를 호출해 폴링합니다.
session, err := a.CreateSession(ctx)
if err != nil {
panic(err)
}
resp, err := a.RunText(
ctx,
"Process this large dataset.",
agent.WithSession(session),
agent.AllowBackgroundResponses(true),
).Collect()
if err != nil {
panic(err)
}
for resp.ContinuationToken != "" {
resp, err = a.Run(
ctx,
nil,
agent.WithSession(session),
agent.WithContinuationToken(resp.ContinuationToken),
).Collect()
if err != nil {
panic(err)
}
}
중단된 스트리밍 실행의 경우, 마지막으로 수신한 업데이트에서 update.ContinuationToken를 캡처한 다음, 이후의 스트리밍 실행에 agent.WithContinuationToken(token) 및 agent.Stream(true)와 함께 전달합니다.
원격 A2A 에이전트를 도구로 사용
각 원격 에이전트를 확인하고, 래핑 a2aprovider.NewAgent하고, 를 사용하여 도구로 agenttool.New변환합니다.
tools := make([]tool.Tool, 0, len(agentURLs))
for _, agentURL := range agentURLs {
card, err := agentcard.DefaultResolver.Resolve(ctx, agentURL)
if err != nil {
panic(err)
}
client, err := a2aclient.NewFromCard(ctx, card)
if err != nil {
panic(err)
}
remoteAgent := a2aprovider.NewAgent(client, a2aprovider.AgentConfig{
Config: agent.Config{
Name: card.Name,
Description: card.Description,
},
})
tools = append(tools, agenttool.New(remoteAgent, agenttool.Config{}))
}
Tip
전체 실행 가능한 예제는 A2A 공급자 샘플 및 A2A 에이전트를 도구 샘플로 참조하세요.
다음 단계
더 자세히 살펴보기: