Skip to main content
Glama
willc121

Garmin Health MCP Server

by willc121

get_sleep

Retrieve sleep statistics like average duration and nights tracked from Garmin health data. Filter results by date range to analyze sleep patterns over time.

Instructions

Get sleep statistics including average duration and total nights tracked. Optionally filter by date range.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
start_dateNoStart date (YYYY-MM-DD)
end_dateNoEnd date (YYYY-MM-DD, exclusive recommended)

Implementation Reference

  • The handler function implementing the get_sleep tool logic. It queries sleep data from Supabase, handles overall summary or date-range filtered stats, computing total nights, average, min/max sleep duration in hours.
    async function getSleep(startDate?: string, endDate?: string) { console.error("[getSleep] called with", { startDate, endDate }); // If no date range, return the summary if (!startDate && !endDate) { const { data, error } = await supabase .from("sleep_summary") .select("*") .single(); if (error) throw error; return { total_nights: data?.nights || 0, average_duration_hours: data?.avg_duration_hours ? Math.round(data.avg_duration_hours * 10) / 10 : null, date_range: { first_night: data?.first_night || null, last_night: data?.last_night || null, }, }; } let query = supabase .from("sleep") .select("calendar_date, duration_hours") .not("duration_hours", "is", null) .order("calendar_date", { ascending: true }); if (startDate) query = query.gte("calendar_date", startDate); // End date is exclusive (recommended) if (endDate) query = query.lt("calendar_date", endDate); const { data, error } = await query; if (error) throw error; const durations = (data || []) .map((s) => s.duration_hours) .filter((d): d is number => typeof d === "number" && d > 0 && d < 24); const avgDuration = durations.length > 0 ? Math.round( (durations.reduce((a, b) => a + b, 0) / durations.length) * 10 ) / 10 : null; return { total_nights: durations.length, average_duration_hours: avgDuration, min_hours: durations.length ? Math.round(Math.min(...durations) * 10) / 10 : null, max_hours: durations.length ? Math.round(Math.max(...durations) * 10) / 10 : null, date_range: { start: startDate || data?.[0]?.calendar_date || null, end: endDate || data?.[data.length - 1]?.calendar_date || null, }, }; }
  • The input schema and metadata for the get_sleep tool as registered in the ListTools handler.
    { name: "get_sleep", description: "Get sleep statistics including average duration and total nights tracked. Optionally filter by date range.", inputSchema: { type: "object", properties: { start_date: { type: "string", description: "Start date (YYYY-MM-DD)" }, end_date: { type: "string", description: "End date (YYYY-MM-DD, exclusive recommended)" }, }, }, },
  • src/index.ts:414-416 (registration)
    Registration in the CallToolRequestSchema handler switch statement, routing calls to the getSleep function.
    case "get_sleep": result = await getSleep(a.start_date, a.end_date); break;
  • src/index.ts:318-393 (registration)
    The ListToolsRequestSchema handler where get_sleep is listed among available tools.
    server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: "get_health_summary", description: "Get an overview of all health data including VO2 max, activities, sleep, and race predictions", inputSchema: { type: "object", properties: {} }, }, { name: "get_vo2max", description: "Get VO2 max history and trends. VO2 max measures cardiovascular fitness in ml/kg/min.", inputSchema: { type: "object", properties: { start_date: { type: "string", description: "Start date (YYYY-MM-DD)" }, end_date: { type: "string", description: "End date (YYYY-MM-DD, exclusive recommended)" }, sport: { type: "string", description: "Filter by sport (e.g., 'running', 'cycling')" }, }, }, }, { name: "get_activities", description: "Get activity breakdown by type, including counts, distances, and durations", inputSchema: { type: "object", properties: { start_date: { type: "string", description: "Start date (YYYY-MM-DD)" }, end_date: { type: "string", description: "End date (YYYY-MM-DD)" }, activity_type: { type: "string", description: "Filter by activity type (e.g., 'running')", }, }, }, }, { name: "get_sleep", description: "Get sleep statistics including average duration and total nights tracked. Optionally filter by date range.", inputSchema: { type: "object", properties: { start_date: { type: "string", description: "Start date (YYYY-MM-DD)" }, end_date: { type: "string", description: "End date (YYYY-MM-DD, exclusive recommended)" }, }, }, }, { name: "get_race_predictions", description: "Get predicted race times for 5K, 10K, half marathon, and marathon based on current fitness", inputSchema: { type: "object", properties: {} }, }, { name: "get_heart_rate_zones", description: "Get personalized heart rate training zones based on max HR and lactate threshold", inputSchema: { type: "object", properties: {} }, }, { name: "get_training_load", description: "Get training load data including acute/chronic workload ratio to assess overtraining risk", inputSchema: { type: "object", properties: { days: { type: "number", description: "Number of days (default: 30)" }, }, }, }, ], }; });

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/willc121/garmin-mcp-server'

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