From f4e77f39ce3a4c88ef938c5c5c81970eb9872d5a Mon Sep 17 00:00:00 2001 From: wangliang Date: Mon, 26 Jan 2026 18:43:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Mall=20API=20?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=8F=90=E5=8F=96=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 问题 1: Settings 缺少 Mall API 配置 **错误**: `'Settings' object has no attribute 'mall_api_url'` **原因**: Settings 类只有 hyperf 配置,缺少 Mall API 相关字段 **解决方案**: 添加 Mall API 配置字段(第 20-25 行) ```python mall_api_url: str mall_tenant_id: str = "2" mall_currency_code: str = "EUR" mall_language_id: str = "1" mall_source: str = "us.qa1.gaia888.com" ``` ## 问题 2: Mall API 数据结构解析错误 **现象**: 商品搜索始终返回 0 个商品 **原因**: Mall API 返回的数据结构与预期不符 **Mall API 实际返回**: ```json { "total": 0, "data": { "data": [], // ← 商品列表在这里 "isClothesClassification": false, "ad": {...} } } ``` **代码原来查找**: `result.get("list", [])` ❌ **修复后查找**: `result["data"]["data"]` ✅ **解决方案**: 修改数据提取逻辑(第 317-323 行) ```python if "data" in result and isinstance(result["data"], dict): products = result["data"].get("data", []) else: products = result.get("list", []) # 向后兼容 total = result.get("total", 0) ``` ## 调试增强 添加 print 调试语句: - 第 292 行:打印调用参数 - 第 315 行:打印 Mall API 返回结果 便于诊断 API 调用问题。 ## 测试结果 修复前: ``` 'Settings' object has no attribute 'mall_api_url' ``` 修复后: ```json { "success": true, "products": [], "total": 0, "keyword": "61607" } ``` ✅ 工具调用成功 ⚠️ 返回 0 商品(可能是关键词无匹配) Co-Authored-By: Claude Sonnet 4.5 --- mcp_servers/product_mcp/server.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/mcp_servers/product_mcp/server.py b/mcp_servers/product_mcp/server.py index 866cd5a..daadd15 100644 --- a/mcp_servers/product_mcp/server.py +++ b/mcp_servers/product_mcp/server.py @@ -17,6 +17,11 @@ class Settings(BaseSettings): """Server configuration""" hyperf_api_url: str hyperf_api_token: str + mall_api_url: str + mall_tenant_id: str = "2" + mall_currency_code: str = "EUR" + mall_language_id: str = "1" + mall_source: str = "us.qa1.gaia888.com" log_level: str = "INFO" model_config = ConfigDict(env_file=".env") @@ -277,6 +282,14 @@ async def search_products( try: from shared.mall_client import MallClient + import logging + logger = logging.getLogger(__name__) + + logger.info( + f"search_products called with keyword={keyword}, " + f"user_token_prefix={user_token[:20] if user_token else None}..." + ) + print(f"[DEBUG] search_products called: keyword={keyword}, user_token={user_token[:20] if user_token else None}...") # 使用用户 token 创建 Mall 客户端 mall = MallClient( @@ -294,8 +307,19 @@ async def search_products( page=page ) + logger.info( + f"Mall API returned: result_type={type(result).__name__}, " + f"result_keys={list(result.keys()) if isinstance(result, dict) else 'not a dict'}, " + f"result={result}" + ) + print(f"[DEBUG] Mall API returned: {result}") + # 解析返回结果 - products = result.get("list", []) + # Mall API 返回结构: {"total": X, "data": {"data": [...], ...}} + if "data" in result and isinstance(result["data"], dict): + products = result["data"].get("data", []) + else: + products = result.get("list", []) total = result.get("total", 0) # 格式化商品数据