get_glucose_stats
Calculate average glucose, GMI (estimated A1C), time-in-range percentages, and glucose variability from your LibreLink data. Gain insights for diabetes management and identify improvement areas.
Instructions
Calculate comprehensive glucose statistics including average glucose, GMI (estimated A1C), time-in-range percentages, and variability metrics. Essential for diabetes management insights and identifying areas for improvement.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| days | No | Number of days to analyze (1-14). Default: 7. Note: LibreLinkUp data availability may be limited. |
Implementation Reference
- src/index.ts:110-126 (registration)Tool definition/registration for 'get_glucose_stats' in the tools array, with name, description, and inputSchema (accepts optional 'days' parameter).
{ name: 'get_glucose_stats', description: 'Calculate comprehensive glucose statistics including average glucose, GMI (estimated A1C), time-in-range percentages, and variability metrics. Essential for diabetes management insights and identifying areas for improvement.', inputSchema: { type: 'object', properties: { days: { type: 'number', description: 'Number of days to analyze (1-14). Default: 7. Note: LibreLinkUp data availability may be limited.' } }, required: [] }, annotations: { readOnlyHint: true } }, - src/index.ts:294-323 (handler)Handler in CallToolRequestSchema that executes 'get_glucose_stats': fetches glucose history for N days, calls analytics.calculateGlucoseStats(), and returns structured JSON with average glucose, GMI, time-in-range percentages, and variability metrics.
case 'get_glucose_stats': { if (!client || !analytics) { throw new Error('LibreLinkUp not configured. Use configure_credentials first.'); } const days = (args?.days as number) || 7; const readings = await client.getGlucoseHistory(days * 24); const stats = analytics.calculateGlucoseStats(readings); return { content: [{ type: 'text', text: JSON.stringify({ analysis_period_days: days, average_glucose: stats.average, glucose_management_indicator: stats.gmi, time_in_range: { target_70_180: stats.timeInRange, below_70: stats.timeBelowRange, above_180: stats.timeAboveRange }, variability: { standard_deviation: stats.standardDeviation, coefficient_of_variation: stats.coefficientOfVariation }, reading_count: stats.readingCount }, null, 2) }] }; } - src/glucose-analytics.ts:19-71 (helper)Core implementation: calculateGlucoseStats() method on GlucoseAnalytics class. Computes average, standard deviation, coefficient of variation, GMI (3.31 + 0.02392*mean), time-in-range percentages based on configurable targetLow/targetHigh thresholds.
calculateGlucoseStats(readings: GlucoseReading[]): GlucoseStats { if (readings.length === 0) { return { average: 0, gmi: 0, timeInRange: 0, timeBelowRange: 0, timeAboveRange: 0, standardDeviation: 0, coefficientOfVariation: 0, readingCount: 0 }; } const values = readings.map(r => r.value); const n = values.length; // Calculate average const sum = values.reduce((a, b) => a + b, 0); const average = sum / n; // Calculate standard deviation const squaredDiffs = values.map(v => Math.pow(v - average, 2)); const avgSquaredDiff = squaredDiffs.reduce((a, b) => a + b, 0) / n; const standardDeviation = Math.sqrt(avgSquaredDiff); // Calculate coefficient of variation const coefficientOfVariation = (standardDeviation / average) * 100; // Calculate GMI (Glucose Management Indicator) // GMI = 3.31 + 0.02392 × [mean glucose in mg/dL] const gmi = 3.31 + (0.02392 * average); // Calculate time in range const inRange = values.filter(v => v >= this.config.targetLow && v <= this.config.targetHigh).length; const belowRange = values.filter(v => v < this.config.targetLow).length; const aboveRange = values.filter(v => v > this.config.targetHigh).length; const timeInRange = (inRange / n) * 100; const timeBelowRange = (belowRange / n) * 100; const timeAboveRange = (aboveRange / n) * 100; return { average: Math.round(average * 100) / 100, gmi: Math.round(gmi * 100) / 100, timeInRange: Math.round(timeInRange * 100) / 100, timeBelowRange: Math.round(timeBelowRange * 100) / 100, timeAboveRange: Math.round(timeAboveRange * 100) / 100, standardDeviation: Math.round(standardDeviation * 100) / 100, coefficientOfVariation: Math.round(coefficientOfVariation * 100) / 100, readingCount: n }; } - src/types.ts:34-43 (schema)GlucoseStats interface defining the return type shape: average, gmi, timeInRange, timeBelowRange, timeAboveRange, standardDeviation, coefficientOfVariation, readingCount.
export interface GlucoseStats { average: number; gmi: number; timeInRange: number; timeBelowRange: number; timeAboveRange: number; standardDeviation: number; coefficientOfVariation: number; readingCount: number; }