random_countries_detailed_info
Retrieve detailed information about random countries with customizable input for the number of countries to fetch using the Aviationstack MCP Server.
Instructions
MCP tool to get random countries detailed info.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| number_of_countries | Yes |
Implementation Reference
- src/aviationstack_mcp/server.py:215-248 (handler)The handler function for the 'random_countries_detailed_info' tool. It fetches country data from the AviationStack API, randomly selects the specified number of countries, extracts detailed information, and returns it as a JSON string.@mcp.tool() def random_countries_detailed_info(number_of_countries: int) -> str: """MCP tool to get random countries detailed info.""" try: data = fetch_flight_data('http://api.aviationstack.com/v1/countries', { 'limit': number_of_countries }) data_list = data.get('data', []) number_of_countries_to_fetch = min(number_of_countries, len(data_list)) # Sample random countries from the data list sampled_countries = random.sample(data_list, number_of_countries_to_fetch) countries = [] for country in sampled_countries: countries.append({ 'country_name': country.get('name'), 'capital': country.get('capital'), 'currency_code': country.get('currency_code'), 'fips_code': country.get('fips_code'), 'country_iso2': country.get('country_iso2'), 'country_iso3': country.get('country_iso3'), 'continent': country.get('continent'), 'country_id': country.get('country_id'), 'currency_name': country.get('currency_name'), 'country_iso_numeric': country.get('country_iso_numeric'), 'phone_prefix': country.get('phone_prefix'), 'population': country.get('population'), }) return json.dumps(countries) except requests.RequestException as e: return f"Request error: {str(e)}" except (KeyError, ValueError, TypeError) as e: return f"Error fetching countries: {str(e)}"
- Helper utility function used by the tool to fetch data from the AviationStack API endpoints, handling API key and requests.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()
- src/aviationstack_mcp/server.py:11-11 (registration)Creation of the FastMCP server instance to which all tools, including 'random_countries_detailed_info', are registered via decorators.mcp = FastMCP("Aviationstack MCP")
- Function signature and docstring defining the input schema (number_of_countries: int) and output (str JSON).def random_countries_detailed_info(number_of_countries: int) -> str: """MCP tool to get random countries detailed info."""