compare_drivers
Analyze and compare Formula One driver performance by season, event, and session using driver codes, providing insights into race results and statistics.
Instructions
Compare performance between multiple Formula One drivers
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| drivers | Yes | Comma-separated list of driver codes (e.g., "HAM,VER,LEC") | |
| event_identifier | Yes | Event name or round number (e.g., "Monaco" or "7") | |
| session_name | Yes | Session name (e.g., "Race", "Qualifying", "Sprint", "FP1", "FP2", "FP3") | |
| year | Yes | Season year (e.g., 2023) |
Input Schema (JSON Schema)
{
"properties": {
"drivers": {
"description": "Comma-separated list of driver codes (e.g., \"HAM,VER,LEC\")",
"type": "string"
},
"event_identifier": {
"description": "Event name or round number (e.g., \"Monaco\" or \"7\")",
"type": "string"
},
"session_name": {
"description": "Session name (e.g., \"Race\", \"Qualifying\", \"Sprint\", \"FP1\", \"FP2\", \"FP3\")",
"type": "string"
},
"year": {
"description": "Season year (e.g., 2023)",
"type": "number"
}
},
"required": [
"year",
"event_identifier",
"session_name",
"drivers"
],
"type": "object"
}
Implementation Reference
- python/f1_data.py:155-195 (handler)Core implementation of the compare_drivers tool. Loads F1 session data using fastf1, computes and compares key performance metrics (fastest lap, average lap time, total laps, etc.) for multiple drivers specified as a comma-separated list.def compare_drivers(year, event_identifier, session_name, drivers): """Compare performance between multiple drivers""" try: year = int(year) drivers_list = drivers.split(",") session = fastf1.get_session(year, event_identifier, session_name) session.load() driver_comparisons = [] for driver in drivers_list: # Get laps and fastest lap for each driver driver_laps = session.laps.pick_driver(driver) fastest_lap = driver_laps.pick_fastest() # Calculate average lap time valid_lap_times = [] for _, lap in driver_laps.iterrows(): if lap['LapTime'] is not None and not pd.isna(lap['LapTime']): valid_lap_times.append(lap['LapTime'].total_seconds()) avg_lap_time = sum(valid_lap_times) / len(valid_lap_times) if valid_lap_times else None # Format lap time as string formatted_fastest = str(fastest_lap['LapTime']) if not pd.isna(fastest_lap['LapTime']) else None # Compile driver data driver_data = { "DriverCode": driver, "FastestLap": formatted_fastest, "FastestLapNumber": int(fastest_lap['LapNumber']) if not pd.isna(fastest_lap['LapNumber']) else None, "TotalLaps": len(driver_laps), "AverageLapTime": avg_lap_time } driver_comparisons.append(driver_data) return {"status": "success", "data": driver_comparisons} except Exception as e: return {"status": "error", "message": str(e), "traceback": traceback.format_exc()}
- src/index.ts:197-222 (registration)MCP tool registration in the listTools response, including name, description, and input schema validation.{ name: 'compare_drivers', description: 'Compare performance between multiple Formula One drivers', inputSchema: { type: 'object', properties: { year: { type: 'number', description: 'Season year (e.g., 2023)', }, event_identifier: { type: 'string', description: 'Event name or round number (e.g., "Monaco" or "7")', }, session_name: { type: 'string', description: 'Session name (e.g., "Race", "Qualifying", "Sprint", "FP1", "FP2", "FP3")', }, drivers: { type: 'string', description: 'Comma-separated list of driver codes (e.g., "HAM,VER,LEC")', }, }, required: ['year', 'event_identifier', 'session_name', 'drivers'], }, },
- src/index.ts:284-284 (schema)TypeScript type definition for the input arguments of the compare_drivers tool.type CompareDriversArgs = { year: number; event_identifier: string; session_name: string; drivers: string };
- src/index.ts:339-347 (handler)TypeScript handler that proxies the compare_drivers tool call to the Python backend via executePythonFunction.case 'compare_drivers': { const typedArgs = args as CompareDriversArgs; result = await executePythonFunction('compare_drivers', [ typedArgs.year.toString(), typedArgs.event_identifier.toString(), typedArgs.session_name.toString(), typedArgs.drivers.toString(), ]); break;
- python/f1_data.py:296-296 (registration)Registration of the compare_drivers function in the Python bridge's function dispatch dictionary."compare_drivers": compare_drivers,