western_to_era
Convert a Gregorian year to the corresponding Japanese era year, returning era name in kanji and English, year of era, and formatted strings like '令和8年' and 'R8'.
Instructions
Convert a Western (Gregorian) year to its Japanese era year.
Note: this returns the era in effect for the majority of the given Gregorian year. For year transitions (e.g. 1989 split between 昭和64 and 平成1), it returns the newer era.
Args: year: Western year (e.g. 2026). Must be 1868 or later.
Returns: dict with keys: - era_kanji: str - era_english: str - year_of_era: int (1 for the first year of an era; written as 元年 in formal Japanese) - era_year_kanji: str (e.g. '令和8年') - era_year_short: str (e.g. 'R8')
Examples: western_to_era(2026) → {"era_kanji": "令和", "era_english": "Reiwa", "year_of_era": 8, "era_year_kanji": "令和8年", "era_year_short": "R8"}
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| year | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/japan_utils_mcp/server.py:126-165 (handler)The `western_to_era` function itself — the actual tool handler that converts a Western year to a Japanese era year. It iterates over `_ERAS` (ordered from newest to oldest), finds the matching era, and returns era_kanji, era_english, year_of_era, era_year_kanji, and era_year_short.
def western_to_era(year: int) -> dict[str, Any]: """Convert a Western (Gregorian) year to its Japanese era year. Note: this returns the era in effect for the *majority* of the given Gregorian year. For year transitions (e.g. 1989 split between 昭和64 and 平成1), it returns the newer era. Args: year: Western year (e.g. 2026). Must be 1868 or later. Returns: dict with keys: - era_kanji: str - era_english: str - year_of_era: int (1 for the first year of an era; written as 元年 in formal Japanese) - era_year_kanji: str (e.g. '令和8年') - era_year_short: str (e.g. 'R8') Examples: western_to_era(2026) → {"era_kanji": "令和", "era_english": "Reiwa", "year_of_era": 8, "era_year_kanji": "令和8年", "era_year_short": "R8"} """ if year < 1868: raise ValueError("Years before 1868 (Meiji 1) are not supported.") for start_year, kanji, english, letter in _ERAS: if year >= start_year: year_of_era = year - start_year + 1 if year_of_era == 1: era_year_kanji = f"{kanji}元年" else: era_year_kanji = f"{kanji}{year_of_era}年" return { "era_kanji": kanji, "era_english": english, "year_of_era": year_of_era, "era_year_kanji": era_year_kanji, "era_year_short": f"{letter}{year_of_era}", } - src/japan_utils_mcp/server.py:125-125 (registration)The `@mcp.tool()` decorator that registers `western_to_era` as an MCP tool on the FastMCP server instance.
@mcp.tool() - src/japan_utils_mcp/server.py:36-42 (helper)The `_ERAS` constant — the era data list used by `western_to_era`. Defines the 5 supported eras (Reiwa, Heisei, Showa, Taisho, Meiji) with their start years, kanji names, English names, and single-letter aliases.
_ERAS: list[tuple[int, str, str, str]] = [ (2019, "令和", "Reiwa", "R"), (1989, "平成", "Heisei", "H"), (1926, "昭和", "Showa", "S"), (1912, "大正", "Taisho", "T"), (1868, "明治", "Meiji", "M"), ] - src/japan_utils_mcp/server.py:44-49 (helper)Lookup dictionaries `_ERA_KANJI_TO_INFO` and `_ERA_LETTER_TO_INFO` built from `_ERAS`, used by `era_to_western` and referenced by the era data structure.
_ERA_KANJI_TO_INFO: dict[str, tuple[int, str, str, str]] = { e[1]: e for e in _ERAS } _ERA_LETTER_TO_INFO: dict[str, tuple[int, str, str, str]] = { e[3]: e for e in _ERAS }