1. 学习
  2. /
  3. 课程
  4. /
  5. Model Context Protocol (MCP) 入门

Connected

练习

构建消息并调用 LLM

现在您已经创建了 get_context_from_mcp(user_query) 辅助函数用于返回资源文本和提示文本,是时候把这些信息传给 LLM 了!

货币服务器、get_context_from_mcp()、get_tools_from_mcp()、call_mcp_tool() 以及 Claude 客户端都已在后台设置完成。您需要完成用于构建提示、调用模型,并处理直接消息或工具调用的函数。我们为您提供了含糊与不含糊的两种用户输入,以检验您的 MCP 提示是否带来了差异!

说明

100 XP
  • 在第 37 行,通过拼接 prompt_text、字符串 "\n\nSupported currencies:\n",以及 resource_text 来构建 full_prompt。
  • 在第 47 行,将 full_prompt(作为用户消息内容)和 anthropic_tools 列表一起发送给模型。
  • 在第 52-55 行,如果响应的 stop_reason 为 "end_turn",返回 str(text)。
  • 在第 58-60 行,如果响应的 stop_reason 为 "tool_use",将工具使用块的 .name 和 .input 传递给 call_mcp_tool()。