← Back to Tutorials

11. Agent-to-Agent Communication

A2A Protocol

The Agent-to-Agent (A2A) protocol enables autonomous communication between agents built with different frameworks, hosted in different environments, and managed by different teams.

Architecture

A2A uses a client-server model:

Hello World A2A Server

from a2a import A2AServer, AgentCard

server = A2AServer()

@server.agent_card
def get_card():
    return AgentCard(
        name="WeatherAgent",
        description="Provides weather information",
        url="http://localhost:8000/a2a",
        capabilities=["get_weather"]
    )

@server.handle("get_weather")
async def get_weather(location: str):
    # Call weather API
    return {"temperature": 22, "conditions": "sunny"}

server.run(port=8000)

Exposing Agents via A2A

# Expose any Foundry agent as an A2A endpoint
from a2a import A2AAdapter
from azure.ai.projects import AIProjectClient

adapter = A2AAdapter(client, agent_id)
adapter.expose(port=8000)

Streaming Responses

A2A supports streaming for real-time agent communication:

async for chunk in a2a_client.send_message_stream("What's the weather?", agent_url):
    print(chunk.text, end="")

MCP + A2A

Combine both protocols: use MCP for tool exposure and A2A for agent-to-agent communication. MCP provides tools; A2A provides agent orchestration across boundaries.

Publishing to Azure

Deploy A2A servers to Azure Container Apps, AKS, or Azure Functions. Use Azure API Management to manage, secure, and monitor A2A endpoints.

✏️ Exercise: Create two agents: a Weather Agent (A2A server) and a Travel Agent (A2A client). The Travel Agent should discover and call the Weather Agent to get forecasts for trip planning. Implement streaming responses.