跳转至

5. 启动服务器

现在我们已经有了 Agent Card 和 Agent Executor,我们可以设置并启动 A2A 服务器。

A2A Python SDK 提供了 A2AStarletteApplication 类,它简化了运行符合 A2A 规范的 HTTP 服务器。它使用 Starlette 作为 Web 框架,通常与 ASGI 服务器如 Uvicorn 一起运行。

Helloworld 中的服务器设置

让我们再次查看 __main__.py 来了解服务器是如何初始化和启动的。

import uvicorn

from a2a.server.apps import A2AStarletteApplication
from a2a.server.request_handlers import DefaultRequestHandler
from a2a.server.tasks import InMemoryTaskStore
from a2a.types import (
    AgentCapabilities,
    AgentCard,
    AgentSkill,
)
from agent_executor import (
    HelloWorldAgentExecutor,  # type: ignore[import-untyped]
)


if __name__ == '__main__':
    skill = AgentSkill(
        id='hello_world',
        name='Returns hello world',
        description='just returns hello world',
        tags=['hello world'],
        examples=['hi', 'hello world'],
    )

    extended_skill = AgentSkill(
        id='super_hello_world',
        name='Returns a SUPER Hello World',
        description='A more enthusiastic greeting, only for authenticated users.',
        tags=['hello world', 'super', 'extended'],
        examples=['super hi', 'give me a super hello'],
    )

    # This will be the public-facing agent card
    public_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(streaming=True),
        skills=[skill],  # Only the basic skill for the public card
        supportsAuthenticatedExtendedCard=True,
    )

    # This will be the authenticated extended agent card
    # It includes the additional 'extended_skill'
    specific_extended_agent_card = public_agent_card.model_copy(
        update={
            'name': 'Hello World Agent - Extended Edition',  # Different name for clarity
            'description': 'The full-featured hello world agent for authenticated users.',
            'version': '1.0.1',  # Could even be a different version
            # Capabilities and other fields like url, defaultInputModes, defaultOutputModes,
            # supportsAuthenticatedExtendedCard are inherited from public_agent_card unless specified here.
            'skills': [
                skill,
                extended_skill,
            ],  # Both skills for the extended card
        }
    )

    request_handler = DefaultRequestHandler(
        agent_executor=HelloWorldAgentExecutor(),
        task_store=InMemoryTaskStore(),
    )

    server = A2AStarletteApplication(
        agent_card=public_agent_card,
        http_handler=request_handler,
        extended_agent_card=specific_extended_agent_card,
    )

    uvicorn.run(server.build(), host='0.0.0.0', port=9999)

让我们分解一下:

  1. DefaultRequestHandler

    • SDK 提供了 DefaultRequestHandler。这个处理器接收您的 AgentExecutor 实现(这里是 HelloWorldAgentExecutor)和一个 TaskStore(这里是 InMemoryTaskStore)。
    • 它将传入的 A2A RPC 调用路由到您的执行器的适当方法(如 executecancel)。
    • TaskStoreDefaultRequestHandler 用来管理任务的生命周期,特别是对于有状态的交互、流式处理和重新订阅。即使您的 agent 执行器很简单,处理器也需要一个任务存储。
  2. A2AStarletteApplication

    • A2AStarletteApplication 类使用 agent_cardrequest_handler(在其构造函数中称为 http_handler)进行实例化。
    • agent_card 至关重要,因为服务器将在 /.well-known/agent.json 端点(默认)暴露它。
    • request_handler 负责通过与您的 AgentExecutor 交互来处理所有传入的 A2A 方法调用。
  3. uvicorn.run(server_app_builder.build(), ...)

    • A2AStarletteApplication 有一个 build() 方法,用于构建实际的 Starlette 应用程序。
    • 然后使用 uvicorn.run() 运行这个应用程序,使您的 agent 可以通过 HTTP 访问。
    • host='0.0.0.0' 使服务器可以在您机器的所有网络接口上访问。
    • port=9999 指定要监听的端口。这与 AgentCard 中的 url 相匹配。

运行 Helloworld 服务器

在终端中导航到 a2a-samples 目录(如果您还没有在那里),并确保您的虚拟环境已激活。

要运行 Helloworld 服务器:

# 从 a2a-samples 目录
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://0.0.0.0:9999 (Press CTRL+C to quit)

您的 A2A Helloworld agent 现在已经启动并正在监听请求!在下一步中,我们将与它交互。