get_latest_usd_mxn_rate
Retrieve the current USD/MXN exchange rate and its corresponding date from Banxico. Use this to access accurate currency conversion data for financial decision-making.
Instructions
Get the latest USD/MXN exchange rate from Banxico.
Returns: The most recent USD/MXN exchange rate with date
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- banxico_mcp_server.py:322-340 (handler)The main handler function for the 'get_latest_usd_mxn_rate' tool. It checks for the API token, makes a request to the Banxico SIE API timely data endpoint for USD/MXN (SF63528), handles errors, and formats the response using helper functions.@mcp.tool() async def get_latest_usd_mxn_rate() -> str: """ Get the latest USD/MXN exchange rate from Banxico. Returns: The most recent USD/MXN exchange rate with date """ if not BANXICO_TOKEN: return "Error: BANXICO_API_TOKEN environment variable not set. Please configure your API token." endpoint = "series/SF63528/datos/oportuno" data = await make_banxico_request(endpoint, BANXICO_TOKEN) if not data: return "Failed to retrieve exchange rate data. Please check your API token and network connection." return format_exchange_rate_data(data)
- banxico_mcp_server.py:43-69 (helper)Core helper function that performs authenticated HTTP GET requests to the Banxico API, handles HTTP errors and timeouts, and returns parsed JSON or None on failure.async def make_banxico_request(endpoint: str, token: str) -> dict[str, Any] | None: """ Make a request to the Banxico SIE API with proper error handling. Args: endpoint: The API endpoint to call (without base URL) token: The Banxico API token Returns: JSON response data or None if request failed """ url = f"{BANXICO_API_BASE}/{endpoint}" headers = {"User-Agent": USER_AGENT} params = {"token": token} try: async with httpx.AsyncClient() as client: response = await client.get(url, headers=headers, params=params, timeout=30.0) response.raise_for_status() return response.json() except httpx.HTTPError as e: logger.error(f"HTTP error occurred: {e}") return None except Exception as e: logger.error(f"An error occurred: {e}") return None
- banxico_mcp_server.py:71-123 (helper)Helper function specifically for formatting exchange rate data from the API response into a readable multi-line string, displaying series metadata and recent/historical data points.def format_exchange_rate_data(data: dict[str, Any]) -> str: """ Format exchange rate data into a readable string. Args: data: Raw JSON response from Banxico API Returns: Formatted string with exchange rate information """ if not data or "bmx" not in data: return "No data available" series_list = data["bmx"].get("series", []) if not series_list: return "No series data found" result = [] for series in series_list: series_title = series.get("titulo", "Unknown Series") series_id = series.get("idSerie", "Unknown ID") result.append(f"Series: {series_title} (ID: {series_id})") datos = series.get("datos", []) if not datos: result.append(" No data points available") else: result.append(f" Total data points: {len(datos)}") # Show first few and last few data points if len(datos) <= 10: for dato in datos: fecha = dato.get("fecha", "Unknown date") valor = dato.get("dato", "N/A") result.append(f" {fecha}: {valor}") else: # Show first 5 for i, dato in enumerate(datos[:5]): fecha = dato.get("fecha", "Unknown date") valor = dato.get("dato", "N/A") result.append(f" {fecha}: {valor}") result.append(f" ... ({len(datos) - 10} more data points) ...") # Show last 5 for dato in datos[-5:]: fecha = dato.get("fecha", "Unknown date") valor = dato.get("dato", "N/A") result.append(f" {fecha}: {valor}") result.append("") # Empty line between series return "\n".join(result)
- banxico_mcp_server.py:322-322 (registration)The @mcp.tool() decorator registers the get_latest_usd_mxn_rate function as an MCP tool in the FastMCP server.@mcp.tool()