get_exchange_rate
Retrieve current exchange rates from a specified base currency to all supported currencies using ISO 4217 currency codes.
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 tool handler for 'get_exchange_rate', decorated with @mcp.tool() for automatic registration, including input schema via type hints and docstring, and core logic delegating to the ExchangeRateClient helper.@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:5-44 (helper)Supporting ExchangeRateClient class with get_rates method that performs the actual API call, error handling, and JSON formatting for exchange rates.class ExchangeRateClient: def __init__(self, api_key: str): self.api_key = api_key self.base_url = "https://v6.exchangerate-api.com/v6" 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)Global instantiation of the ExchangeRateClient helper instance used by the tool handler.exchange_rate_client = ExchangeRateClient(os.getenv("EXCHANGERATE_API_KEY"))
- server.py:6-6 (helper)Import of the ExchangeRateClient helper module.from exchange_rates import ExchangeRateClient