get_exchange_rate
Convert currency values by retrieving real-time exchange rates between two specified currencies, returning null if an error occurs during the process.
Instructions
Obtiene la cotización entre dos monedas. Devuelve None si hay error.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from_currency | Yes | ||
| to_currency | Yes |
Implementation Reference
- server.py:25-35 (handler)The get_exchange_rate tool implementation - decorated with @mcp.tool() and contains the handler logic that makes an API call to exchangerate-api.com to fetch exchange rates between two currencies
@mcp.tool() def get_exchange_rate(from_currency: str, to_currency: str) -> float | None: """Obtiene la cotización entre dos monedas. Devuelve None si hay error.""" try: response = requests.get(f"https://api.exchangerate-api.com/v4/latest/{from_currency}", timeout=5) response.raise_for_status() data = response.json() return data.get("rates", {}).get(to_currency) except Exception as e: print(f"Error al obtener cotización: {e}") return None - server.py:1-9 (registration)Server setup and FastMCP initialization - creates the MCP server instance that registers the tool decorator used by get_exchange_rate
from mcp.server.fastmcp import FastMCP import requests import os from dotenv import load_dotenv load_dotenv() # Create an MCP server mcp = FastMCP("Demo", host="0.0.0.0", port=int(os.getenv("PORT", 8000)))