mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
5affc9c333
* Reorganize A2A samples: client demos in 02-agents, use package A2AExecutor - Move client samples (agent_with_a2a, a2a_agent_as_function_tools) to samples/02-agents/a2a/ - Add new concept samples: polling, stream reconnection, protocol selection - Replace sample agent_executor.py with package-level A2AExecutor (stream=True) - Update 04-hosting/a2a to focus on server-side, point to 02-agents for clients - Add README.md for the new 02-agents/a2a/ sample collection Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix streaming artifact coalescing and address PR review feedback A2AExecutor fix: - Generate a stable artifact_id per stream in _run_stream so all streaming chunks share the same ID, enabling proper append=True coalescing per the A2A spec (TaskArtifactUpdateEvent with same artifactId). - Previously, item.message_id was None for OpenAI/Foundry streaming updates, causing the SDK to generate a new random UUID per token (100+ separate artifacts instead of 1 appended artifact). Sample improvements: - Replace join workaround with response.text now that coalescing works - Add background=True to stream reconnection resume call (required for continuation token emission on in-progress tasks) - Fix type ignore specificity in polling sample Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
110 lines
4.0 KiB
Python
110 lines
4.0 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
import asyncio
|
|
import os
|
|
|
|
import httpx
|
|
from a2a.client import A2ACardResolver
|
|
from agent_framework.a2a import A2AAgent
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables from .env file
|
|
load_dotenv()
|
|
|
|
"""
|
|
Agent2Agent (A2A) Protocol Integration Sample
|
|
|
|
This sample demonstrates how to connect to and communicate with external agents using
|
|
the A2A protocol. A2A is a standardized communication protocol that enables interoperability
|
|
between different agent systems, allowing agents built with different frameworks and
|
|
technologies to communicate seamlessly.
|
|
|
|
By default the A2AAgent waits for the remote agent to finish before returning (background=False).
|
|
This means long-running A2A tasks are handled transparently — the caller simply awaits the result.
|
|
For advanced scenarios where you need to poll or resubscribe to in-progress tasks, see the
|
|
a2a_polling and a2a_stream_reconnection samples in this folder.
|
|
|
|
For more information about the A2A protocol specification, visit: https://a2a-protocol.org/latest/
|
|
|
|
Key concepts demonstrated:
|
|
- Discovering A2A-compliant agents using AgentCard resolution
|
|
- Creating A2AAgent instances to wrap external A2A endpoints
|
|
- Non-streaming request/response
|
|
- Streaming responses to receive incremental updates via SSE
|
|
|
|
To run this sample:
|
|
1. Set the A2A_AGENT_HOST environment variable to point to an A2A-compliant agent endpoint
|
|
Example: export A2A_AGENT_HOST="https://your-a2a-agent.example.com"
|
|
2. Ensure the target agent exposes its AgentCard at /.well-known/agent.json
|
|
3. Run: uv run python agent_with_a2a.py
|
|
|
|
Visit the README.md for more details on setting up and running A2A agents.
|
|
"""
|
|
|
|
|
|
async def main():
|
|
"""Demonstrates connecting to and communicating with an A2A-compliant agent."""
|
|
# 1. Get A2A agent host from environment.
|
|
a2a_agent_host = os.getenv("A2A_AGENT_HOST")
|
|
if not a2a_agent_host:
|
|
raise ValueError("A2A_AGENT_HOST environment variable is not set")
|
|
|
|
print(f"Connecting to A2A agent at: {a2a_agent_host}")
|
|
|
|
# 2. Resolve the agent card to discover capabilities.
|
|
async with httpx.AsyncClient(timeout=60.0) as http_client:
|
|
resolver = A2ACardResolver(httpx_client=http_client, base_url=a2a_agent_host)
|
|
agent_card = await resolver.get_agent_card()
|
|
print(f"Found agent: {agent_card.name} - {agent_card.description}")
|
|
|
|
# 3. Create A2A agent instance.
|
|
async with A2AAgent(
|
|
name=agent_card.name,
|
|
description=agent_card.description,
|
|
agent_card=agent_card,
|
|
url=a2a_agent_host,
|
|
) as agent:
|
|
# 4. Simple request/response — the agent waits for completion internally.
|
|
# Even if the remote agent takes a while, background=False (the default)
|
|
# means the call blocks until a terminal state is reached.
|
|
print("\n--- Non-streaming response ---")
|
|
response = await agent.run("What are your capabilities?")
|
|
|
|
print(f"Agent Response:\n {response.text}")
|
|
|
|
# 5. Stream a response — the natural model for A2A.
|
|
# Updates arrive as Server-Sent Events, letting you observe
|
|
# progress in real time as the remote agent works.
|
|
print("\n--- Streaming response ---")
|
|
stream = agent.run("Tell me about yourself", stream=True)
|
|
async for update in stream:
|
|
for content in update.contents:
|
|
if content.text:
|
|
print(content.text, end="", flush=True)
|
|
print() # newline after streaming completes
|
|
|
|
response = await stream.get_final_response()
|
|
print(f"\nFinal response:\n {response.text}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|
|
|
|
|
|
"""
|
|
Sample output:
|
|
|
|
Connecting to A2A agent at: http://localhost:5001/
|
|
Found agent: MyAgent - A helpful AI assistant
|
|
|
|
--- Non-streaming response ---
|
|
Agent Response:
|
|
I can help with code generation, analysis, and general Q&A.
|
|
|
|
--- Streaming response ---
|
|
I am an AI assistant built to help with various tasks.
|
|
|
|
Final response:
|
|
I am an AI assistant built to help with various tasks.
|
|
"""
|