Skip to main content
Glama

get-recent-activities

Retrieve the latest activities for the authenticated athlete from Strava API, with customizable pagination to specify the number of results.

Instructions

Fetches the most recent activities for the authenticated athlete.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
perPageNoNumber of activities to retrieve (default: 30)

Implementation Reference

  • The execute handler function implementing the tool logic: validates token, calls stravaClient.getRecentActivities, formats activities into MCP content items with error handling.
    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 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." }] }; } // 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}m` : '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, }; } }
  • Zod schema defining the input parameters for the tool (optional perPage, default 30).
    const GetRecentActivitiesInputSchema = z.object({ perPage: z.number().int().positive().optional().default(30).describe("Number of activities to retrieve (default: 30)"), });
  • src/server.ts:68-73 (registration)
    Registration of the tool in the MCP server via server.tool(), passing name, description, inputSchema shape, and execute function.
    server.tool( getRecentActivities.name, getRecentActivities.description, getRecentActivities.inputSchema?.shape ?? {}, getRecentActivities.execute );
  • Supporting function in stravaClient.ts that makes the actual Strava API call to /athlete/activities, handles validation with Zod, token refresh, and errors.
    export async function getRecentActivities(accessToken: string, perPage = 30): Promise<any[]> { if (!accessToken) { throw new Error("Strava access token is required."); } try { const response = await stravaApi.get<unknown>("athlete/activities", { headers: { Authorization: `Bearer ${accessToken}` }, params: { per_page: perPage } }); const validationResult = StravaActivitiesResponseSchema.safeParse(response.data); if (!validationResult.success) { console.error("Strava API response validation failed (getRecentActivities):", validationResult.error); throw new Error(`Invalid data format received from Strava API: ${validationResult.error.message}`); } return validationResult.data; } catch (error) { // Pass a retry function to handleApiError return await handleApiError<any[]>(error, 'getRecentActivities', async () => { // Use new token from environment after refresh const newToken = process.env.STRAVA_ACCESS_TOKEN!; return getRecentActivities(newToken, perPage); }); } }

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/r-huijts/strava-mcp'

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