5. 启动服务器

有了 Agent Card 与执行器,就可以搭建并启动 A2A 服务端了。

Python SDK 提供了路由工厂与辅助函数(create_agent_card_routes、create_jsonrpc_routes、create_rest_routes),用于创建 A2A 服务端各项服务的路由。这些路由可以原生挂载到 Starlette 、FastAPI 等常用框架上,让你更好地控制认证、日志等功能。本教程使用 Starlette 搭配 Uvicorn 。

Helloworld 的服务端搭建

再来看 __main__.py,了解服务端如何初始化和启动。

先创建请求处理器:

# The RequestHandler processes incoming requests and manages tasks
request_handler = DefaultRequestHandler(
    # Agent executor handles the execution of the client requests
    agent_executor=HelloWorldAgentExecutor(),
    # The task_store is used to store and manage tasks
    task_store=InMemoryTaskStore(),
    # Public agent card for unauthenticated users
    agent_card=public_agent_card,
    # Extended agent card for authenticated users
    extended_agent_card=extended_agent_card,
)

再创建路由、构建应用并运行:

# Creating the routes for the A2A server
# These routes handle the incoming requests from the clients
# and the outgoing responses to the clients
routes = []

# Create routes for the agent card
routes.extend(create_agent_card_routes(public_agent_card))

# Create routes for the JSONRPC protocol
# Alternatively, you can choose GRPC or HTTP_JSON as protocol bindings
# based on your requirements
routes.extend(create_jsonrpc_routes(request_handler, '/'))

# Create a web app with the defined routes
# Here we are using Starlette, a lightweight ASGI web framework to serve the agent
# Alternatively, you can choose FastAPI or other ASGI frameworks
app = Starlette(routes=routes)

# Run the app
# Uvicorn is a production-ready ASGI HTTP server
uvicorn.run(app, host='127.0.0.1', port=9999)

逐项说明:

1. DefaultRequestHandler

  • SDK 提供了 DefaultRequestHandler。它接收你实现的 AgentExecutor(HelloWorldAgentExecutor)、一个 TaskStore(InMemoryTaskStore),以及公开版与扩展版 AgentCard;
  • 它把传入的 A2A RPC 调用路由到执行器的对应方法(如 execute 或 cancel);
  • TaskStore 供 DefaultRequestHandler 管理任务的生命周期,对有状态交互、流式传输与重新订阅尤其重要。即使执行器很简单,处理器也需要任务存储;
  • agent_card 传给处理器,使它在处理请求时核对智能体声明的能力,例如在处理流式或推送通知请求前检查是否支持;
  • extended_agent_card 传给处理器,使它可以通过 GetExtendedAgentCard 方法向已认证的客户端提供扩展卡片。

2. create_agent_card_routes 与 create_jsonrpc_routes

  • create_agent_card_routes(public_agent_card) 返回 Starlette 路由,在 /.well-known/agent-card.json 端点公开 Agent Card,供公开发现;
  • create_jsonrpc_routes(request_handler, '/') 返回处理所有 A2A JSON-RPC 方法调用的 Starlette 路由,并委托给 request_handler;
  • 这些路由列表合并后传给标准的 Starlette 应用。

3. uvicorn.run(app, ...)

  • 构建好的 Starlette 应用通过 uvicorn.run() 运行,使智能体可通过 HTTP 访问;
  • host='127.0.0.1' 表示只能在本机访问;
  • port=9999 指定监听端口,与 AgentCard 的 supported_interfaces 中定义的端点一致。

运行 Helloworld 服务器

在终端中进入 a2a-samples 目录(如果尚未在该目录),并确认已激活虚拟环境,然后运行:

# from the a2a-samples directory
python samples/python/agents/helloworld/__main__.py

你应该会看到类似下面的输出,表明服务已经运行:

INFO:     Started server process [xxxxx]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:9999 (Press CTRL+C to quit)

Helloworld 智能体现在已经上线并在监听请求。下一步,我们来与它交互 。

来源:A2A 官方文档 · Starting the Server