main.py•4.4 kB
from mcp.server.fastmcp import FastMCP
import httpx
import os
from dotenv import load_dotenv
import pandas as pd
# Load environment variables
load_dotenv()
# Initialize MCP server
mcp = FastMCP(
name="NFT Analytics",
dependencies=["httpx", "python-dotenv", "pandas"]
)
# Configuration
DUNE_API_KEY = os.getenv("DUNE_API_KEY")
BASE_URL = "https://api.dune.com/api/v1"
HEADERS = {"X-Dune-API-Key": DUNE_API_KEY}
def get_latest_result(query_id: int, limit: int = 1000) -> list:
"""
Fetch the latest results from a Dune Analytics query.
Args:
query_id (int): The ID of the Dune query to fetch results from.
limit (int, optional): Maximum number of rows to return. Defaults to 1000.
Returns:
list: A list of dictionaries containing the query results, or an empty list if the request fails.
Raises:
httpx.HTTPStatusError: If the API request fails due to a client or server error.
"""
url = f"{BASE_URL}/query/{query_id}/results"
params = {"limit": limit}
with httpx.Client() as client:
response = client.get(url, params=params, headers=HEADERS, timeout=300)
response.raise_for_status()
data = response.json()
result_data = data.get("result", {}).get("rows", [])
return result_data
@mcp.tool()
def get_daily_trading_volume_by_collection(limit: int = 1000) -> str:
"""
Retrieve daily trading volume for top 5 Ethereum NFT collections.
Args:
limit (int, optional): Maximum number of rows to fetch from the query. Defaults to 1000.
Returns:
str: Markdown table of daily trading volumes by collection, or error message if the query fails.
"""
try:
data = get_latest_result(5140422, limit=limit)
df = pd.DataFrame(data)
df["day"] = pd.to_datetime(df["day"]).dt.date
pivot_df = df.pivot(index="day", columns="collection", values="daily_volume")
pivot_df = pivot_df.sort_index(ascending=False)
return pivot_df.to_markdown()
except Exception as e:
return str(e)
@mcp.tool()
def get_daily_sales_by_collection(limit: int = 1000) -> str:
"""
Retrieve number of daily sales for NFT collections.
Args:
limit (int, optional): Maximum number of rows to fetch from the query. Defaults to 1000.
Returns:
str: Markdown table of daily sales counts by collection, or error message if the query fails.
"""
try:
data = get_latest_result(5140487, limit=limit)
df = pd.DataFrame(data)
df["day"] = pd.to_datetime(df["day"]).dt.date
pivot_df = df.pivot(index="day", columns="collection", values="sales_count")
pivot_df = pivot_df.sort_index(ascending=False)
return pivot_df.to_markdown()
except Exception as e:
return str(e)
@mcp.tool()
def get_average_price_by_collection(limit: int = 1000) -> str:
"""
Retrieve average selling price for NFT collections.
Args:
limit (int, optional): Maximum number of rows to fetch from the query. Defaults to 1000.
Returns:
str: Markdown table of average prices by collection, or error message if the query fails.
"""
try:
data = get_latest_result(5140470, limit=limit)
df = pd.DataFrame(data)
return df.to_markdown()
except Exception as e:
return str(e)
@mcp.tool()
def get_unique_traders_by_collection(limit: int = 1000) -> str:
"""
Retrieve count of unique buyers and sellers for NFT collections.
Args:
limit (int, optional): Maximum number of rows to fetch from the query. Defaults to 1000.
Returns:
str: Markdown table of unique traders by collection, or error message if the query fails.
"""
try:
data = get_latest_result(5140464, limit=limit)
df = pd.DataFrame(data)
return df.to_markdown()
except Exception as e:
return str(e)
@mcp.tool()
def get_new_owners() -> str:
"""
Retrieve count of new NFT owners.
Returns:
str: Count of new wallet owners as a string, or "N/A" if no data is available,
or error message if the query fails.
"""
try:
data = get_latest_result(5140497)
return data[0].get("new_wallets", "N/A")
except Exception as e:
return str(e)
# Run the server
if __name__ == "__main__":
mcp.run()