get_stats_data_csv
Retrieve statistical data from Japan's e-Stat portal in CSV format for analysis, filtering by categories, time periods, and geographic areas.
Instructions
統計データをCSVで取得する.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| stats_data_id | Yes | ||
| cdcat01 | No | ||
| cdcat02 | No | ||
| cdcat03 | No | ||
| lvcat01 | No | ||
| lvcat02 | No | ||
| lvcat03 | No | ||
| lvcat04 | No | ||
| lvcat05 | No | ||
| lvcat06 | No | ||
| lvcat07 | No | ||
| lvcat08 | No | ||
| lvcat09 | No | ||
| lvcat10 | No | ||
| lvcat11 | No | ||
| lvcat12 | No | ||
| lvcat13 | No | ||
| lvcat14 | No | ||
| lvcat15 | No | ||
| cdtime | No | ||
| cdarea | No | ||
| start_position | No | ||
| section_header_flg | No | ||
| cnt_get_flg | No | ||
| limit | No |
Input Schema (JSON Schema)
{
"properties": {
"cdarea": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"cdcat01": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"cdcat02": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"cdcat03": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"cdtime": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"cnt_get_flg": {
"default": false,
"type": "boolean"
},
"limit": {
"default": 100,
"type": "integer"
},
"lvcat01": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat02": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat03": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat04": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat05": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat06": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat07": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat08": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat09": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat10": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat11": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat12": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat13": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat14": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"lvcat15": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null
},
"section_header_flg": {
"default": false,
"type": "boolean"
},
"start_position": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null
},
"stats_data_id": {
"type": "string"
}
},
"required": [
"stats_data_id"
],
"type": "object"
}
Implementation Reference
- e_stats_mcp/tools/stats.py:387-447 (handler)The primary handler function that implements the tool logic. It builds request parameters using _build_stats_data_params and fetches CSV data from the e-Stat API's getSimpleStatsData endpoint.async def get_stats_data_csv( stats_data_id: str, cdcat01: str | None = None, cdcat02: str | None = None, cdcat03: str | None = None, lvcat01: str | None = None, lvcat02: str | None = None, lvcat03: str | None = None, lvcat04: str | None = None, lvcat05: str | None = None, lvcat06: str | None = None, lvcat07: str | None = None, lvcat08: str | None = None, lvcat09: str | None = None, lvcat10: str | None = None, lvcat11: str | None = None, lvcat12: str | None = None, lvcat13: str | None = None, lvcat14: str | None = None, lvcat15: str | None = None, cdtime: str | None = None, cdarea: str | None = None, start_position: int | None = None, section_header_flg: bool = False, cnt_get_flg: bool = False, limit: int = 100, ) -> str: """統計データをCSVで取得する.""" params = _build_stats_data_params( stats_data_id=stats_data_id, cdcat01=cdcat01, cdcat02=cdcat02, cdcat03=cdcat03, lvcat01=lvcat01, lvcat02=lvcat02, lvcat03=lvcat03, lvcat04=lvcat04, lvcat05=lvcat05, lvcat06=lvcat06, lvcat07=lvcat07, lvcat08=lvcat08, lvcat09=lvcat09, lvcat10=lvcat10, lvcat11=lvcat11, lvcat12=lvcat12, lvcat13=lvcat13, lvcat14=lvcat14, lvcat15=lvcat15, cdtime=cdtime, cdarea=cdarea, start_position=start_position, section_header_flg=section_header_flg, cnt_get_flg=cnt_get_flg, limit=limit, ) response = await _make_request( "getSimpleStatsData", params, format="csv", ) return cast(str, response)
- e_stats_mcp/main.py:49-49 (registration)Registers the get_stats_data_csv function as an MCP tool using FastMCP's decorator.mcp.tool()(get_stats_data_csv)
- e_stats_mcp/tools/stats.py:250-322 (helper)Helper function to construct the API request parameters for stats data retrieval, handling optional categorization, levels, time, area, and pagination parameters.def _build_stats_data_params( *, stats_data_id: str, cdcat01: str | None = None, cdcat02: str | None = None, cdcat03: str | None = None, lvcat01: str | None = None, lvcat02: str | None = None, lvcat03: str | None = None, lvcat04: str | None = None, lvcat05: str | None = None, lvcat06: str | None = None, lvcat07: str | None = None, lvcat08: str | None = None, lvcat09: str | None = None, lvcat10: str | None = None, lvcat11: str | None = None, lvcat12: str | None = None, lvcat13: str | None = None, lvcat14: str | None = None, lvcat15: str | None = None, cdtime: str | None = None, cdarea: str | None = None, start_position: int | None = None, section_header_flg: bool = False, cnt_get_flg: bool = False, limit: int = 100, ) -> dict: """getStatsData系の共通パラメータを組み立てる.""" params = { "lang": "J", "statsDataId": stats_data_id, "limit": str(limit), } if cdcat01: params["cdCat01"] = cdcat01 if cdcat02: params["cdCat02"] = cdcat02 if cdcat03: params["cdCat03"] = cdcat03 lv_params = [ lvcat01, lvcat02, lvcat03, lvcat04, lvcat05, lvcat06, lvcat07, lvcat08, lvcat09, lvcat10, lvcat11, lvcat12, lvcat13, lvcat14, lvcat15, ] for idx, value in enumerate(lv_params, start=1): if value: params[f"lvCat{idx:02d}"] = value if cdtime: params["cdTime"] = cdtime if cdarea: params["cdArea"] = cdarea if start_position: params["startPosition"] = str(start_position) if section_header_flg: params["sectionHeaderFlg"] = "1" if cnt_get_flg: params["cntGetFlg"] = "1" return params
- e_stats_mcp/tools/stats.py:17-66 (helper)Core helper function that makes HTTP requests to the e-Stat API, handling authentication, parameters, JSON/CSV responses, and errors.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()