多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调度流程

相关推荐
七夜zippoe6 分钟前
Token 账单翻了 10 倍:Agent 成本失控的排查过程与优化实录
ai·agent·token·成本失控
泯泷43 分钟前
为什么 RL 比 SFT 更会"举一反三"?
人工智能·llm·agent
民乐团扒谱机1 小时前
Agent-Skills 新手入门与实战部署指南
人工智能·ai·agent·skill
泯泷3 小时前
模型"缺课"了:为什么该补底座,而不是急着上 RL
llm·agent·强化学习
小林ixn3 小时前
别再全量塞历史了:用 Redis 给 Agent 装上「短期记忆」
redis·agent
初学AI的小高3 小时前
Agent 工具太多怎么办:BM25 检索、依赖补全与按需加载实战
后端·agent
发现你走远了4 小时前
05-权限不是开关:工具调用如何在放行、拦截与追问之间做决定
人工智能·agent·claude
泯泷4 小时前
RL 训练真正难的不是算法:环境、奖励与样本效率
llm·agent
威哥爱编程7 小时前
模拟面试智能体开发实战 07:答完一题就点评一题,结构化输出给分建议,让每道题都变成复盘
python·agent·harmonyos
威哥爱编程7 小时前
模拟面试智能体开发实战 08:总评报告先过三道闸,防泄题防夸大防错建议,才敢发到候选人手上
python·agent·harmonyos