datawhalechina/hello-agents · error · RuntimeError

未发现天气 MCP 子工具,请检查服务脚本、依赖和启动日志。

Error message

未发现天气 MCP 子工具,请检查服务脚本、依赖和启动日志。

What it means

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.

Source

Thrown at code/chapter10/14_weather_agent.py:30

    """创建天气助手"""
    llm = HelloAgentsLLM()

    assistant = SimpleAgent(
        name="天气助手",
        llm=llm,
        system_prompt="""你是天气助手,可以查询城市天气。
使用 mcp_get_weather 工具查询天气,支持中文城市名。
"""
    )

    # 添加天气 MCP 工具
    server_script = os.path.join(os.path.dirname(__file__), "14_weather_mcp_server.py")
    weather_tool = MCPTool(server_command=["python", server_script])

    # 显式展开并注册 MCP 子工具
    expanded_tools = weather_tool.get_expanded_tools()
    if not expanded_tools:
        raise RuntimeError("未发现天气 MCP 子工具,请检查服务脚本、依赖和启动日志。")

    for expanded_tool in expanded_tools:
        assistant.add_tool(expanded_tool)

    return assistant


def demo():
    """演示"""
    assistant = create_weather_assistant()

    print("\n查询北京天气:")
    response = assistant.run("北京今天天气怎么样?")
    print(f"回答: {response}\n")


def interactive():
    """交互模式"""

View on GitHub (pinned to 606a07d341)

Solutions

  1. Run `python 14_weather_mcp_server.py` standalone and confirm it starts and lists tools without errors
  2. Install server dependencies: pip install mcp (or the project's requirements.txt)
  3. Verify server_script path resolves: it must be in the same directory as 14_weather_agent.py
  4. Run the agent with the same Python interpreter/venv that has the MCP dependencies installed

Example fix

# before
weather_tool = MCPTool(server_command=["python", server_script])
expanded_tools = weather_tool.get_expanded_tools()  # [] -> RuntimeError

# after: pin the interpreter and surface server startup errors
import sys
weather_tool = MCPTool(server_command=[sys.executable, server_script])
expanded_tools = weather_tool.get_expanded_tools()
if not expanded_tools:
    raise RuntimeError("Weather MCP server exposed no tools; check its startup log above.")
Defensive patterns

Strategy: validation

Validate before calling

import os, subprocess, sys

script = os.path.join(os.path.dirname(__file__), '14_weather_mcp_server.py')
assert os.path.exists(script), f'missing MCP server script: {script}'
r = subprocess.run([sys.executable, '-c', f'import mcp'], capture_output=True)
assert r.returncode == 0, 'install the mcp package first'

Try / catch

try:
    tools = MCPTool(server_command=[sys.executable, script]).get_expanded_tools()
except RuntimeError as e:
    raise SystemExit(f'Weather MCP init failed: {e}. Run the server script directly to debug.')

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/e92799b8d9da47a3. Report an issue: GitHub.