search_datasets
Find datasets from Hong Kong's open data portal by searching titles, descriptions, and metadata. Use keywords to discover relevant public data resources for analysis and research.
Instructions
Search for datasets by query term using the package_search API.
This function searches across dataset titles, descriptions, and other metadata to find datasets matching the query term.
Args: query: The solr query string (e.g., "transport", "weather", ":" for all) limit: Maximum number of datasets to return (default: 10, max: 1000) offset: Offset for pagination language: Language code (en, tc, sc)
Returns: A dictionary containing: - count: Total number of matching datasets - results: List of matching datasets (up to limit) - has_more: Boolean indicating if there are more results available
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | No | en | |
| limit | No | ||
| offset | No | ||
| query | No | *:* |
Implementation Reference
- src/mcp_open_data_hk/server.py:176-218 (handler)The handler function for the 'search_datasets' tool, including its @mcp.tool registration decorator. It implements the core logic to search datasets on data.gov.hk using the package_search API endpoint, with input parameters for query, limit, offset, and language.@mcp.tool async def search_datasets( query: str = "*:*", limit: int = 10, offset: int = 0, language: str = "en" ) -> Dict[str, Any]: """ Search for datasets by query term using the package_search API. This function searches across dataset titles, descriptions, and other metadata to find datasets matching the query term. Args: query: The solr query string (e.g., "transport", "weather", "*:*" for all) limit: Maximum number of datasets to return (default: 10, max: 1000) offset: Offset for pagination language: Language code (en, tc, sc) Returns: A dictionary containing: - count: Total number of matching datasets - results: List of matching datasets (up to limit) - has_more: Boolean indicating if there are more results available """ # Using package_search API for search functionality base_url = BASE_URLS.get(language, BASE_URLS["en"]) url = f"{base_url}/package_search" # Limit the maximum number of results rows = min(limit, 1000) params = {"q": query, "rows": rows, "start": offset} 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", []), "has_more": search_result.get("count", 0) > (offset + rows), } else: raise Exception(f"API Error: {result.get('error', 'Unknown error')}")
- src/mcp_open_data_hk/server.py:19-30 (helper)Helper function used by search_datasets (and other tools) 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()