Spaces:
Running
Running
File size: 4,600 Bytes
d7ea2f7 60aaf82 173384d aea1032 173384d aea1032 59ab50d aea1032 59ab50d aea1032 8b1dba0 aea1032 8b1dba0 aea1032 8b1dba0 60aaf82 aea1032 173384d aea1032 59ab50d aea1032 59ab50d aea1032 60aaf82 aea1032 60aaf82 aea1032 59ab50d aea1032 59ab50d aea1032 d7ea2f7 aea1032 60aaf82 aea1032 60aaf82 aea1032 60aaf82 aea1032 d7ea2f7 aea1032 60aaf82 aea1032 8b1dba0 aea1032 8b1dba0 60aaf82 aea1032 60aaf82 aea1032 60aaf82 d7ea2f7 aea1032 60aaf82 8b1dba0 d7ea2f7 aea1032 173384d aea1032 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
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) |