random_aircraft_type
Generate random aircraft types for aviation data analysis, testing, or simulation purposes using real-time flight data.
Instructions
MCP tool to get random aircraft type.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| number_of_aircraft | Yes |
Implementation Reference
- src/aviationstack_mcp/server.py:155-177 (handler)Handler function for 'random_aircraft_type' tool. Fetches aircraft types data from AviationStack API, samples a random subset based on input number, extracts name and ICAO code, and returns as JSON string. Includes error handling for requests and data parsing.def random_aircraft_type(number_of_aircraft: int) -> str: """MCP tool to get random aircraft type.""" try: data = fetch_flight_data('http://api.aviationstack.com/v1/aircraft_types', { 'limit': number_of_aircraft }) data_list = data.get('data', []) number_of_aircraft_to_fetch = min(number_of_aircraft, len(data_list)) # Sample random aircraft types from the data list sampled_aircraft_types = random.sample(data_list, number_of_aircraft_to_fetch) aircraft_types = [] for aircraft_type in sampled_aircraft_types: aircraft_types.append({ 'aircraft_name': aircraft_type.get('aircraft_name'), 'icao_code': aircraft_type.get('iata_code'), }) return json.dumps(aircraft_types) except requests.RequestException as e: return f"Request error: {str(e)}" except (KeyError, ValueError, TypeError) as e: return f"Error fetching aircraft type: {str(e)}"
- src/aviationstack_mcp/server.py:155-155 (registration)Registration of the 'random_aircraft_type' tool using the @mcp.tool() decorator on the FastMCP instance.def random_aircraft_type(number_of_aircraft: int) -> str:
- Shared helper function that fetches data from AviationStack API endpoints, adds API key from environment, handles HTTP requests and returns parsed JSON. Used by 'random_aircraft_type' and other tools.def fetch_flight_data(url: str, params: dict) -> dict: """Fetch flight data from the AviationStack API.""" api_key = os.getenv('AVIATION_STACK_API_KEY') if not api_key: raise ValueError("AVIATION_STACK_API_KEY not set in environment.") params = {'access_key': api_key, **params} response = requests.get(url, params=params, timeout=10) response.raise_for_status() return response.json()