from dotenv import load_dotenv
from mcp.server.fastmcp import FastMCP
from tavily import TavilyClient
import os
import requests
from dice_roller import DiceRoller
load_dotenv()
mcp = FastMCP("mcp-server")
client = TavilyClient(os.getenv("TAVILY_API_KEY"))
@mcp.tool()
def web_search(query: str) -> str:
"""Search the web for information about the given query"""
search_results = client.get_search_context(query=query)
return search_results
@mcp.tool()
def roll_dice(notation: str, num_rolls: int = 1) -> str:
"""Roll the dice with the given notation"""
roller = DiceRoller(notation, num_rolls)
return str(roller)
"""
Add your own tool here, and then use it through Cursor!
"""
@mcp.tool()
def get_crypto_price(coin_id: str, vs_currency: str = "usd") -> str:
"""
Get the current price and market data for a cryptocurrency.
Args:
coin_id: The cryptocurrency ID (e.g., 'bitcoin', 'ethereum', 'dogecoin')
vs_currency: The currency to compare against (default: 'usd')
Returns:
Formatted string with current price and market data
"""
try:
# CoinGecko API endpoint - free tier, no API key required for basic endpoints
api_key = os.getenv("COINGECKO_API_KEY")
base_url = "https://api.coingecko.com/api/v3"
# Add API key to headers if available (for higher rate limits)
headers = {}
if api_key:
headers["x-cg-demo-api-key"] = api_key
# Get simple price endpoint
url = f"{base_url}/simple/price"
params = {
"ids": coin_id.lower(),
"vs_currencies": vs_currency.lower(),
"include_market_cap": "true",
"include_24hr_vol": "true",
"include_24hr_change": "true"
}
response = requests.get(url, params=params, headers=headers, timeout=10)
response.raise_for_status()
data = response.json()
if coin_id.lower() not in data:
return f"Error: Cryptocurrency '{coin_id}' not found. Common IDs: bitcoin, ethereum, dogecoin, solana, cardano"
coin_data = data[coin_id.lower()]
currency_key = vs_currency.lower()
# CoinGecko returns: currency_key (price), currency_key_market_cap, currency_key_24h_vol, currency_key_24h_change
price = coin_data.get(currency_key)
market_cap = coin_data.get(f"{currency_key}_market_cap")
volume_24h = coin_data.get(f"{currency_key}_24h_vol")
change_24h = coin_data.get(f"{currency_key}_24h_change")
if price is None:
return f"Error: Could not fetch price data for {coin_id}"
# Format the response
result = f"📊 {coin_id.upper()} Price Information:\n"
result += f"💰 Current Price: ${price:,.2f} {vs_currency.upper()}\n"
if market_cap is not None:
result += f"📈 Market Cap: ${market_cap:,.2f}\n"
if volume_24h is not None:
result += f"💹 24h Volume: ${volume_24h:,.2f}\n"
if change_24h is not None:
change_symbol = "📈" if change_24h >= 0 else "📉"
result += f"{change_symbol} 24h Change: {change_24h:+.2f}%\n"
return result
except requests.exceptions.RequestException as e:
return f"Error fetching cryptocurrency data: {str(e)}"
except Exception as e:
return f"Unexpected error: {str(e)}"
if __name__ == "__main__":
mcp.run(transport="stdio")