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>
This commit is contained in:
@@ -37,68 +37,6 @@ from shared.hyperf_client import HyperfClient
|
|||||||
hyperf = HyperfClient(settings.hyperf_api_url, settings.hyperf_api_token)
|
hyperf = HyperfClient(settings.hyperf_api_url, settings.hyperf_api_token)
|
||||||
|
|
||||||
|
|
||||||
@mcp.tool()
|
|
||||||
async def search_products(
|
|
||||||
query: str,
|
|
||||||
category: Optional[str] = None,
|
|
||||||
brand: Optional[str] = None,
|
|
||||||
price_min: Optional[float] = None,
|
|
||||||
price_max: Optional[float] = None,
|
|
||||||
sort: str = "relevance",
|
|
||||||
page: int = 1,
|
|
||||||
page_size: int = 20
|
|
||||||
) -> dict:
|
|
||||||
"""Search products
|
|
||||||
|
|
||||||
Args:
|
|
||||||
query: Search keywords
|
|
||||||
category: Category filter
|
|
||||||
brand: Brand filter
|
|
||||||
price_min: Minimum price filter
|
|
||||||
price_max: Maximum price filter
|
|
||||||
sort: Sort order (relevance, price_asc, price_desc, sales, latest)
|
|
||||||
page: Page number (default: 1)
|
|
||||||
page_size: Items per page (default: 20)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of matching products
|
|
||||||
"""
|
|
||||||
payload = {
|
|
||||||
"query": query,
|
|
||||||
"sort": sort,
|
|
||||||
"page": page,
|
|
||||||
"page_size": page_size,
|
|
||||||
"filters": {}
|
|
||||||
}
|
|
||||||
|
|
||||||
if category:
|
|
||||||
payload["filters"]["category"] = category
|
|
||||||
if brand:
|
|
||||||
payload["filters"]["brand"] = brand
|
|
||||||
if price_min is not None or price_max is not None:
|
|
||||||
payload["filters"]["price_range"] = {}
|
|
||||||
if price_min is not None:
|
|
||||||
payload["filters"]["price_range"]["min"] = price_min
|
|
||||||
if price_max is not None:
|
|
||||||
payload["filters"]["price_range"]["max"] = price_max
|
|
||||||
|
|
||||||
try:
|
|
||||||
result = await hyperf.post("/products/search", json=payload)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"success": True,
|
|
||||||
"products": result.get("products", []),
|
|
||||||
"total": result.get("total", 0),
|
|
||||||
"pagination": result.get("pagination", {})
|
|
||||||
}
|
|
||||||
except Exception as e:
|
|
||||||
return {
|
|
||||||
"success": False,
|
|
||||||
"error": str(e),
|
|
||||||
"products": []
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
async def get_product_detail(
|
async def get_product_detail(
|
||||||
product_id: str
|
product_id: str
|
||||||
|
|||||||
Reference in New Issue
Block a user