getSteps
Retrieve step count data from Fitbit for specified dates or periods to track daily activity and monitor fitness progress.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | No | Date in YYYY-MM-DD format. If not specified, will use today. | |
| period | No | Period for step data: 1d, 7d, 30d, 1w, 1m |
Implementation Reference
- src/server.ts:267-308 (handler)Handler function that executes the logic for the 'getSteps' tool. It processes optional 'date' and 'period' parameters, calls formatDate helper, constructs Fitbit API endpoint for steps, fetches data with makeApiRequest, and returns structured JSON response or error.async ({ date, period }) => { try { const formattedDate = formatDate(date); let endpoint = ""; if (period) { endpoint = `/user/-/activities/steps/date/${formattedDate}/${period}.json`; } else { endpoint = `/user/-/activities/steps/date/${formattedDate}/1d.json`; } const data = await makeApiRequest(endpoint); return { content: [ { type: "text", text: JSON.stringify( { date: formattedDate, steps: data["activities-steps"] || [], intraday: data["activities-steps-intraday"] || {}, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } }
- src/server.ts:257-266 (schema)Input schema for the 'getSteps' tool using Zod, defining optional parameters 'date' (string YYYY-MM-DD) and 'period' (string like 1d, 7d).{ date: z .string() .optional() .describe("Date in YYYY-MM-DD format. If not specified, will use today."), period: z .string() .optional() .describe("Period for step data: 1d, 7d, 30d, 1w, 1m"), },
- src/server.ts:255-309 (registration)Registration of the 'getSteps' MCP tool on the server instance, specifying name, input schema, and handler function.server.tool( "getSteps", { date: z .string() .optional() .describe("Date in YYYY-MM-DD format. If not specified, will use today."), period: z .string() .optional() .describe("Period for step data: 1d, 7d, 30d, 1w, 1m"), }, async ({ date, period }) => { try { const formattedDate = formatDate(date); let endpoint = ""; if (period) { endpoint = `/user/-/activities/steps/date/${formattedDate}/${period}.json`; } else { endpoint = `/user/-/activities/steps/date/${formattedDate}/1d.json`; } const data = await makeApiRequest(endpoint); return { content: [ { type: "text", text: JSON.stringify( { date: formattedDate, steps: data["activities-steps"] || [], intraday: data["activities-steps-intraday"] || {}, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } } );
- src/server.ts:44-65 (helper)Shared helper function used by getSteps (and other tools) to make authenticated API requests to Fitbit endpoints.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; } }
- src/server.ts:68-72 (helper)Shared helper function used by getSteps to format or default the date parameter to YYYY-MM-DD format.function formatDate(date?: string): string { if (date) return date; const today = new Date(); return today.toISOString().split("T")[0]; // YYYY-MM-DD }