{"record":{"id":"4880af725b2375d7","repo":"datawhalechina/hello-agents","slug":"error-4880af","errorCode":null,"errorMessage":"除数不能为零","messagePattern":"除数不能为零","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"Co-creation-projects/AstrumPush-Smart-Recipe-Agent/protocol_tools.py","lineNumber":229,"sourceCode":"            def add(a: float, b: float) -> float:\n                \"\"\"加法计算器\"\"\"\n                return a + b\n\n            @server.tool()\n            def subtract(a: float, b: float) -> float:\n                \"\"\"减法计算器\"\"\"\n                return a - b\n\n            @server.tool()\n            def multiply(a: float, b: float) -> float:\n                \"\"\"乘法计算器\"\"\"\n                return a * b\n\n            @server.tool()\n            def divide(a: float, b: float) -> float:\n                \"\"\"除法计算器\"\"\"\n                if b == 0:\n                    raise ValueError(\"除数不能为零\")\n                return a / b\n\n            @server.tool()\n            def greet(name: str = \"World\") -> str:\n                \"\"\"友好问候\"\"\"\n                return f\"Hello, {name}! 欢迎使用 HelloAgents MCP 工具！\"\n\n            @server.tool()\n            def get_system_info() -> dict:\n                \"\"\"获取系统信息\"\"\"\n                import platform\n                import sys\n                return {\n                    \"platform\": platform.system(),\n                    \"python_version\": sys.version,\n                    \"server_name\": \"HelloAgents-BuiltinServer\",\n                    \"tools_count\": 6\n                }","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/AstrumPush-Smart-Recipe-Agent/protocol_tools.py#L211-L247","documentation":"A ValueError raised by the divide MCP tool in protocol_tools.py when its b parameter equals 0. It is a deliberate input-validation guard inside the built-in MCP server's calculator tool set (add/subtract/multiply/divide), following the standard practice of refusing division by zero rather than returning inf or raising ZeroDivisionError deep in Python.","triggerScenarios":"An MCP client invokes the 'divide' tool with arguments {'a': <number>, 'b': 0} or {'b': 0.0}. The tool checks b == 0 before computing a / b, so any zero denominator — including -0.0 and integer 0 — triggers it. The error propagates back to the client as a tool-execution error (FastMCP wraps it in the MCP error response).","commonSituations":"LLM-driven agents passing a computed denominator that evaluates to zero; clients forwarding user input without validation; unit tests probing tool edge cases; downstream formula bugs that occasionally produce 0 denominators.","solutions":["Validate the denominator client-side before invoking divide; return a domain-appropriate result (None, inf, or an error message) instead of calling the tool with 0","If zero denominators are legitimate in your workflow, wrap the call in a conditional and handle the case locally","Catch the tool error in the agent loop and feed a corrective message back to the LLM so it adjusts arguments","For batch jobs, pre-filter or sanitize argument dicts to reject zero denominators early"],"exampleFix":"# before\nresult = await client.call_tool('divide', {'a': x, 'b': y})\n\n# after\nif y == 0:\n    result = None  # or float('inf'), per your domain\nelse:\n    result = await client.call_tool('divide', {'a': x, 'b': y})","handlingStrategy":"validation","validationCode":"def safe_divide_args(a: float, b: float) -> dict | None:\n    if b == 0 or b == -0.0:\n        return None\n    return {'a': a, 'b': b}\n\nargs = safe_divide_args(x, y)\nif args is None:\n    return None  # caller-defined semantics for zero denominator","typeGuard":"def is_dividable(b: float) -> bool:\n    return b != 0  # covers 0, 0.0, -0.0 (and numpy zeros via !=)","tryCatchPattern":"try:\n    result = await client.call_tool('divide', {'a': a, 'b': b})\nexcept ToolError as e:\n    if '除数不能为零' in str(e):\n        return None  # expected domain case, not a crash\n    raise","preventionTips":["Check denominators client-side before invoking the divide tool","In agent prompts, state explicitly that divide requires a nonzero b","Log zero-denominator attempts — they often reveal upstream formula bugs"],"tags":["mcp","validation","division-by-zero","fastmcp","calculator"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}