list_products
Retrieve a list of products from a Shopify store using the Shopify Python MCP Server. Specify the number of products to fetch, up to 250, for efficient product management.
Instructions
商品一覧を取得する
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | 取得する商品数(最大250) |
Implementation Reference
- src/shopify_py_mcp/server.py:411-439 (handler)The handler function that implements the core logic for the 'list_products' tool. It extracts the optional 'limit' from arguments, fetches products using the helper get_all_shopify_products, summarizes each product, and returns a JSON-formatted list as TextContent.async def handle_list_products(arguments: dict) -> list[types.TextContent]: """商品一覧を取得する""" limit = int(arguments.get("limit", 50)) products = get_all_shopify_products( total_limit=limit, per_page_limit=250 # 1回のリクエストで最大250件取得 ) result = [] for product in products: result.append( { "id": product.id, "title": product.title, "vendor": product.vendor, "product_type": product.product_type, "created_at": product.created_at, "updated_at": product.updated_at, "status": product.status, "variants_count": len(product.variants), "images_count": len(product.images), } ) return [ types.TextContent( type="text", text=json.dumps(result, indent=2, ensure_ascii=False), ) ]
- src/shopify_py_mcp/server.py:155-169 (registration)The registration of the 'list_products' tool in the MCP server's list_tools handler, specifying name, description, and input schema.name="list_products", description="商品一覧を取得する", inputSchema={ "type": "object", "properties": { "limit": { "type": "number", "description": "取得する商品数(最大250)", "minimum": 1, "maximum": 250, "default": 50, }, }, }, ),
- src/shopify_py_mcp/server.py:158-168 (schema)JSON Schema defining the input parameters for the 'list_products' tool: an object with an optional 'limit' number (1-250, default 50)."type": "object", "properties": { "limit": { "type": "number", "description": "取得する商品数(最大250)", "minimum": 1, "maximum": 250, "default": 50, }, }, },
- src/shopify_py_mcp/server.py:29-95 (helper)Key helper function that implements pagination-aware fetching of Shopify products using the shopify Python library, handling Link headers for next pages, rate limiting, and configurable limits.def get_all_shopify_products(total_limit=None, per_page_limit=250): """ ShopifyAPIライブラリを使用して複数ページにわたる商品一覧を取得する関数 Parameters: total_limit (int): 取得する総商品数(Noneの場合はすべての商品を取得) per_page_limit (int): 1回のリクエストあたりの商品数(最大250) Returns: list: 商品のリスト """ # 1ページあたりの上限を250に制限 per_page_limit = min(per_page_limit, 250) all_products = [] next_page_url = None try: while True: # 既に十分な商品が取得されているか確認 if total_limit is not None and len(all_products) >= total_limit: break # 残りの取得数を計算 current_limit = per_page_limit if total_limit is not None: current_limit = min(per_page_limit, total_limit - len(all_products)) if current_limit <= 0: break # 商品一覧の取得 if next_page_url: # next_page_urlからpage_infoを抽出 page_info = extract_page_info(next_page_url) products = shopify.Product.find( limit=current_limit, page_info=page_info ) else: products = shopify.Product.find(limit=current_limit) # 結果が空の場合は終了 if not products: break # 取得した商品を追加 all_products.extend(products) # レスポンスヘッダーからページネーション情報を取得 response_headers = shopify.ShopifyResource.connection.response.headers link_header = response_headers.get("Link", "") # 次のページURLを抽出 next_page_url = extract_next_page_url(link_header) if not next_page_url: break # レート制限を避けるために少し待機 time.sleep(0.5) except Exception as e: print(f"エラーが発生しました: {e}") # total_limitが指定されている場合、指定した数だけ返す if total_limit is not None: return all_products[:total_limit] return all_products