getSteps
Retrieve step count data from Fitbit for specified date ranges. Use this tool to access daily, weekly, or monthly step tracking information for health monitoring and fitness analysis.
Input 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 |
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 step data: 1d, 7d, 30d, 1w, 1m",
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/server.ts:267-308 (handler)Handler function for the 'getSteps' tool that fetches daily or periodic step data from the Fitbit API, formats the response as JSON, and handles errors.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)Zod input schema defining optional 'date' and 'period' parameters for the getSteps tool.{ 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)MCP server.tool registration call for the 'getSteps' tool, including schema and inline 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:68-72 (helper)Helper function to format or default date to today in YYYY-MM-DD format, used by getSteps handler.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-65 (helper)Helper function to make authenticated API requests to Fitbit endpoints, used by getSteps handler.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; } }