Spaces:
Sleeping
Sleeping
| from mcp.server.fastmcp import FastMCP | |
| from cryptopanic_news import get_posts | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv(override=True) | |
| mcp = FastMCP("cryptopanic_news_server") | |
| async def get_crypto_news(currencies_ls: list | str | None = None) -> str: | |
| """Fetch latest cryptocurrency news from CryptoPanic. | |
| Args: | |
| currencies_ls: The list of cryptocurrency symbols to filter news by. Accepts a single string (e.g. "BTC") or a list of symbols (e.g. ["HYPE", "ETC", "BTC", "XRP"]). | |
| If set to None, retrieves the general CryptoPanic news feed | |
| without filtering by any specific currency. | |
| """ | |
| token = os.getenv("CRYPTOPANIC_API_KEY") | |
| if not token: | |
| return "Error: CRYPTOPANIC_API_KEY not set." | |
| # Allow a single string or a list | |
| if isinstance(currencies_ls, str): | |
| currencies_ls = [currencies_ls] | |
| data = get_posts( | |
| auth_token=token, | |
| kind="news", | |
| currencies=currencies_ls, | |
| page=1 | |
| ) | |
| if not isinstance(data, dict): | |
| return f"Error: Unexpected API response: {data!r}" | |
| results = [] | |
| for post in data.get("results", []): | |
| title = post.get("title") or "No title" | |
| desc = post.get("description") or "" | |
| pub = post.get("published_at") or "Unknown time" | |
| results.append(f"title: {title}\n{desc[:200]}\npublished at: {pub}\n") | |
| return "\n".join(results) if results else "No recent crypto news found." | |
| if __name__ == "__main__": | |
| mcp.run(transport='stdio') |