datawhalechina/hello-agents · warning · ValueError
除数不能为零
Error message
除数不能为零
What it means
Plain ValueError raised by the divide tool of the my_mcp_server.py MCP server when argument b is 0. It is an intentional domain validation, documented in the tool's docstring; the value flows from the LLM's tool-call arguments through the MCP framework.
Source
Thrown at code/chapter10/my_mcp_server.py:85
@mcp.tool()
def divide(a: float, b: float) -> float:
"""
除法计算器
Args:
a: 被除数
b: 除数
Returns:
两数之商
Raises:
ValueError: 当除数为0时
"""
if b == 0:
raise ValueError("除数不能为零")
return a / b
# ==================== 文本处理工具 ====================
@mcp.tool()
def reverse_text(text: str) -> str:
"""
反转文本
Args:
text: 要反转的文本
Returns:
反转后的文本
"""
return text[::-1]
View on GitHub (pinned to 606a07d341)
Solutions
- Validate/catch in the caller: guard b != 0 before invoking divide
- If the LLM drives the call, add a note in the tool description that b must be non-zero so the model avoids it
- In an agent loop, catch ValueError and feed the message back to the model to self-correct
Example fix
# before
result = divide(a, b) # ValueError('除数不能为零') when b == 0
# after
if b == 0:
return "Error: divisor must not be zero"
result = divide(a, b) Defensive patterns
Strategy: validation
Validate before calling
def safe_divide(a: float, b: float):
if b == 0:
return None # or an error string for LLM consumption
return divide(a, b) Type guard
def is_dividable(b) -> bool:
return isinstance(b, (int, float)) and b != 0 Try / catch
try:
result = divide(a, b)
except ValueError as e:
result = f'error: {e}' # feed back to the LLM for self-correction Prevention
- State constraints (b != 0) in the tool docstring so LLM callers avoid them
- Validate numeric arguments at the boundary before tool dispatch
- Convert domain ValueErrors into tool-result messages in agent loops instead of crashing
When it happens
Trigger: An LLM client (or direct caller) invokes the divide tool with b=0; a model hallucinating a zero denominator from user input like 'what is 5 divided by 0'.
Common situations: Testing the MCP server with a zero divisor; an agent chain passing unvalidated numeric arguments to the tool.
Related errors
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/a00943e5405a6f52.
Report an issue: GitHub.