getCalories
Retrieve calorie data from Fitbit for specific dates or time periods to track energy expenditure and support fitness monitoring.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | No | Date in YYYY-MM-DD format. If not specified, will use today. | |
| period | No | Period for data: 1d, 7d, 30d, 1w, 1m |
Input Schema (JSON Schema)
{
"properties": {
"date": {
"description": "Date in YYYY-MM-DD format. If not specified, will use today.",
"type": "string"
},
"period": {
"description": "Period for data: 1d, 7d, 30d, 1w, 1m",
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/server.ts:669-710 (handler)The core handler function for the getCalories tool. It constructs the Fitbit API endpoint for calories data based on date and period, calls makeApiRequest, and returns the parsed data as a text content response or error.async ({ date, period }) => { try { const formattedDate = formatDate(date); let endpoint = ""; if (period) { endpoint = `/user/-/activities/calories/date/${formattedDate}/${period}.json`; } else { endpoint = `/user/-/activities/calories/date/${formattedDate}/1d.json`; } const data = await makeApiRequest(endpoint); return { content: [ { type: "text", text: JSON.stringify( { date: formattedDate, calories: data["activities-calories"] || [], intraday: data["activities-calories-intraday"] || {}, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } }
- src/server.ts:659-668 (schema)Input schema for the getCalories tool, defining optional 'date' and 'period' parameters using Zod validation.{ date: z .string() .optional() .describe("Date in YYYY-MM-DD format. If not specified, will use today."), period: z .string() .optional() .describe("Period for data: 1d, 7d, 30d, 1w, 1m"), },
- src/server.ts:657-711 (registration)Registration of the getCalories tool on the MCP server using server.tool(name, inputSchema, handler).server.tool( "getCalories", { date: z .string() .optional() .describe("Date in YYYY-MM-DD format. If not specified, will use today."), period: z .string() .optional() .describe("Period for data: 1d, 7d, 30d, 1w, 1m"), }, async ({ date, period }) => { try { const formattedDate = formatDate(date); let endpoint = ""; if (period) { endpoint = `/user/-/activities/calories/date/${formattedDate}/${period}.json`; } else { endpoint = `/user/-/activities/calories/date/${formattedDate}/1d.json`; } const data = await makeApiRequest(endpoint); return { content: [ { type: "text", text: JSON.stringify( { date: formattedDate, calories: data["activities-calories"] || [], intraday: data["activities-calories-intraday"] || {}, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } } );
- src/server.ts:68-72 (helper)Helper function used by getCalories (and other tools) to format or default the date parameter to YYYY-MM-DD.function formatDate(date?: string): string { if (date) return date; const today = new Date(); return today.toISOString().split("T")[0]; // YYYY-MM-DD }
- src/server.ts:44-64 (helper)Shared helper function used by getCalories (and other tools) to make authenticated requests to the Fitbit API.async function makeApiRequest(endpoint: string): Promise<any> { try { const url = `${baseUrl}${endpoint}`; const response = await fetch(url, { headers: { Authorization: `Bearer ${accessToken}`, Accept: "application/json", }, }); if (!response.ok) { throw new Error( `Fitbit API error: ${response.status} ${response.statusText}` ); } return await response.json(); } catch (error) { console.error(`Error making request to ${endpoint}:`, error); throw error; }