get-recent-activities
Retrieve recent Strava activities to analyze workouts and track fitness progress. Fetches activity stats, heart rate data, and segment insights for performance monitoring.
Instructions
Fetches the most recent activities for the authenticated athlete.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| perPage | No | Number of activities to retrieve (default: 30) |
Implementation Reference
- src/tools/getRecentActivities.ts:17-71 (handler)The 'execute' function within the 'getRecentActivities' object contains the logic to fetch recent activities, handle authentication, and format the output.
execute: async ({ perPage }: GetRecentActivitiesInput) => { const token = process.env.STRAVA_ACCESS_TOKEN; // --- DEBUGGING: Print the token being used --- console.error(`[DEBUG] Using STRAVA_ACCESS_TOKEN: ${token?.substring(0, 5)}...${token?.slice(-5)}`); // --------------------------------------------- if (!token || token === 'YOUR_STRAVA_ACCESS_TOKEN_HERE') { console.error("Missing or placeholder STRAVA_ACCESS_TOKEN in .env"); // Use literal type for content item return { content: [{ type: "text" as const, text: "β Configuration Error: STRAVA_ACCESS_TOKEN is missing or not set in the .env file." }], isError: true, }; } try { console.error(`Fetching ${perPage} recent activities...`); const athlete = await getAuthenticatedAthlete(token); const activities = await fetchActivities(token, perPage); console.error(`Successfully fetched ${activities?.length ?? 0} activities.`); if (!activities || activities.length === 0) { return { content: [{ type: "text" as const, text: " MNo recent activities found." }] }; } const distanceFactor = athlete.measurement_preference === 'feet' ? 0.000621371 : 0.001; const distanceUnit = athlete.measurement_preference === 'feet' ? 'mi' : 'km'; // Map to content items with literal type const contentItems = activities.map(activity => { const dateStr = activity.start_date ? new Date(activity.start_date).toLocaleDateString() : 'N/A'; const distanceStr = activity.distance ? `${(activity.distance * distanceFactor).toFixed(2)} ${distanceUnit}` : 'N/A'; // Ensure each item conforms to { type: "text", text: string } const item: { type: "text", text: string } = { type: "text" as const, text: `π ${activity.name} (ID: ${activity.id ?? 'N/A'}) β ${distanceStr} on ${dateStr}` }; return item; }); // Return the basic McpResponse structure return { content: contentItems }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "An unknown error occurred"; console.error("Error in get-recent-activities tool:", errorMessage); return { content: [{ type: "text" as const, text: `β API Error: ${errorMessage}` }], isError: true, }; } } - src/tools/getRecentActivities.ts:5-7 (schema)Zod schema definition for validating the 'perPage' input parameter.
const GetRecentActivitiesInputSchema = z.object({ perPage: z.number().int().positive().optional().default(30).describe("Number of activities to retrieve (default: 30)"), }); - src/tools/getRecentActivities.ts:12-15 (registration)Tool definition including name, description, input schema, and execution handler.
export const getRecentActivities = { name: "get-recent-activities", description: "Fetches the most recent activities for the authenticated athlete.", inputSchema: GetRecentActivitiesInputSchema,