Build an A2A Agent That Discovers and Calls Another Agent
In this article
This guide walks you through building an Agent-to-Agent (A2A) system where one agent can discover another agent's capabilities and call it to complete a task. You'll end up with a working prototype using the A2A protocol, which is an open standard for communication between AI agents. This is useful for creating modular AI systems where specialized agents handle different tasks, such as a coordinator agent that delegates calculations, data fetching, or text processing to other agents.
What You'll Need
- Python 3.9+ installed on your machine.
- Basic familiarity with Python, HTTP requests, and JSON.
- An API key for an LLM provider (like OpenAI, Anthropic, or a local model) if you want the agents to have natural language understanding. For a purely programmatic demo, you can skip this.
- Two terminal windows or two separate project folders.
Step-by-Step
- Set up the A2A agent framework. Create a new directory for your project and install the official A2A Python SDK:
pip install a2a-sdk. This SDK provides the core classes for building agents and handling the A2A protocol. - Create the first agent (the "worker"). This agent will expose a specific capability, for example, performing a calculation. Create a file named
worker_agent.py. Import the necessary classes:from a2a import Agent, A2AClient, A2AServer(the exact imports may vary slightly depending on the SDK version; check the documentation). Define a function that does the work, likedef add_numbers(a, b): return a + b. - Wrap the function as an A2A agent. Instantiate an
Agentobject, passing a name, description, and a list of capabilities. For each capability, you define a name (e.g., "add"), a description, and a handler function that takes a JSON payload and returns a JSON response. The handler should parse the input, call your function, and return the result in a structured format. - Start the worker agent's server. Use
A2AServerto run the agent over HTTP. Start it on a specific port, say 5001. You'll need to serve the agent's card (a JSON file that describes the agent's capabilities) at a known endpoint, typically/.well-known/agent-card.json. The SDK often provides a helper to do this automatically. - Test the worker agent. Use a tool like
curlor a browser to fetch the agent card:curl http://localhost:5001/.well-known/agent-card.json. You should see JSON listing the agent's name, description, and the "add" capability. - Create the second agent (the "discoverer"). In a new file
discoverer_agent.py, create anotherAgentinstance. This agent will have a capability called "delegate_add" that takes two numbers, discovers the worker agent, and calls it. - Implement discovery. In the handler for "delegate_add", use the A2A client to fetch the worker agent's card. The client can do this by sending a GET request to the worker's URL. Parse the card to find the "add" capability and its endpoint (usually the same server URL).
- Call the discovered agent. Use the A2A client to send a task to the worker agent. The client will POST a JSON-RPC message to the agent's endpoint. Pass the numbers as parameters. Await the response and extract the result.
- Run both agents. Start the worker agent in one terminal (e.g.,
python worker_agent.py), then start the discoverer agent in another terminal (on a different port, say 5002). - Test the full flow. Send a request to the discoverer agent to trigger its "delegate_add" capability. You can do this by calling its server endpoint with a JSON payload containing the two numbers. The discoverer agent should discover the worker, call it, and return the sum.
Common Mistakes
- Forgetting to start the worker agent first. The discoverer agent will fail to fetch the worker's card if the worker isn't running.
- Incorrect agent card URL. The A2A standard specifies the card should be at
/.well-known/agent-card.json. If you serve it elsewhere, discovery will fail. - Mismatched JSON payloads. The worker agent expects a specific input format (e.g., {"a": 5, "b": 3}). If the discoverer sends a different structure, the worker's handler will error.
- Not handling errors in the worker. If the worker throws an exception, the A2A protocol expects a structured error response. Wrap your handler logic in try/except and return a proper error JSON.
- Using the wrong HTTP method. A2A uses POST for JSON-RPC calls. If you try GET, it will fail.
Tips
- Use a registry for multiple agents. For more than two agents, consider a central registry that lists all agent cards. The discoverer agent can query the registry instead of hardcoding URLs.
- Add authentication. In production, secure your agent endpoints with API keys or OAuth. The A2A protocol supports auth headers.
- Log everything. Add print statements or logging to both agents to see the requests and responses. This helps debugging.
- Keep capabilities atomic. Each agent should do one thing well. This makes discovery and delegation simpler.
- Use async handlers. If your worker does I/O (like fetching a URL), make the handler async to avoid blocking.
Troubleshooting
- Discovery fails with 404. Check that the worker agent is running and that the agent card is served at the correct path. Verify the URL in the discoverer's code.
- Connection refused. Ensure both agents are running on different ports and that the ports are not blocked by a firewall.
- JSON-RPC error -32601 (Method not found). The worker agent doesn't recognize the method name. Check the capability name in the agent card and the method sent by the client.
- Timeout. If the worker takes too long, increase the timeout in the A2A client configuration.
- Unexpected response format. The worker's response must be a valid A2A message. Make sure your handler returns the result inside the correct structure (e.g.,
{"result": ...}).
Frequently Asked Questions
What is the A2A protocol exactly?
A2A (Agent-to-Agent) is an open protocol developed by Google that defines how AI agents can communicate, discover each other, and delegate tasks. It uses JSON-RPC over HTTP and includes an agent card for discovery.
Do I need an LLM for this to work?
No. The core discovery and calling mechanism works with plain functions. An LLM is only needed if you want the agents to interpret natural language instructions. In that case, you'd add an LLM layer on top of the A2A communication.
Can I use a different programming language?
Yes. The A2A protocol is language-agnostic. There are SDKs for Python, JavaScript, and others. You can also implement the JSON-RPC calls manually.
How do I make the discoverer agent choose between multiple workers?
You can implement logic in the discoverer agent to inspect each worker's agent card and select one based on the capability description or a scoring mechanism. For example, you could compare the description to the task at hand using simple keyword matching or an LLM.
Is A2A production-ready?
As of 2025, A2A is still evolving but has strong industry backing. For production, you should add security, error handling, and monitoring. The protocol itself is stable enough for prototypes and early deployments.
What if the worker agent is offline?
The discoverer agent should handle connection errors gracefully. You can implement retries, a fallback to another worker, or return an error message to the user.