Skip to main content
Glama
caleb-conner

Open Food Facts MCP Server

by caleb-conner

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
NameRequiredDescriptionDefault
barcodeYesThe barcode/ID of the product to analyze

Implementation Reference

  • 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,
          },
        ],
      };
    }
  • 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);
  • 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');
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/caleb-conner/open-food-facts-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server