getEcoScore
Get the Eco-Score environmental rating (A to E) for any food product by entering its name or barcode. Assess a product's environmental impact with data from OpenFoodFacts.
Instructions
Get the Eco-Score (environmental impact rating A-E) for a product
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| nameOrBarcode | Yes | Product name or barcode (EAN/UPC) |
Implementation Reference
- src/tools/helpers.ts:249-263 (handler)The core handler function that computes the Eco-Score for a product. Extracts ecoscore_grade and ecoscore_score from the product, then returns an EcoScoreResult with grade, score, packaging, origins, and explanation.
export function getEcoScore(product: any): EcoScoreResult { const grade = product.ecoscore_grade || 'unknown'; const score = product.ecoscore_score ?? null; return { barcode: product.barcode || product.code, productName: product.name || product.product_name || 'Unknown', brand: product.brands || 'Unknown', ecoScoreGrade: grade.toUpperCase(), ecoScoreScore: score, packaging: product.packaging || 'Not specified', origins: product.origins || 'Not specified', explanation: ECO_SCORE_EXPLANATIONS[grade.toLowerCase()] || ECO_SCORE_EXPLANATIONS['unknown'] }; } - src/tools/types.ts:32-41 (schema)The EcoScoreResult interface defining the return type shape: barcode, productName, brand, ecoScoreGrade, ecoScoreScore, packaging, origins, and explanation.
export interface EcoScoreResult { barcode: string; productName: string; brand: string; ecoScoreGrade: string; ecoScoreScore: number | null; packaging: string; origins: string; explanation: string; } - src/tools/types.ts:146-153 (schema)The ECO_SCORE_EXPLANATIONS constant mapping grades A-E (and 'unknown') to human-readable explanation strings.
export const ECO_SCORE_EXPLANATIONS: Record<string, string> = { 'a': 'Very low environmental impact - Excellent eco choice!', 'b': 'Low environmental impact - Good for the planet.', 'c': 'Moderate environmental impact - Consider the environment.', 'd': 'High environmental impact - Consider eco-friendlier options.', 'e': 'Very high environmental impact - Significant environmental footprint.', 'unknown': 'Eco-Score not available for this product.' }; - src/tools/nutrition-tools.ts:45-59 (registration)The tool registration for 'getEcoScore' on the MCP server, including description, input schema (nameOrBarcode), and the async handler that looks up a product then calls getEcoScore().
server.registerTool('getEcoScore', { description: 'Get the Eco-Score (environmental impact rating A-E) for a product', inputSchema: productIdentifierSchema }, async ({ nameOrBarcode }) => { try { const product = await findProduct(nameOrBarcode); if (!product) { return { content: [{ type: 'text' as const, text: `Product "${nameOrBarcode}" not found.` }], isError: true }; } const result = getEcoScore(product); return { content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }] }; } catch (error: any) { return { content: [{ type: 'text' as const, text: `Error: ${error.message}` }], isError: true }; } }); - The getEcoScoreGuide() function providing a markdown resource explaining the Eco-Score grading system.
function getEcoScoreGuide(): string { return `# Eco-Score Guide Eco-Score rates the environmental impact of food from **A** (lowest impact) to **E** (highest impact). ## The Grades ### Grade A - Very Low Impact Minimal environmental footprint. Examples: - Local seasonal produce - Tap water - Bulk legumes ### Grade B - Low Impact Small environmental footprint. Examples: - Locally sourced products - Minimal packaging - Plant-based proteins ### Grade C - Moderate Impact Average environmental footprint. Examples: - Standard packaged foods - Mixed origin products ### Grade D - High Impact Significant environmental footprint. Examples: - Air-freighted produce - Heavily packaged items - Some meat products ### Grade E - Very High Impact Major environmental footprint. Examples: - Out-of-season air-freighted foods - Products with deforestation links - Excessive packaging ## What Affects Eco-Score - **Production method** - Organic, conventional, intensive - **Transportation** - Local vs imported, air vs ship - **Packaging** - Recyclable, plastic, glass - **Seasonality** - In-season vs greenhouse/imported - **Biodiversity** - Palm oil, deforestation risk ## Tips - Buy **local and seasonal** when possible - Choose products with **less packaging** - Prefer **plant-based** options - Look for **organic** and **fair-trade** labels `; }