学习极客时间 MCP 和 A2A 协议时,课后习题 + 执行代码中遇到的问题
课后习题
1、请你研究一下 Agents 目录中的外部 Agents,每一个具有什么能力,能完成什么任务。

各 Agent 详细说明
Agent Card 中的 Description 和 Skill 有对应的说明。

1.1 ag2 Agent
功能描述:能够从YouTube视频中提取隐藏字幕(closed captions)和文本转录(transcripts)的AI代理。该代理提供原始转录数据,可用于进一步处理。
技能:从YouTube视频中获取隐藏字幕/文本转录。
示例:
'Extract the transcript from this YouTube video: https://www.youtube.com/watch?v=dQw4w9WgXcQ',
'Download the captions for this YouTube tutorial',
1. "提取该YouTube视频的字幕文本:[https://www.youtube.com/watch?v=dQw4w9WgXcQ"](https://www.youtube.com/watch?v=dQw4w9WgXcQ)
2. "下载此YouTube教程的隐藏字幕"
1.2 crewai

功能描述:按需生成惊艳的高质量图像,并利用强大的编辑能力对视觉内容进行修改、增强或彻底改造。
技能:同上。
示例:
Generate a photorealistic image of raspberry lemonade
(生成一张树莓柠檬水的照片级真实感图像)
1.3 google_adk

功能描述:该代理根据报销的金额和目的为员工处理报销流程。
技能:根据报销金额和用途,协助用户完成报销流程。
示例:
Can you reimburse me $20 for my lunch with the clients?
(能否报销我与客户午餐的20美元费用?)
1.4 llama_index_file_chat

功能描述:解析文件,然后使用解析的内容作为上下文与用户聊天。
技能:解析用户文件内容,并基于解析后的内容作为上下文与用户进行对话。
示例:
What does this file talk about?
1.5 marvin

功能描述:使用 Marvin 的提取功能从文本中提取结构化的联系信息。
技能:同上。
示例:
"My name is John Doe, email: john@example.com, phone: (555) 123-4567"
1.6 mindsdb

功能描述:允许您使用 MindsDB 的功能通过自然语言查询与数据交互的代理。以对话方式查询和分析您的数据库。
技能:通过MindsDB实现自然语言交互的数据查询分析代理:用对话方式探索数据库
示例:
'What TABLES are in my database?',
'What are some good queries to run on my data?'
1.7 semantickernel Agent

功能描述:基于 Semantic Kernel 的旅行社,提供全面的旅行计划服务,包括货币兑换和个性化活动计划。
技能:负责全面的行程规划,包括货币兑换、行程制定、观光、餐饮推荐和活动预订,并使用 Frankfurter API 进行货币转换。”
示例:
'Plan a budget-friendly day trip to Seoul including currency exchange.',
"What's the exchange rate and recommended itinerary for visiting Tokyo?",
2、请你在这个应用程序中,添加更多的 Agent,让多个 Agent 协作完成更为复杂的任务,比如发票的报销工作。


后面一直卡在 working 状态,还未找到处理方案。
INFO: 127.0.0.1:58784 - "POST / HTTP/1.1" 200 OK
ERROR:task_manager:An error occurred while streaming the response: Failed to parse the parameter date: str | None = None of function create_request_form for automatic function calling. Automatic function calling works best with simpler function signature schema,consider manually parse your function declaration for function create_request_form.
遇到的问题
Google API Key 申请地址
最开始申请的地址如下:
https://cloud.google.com/docs/authentication/api-keys?hl=zh-cn
别点这个“免费开始使用吧” 按钮

往下滑,会看到这个,点击进入“凭据”页面,就可以创建自己的 API 了。


启动 A2A Demo 项目,下载包时可能会超时
自动安装依赖包,如果超时,可以重新运行。

启动完成。

403 Generative Language API has not been used
对话时,卡在 Working

host agent 报错信息如下:

原因
未启用 Gemini API 功能
解决方案
启用 Gemini API 功能。
链接地址如下:
https://console.cloud.google.com/apis/library/generativelanguage.googleapis.com?project=silicon-brace-463315-c5&inv=1&invt=Ab0dbA
点击启用。

问题 400 FAILED_PRECONDITION,User location is not supported for the API use
google.genai.errors.ClientError: 400 FAILED_PRECONDITION. {'error': {'code': 400, 'message': 'User location is not supported for the API use.', 'status': 'FAILED_PRECONDITION'}}

原因
本地代理的区域不支持使用 Google API。
解决方案
需要切换下代理地址,如香港切换到新加坡
再次测试对话功能,正常返回消息。

报错 Agent knowledge_agent not found
当我提问 比萨斜塔的倾斜角度问题时,AI 卡在 Working 状态。

host agent 报错信息如下:
File "/Users/wukong/00.Study/04-geektime/02.code/a2a-in-action/hosts/multiagent/host_agent.py", line 151, in send_task
raise ValueError(f'Agent {agent_name} not found')
ValueError: Agent knowledge_agent not found
意思就是未找到 knowledge_agent,说明缺少 “knowledge”的智能体。
原因
remote agent 列表中没有 knowledge_agent。
如果你加了一个汇率换算的 Agent,对话时,提示信息也会不一样。AI 会告诉我们只有一个 Currency Agent,无法回答比萨斜塔的倾斜角度问题。

解决方案
需要加下 knowledge_agent。因为 Demo 中也没有这种 Agent,所以需要自己写一个或找一个这种 Agent。
报错 400: Expected response header Content-Type to contain 'text/event-stream', got ''
调用汇率换算的 Agent 时,卡在 Working 状态,查看 host agent 的打印窗口,显示如下报错信息:
common.types.A2AClientHTTPError: HTTP Error 400: Expected response header Content-Type to contain 'text/event-stream', got ''

原因
汇率换算的 Remote Agent 有问题。
可以等到该 Agent 超时 后,会打印如下信息:
INFO:common.server.task_manager:Upserting task b86b295e-cc2b-4fee-a5c8-b8e6cdaafa57
WARNING:langchain_google_genai.chat_models:Retrying langchain_google_genai.chat_models._chat_with_retry.<locals>._chat_with_retry in 2.0 seconds as it raised RetryError: Timeout of 600.0s exceeded, last exception: 503 failed to connect to all addresses; last error: UNAVAILABLE: ipv4:142.250.107.95:443: Failed to connect to remote host: Timeout occurred: FD shutdown.
试下这个请求是否可以通,
SH curl -v https://142.250.69.170:443
我最开始切换过多个代理,都是不通的。然后我将电脑连接的联通 WIFI 切换到手机的移动热点,就可以访问了。

解决方案
切换网络。
OpenAI API 报错 429 Too Many Requests
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 429 Too Many Requests"
ERROR:agents.semantickernel.task_manager:Streaming agent encountered error: ("<class 'semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion'> service failed to complete the prompt", RateLimitError("Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}"))
网络被限制了。
调试技巧
可以用 print 方法打印请求参数和响应参数。
比如可以在汇率换算的 Agent 入口处,打印入参和响应参数,这样可以判断请求是否进来了,以及参数是否符合预期。

汇率换算 Agent 的打印结果:
