list_countries
Retrieve available countries for a specific IMF dataset to filter and access relevant economic data for analysis.
Instructions
Returns a list of available countries for the specified dataset, read from the corresponding .json file in the local areas directory.
Args:
dataset_id (str): Dataset ID, such as "IFS", "DOT", "BOP", etc.
Returns:
list: List of countries.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dataset_id | Yes |
Implementation Reference
- imf_data_mcp/__init__.py:313-334 (handler)Handler function that implements the list_countries tool by loading a dataset-specific JSON file containing country codes from the resources/areas directory and returning the list or an appropriate error.def list_countries(dataset_id: str) -> list: """ Returns a list of available countries for the specified dataset, read from the corresponding .json file in the local areas directory. Args: dataset_id (str): Dataset ID, such as "IFS", "DOT", "BOP", etc. Returns: list: List of countries. """ file_path = os.path.join(os.path.dirname(__file__), "resources", "areas", f"{dataset_id.lower()}.json") try: with open(file_path, 'r', encoding='utf-8') as file: data = json.load(file) return data except FileNotFoundError: return {"error": f"File not found: {file_path}"} except json.JSONDecodeError: return {"error": f"Error decoding JSON from file: {file_path}"} except Exception as e: return {"error": f"Error reading file: {str(e)}"}
- imf_data_mcp/__init__.py:312-312 (registration)Registers the list_countries function as a tool in the FastMCP server.@mcp.tool()
- imf_data_mcp/__init__.py:313-322 (schema)Type hints and docstring defining the input parameter dataset_id (str) and return type list for the list_countries tool.def list_countries(dataset_id: str) -> list: """ Returns a list of available countries for the specified dataset, read from the corresponding .json file in the local areas directory. Args: dataset_id (str): Dataset ID, such as "IFS", "DOT", "BOP", etc. Returns: list: List of countries. """
- imf_data_mcp/__init__.py:336-363 (helper)Prompt template that references and describes the list_countries tool for use in guiding users.@mcp.prompt() def imf_query_prompt() -> str: """ Returns a prompt template explaining how to query IMF data with indicators and user intentions. Returns: str: A prompt template for guiding users on querying IMF data. """ prompt_text = """ You are a professional IMF data analysis assistant. Please follow these steps to help users obtain and analyze IMF data: 1. First, use the imf://datasets resource to get a list of available datasets and show the user the 5-10 most commonly used datasets with a brief description. 2. When the user selects a dataset, use the following two tools to get detailed information about the dataset: - list_countries: List available country or region codes - list_indicators: List available indicator codes and names 3. Assist the user in determining their interests: - Country or region (provide codes, and multiple country codes can be connected with "+") - Indicator (provide codes and names) - Time range (start year and end year) - Data frequency (if applicable: annual A, quarterly Q, monthly M, etc.) 4. Based on the user's selection, construct appropriate query parameters and use the fetch_compact_data tool to get the data ** Note: When you get warnings like "Warning: No indicator value" or "Warning: No indicator value for {country} in that Year," it means there is a lack of data for that period. Do not perform further analysis or retry. ** """ return prompt_text