5. Starting the Server¶
Now that we have an Agent Card and an Agent Executor, we can set up and start the A2A server.
The A2A Python SDK provides an A2AStarletteApplication
class that simplifies running an A2A-compliant HTTP server. It uses Starlette for the web framework and is typically run with an ASGI server like Uvicorn.
Server Setup in Helloworld¶
Let's look at examples/helloworld/__main__.py
again to see how the server is initialized and started.
# examples/helloworld/__main__.py
from agent_executor import HelloWorldAgentExecutor
from a2a.server.apps import A2AStarletteApplication
from a2a.server.request_handlers import DefaultRequestHandler
from a2a.server.tasks import InMemoryTaskStore # For task state management
from a2a.types import (
# ... other imports ...
AgentCard,
AgentSkill,
AgentCapabilities,
AgentAuthentication,
# ...
)
import uvicorn
if __name__ == '__main__':
# ... AgentSkill and AgentCard definition from previous steps ...
skill = AgentSkill(
id='hello_world',
name='Returns hello world',
description='just returns hello world',
tags=['hello world'],
examples=['hi', 'hello world'],
)
agent_card = AgentCard(
name='Hello World Agent',
description='Just a hello world agent',
url='http://localhost:9999/',
version='1.0.0',
defaultInputModes=['text'],
defaultOutputModes=['text'],
capabilities=AgentCapabilities(),
skills=[skill],
authentication=AgentAuthentication(schemes=['public']),
)
# 1. Request Handler
request_handler = DefaultRequestHandler(
agent_executor=HelloWorldAgentExecutor(),
task_store=InMemoryTaskStore(), # Provide a task store
)
# 2. A2A Starlette Application
server_app_builder = A2AStarletteApplication(
agent_card=agent_card, http_handler=request_handler
)
# 3. Start Server using Uvicorn
uvicorn.run(server_app_builder.build(), host='0.0.0.0', port=9999)
Let's break this down:
-
DefaultRequestHandler
:- The SDK provides
DefaultRequestHandler
. This handler takes yourAgentExecutor
implementation (here,HelloWorldAgentExecutor
) and aTaskStore
(here,InMemoryTaskStore
). - It routes incoming A2A RPC calls to the appropriate methods on your executor (like
execute
orcancel
). - The
TaskStore
is used by theDefaultRequestHandler
to manage the lifecycle of tasks, especially for stateful interactions, streaming, and resubscription. Even if your agent executor is simple, the handler needs a task store.
- The SDK provides
-
A2AStarletteApplication
:- The
A2AStarletteApplication
class is instantiated with theagent_card
and therequest_handler
(referred to ashttp_handler
in its constructor). - The
agent_card
is crucial because the server will expose it at the/.well-known/agent.json
endpoint (by default). - The
request_handler
is responsible for processing all incoming A2A method calls by interacting with yourAgentExecutor
.
- The
-
uvicorn.run(server_app_builder.build(), ...)
:- The
A2AStarletteApplication
has abuild()
method that constructs the actual Starlette application. - This application is then run using
uvicorn.run()
, making your agent accessible over HTTP. host='0.0.0.0'
makes the server accessible on all network interfaces on your machine.port=9999
specifies the port to listen on. This matches theurl
in theAgentCard
.
- The
Running the Helloworld Server¶
Navigate to the a2a-python
directory in your terminal (if you're not already there) and ensure your virtual environment is activated.
To run the Helloworld server:
You should see output similar to this, indicating the server is running:
INFO: Started server process [xxxxx]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:9999 (Press CTRL+C to quit)
Your A2A Helloworld agent is now live and listening for requests! In the next step, we'll interact with it.