{"record":{"id":"e92799b8d9da47a3","repo":"datawhalechina/hello-agents","slug":"mcp","errorCode":null,"errorMessage":"未发现天气 MCP 子工具，请检查服务脚本、依赖和启动日志。","messagePattern":"未发现天气 MCP 子工具，请检查服务脚本、依赖和启动日志。","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"code/chapter10/14_weather_agent.py","lineNumber":30,"sourceCode":"    \"\"\"创建天气助手\"\"\"\n    llm = HelloAgentsLLM()\n\n    assistant = SimpleAgent(\n        name=\"天气助手\",\n        llm=llm,\n        system_prompt=\"\"\"你是天气助手，可以查询城市天气。\n使用 mcp_get_weather 工具查询天气，支持中文城市名。\n\"\"\"\n    )\n\n    # 添加天气 MCP 工具\n    server_script = os.path.join(os.path.dirname(__file__), \"14_weather_mcp_server.py\")\n    weather_tool = MCPTool(server_command=[\"python\", server_script])\n\n    # 显式展开并注册 MCP 子工具\n    expanded_tools = weather_tool.get_expanded_tools()\n    if not expanded_tools:\n        raise RuntimeError(\"未发现天气 MCP 子工具，请检查服务脚本、依赖和启动日志。\")\n\n    for expanded_tool in expanded_tools:\n        assistant.add_tool(expanded_tool)\n\n    return assistant\n\n\ndef demo():\n    \"\"\"演示\"\"\"\n    assistant = create_weather_assistant()\n\n    print(\"\\n查询北京天气：\")\n    response = assistant.run(\"北京今天天气怎么样？\")\n    print(f\"回答: {response}\\n\")\n\n\ndef interactive():\n    \"\"\"交互模式\"\"\"","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/code/chapter10/14_weather_agent.py#L12-L48","documentation":"RuntimeError raised by create_weather_assistant() when MCPTool.get_expanded_tools() returns an empty list, meaning the weather MCP server subprocess (14_weather_mcp_server.py) started but exposed no tools — or failed to start at all. Expansion is how agno/MCPTool flattens a server's tool list into individually registered tools; empty expansion = unusable tool.","triggerScenarios":"14_weather_mcp_server.py missing from the same directory; the server script crashing on import (missing `mcp` or `fastmcp` package, wrong Python interpreter); server taking too long to initialize so the tool list is queried before readiness; the server exposing zero @mcp.tool() functions.","commonSituations":"Running the agent from a different working directory so the relative script path breaks; not installing requirements (pip install mcp); using a venv where the server script's dependencies are absent; MCP protocol version mismatch between client and server packages.","solutions":["Run `python 14_weather_mcp_server.py` standalone and confirm it starts and lists tools without errors","Install server dependencies: pip install mcp (or the project's requirements.txt)","Verify server_script path resolves: it must be in the same directory as 14_weather_agent.py","Run the agent with the same Python interpreter/venv that has the MCP dependencies installed"],"exampleFix":"# before\nweather_tool = MCPTool(server_command=[\"python\", server_script])\nexpanded_tools = weather_tool.get_expanded_tools()  # [] -> RuntimeError\n\n# after: pin the interpreter and surface server startup errors\nimport sys\nweather_tool = MCPTool(server_command=[sys.executable, server_script])\nexpanded_tools = weather_tool.get_expanded_tools()\nif not expanded_tools:\n    raise RuntimeError(\"Weather MCP server exposed no tools; check its startup log above.\")","handlingStrategy":"validation","validationCode":"import os, subprocess, sys\n\nscript = os.path.join(os.path.dirname(__file__), '14_weather_mcp_server.py')\nassert os.path.exists(script), f'missing MCP server script: {script}'\nr = subprocess.run([sys.executable, '-c', f'import mcp'], capture_output=True)\nassert r.returncode == 0, 'install the mcp package first'","typeGuard":null,"tryCatchPattern":"try:\n    tools = MCPTool(server_command=[sys.executable, script]).get_expanded_tools()\nexcept RuntimeError as e:\n    raise SystemExit(f'Weather MCP init failed: {e}. Run the server script directly to debug.')","preventionTips":["Smoke-test the MCP server standalone before wiring it into the agent","Pin dependencies (mcp/fastmcp) in requirements.txt to avoid protocol drift","Launch the subprocess with the same interpreter (sys.executable), never a bare 'python'"],"tags":["mcp","subprocess","dependencies","agent"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}