get_current_crypto_price
Retrieve real-time cryptocurrency prices by specifying the ticker symbol, facilitating accurate financial analysis and decision-making within the Financial Datasets MCP Server environment.
Instructions
Get the current / latest price of a crypto currency.
Args:
ticker: Ticker symbol of the crypto currency (e.g. BTC-USD). The list of available crypto tickers can be retrieved via the get_available_crypto_tickers tool.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ticker | Yes |
Implementation Reference
- server.py:311-334 (handler)The @mcp.tool()-decorated handler function implementing the get_current_crypto_price tool. It fetches the latest cryptocurrency price snapshot from the Financial Datasets API endpoint /crypto/prices/snapshot/, handles errors, and returns formatted JSON.@mcp.tool() async def get_current_crypto_price(ticker: str) -> str: """Get the current / latest price of a crypto currency. Args: ticker: Ticker symbol of the crypto currency (e.g. BTC-USD). The list of available crypto tickers can be retrieved via the get_available_crypto_tickers tool. """ # Fetch data from the API url = f"{FINANCIAL_DATASETS_API_BASE}/crypto/prices/snapshot/?ticker={ticker}" data = await make_request(url) # Check if data is found if not data: return "Unable to fetch current price or no current price found." # Extract the current price snapshot = data.get("snapshot", {}) # Check if current price is found if not snapshot: return "Unable to fetch current price or no current price found." # Stringify the current price return json.dumps(snapshot, indent=2)
- server.py:25-41 (helper)Helper function used by get_current_crypto_price (and other tools) to make authenticated HTTP GET requests to the Financial Datasets API, handling API key from env 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:311-311 (registration)The @mcp.tool() decorator registers the get_current_crypto_price function as an MCP tool, using the function name as the tool name.@mcp.tool()