import gradio as gr import requests COINGECKO_API = "https://api.coingecko.com/api/v3" def get_simple_price( ids: str = "bitcoin,ethereum", vs_currencies: str = "usd" ) -> dict: """ Get current price for one or more coins. """ url = f"{COINGECKO_API}/simple/price" params = { "ids": ids, "vs_currencies": vs_currencies, "include_market_cap": "true", "include_24hr_vol": "true", "include_24hr_change": "true", "include_last_updated_at": "true" } resp = requests.get(url, params=params) try: return resp.json() except Exception: return {"error": resp.text} def get_coin_metadata(coin_id: str = "bitcoin") -> dict: """ Get full metadata for a given coin. """ url = f"{COINGECKO_API}/coins/{coin_id}" resp = requests.get(url) try: data = resp.json() return { "id": data.get("id"), "symbol": data.get("symbol"), "name": data.get("name"), "description": data.get("description", {}).get("en"), "homepage": data.get("links", {}).get("homepage", [None])[0], "genesis_date": data.get("genesis_date"), "market_data": data.get("market_data"), "image": data.get("image", {}).get("large") } except Exception: return {"error": resp.text} def get_markets( vs_currency: str = "usd", per_page: int = 10, page: int = 1 ) -> dict: """ Get a list of coins with price/market data (like top 10 by market cap). """ url = f"{COINGECKO_API}/coins/markets" params = { "vs_currency": vs_currency, "order": "market_cap_desc", "per_page": per_page, "page": page, "sparkline": "false" } resp = requests.get(url, params=params) try: return resp.json() except Exception: return {"error": resp.text} def get_coin_history( coin_id: str = "bitcoin", date: str = "01-01-2023" ) -> dict: """ Get historical data for a coin (dd-mm-yyyy). """ url = f"{COINGECKO_API}/coins/{coin_id}/history" params = {"date": date} resp = requests.get(url, params=params) try: data = resp.json() price = data.get("market_data", {}).get("current_price", {}).get("usd") return { "coin": coin_id, "date": date, "price_usd": price, "snapshot": data.get("market_data", {}) } except Exception: return {"error": resp.text} def get_global() -> dict: """ Get global crypto stats (total market cap, #coins, #markets, etc). """ url = f"{COINGECKO_API}/global" resp = requests.get(url) try: return resp.json() except Exception: return {"error": resp.text} with gr.Blocks() as demo: gr.Markdown( "# 🦎 CoinGecko MCP API\n\nPublic CoinGecko endpoints exposed as MCP tools!" ) with gr.Tab("Simple Price"): gr.Interface( fn=get_simple_price, inputs=[ gr.Textbox(value="bitcoin,ethereum", label="Coin IDs (comma separated)"), gr.Textbox(value="usd", label="Vs Currencies (comma separated)") ], outputs=gr.JSON(), allow_flagging="never" ) with gr.Tab("Markets (Top N)"): gr.Interface( fn=get_markets, inputs=[ gr.Textbox(value="usd", label="Fiat Currency"), gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Results per page"), gr.Slider(minimum=1, maximum=10, value=1, step=1, label="Page Number") ], outputs=gr.JSON(), allow_flagging="never" ) with gr.Tab("Coin Metadata"): gr.Interface( fn=get_coin_metadata, inputs=[gr.Textbox(value="bitcoin", label="Coin ID")], outputs=gr.JSON(), allow_flagging="never" ) with gr.Tab("Coin History"): gr.Interface( fn=get_coin_history, inputs=[ gr.Textbox(value="bitcoin", label="Coin ID"), gr.Textbox(value="01-01-2023", label="Date (dd-mm-yyyy)") ], outputs=gr.JSON(), allow_flagging="never" ) with gr.Tab("Global Stats"): gr.Interface( fn=get_global, inputs=[], outputs=gr.JSON(), allow_flagging="never" ) if __name__ == "__main__": demo.launch(mcp_server=True)