get_municipality_data
Retrieve Japanese municipality data including names and codes, with filtering by prefecture or specific municipality codes and customizable field selection.
Instructions
市区町村名・市区町村コード一覧を取得する。
使い方:
- フィルタなし(既定): 全国すべての市区町村を返します(大量件数)。
- 都道府県で絞り込み: pref_codes=["13"] のように都道府県コードを指定(複数可)。
- 市区町村コードで絞り込み: muni_codes=["13101","13102"] のように6桁コードを指定(複数可)。
- 取得フィールドを最小化: fields=["code_as_string","name"] のように必要フィールドだけ指定(クライアント最適化)。
例:
- 全国の市区町村(コード/名称のみ):
pref_codes=[], muni_codes=[], fields=["code_as_string","name"]
- 東京都の市区町村一覧:
pref_codes=["13"], fields=["code_as_string","prefecture_code","name","katakana"]
- 特定の市区町村コードを直接取得:
muni_codes=["13101","13102"], fields=["code_as_string","name","romaji"]
注意:
- GraphQL仕様: `municipalities(muniCodes:[Any], prefCodes:[Any]): [MunicipalityClass]`。
パラメータ未指定時は**全件**を返します(大量になるため fields での軽量化推奨)。:contentReference[oaicite:1]{index=1}
- コードは数値/文字列どちらでも指定可能ですが、アプリ側で扱いやすいのは **6桁の文字列** `code_as_string` です(例: "13101")。:contentReference[oaicite:2]{index=2}
- 返却クラス `MunicipalityClass` には、`name`(郡名/市区町村名/政令市区名を組み合わせた正式名称)、`katakana`、`romaji`、`prefecture_code`、および有効期間(`used_from`/`used_until`)等が含まれます。:contentReference[oaicite:3]{index=3}
- 政令指定都市の区は**独立したエントリ**として返ります(例: 札幌市中央区 など)。:contentReference[oaicite:4]{index=4}Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pref_codes | No | 都道府県コードの配列 (例: ['13', '27']) | |
| muni_codes | No | 市区町村コードの配列 (例: ['13101', '13102']) | |
| fields | No | 取得するフィールド名の配列。デフォルト: ['code_as_string', 'prefecture_code', 'name'] | |
| pref_code | No | 単一都道府県コード(後方互換性用)。pref_codesの使用を推奨 |
Implementation Reference
- src/server.py:1330-1337 (handler)The 'get_municipality_data' tool handler, which validates input using Pydantic and then calls the client's 'get_municipalities' method.
elif name == "get_municipality_data": p = GetMunicipalitiesParams.model_validate(arguments) data = await client.get_municipalities( pref_codes=p.pref_codes, muni_codes=p.muni_codes, fields=p.fields, ) - src/client.py:789-820 (handler)The client implementation of 'get_municipalities', which constructs and posts the GraphQL query to retrieve municipality data.
async def get_municipalities( self, pref_codes: Optional[List[str]] = None, muni_codes: Optional[List[str]] = None, *, fields: Optional[List[str]] = None, ) -> Dict[str, Any]: if not pref_codes and not muni_codes: raise ValueError("Either pref_codes or muni_codes must be provided") def q(s: str) -> str: return '"' + s.replace('"', '\\"') + '"' args: List[str] = [] if pref_codes: pref = "[" + ", ".join(q(c) for c in pref_codes) + "]" args.append(f"prefCodes: {pref}") if muni_codes: muni = "[" + ", ".join(q(c) for c in muni_codes) + "]" args.append(f"muniCodes: {muni}") default_fields = ["code_as_string", "prefecture_code", "name"] out_fields = " ".join(fields if fields else default_fields) ql = f""" query {{ municipalities({", ".join(args)}) {{ {out_fields} }} }} """.strip() return await self.post_query(ql)