Skip to main content
Glama

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
NameRequiredDescriptionDefault
nombreYes

Implementation Reference

  • 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
  • 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()}
  • 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()
  • FastMCP decorator that registers the search_municipality_code tool.
    @mcp.tool()
  • 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. """

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/AnCode666/aemet-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server