get_stats_list_csv
Retrieve Japanese government statistics in CSV format from e-Stat by filtering with keywords, years, fields, or codes for data analysis.
Instructions
統計表情報をCSVで取得する.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| search_word | No | ||
| survey_years | No | ||
| stats_field | No | ||
| stats_code | No | ||
| gov_code | No | ||
| open_years | No | ||
| stats_name_list | No | ||
| start_position | No | ||
| updated_date | No | ||
| limit | No |
Implementation Reference
- e_stats_mcp/tools/stats.py:340-370 (handler)The core handler function implementing the get_stats_list_csv tool. It builds parameters using _build_stats_list_params and makes an API request to the e-Stat 'getSimpleStatsList' endpoint in CSV format.async def get_stats_list_csv( search_word: str | None = None, survey_years: str | None = None, stats_field: str | None = None, stats_code: str | None = None, gov_code: str | None = None, open_years: str | None = None, stats_name_list: str | None = None, start_position: int | None = None, updated_date: str | None = None, limit: int = 10, ) -> str: """統計表情報をCSVで取得する.""" params = _build_stats_list_params( search_word=search_word, survey_years=survey_years, stats_field=stats_field, stats_code=stats_code, gov_code=gov_code, open_years=open_years, stats_name_list=stats_name_list, start_position=start_position, updated_date=updated_date, limit=limit, ) response = await _make_request( "getSimpleStatsList", params, format="csv", ) return cast(str, response)
- e_stats_mcp/main.py:47-47 (registration)Registers the get_stats_list_csv function as an MCP tool using FastMCP's @mcp.tool() decorator.mcp.tool()(get_stats_list_csv)
- e_stats_mcp/main.py:22-25 (registration)Imports the get_stats_list_csv function from e_stats_mcp.tools for use in tool registration.get_stats_list_csv, post_dataset, search_stats_by_keyword, )
- e_stats_mcp/tools/stats.py:114-152 (helper)Helper function to build query parameters for stats list API calls, used by get_stats_list_csv.def _build_stats_list_params( *, search_word: str | None = None, survey_years: str | None = None, stats_field: str | None = None, stats_code: str | None = None, gov_code: str | None = None, open_years: str | None = None, stats_name_list: str | None = None, start_position: int | None = None, updated_date: str | None = None, limit: int = 10, ) -> dict: """getStatsList系の共通パラメータを組み立てる.""" params = { "lang": "J", "limit": str(min(limit, 100)), } if search_word: params["searchWord"] = search_word if survey_years: params["surveyYears"] = survey_years if stats_field: params["statsField"] = stats_field if stats_code: params["statsCode"] = stats_code if gov_code: params["governmentCode"] = gov_code if open_years: params["openYears"] = open_years if stats_name_list: params["statsNameList"] = stats_name_list if start_position: params["startPosition"] = str(start_position) if updated_date: params["updatedDate"] = updated_date return params
- e_stats_mcp/tools/stats.py:17-66 (helper)Core helper function that performs HTTP requests to the e-Stat API, handling both JSON and CSV responses, used by get_stats_list_csv.async def _make_request( endpoint: str, params: dict | None = None, *, method: str = "GET", format: str = "json", data: dict | None = None, ) -> dict[str, Any] | str: """e-Stat APIへのリクエストを実行する. Args: endpoint: APIエンドポイント(例: \"json/getStatsList\") params: クエリパラメータ method: HTTPメソッド(GET/POST) format: レスポンス形式(\"json\" または \"csv\") data: POST時のフォームデータ Returns: JSONの場合はdict、CSVの場合は文字列 Raises: ValueError: APIキーが設定されていない場合 httpx.HTTPError: API通信エラー """ settings = get_settings() if not settings.E_STAT_APP_ID: raise ValueError( "E_STAT_APP_ID環境変数が設定されていません。" "https://www.e-stat.go.jp/api/ からアプリケーションIDを取得してください。" ) url = f"{E_STAT_API_BASE}/{endpoint}" request_params = {"appId": settings.E_STAT_APP_ID, **(params or {})} http_method = method.upper() async with httpx.AsyncClient() as client: if http_method == "POST": response = await client.post( url, params=request_params, data=data, timeout=DEFAULT_TIMEOUT ) else: response = await client.get( url, params=request_params, timeout=DEFAULT_TIMEOUT ) response.raise_for_status() if format == "csv": return response.text return response.json()