search_datasets_with_facets
Search Hong Kong government open datasets with faceted filtering to explore and discover relevant public data resources efficiently.
Instructions
Search for datasets and return faceted results for better data exploration.
Args: query: The solr query string language: Language code (en, tc, sc)
Returns: A dictionary containing: - count: Total number of matching datasets - search_facets: Faceted information about the results - sample_results: First 3 matching datasets
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | No | en | |
| query | No | *:* |
Implementation Reference
- src/mcp_open_data_hk/server.py:232-266 (handler)The main handler function for the 'search_datasets_with_facets' tool, decorated with @mcp.tool for registration. It queries the data.gov.hk package_search API with facet parameters to return search results including sample datasets.@mcp.tool async def search_datasets_with_facets( query: str = "*:*", language: str = "en" ) -> Dict[str, Any]: """ Search for datasets and return faceted results for better data exploration. Args: query: The solr query string language: Language code (en, tc, sc) Returns: A dictionary containing: - count: Total number of matching datasets - search_facets: Faceted information about the results - sample_results: First 3 matching datasets """ # Using package_search API for search functionality base_url = BASE_URLS.get(language, BASE_URLS["en"]) url = f"{base_url}/package_search" rows = 3 # Number of sample results to return params = {"q": query, "rows": rows, "start": 0, "facet": "true", "facet.limit": 10} 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) > 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 the tool 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()
- src/mcp_open_data_hk/server.py:232-232 (registration)The @mcp.tool decorator that registers the search_datasets_with_facets function as an MCP tool.@mcp.tool