get_activity_by_id
Retrieve detailed Strava activity metrics, including type, distance, and time, using the activity ID. Ideal for extracting specific workout data for analysis or integration.
Instructions
Get activity details from Strava
Args:
activity_id (Union[str, int]): Activity ID to fetch
Returns:
Dict: Activity details including type, distance, time and other metrics
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| activity_id | Yes |
Implementation Reference
- src/server.py:307-335 (handler)The main handler function for the 'get_activity_by_id' MCP tool. Registered with @mcp.tool() decorator. Converts activity_id to string, calls internal get_activity helper, and returns structured response with success/error handling.async def get_activity_by_id(activity_id: Union[str, int]) -> Dict: """Get activity details from Strava Args: activity_id (Union[str, int]): Activity ID to fetch Returns: Dict: Activity details including type, distance, time and other metrics """ try: activity_id = str(activity_id) activity = get_activity(activity_id) return { "status": "success", "data": activity, "metadata": { "activity_id": activity_id, "timestamp": datetime.now().isoformat() } } except Exception as e: logger.error(f"Error getting activity {activity_id}: {e}") return { "status": "error", "error": str(e), "activity_id": activity_id }
- src/server.py:143-174 (helper)Supporting resource function get_activity that fetches activity details from Strava API with caching. Directly called by the get_activity_by_id tool handler.def get_activity(activity_id: str) -> dict: """Получить детали конкретной активности""" cache_key = f"activity_{activity_id}" # Проверяем кэш cached = strava_cache.get(cache_key) if cached: logger.debug(f"Возвращаем активность {activity_id} из кэша") return cached try: access_token = strava_auth.get_access_token() response = strava_auth.make_request( "GET", f"/activities/{activity_id}", headers={"Authorization": f"Bearer {access_token}"}, ) activity = response.json() # Сохраняем в кэш strava_cache.set(cache_key, activity) logger.info(f"Получена и закэширована активность {activity_id}: {activity.get('type')}") return activity except Exception as e: logger.error(f"Ошибка получения активности {activity_id}: {e}") if isinstance(e, requests.exceptions.RequestException): raise StravaApiError(f"Ошибка API Strava: {str(e)}") raise RuntimeError(f"Не удалось получить активность: {str(e)}")