analyze_product
Analyze food products by barcode to retrieve nutritional data, ingredient information, and environmental impact scores for informed dietary decisions.
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 main handler function that fetches the product data using OpenFoodFactsClient and generates nutritional analysis by calling the private analyzeNutrition method, returning formatted text content.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 in the tools array, specifying the name, description, and input schema that requires a 'barcode' string.{ 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)Registration in the MCP server's CallToolRequestHandler switch statement, dispatching 'analyze_product' tool calls to handlers.handleAnalyzeProduct with the barcode argument.case 'analyze_product': return await handlers.handleAnalyzeProduct(args.barcode);
- src/handlers.ts:212-248 (helper)Core helper method implementing the nutritional analysis logic, interpreting Nutri-Score, NOVA, Eco-Score, and breaking down key nutriments with qualitative assessments.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'); }