多Agent协作,A2A协议深度解析

A2A协议

2025年4月 :谷歌在 Google Cloud Next '25 大会上正式发布并开源了 A2A 协议。

A2A协议作用在Agent与Agent之间的,MCP协议作用在Agent内部的(MCP Server有哪些函数,这些函数如何调用)

每一个Agent都是一个http服务器,多Agent时符合A2A协议,保持Agent与Agent之间的通信

A2A服务启动

  1. 下载调度Agent:

    github地址: https://github.com/a2aproject/a2a-samples

  2. 启动调度Agent, demo/ui/main.py所创建的角色:python main.py

  3. 天气Agent

    • main.py

      python 复制代码
      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 WeatherAgentExecutor
      
      
      def main(host: str, port: int):
          capabilities = AgentCapabilities(streaming=False)
          forecast_skill = AgentSkill(
              id='天气预告',
              name='天气预告',
              description='给出某地的天气预告',
              tags=['天气', '预告'],
              examples=['给我纽约未来 7 天的天气预告'],
          )
          air_quality_skill = AgentSkill(
              id='空气质量报告',
              name='空气质量报告',
              description='给出某地当前时间的空气质量报告,不做预告',
              tags=['空气', '质量'],
              examples=['给我纽约当前的空气质量报告'],
          )
      
          agent_card = AgentCard(
              name='天气 Agent',
              description='提供天气相关的查询功能',
              url=f'http://{host}:{port}',
              version='1.0.0',
              defaultInputModes=['text'],
              defaultOutputModes=['text'],
              capabilities=capabilities,
              skills=[forecast_skill, air_quality_skill],
          )
      
          request_handler = DefaultRequestHandler(
              agent_executor=WeatherAgentExecutor(),
              task_store=InMemoryTaskStore(),
          )
          server = A2AStarletteApplication(
              agent_card=agent_card, http_handler=request_handler
          )
          import uvicorn
      
          uvicorn.run(server.build(), host=host, port=port)
      
      
      if __name__ == '__main__':
          main("127.0.0.1", 10000)
    • agent_executor.py

      python 复制代码
      from a2a.server.agent_execution import AgentExecutor, RequestContext
      from a2a.server.events import EventQueue
      from a2a.types import (Part, Task, TextPart, UnsupportedOperationError)
      from a2a.utils import (completed_task, new_artifact)
      from a2a.utils.errors import ServerError
      
      
      class WeatherAgentExecutor(AgentExecutor):
      
          async def execute(
              self,
              context: RequestContext,
              event_queue: EventQueue,
          ) -> None:
              text="""未来 3 天的天气如下:1. 明天(2025年6月1日):晴天;2. 后天(2025年6月2日):小雨;3. 大后天(2025年6月3日):大雨。"""
              event_queue.enqueue_event(
                  completed_task(
                      context.task_id,
                      context.context_id,
                      [new_artifact(parts=[Part(root=TextPart(text=text))], name="天气查询结果")],
                      [context.message],
                  )
              )
      
          async def cancel(
              self, request: RequestContext, event_queue: EventQueue
          ) -> Task | None:
              raise ServerError(error=UnsupportedOperationError())

注册天气Agent到Agent调度平台

Wireshark网络抓包分析A2A协议

Wireshark抓取本地回环的包,过滤:http and tcp.dstport == 10000,查看包的内容:

json 复制代码
{
    "capabilities": {
        "streaming": false
    },
    "defaultInputModes": [
        "text"
    ],
    "defaultOutputModes": [
        "text"
    ],
    "description": "提供天气相关的查询功能",
    "name": "天气 Agent",
    "preferredTransport": "JSONRPC",
    "protocolVersion": "0.3.0",
    "skills": [
        {
            "description": "给出某地的天气预告",
            "examples": [
                "给我纽约未来 7 天的天气预告"
            ],
            "id": "天气预告",
            "name": "天气预告",
            "tags": [
                "天气",
                "预告"
            ]
        },
        {
            "description": "给出某地当前时间的空气质量报告,不做预告",
            "examples": [
                "给我纽约当前的空气质量报告"
            ],
            "id": "空气质量报告",
            "name": "空气质量报告",
            "tags": [
                "空气",
                "质量"
            ]
        }
    ],
    "url": "http://127.0.0.1:10000",
    "version": "1.0.0"
}
  • skills:是Agent所包含的技能列表

代理Agent示例请求JSON

json 复制代码
{
    "id": "90320f73-d6be-462f-bf3d-020ab5043924",
    "jsonrpc": "2.0",
    "method": "message/send",
    "params": {
        "configuration": {
            "acceptedOutputModes": [
                "text",
                "text/plain",
                "image/png"
            ]
        },
        "message": {
            "contextId": "f0b3fa91-b771-47c5-9ded-cd840e770d1b",
            "kind": "message",
            "messageId": "66675023-04aa-4584-8452-746f271f1c9a",
            "parts": [
                {
                    "kind": "text",
                    "text": "西雅图明天的天气怎么样?"
                }
            ],
            "role": "user"
        }
    }
}

Agent示例返回JSON

json 复制代码
{
    "id": "90320f73-d6be-462f-bf3d-020ab5043924",
    "jsonrpc": "2.0",
    "result": {
        "artifacts": [
            {
                "artifactId": "8401aa09-cb36-4d5c-8d75-ad8308d2c6c4",
                "description": "",
                "name": "天气查询结果",
                "parts": [
                    {
                        "kind": "text",
                        "text": "未来 3 天的天气如下:1. 明天(2025年6月1日):晴天;2. 后天(2025年6月2日):小雨;3. 大后天(2025年6月3日):大雨。"
                    }
                ]
            }
        ],
        "contextId": "f0b3fa91-b771-47c5-9ded-cd840e770d1b",
        "history": [
            {
                "contextId": "f0b3fa91-b771-47c5-9ded-cd840e770d1b",
                "kind": "message",
                "messageId": "66675023-04aa-4584-8452-746f271f1c9a",
                "parts": [
                    {
                        "kind": "text",
                        "text": "西雅图明天的天气怎么样?"
                    }
                ],
                "role": "user",
                "taskId": "e3ee4048-afca-47a9-9ba4-7e2553726c20"
            }
        ],
        "id": "e3ee4048-afca-47a9-9ba4-7e2553726c20",
        "kind": "task",
        "status": {
            "state": "completed"
        }
    }
}

Powershell请求天气Agent,查看天气情况

shell 复制代码
$body = @{
  jsonrpc = "2.0"
  id      = "req-1"
  method  = "message/send"
  params  = @{
    message = @{
      messageId = "msg-1"
      role      = "user"
      parts     = @(
        @{
          kind = "text"
          text = "河北保定今天天气如何?"
        }
      )
    }
  }
} | ConvertTo-Json -Depth 10

$r = Invoke-WebRequest -Uri "http://127.0.0.1:10000/" -Method Post -ContentType "application/json" -Body $body

执行返回结果:

json 复制代码
{
    "id": "req-1",
    "jsonrpc": "2.0",
    "result": {
        "artifacts": [
            {
                "artifactId": "e7e62b53-b4ed-472d-9641-060db8fab368",
                "description": "",
                "name": "天气查询结果",
                "parts": [
                    {
                        "kind": "text",
                        "text": "未来 3 天的天气如下:1. 明天(2025年6月1日):晴天;2. 后天(2025年6月2日):小雨;3. 大后天(2025年6月3日):大雨。"
                    }
                ]
            }
        ],
        "contextId": "9008169e-39c5-4ecb-86e2-f4c14f0c6dcb",
        "history": [
            {
                "contextId": "9008169e-39c5-4ecb-86e2-f4c14f0c6dcb",
                "kind": "message",
                "messageId": "msg-1",
                "parts": [
                    {
                        "kind": "text",
                        "text": "???????????"
                    }
                ],
                "role": "user",
                "taskId": "f04578fe-283d-42ed-b2bd-de8986926a83"
            }
        ],
        "id": "f04578fe-283d-42ed-b2bd-de8986926a83",
        "kind": "task",
        "status": {
            "state": "completed"
        }
    }
}

特定词解析

  • Agent Card: 用于描述Agent基本信息的一段JSON,访问地址:(http://127.0.0.1:10000/.well-known/agent-card.json)
  • message: 用于在Agent之间交换信息用的结构体
  • Artifact: 被调用的Agent的产物
  • Part: Message或者Artifact里面的一段信息
  • Task: 被调用Agent根据请求所生成的任务
  • 调度Agent: A2A Client(Client Agent)
  • 天气Agent: A2A Server(Remote Agent)

不同阶段

  • Agent注册阶段

  • Agent问答阶段

多Agent调度流程

相关推荐
安逸sgr3 小时前
Hermes Agent + Obsidian 打造第二大脑(六):分层记忆系统的设计逻辑——L0/L1/L2/L3 四层记忆详解
数据库·agent·知识库·hermes·hermesagent
潇楠Web3哨兵4 小时前
《「潇楠WEB3哨兵」Agent 全栈架构:从记忆系统到技能扩展,桌面端 AI 投研助手的完整技术实现》
web3·agent
Baihai_IDP4 小时前
为什么 AI Agent 重新爱上了文件系统(Filesystems)
人工智能·llm·agent
明月(Alioo)4 小时前
给 AI Agent 装上“大脑“:Java语言中Code Interpreter 的设计与实现
java·ai·agent
薛定谔的猫3695 小时前
AI Agent 与 MCP 协议:构建标准化大模型交互的新范式
ai·llm·agent·mcp·software engineering
风雅GW5 小时前
多 Agent 系统设计参考框架(OpenClaw 实现版)
人工智能·ai·agent·openclaw
Vastog5 小时前
skill最佳实践
agent·ai应用开发
维元码簿6 小时前
Claude Code 深度拆解:CLI 交互模块 1 — REPL 架构
ai·agent·claude code·ai coding
Resistance丶未来7 小时前
TradingAgents 多智能体交易框架深度评测
gpt·大模型·llm·agent·claude·多智能体·trading agents