Skip to content

Serving

any-agent provides a simple way of serving agents from any of the supported frameworks using the Agent2Agent Protocol (A2A), via the A2A Python SDK. You can refer to the link for more information on the protocol, as explaining it is out of the scope of this page.

Warning

The A2A protocol is in early stages of development and so is the functionality provided by any-agent here.

In order to use A2A serving, you must first install the 'a2a' extra: pip install 'any-agent[a2a]'

You can configure and serve an agent using the ServingConfig and the AnyAgent.serve or AnyAgent.serve_async method.

Example

For illustrative purposes, we are going to define 2 separate scripts, each defining an agent to answer questions about a specific agent framework (either OpenAI Agents SDK or Google ADK):

# google_expert.py
from any_agent import AgentConfig, AnyAgent
from any_agent.config import ServingConfig
from any_agent.tools import search_web

agent = AnyAgent.create(
    "google",
    AgentConfig(
        name="google_expert",
        model_id="gpt-4.1-mini",
        description="An agent that can answer questions specifically and only about the Google Agents Development Kit (ADK). Reject questions about anything else.",
        tools=[search_web]
    )
)

agent.serve(ServingConfig(port=5001))
# openai_expert.py
from any_agent import AgentConfig, AnyAgent
from any_agent.config import ServingConfig
from any_agent.tools import search_web

agent = AnyAgent.create(
    "openai",
    AgentConfig(
        name="openai-expert",
        model_id="gpt-4.1-nano",
        instructions="You can answer questions about the OpenAI Agents SDK but nothing else.",
        description="An agent that can answer questions specifically about the OpenAI Agents SDK.",
        tools=[search_web]
    )
)

agent.serve(ServingConfig(port=5002))

We can then run each of the scripts in a separate terminal and leave them running in the background.

Now, using a simple python script that implements the A2A client, we can communicate with these agents! For this example, we use the A2A Python SDK

from uuid import uuid4
import asyncio
import httpx
from a2a.client import A2ACardResolver, A2AClient
from a2a.types import AgentCard, MessageSendParams, SendMessageRequest

async def main():
    async with httpx.AsyncClient() as httpx_client:
        agent_card: AgentCard = await A2ACardResolver(
            httpx_client,
            base_url="http://localhost:5001",
        ).get_agent_card(http_kwargs=None)
        client = A2AClient(httpx_client=httpx_client, agent_card=agent_card)
        send_message_payload = {
            "message": {
                "role": "user",
                "parts": [{"kind": "text", "text": "What do you know about the Google ADK?"}],
                "messageId": uuid4().hex,
            },
        }
        request = SendMessageRequest(params=MessageSendParams(**send_message_payload))
        response = await client.send_message(request, http_kwargs={"timeout": 60})
        print(f" Response from first agent: {response.model_dump_json(indent=2)}")

        agent_card: AgentCard = await A2ACardResolver(
            httpx_client,
            base_url="http://localhost:5002",
        ).get_agent_card(http_kwargs=None)
        client = A2AClient(httpx_client=httpx_client, agent_card=agent_card)
        send_message_payload = {
            "message": {
                "role": "user",
                "parts": [{"kind": "text", "text": "What do you know about the Google ADK?"}],
                "messageId": uuid4().hex,
            },
        }
        request = SendMessageRequest(params=MessageSendParams(**send_message_payload))
        response = await client.send_message(request, http_kwargs={"timeout": 60})
        print(f" Response from second agent: {response.model_dump_json(indent=2)}")
if __name__ == "__main__":
    asyncio.run(main())

You will see that the first agent answered the question, but the second agent did not answer the question. This is because the question was about Google ADK, but the agent was told it could only answer questions about the OpenAI Agents SDK.

More Examples

Check out our cookbook example for building and serving an agent via A2A:

👉 Serve an Agent with A2A (Jupyter Notebook)

Accessing an A2A agent using tools

As described in the tools section, an agent can request actions from other agents by using the a2a_tool function. It retrieves the agent card, and builds another function that relays the request via the A2A protocol and unpacks the result.