5. Starting the Server
5. Starting the Server
Section titled “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
Section titled “Server Setup in Helloworld”Let’s look at __main__.py again to see how the server is initialized and started.
--8<-- "https://raw.githubusercontent.com/a2aproject/a2a-samples/refs/heads/main/samples/python/agents/helloworld/__main__.py"Let’s break this down:
-
DefaultRequestHandler:- The SDK provides
DefaultRequestHandler. This handler takes yourAgentExecutorimplementation (here,HelloWorldAgentExecutor) and aTaskStore(here,InMemoryTaskStore). - It routes incoming A2A RPC calls to the appropriate methods on your executor (like
executeorcancel). - The
TaskStoreis used by theDefaultRequestHandlerto 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
A2AStarletteApplicationclass is instantiated with theagent_cardand therequest_handler(referred to ashttp_handlerin its constructor). - The
agent_cardis crucial because the server will expose it at the/.well-known/agent-card.jsonendpoint (by default). - The
request_handleris responsible for processing all incoming A2A method calls by interacting with yourAgentExecutor.
- The
-
uvicorn.run(server_app_builder.build(), ...):- The
A2AStarletteApplicationhas 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=9999specifies the port to listen on. This matches theurlin theAgentCard.
- The
Running the Helloworld Server
Section titled “Running the Helloworld Server”Navigate to the a2a-samples directory in your terminal (if you’re not already there) and ensure your virtual environment is activated.
To run the Helloworld server:
# from the a2a-samples directorypython samples/python/agents/helloworld/__main__.pyYou 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.