Commit Graph

5 Commits

Author SHA1 Message Date
wangliang
f4e77f39ce fix: 修复 Mall API 数据提取逻辑和添加配置字段
## 问题 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 <noreply@anthropic.com>
2026-01-26 18:43:46 +08:00
wangliang
74c28eb838 fix: 添加工具注册表和 HTTP 路由,修复 MCP 工具调用 404 错误
## 问题
Product MCP 工具调用返回 404:
```
POST /tools/search_products HTTP/1.1 404 Not Found
```

## 根本原因
1. Product MCP 缺少 `/tools/{tool_name}` HTTP 路由
2. FastMCP 的 `mcp.http_app()` 默认不暴露此路由
3. Order MCP 有自定义路由处理,Product MCP 没有

## 解决方案

### 1. 添加工具注册表
**位置**: 第 32-41 行

```python
# Tool registry for HTTP access
_tools: Dict[str, Any] = {}

def register_tool(name: str):
    """Decorator to register tool in _tools dict"""
    def decorator(func):
        _tools[name] = func
        return func
    return decorator
```

### 2. 为所有工具添加注册装饰器
**修改的工具**:
- `get_product_detail`
- `recommend_products`
- `get_quote`
- `check_inventory`
- `get_categories`
- `search_products`
- `health_check`

**示例**:
```python
@register_tool("search_products")
@mcp.tool()
async def search_products(...):
```

### 3. 添加 HTTP 路由处理
**位置**: 第 352-401 行

参考 Order MCP 实现,添加:
- `/tools/{tool_name}` POST 路由
- 工具调用逻辑:`tool_obj.run(arguments)`
- 结果提取和 JSON 解析
- 错误处理(404, 400, 500)

### 4. 配置路由列表
**位置**: 第 407-415 行

```python
routes = [
    Route('/health', health_check, methods=['GET']),
    Route('/tools/{tool_name}', execute_tool, methods=['POST'])
]
```

## 测试结果

```bash
curl -X POST http://localhost:8004/tools/search_products \
  -H "Content-Type: application/json" \
  -d '{"keyword": "ring"}'
```

返回:
```json
{
  "success": true,
  "result": {
    "success": false,
    "error": "用户未登录,请先登录账户以搜索商品",
    "products": [],
    "total": 0,
    "require_login": true
  }
}
```

 工具调用成功(user_token 缺失是预期行为)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-26 18:32:16 +08:00
wangliang
ad7f30d54c fix: 删除旧的 search_products 工具,解决工具名冲突
## 问题
Product MCP 启动时出现警告:
```
WARNING Tool already exists: search_products
```

导致工具调用时返回 404 错误:
```
POST /tools/search_products HTTP/1.1 404 Not Found
```

## 根本原因
Product MCP 中有两个同名工具:
1. **第 40-99 行**:旧的 `search_products`(使用 Hyperf API)
2. **第 292-378 行**:新的 `search_products`(使用 Mall API)

FastMCP 无法注册同名工具,导致注册失败。

## 解决方案
删除旧的 `search_products` 工具定义(第 40-99 行),保留新的使用 Mall API 的版本。

## 修改内容
**文件**: mcp_servers/product_mcp/server.py
- 删除第 40-99 行(旧的 search_products 工具)
- 保留第 291 行开始的新的 search_products 工具

## 影响
- 移除了基于 Hyperf API 的旧搜索功能
- 所有商品搜索统一使用 Mall API
- 不再支持复杂过滤条件(category, brand, price_range 等)
- 简化为关键词搜索,返回商品卡片格式

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-26 18:25:06 +08:00
wangliang
7b676f8015 refactor: 将 search_spu_products 重命名为 search_products
## 修改内容

### 1. Product Agent prompt
- 将工具名从 `search_spu_products` 改为 `search_products`
- 更新所有示例代码
- 保持功能说明不变(Mall API SPU 搜索)

### 2. Product Agent 代码
**文件**: agent/agents/product.py

**修改**:
- 第 24 行:工具名改为 `search_products`
- 第 65、77 行:示例中的工具名更新
- 第 219-230 行:注入逻辑改为检查 `search_products`
- 第 284 行:工具结果检查改为 `search_products`
- 第 279-333 行:变量名 `spu_products` → `products`
- 第 280 行:`has_spu_search_result` → `has_product_search_result`

### 3. Product MCP Server
**文件**: mcp_servers/product_mcp/server.py

**修改**:
- 第 292 行:函数名 `search_spu_products` → `search_products`
- 第 300 行:文档字符串更新
- 功能完全相同,只是重命名

### 4. 移除映射逻辑
- 移除了 `search_products` → `search_spu_products` 的工具名映射
- 保留了 `query` → `keyword` 的参数映射(向后兼容)

## 好处

1. **简化命名**:`search_products` 比 `search_spu_products` 更简洁
2. **统一接口**:与系统中其他搜索工具命名一致
3. **降低复杂度**:减少名称长度和冗余

## 向后兼容

参数映射保留:
```python
# 仍然支持旧参数名
{"query": "ring"} → {"keyword": "ring"}
```

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-26 18:22:23 +08:00
wl
3ad6eee0d9 feat: 初始化 B2B AI Shopping Assistant 项目
- 配置 Docker Compose 多服务编排
- 实现 Chatwoot + Agent 集成
- 配置 Strapi MCP 知识库
- 支持 7 种语言的 FAQ 系统
- 实现 LangGraph AI 工作流

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-14 19:25:22 +08:00