{"record":{"id":"a00943e5405a6f52","repo":"datawhalechina/hello-agents","slug":"error-a00943","errorCode":null,"errorMessage":"除数不能为零","messagePattern":"除数不能为零","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"code/chapter10/my_mcp_server.py","lineNumber":85,"sourceCode":"\n\n@mcp.tool()\ndef divide(a: float, b: float) -> float:\n    \"\"\"\n    除法计算器\n    \n    Args:\n        a: 被除数\n        b: 除数\n    \n    Returns:\n        两数之商\n    \n    Raises:\n        ValueError: 当除数为0时\n    \"\"\"\n    if b == 0:\n        raise ValueError(\"除数不能为零\")\n    return a / b\n\n\n# ==================== 文本处理工具 ====================\n\n@mcp.tool()\ndef reverse_text(text: str) -> str:\n    \"\"\"\n    反转文本\n    \n    Args:\n        text: 要反转的文本\n    \n    Returns:\n        反转后的文本\n    \"\"\"\n    return text[::-1]\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/code/chapter10/my_mcp_server.py#L67-L103","documentation":"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.","triggerScenarios":"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'.","commonSituations":"Testing the MCP server with a zero divisor; an agent chain passing unvalidated numeric arguments to the tool.","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"],"exampleFix":"# before\nresult = divide(a, b)  # ValueError('除数不能为零') when b == 0\n\n# after\nif b == 0:\n    return \"Error: divisor must not be zero\"\nresult = divide(a, b)","handlingStrategy":"validation","validationCode":"def safe_divide(a: float, b: float):\n    if b == 0:\n        return None  # or an error string for LLM consumption\n    return divide(a, b)","typeGuard":"def is_dividable(b) -> bool:\n    return isinstance(b, (int, float)) and b != 0","tryCatchPattern":"try:\n    result = divide(a, b)\nexcept ValueError as e:\n    result = f'error: {e}'  # feed back to the LLM for self-correction","preventionTips":["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"],"tags":["mcp","validation","arithmetic","math"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}