get_datasets_by_format
Filter Hong Kong government open datasets by file format to find CSV, JSON, or GeoJSON resources for data analysis and integration.
Instructions
Get datasets that have resources in a specific file format.
Args: file_format: The file format to filter by (e.g., "CSV", "JSON", "GeoJSON") limit: Maximum number of datasets to return language: Language code (en, tc, sc)
Returns: A dictionary containing: - count: Total number of matching datasets - results: List of matching datasets
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_format | Yes | ||
| limit | No | ||
| language | No | en |
Implementation Reference
- src/mcp_open_data_hk/server.py:269-305 (handler)The @mcp.tool decorated handler function that implements the get_datasets_by_format tool. It searches the data.gov.hk package_search API using a res_format filter query to find datasets available in the specified file format.@mcp.tool async def get_datasets_by_format( file_format: str, limit: int = 10, language: str = "en" ) -> Dict[str, Any]: """ Get datasets that have resources in a specific file format. Args: file_format: The file format to filter by (e.g., "CSV", "JSON", "GeoJSON") limit: Maximum number of datasets to return language: Language code (en, tc, sc) Returns: A dictionary containing: - count: Total number of matching datasets - results: List of matching datasets """ # Using package_search API with format filter base_url = BASE_URLS.get(language, BASE_URLS["en"]) url = f"{base_url}/package_search" # Create a query that filters by format query = f"res_format:{file_format}" params = {"q": query, "rows": min(limit, 1000), "start": 0} result = await make_api_request(url, params) if result.get("success"): search_result = result["result"] return { "count": search_result.get("count", 0), "results": search_result.get("results", []), } else: raise Exception(f"API Error: {result.get('error', 'Unknown error')}")
- src/mcp_open_data_hk/server.py:269-269 (registration)The FastMCP decorator that registers the get_datasets_by_format tool.@mcp.tool
- src/mcp_open_data_hk/server.py:19-30 (helper)Helper function used by get_datasets_by_format to make HTTP requests to the data.gov.hk API.async def make_api_request( url: str, params: Optional[Dict[str, Any]] = None ) -> Dict[str, Any]: """Make an API request to data.gov.hk""" async with httpx.AsyncClient() as client: # Print the request for debugging print(f"Making request to {url} with params {params}") response = await client.get(url, params=params) print(f"Response status: {response.status_code}") response.raise_for_status() return response.json()