## 问题 商品搜索时工具名丢失,导致 404 错误: ``` HTTP Request: POST http://product_mcp:8004/tools/ "HTTP/1.1 404 Not Found" ``` URL 应该是 `/tools/search_products` 但实际是 `/tools/`(工具名丢失) ## 根本原因 当 LLM 返回带 ```json``` 代码块格式的 JSON 时: ``` ```json { "action": "call_tool", "tool_name": "search_products", "arguments": {"keyword": "ring"} } ``` ``` 解析逻辑处理后: 1. 移除 ```` → 得到 `json\n{\n...` 2. 移除 `json` → 得到 `\n{\n...` 3. 内容以换行符开头,不是 `{` 4. 被误判为非 JSON 格式(`tool_name\n{args}`) 5. 按换行符分割,第一行为空 → `tool_name = ""` ## 解决方案 **第 189 行**:添加 `content.strip()` 去除前后空白 ```python if content.startswith("```"): content = content.split("```")[1] if content.startswith("json"): content = content[4:] # Remove leading/trailing whitespace after removing code block markers content = content.strip() # ← 新增 ``` ## 额外改进 **第 217-224 行**:添加工具调用日志 ```python logger.info( "Product agent calling tool", tool_name=tool_name, arguments=arguments, conversation_id=state["conversation_id"] ) ``` 便于调试工具调用问题。 ## 测试验证 修复前: ``` tool_name = "" (空字符串) URL: /tools/ (缺少工具名) ``` 修复后: ``` tool_name = "search_products" (正确) URL: /tools/search_products (完整路径) ``` Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
13 KiB
13 KiB