datawhalechina/hello-agents · error · ValueError

除数不能为零

Error message

除数不能为零

What it means

ValueError raised by the 'divide' tool of the built-in FastMCP server in protocol_tools.py when the b argument is 0. It is an intentional domain error surfaced through MCP so the calling agent receives a clear message instead of a ZeroDivisionError traceback.

Source

Thrown at Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/protocol_tools.py:217

            def add(a: float, b: float) -> float:
                """加法计算器"""
                return a + b

            @server.tool()
            def subtract(a: float, b: float) -> float:
                """减法计算器"""
                return a - b

            @server.tool()
            def multiply(a: float, b: float) -> float:
                """乘法计算器"""
                return a * b

            @server.tool()
            def divide(a: float, b: float) -> float:
                """除法计算器"""
                if b == 0:
                    raise ValueError("除数不能为零")
                return a / b

            @server.tool()
            def greet(name: str = "World") -> str:
                """友好问候"""
                return f"Hello, {name}! 欢迎使用 HelloAgents MCP 工具!"

            @server.tool()
            def get_system_info() -> dict:
                """获取系统信息"""
                import platform
                import sys
                return {
                    "platform": platform.system(),
                    "python_version": sys.version,
                    "server_name": "HelloAgents-BuiltinServer",
                    "tools_count": 6
                }

View on GitHub (pinned to 606a07d341)

Solutions

  1. Guard the denominator at the call site: skip, clamp, or ask the model to recompute when it is 0.
  2. Fix swapped arguments if the numerator was intended as the denominator.
  3. Catch the ValueError/tool error in the MCP client and feed the message back to the agent for self-correction.

Example fix

# before
result = divide(a=total, b=count)  # count == 0 -> error

# after
if count == 0:
    result = 0.0  # or handle empty case explicitly
else:
    result = divide(a=total, b=count)
Defensive patterns

Strategy: type-guard

Validate before calling

def safe_divide_args(a: float, b: float) -> bool:
    return b != 0 and abs(b) > 1e-12  # also guard near-zero floats

Type guard

def denominator_safe(b: float) -> bool:
    return isinstance(b, (int, float)) and b != 0

Try / catch

try:
    result = await client.call_tool('divide', {'a': total, 'b': count})
except Exception as e:  # MCP surfaces tool errors as exceptions/results
    if '除数不能为零' in str(e) or 'zero' in str(e).lower():
        result = None  # handle empty aggregate explicitly
    else:
        raise

Prevention

When it happens

Trigger: Calling the builtin MCP divide tool with b=0 (including 0.0, and -0.0) or a computed denominator that evaluates to zero; LLM tool calls passing the wrong positional argument into b.

Common situations: Agent arithmetic chains (divide then average) where an intermediate result is 0; argument-order mixups swapping numerator and denominator; unit tests probing error handling of MCP tools.

Related errors


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