server_fastmcp.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """FastMCP transport wrapper for crypto tools (compliance-first)."""
  2. from fastapi import FastAPI
  3. from mcp.server.fastmcp import FastMCP
  4. from mcp.server.transport_security import TransportSecuritySettings
  5. import services
  6. from mcp_tools import MCP_TOOLS
  7. from cache import get_cache_stats
  8. mcp = FastMCP(
  9. "crypto-mcp",
  10. transport_security=TransportSecuritySettings(
  11. enable_dns_rebinding_protection=False,
  12. ),
  13. )
  14. @mcp.tool(description="Get the current USD price of a cryptocurrency.")
  15. async def get_price(symbol: str):
  16. return await services.get_price(symbol)
  17. @mcp.tool(description="Get OHLCV candlestick data for a crypto asset.")
  18. async def get_ohlcv(symbol: str, timeframe: str = "1h", limit: int = 100):
  19. return await services.get_ohlcv(symbol, timeframe, limit)
  20. @mcp.tool(description="Compute a technical indicator for a crypto asset.")
  21. async def get_indicator(symbol: str, indicator: str, timeframe: str = "1h", params: dict | None = None):
  22. return await services.get_indicator(symbol, indicator, timeframe, params or {})
  23. @mcp.tool(description="Get a compact market snapshot for a crypto asset.")
  24. async def get_market_snapshot(symbol: str):
  25. return await services.get_market_snapshot(symbol)
  26. @mcp.tool(description="Get top gaining and losing crypto assets by 24h % change.")
  27. async def get_top_movers(limit: int = 10):
  28. return await services.get_top_movers(limit)
  29. app = FastAPI(title="Crypto MCP Server")
  30. app.mount("/mcp", mcp.sse_app())
  31. @app.get("/")
  32. def root():
  33. return {"status": "ok", "transport": "fastmcp+sse", "mount": "/mcp", "tools": [t["name"] for t in MCP_TOOLS]}
  34. @app.get("/health")
  35. def health():
  36. return {"status": "ok", "cache": get_cache_stats(), "tools": [t["name"] for t in MCP_TOOLS]}