analyze_data_tool
Analyze Strava activity data by executing custom Python code to calculate metrics, summarize workouts, and extract insights from athlete statistics.
Instructions
Execute Python code to analyze Strava data safely using Monty.
Args: code: Python code to execute. The data is available as a variable named 'data'. Example: "sum(activity['distance'] for activity in data) / 1000" data: The data to analyze (e.g. list of activities, athlete stats). Can be passed as a JSON object (list/dict) or a JSON string.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| data | Yes |
Implementation Reference
- server.py:142-164 (handler)The main MCP tool handler that executes Python code to analyze Strava data. Handles JSON string parsing, delegates to analyze_data helper, and provides error handling.
@mcp.tool() def analyze_data_tool(code: str, data: Any) -> Any: """ Execute Python code to analyze Strava data safely using Monty. Args: code: Python code to execute. The data is available as a variable named 'data'. Example: "sum(activity['distance'] for activity in data) / 1000" data: The data to analyze (e.g. list of activities, athlete stats). Can be passed as a JSON object (list/dict) or a JSON string. """ # If data is a string, try to parse it as JSON if isinstance(data, str): try: data = json.loads(data) except json.JSONDecodeError: pass # Treat as raw string if not valid JSON try: result = analyze_data(code, data) return result except Exception as e: return f"Error executing code: {str(e)}" - strava_mcp/services/analysis.py:5-29 (helper)Core implementation using pydantic_monty to safely execute Python code. Injects data as a variable and returns the execution result with error handling.
def analyze_data(code: str, data: Any) -> Any: """ Executes Python code safely using Monty, passing 'data' as a variable. Args: code: The Python code snippet to execute. data: The data structure (dict, list, etc.) to inject as the 'data' variable. """ # Always inject data as 'data' variable inputs = {"data": data} input_names = ["data"] try: # Initialize Monty with the code and expected input variables # Using strict limits by default for safety m = pydantic_monty.Monty(code, inputs=input_names) # Execute the code result = m.run(inputs=inputs) return result except Exception as e: # Raise a clear error message that the MCP client can display raise RuntimeError(f"Analysis failed: {str(e)}") from e - server.py:142-144 (registration)Registration of analyze_data_tool with FastMCP using the @mcp.tool() decorator.
@mcp.tool() def analyze_data_tool(code: str, data: Any) -> Any: """