get_crypto_prices
Retrieve historical cryptocurrency price data for analysis by specifying ticker, date range, and interval parameters.
Instructions
Gets historical prices for a crypto currency.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ticker | Yes | ||
| start_date | Yes | ||
| end_date | Yes | ||
| interval | No | day | |
| interval_multiplier | No |
Implementation Reference
- server.py:245-272 (handler)The handler function decorated with @mcp.tool() for the get_crypto_prices MCP tool. It fetches historical cryptocurrency prices from the Financial Datasets API based on ticker, date range, and interval parameters, handles errors, and returns the prices as a formatted JSON string.@mcp.tool() async def get_crypto_prices( ticker: str, start_date: str, end_date: str, interval: str = "day", interval_multiplier: int = 1, ) -> str: """ Gets historical prices for a crypto currency. """ # Fetch data from the API url = f"{FINANCIAL_DATASETS_API_BASE}/crypto/prices/?ticker={ticker}&interval={interval}&interval_multiplier={interval_multiplier}&start_date={start_date}&end_date={end_date}" data = await make_request(url) # Check if data is found if not data: return "Unable to fetch prices or no prices found." # Extract the prices prices = data.get("prices", []) # Check if prices are found if not prices: return "Unable to fetch prices or no prices found." # Stringify the prices return json.dumps(prices, indent=2)
- server.py:25-41 (helper)Helper function used by the get_crypto_prices tool to make authenticated HTTP GET requests to the Financial Datasets API, loading API key from environment, handling timeouts and errors.async def make_request(url: str) -> dict[str, any] | None: """Make a request to the Financial Datasets API with proper error handling.""" # Load environment variables from .env file load_dotenv() headers = {} if api_key := os.environ.get("FINANCIAL_DATASETS_API_KEY"): headers["X-API-KEY"] = api_key async with httpx.AsyncClient() as client: try: response = await client.get(url, headers=headers, timeout=30.0) response.raise_for_status() return response.json() except Exception as e: return {"Error": str(e)}
- server.py:21-21 (helper)Constant defining the base URL for the Financial Datasets API, used in constructing endpoints for crypto prices.FINANCIAL_DATASETS_API_BASE = "https://api.financialdatasets.ai"