get_exchange_rate
Retrieve current exchange rates from a specified base currency to all supported currencies using ISO 4217 codes for accurate financial data.
Instructions
Get the latest exchange rates from provided base currency code (ISO 4217) to all other supported currencies
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| currency_code | Yes |
Implementation Reference
- server.py:29-33 (handler)The handler function for the 'get_exchange_rate' tool, registered with @mcp.tool(). It invokes the ExchangeRateClient to fetch and return exchange rates for the given currency code.@mcp.tool() async def get_exchange_rate(currency_code: str) -> str: """Get the latest exchange rates from provided base currency code (ISO 4217) to all other supported currencies""" exchange_rates = exchange_rate_client.get_rates(code=currency_code) return exchange_rates
- exchange_rates.py:10-44 (helper)Supporting method in ExchangeRateClient class that handles the HTTP request to the exchange rate API, parses the response, and formats the output as JSON.def get_rates(self, code: str) -> str: """Get latest exchange rates for the specified base currency code""" if not self.api_key: raise ValueError("API key is required") url = f"{self.base_url}/{self.api_key}/latest/{code.upper()}" try: response = requests.get(url) response.raise_for_status() data = response.json() if data.get("result") == "success": rates = data.get("conversion_rates", {}) base_code = data.get("base_code") last_update = data.get("time_last_update_utc") result = { "base_currency": base_code, "last_updated": last_update, "exchange_rates": rates } return json.dumps(result, indent=2) else: error_type = data.get("error-type", "unknown") return f"Error: {error_type}" except requests.exceptions.RequestException as e: return f"Network error: {str(e)}" except json.JSONDecodeError: return "Error: Invalid response format" except Exception as e: return f"Unexpected error: {str(e)}"
- server.py:12-12 (helper)Instantiation of the ExchangeRateClient with API key from environment variable, used by the get_exchange_rate tool.exchange_rate_client = ExchangeRateClient(os.getenv("EXCHANGERATE_API_KEY"))
- server.py:29-29 (registration)The @mcp.tool() decorator registers the get_exchange_rate function as an MCP tool.@mcp.tool()
- server.py:30-31 (schema)Function signature with type hints and docstring defining the input (currency_code: str) and output (str), serving as the tool schema.async def get_exchange_rate(currency_code: str) -> str: """Get the latest exchange rates from provided base currency code (ISO 4217) to all other supported currencies"""