compare_products
Compare nutritional, ingredient, environmental, or processing aspects of up to 10 food products by entering their barcodes. Use this tool to make informed food choices efficiently.
Instructions
Compare nutritional information between multiple products
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| barcodes | Yes | Array of product barcodes to compare (max 10) | |
| focus | No | Focus comparison on specific aspect |
Implementation Reference
- src/handlers.ts:89-114 (handler)Main handler function for the compare_products tool. Fetches products using barcodes, validates at least 2 products, generates comparison string using helper, and returns as text content.async handleCompareProducts(barcodes: string[], focus: string = 'nutrition') { const responses = await this.client.getProductsByBarcodes(barcodes); const validProducts = responses.filter(r => r.status !== 0 && r.product); if (validProducts.length < 2) { return { content: [ { type: "text" as const, text: "Need at least 2 valid products to compare.", }, ], }; } const comparison = this.compareProductsByFocus(validProducts.map(r => r.product!), focus); return { content: [ { type: "text" as const, text: comparison, }, ], }; }
- src/tools.ts:82-105 (schema)Tool schema definition including name, description, and input schema for barcodes array and optional focus parameter.{ name: "compare_products", description: "Compare nutritional information between multiple products", inputSchema: { type: "object", properties: { barcodes: { type: "array", items: { type: "string", }, description: "Array of product barcodes to compare (max 10)", maxItems: 10, minItems: 2, }, focus: { type: "string", description: "Focus comparison on specific aspect", enum: ["nutrition", "ingredients", "environmental", "processing"], }, }, required: ["barcodes"], }, },
- src/index.ts:54-55 (registration)Registration in the MCP server request handler switch statement, dispatching CallToolRequest for 'compare_products' to the handler.case 'compare_products': return await handlers.handleCompareProducts(args.barcodes, args.focus);
- src/handlers.ts:250-292 (helper)Core comparison logic that generates a formatted string comparing products based on the specified focus (nutrition, environmental, processing, ingredients).private compareProductsByFocus(products: Product[], focus: string): string { const comparison = [`**Product Comparison (${focus})**\n`]; products.forEach((product, index) => { comparison.push(`**${index + 1}. ${product.product_name || 'Unknown'}**`); switch (focus) { case 'nutrition': if (product.nutriscore_grade) { comparison.push(`Nutri-Score: ${product.nutriscore_grade.toUpperCase()}`); } if (product.nutriments) { const keyNutrients = this.extractKeyNutrients(product.nutriments); if (keyNutrients) comparison.push(`Nutrients:\n${keyNutrients}`); } break; case 'environmental': if (product.ecoscore_grade) { comparison.push(`Eco-Score: ${product.ecoscore_grade.toUpperCase()}`); } if (product.packaging) { comparison.push(`Packaging: ${product.packaging}`); } break; case 'processing': if (product.nova_group) { comparison.push(`NOVA Group: ${product.nova_group} (${this.getNovaDescription(String(product.nova_group))})`); } break; case 'ingredients': if (product.ingredients_text) { comparison.push(`Ingredients: ${product.ingredients_text.substring(0, 200)}${product.ingredients_text.length > 200 ? '...' : ''}`); } break; } comparison.push(''); }); return comparison.join('\n'); }