search_municipality_code
Find Spanish municipality codes and provinces by entering partial or approximate names, supporting accent-insensitive and typo-tolerant searches for quick, accurate results.
Instructions
Search Spanish municipalities by name or province (accent-insensitive, typo-tolerant).
Args: nombre: Partial or approximate name of a municipality or province.
Returns: A list of matching municipalities with their codes and provinces.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| nombre | Yes |
Implementation Reference
- src/aemet_mcp/server.py:116-153 (handler)The @mcp.tool()-decorated handler function implementing the core logic for searching municipality codes with fuzzy matching on names and provinces using preloaded JSON data.@mcp.tool() async def search_municipality_code(nombre: str): """ Search Spanish municipalities by name or province (accent-insensitive, typo-tolerant). Args: nombre: Partial or approximate name of a municipality or province. Returns: A list of matching municipalities with their codes and provinces. """ entrada = normalize(nombre.strip()) resultados = [] for municipio, codigo in MUNICIPIOS.items(): nombre_mun = normalize(municipio) cod_prov = codigo[:2] nombre_prov = normalize(CODIGO_A_PROVINCIA.get(cod_prov, "")) # Coincidencia exacta o parcial if entrada in nombre_mun or entrada in nombre_prov: resultados.append({ "municipio": municipio, "codigo": codigo, "provincia": CODIGO_A_PROVINCIA.get(cod_prov, "") }) # Coincidencia aproximada elif any(difflib.SequenceMatcher(None, entrada, campo).ratio() > 0.75 for campo in [nombre_mun, nombre_prov]): resultados.append({ "municipio": municipio, "codigo": codigo, "provincia": CODIGO_A_PROVINCIA.get(cod_prov, "") }) if not resultados: return {"error": f"No municipality matches found for '{nombre}'."} return resultados
- src/aemet_mcp/server.py:39-53 (helper)Preloading of municipality codes (MUNICIPIOS) and province code-to-name mapping (CODIGO_A_PROVINCIA) from embedded JSON resources, directly used in the tool handler.MUNICIPIOS = json.loads( files("aemet_mcp.res") .joinpath("Municipallity_code.json") .read_text(encoding="utf-8") ) PROVINCIAS = json.loads( files("aemet_mcp.res") .joinpath("Provinces_code.json") .read_text(encoding="utf-8") ) # Invertimos el dict para obtener código → nombre CODIGO_A_PROVINCIA = {code.zfill(2): name for code, name in PROVINCIAS.items()}
- src/aemet_mcp/server.py:78-80 (helper)Normalization utility function used for accent-insensitive string matching in the tool.def normalize(text: str) -> str: return unicodedata.normalize("NFKD", text).encode("ascii", "ignore").decode("ascii").lower()
- src/aemet_mcp/server.py:116-116 (registration)FastMCP decorator that registers the search_municipality_code tool.@mcp.tool()
- src/aemet_mcp/server.py:118-126 (schema)Docstring defining the tool's input (nombre: str) and output schema.""" Search Spanish municipalities by name or province (accent-insensitive, typo-tolerant). Args: nombre: Partial or approximate name of a municipality or province. Returns: A list of matching municipalities with their codes and provinces. """