analyze_product
Retrieve detailed nutritional analysis and scores for any food product by inputting its barcode using Open Food Facts database to support informed food choices.
Instructions
Get nutritional analysis and scores for a product by barcode
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| barcode | Yes | The barcode/ID of the product to analyze |
Implementation Reference
- src/handlers.ts:63-87 (handler)The core handler function for the 'analyze_product' tool. Fetches product data via OpenFoodFactsClient and generates nutritional analysis.async handleAnalyzeProduct(barcode: string) { const response = await this.client.getProduct(barcode); if (response.status === 0 || !response.product) { return { content: [ { type: "text" as const, text: `Product not found for barcode: ${barcode}`, }, ], }; } const analysis = this.analyzeNutrition(response.product); return { content: [ { type: "text" as const, text: analysis, }, ], }; }
- src/tools.ts:68-81 (schema)Tool schema definition with name, description, and input schema requiring a barcode.{ name: "analyze_product", description: "Get nutritional analysis and scores for a product by barcode", inputSchema: { type: "object", properties: { barcode: { type: "string", description: "The barcode/ID of the product to analyze", }, }, required: ["barcode"], }, },
- src/index.ts:51-52 (registration)Dispatch case in the MCP server CallToolRequestHandler that routes 'analyze_product' calls to the handler.case 'analyze_product': return await handlers.handleAnalyzeProduct(args.barcode);
- src/handlers.ts:212-248 (helper)Key helper method that generates the nutritional analysis text including scores and nutrient breakdown.private analyzeNutrition(product: Product): string { const sections = [`**Nutritional Analysis: ${product.product_name || 'Unknown Product'}**\n`]; // Scores interpretation const scoreAnalysis = []; if (product.nutriscore_grade) { const grade = product.nutriscore_grade.toUpperCase(); const gradeDesc = this.getNutriscoreDescription(grade); scoreAnalysis.push(`• Nutri-Score ${grade}: ${gradeDesc}`); } if (product.nova_group) { const group = String(product.nova_group); const groupDesc = this.getNovaDescription(group); scoreAnalysis.push(`• NOVA Group ${group}: ${groupDesc}`); } if (product.ecoscore_grade) { const grade = product.ecoscore_grade.toUpperCase(); const gradeDesc = this.getEcoscoreDescription(grade); scoreAnalysis.push(`• Eco-Score ${grade}: ${gradeDesc}`); } if (scoreAnalysis.length > 0) { sections.push(`**Scores:**\n${scoreAnalysis.join('\n')}\n`); } // Detailed nutrition if (product.nutriments) { const nutrition = this.analyzeNutriments(product.nutriments); if (nutrition) { sections.push(`**Nutritional Breakdown:**\n${nutrition}\n`); } } return sections.join('\n'); }