mcp_tools.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from __future__ import annotations
  2. from typing import Any
  3. from .config import DB_PATH
  4. from .storage import last_candle, latest_candles, stats
  5. from .swissquote import SwissquoteClient
  6. client = SwissquoteClient()
  7. def get_capabilities() -> dict[str, Any]:
  8. return {
  9. "server": "metals-mcp",
  10. "transport": "fastmcp+sse",
  11. "tools": [
  12. "get_price",
  13. "get_ohlcv",
  14. "get_indicator",
  15. "get_market_snapshot",
  16. "get_top_movers",
  17. "get_capabilities",
  18. "get_regime",
  19. ],
  20. "backend": "swissquote-poller+sqlite",
  21. "database": str(DB_PATH),
  22. }
  23. def get_price(symbol: str) -> dict[str, Any]:
  24. quote = client.fetch_quote(symbol)
  25. if not quote:
  26. return {"symbol": symbol, "price": None, "timestamp": None, "source": "swissquote", "status": "unavailable"}
  27. return {"symbol": symbol, "price": quote.mid, "timestamp": quote.timestamp, "source": "swissquote"}
  28. def get_ohlcv(symbol: str, timeframe: str = "5m", limit: int = 100) -> dict[str, Any]:
  29. candles = latest_candles(DB_PATH, symbol.upper(), timeframe, limit)
  30. return {"symbol": symbol.upper(), "timeframe": timeframe, "limit": limit, "candles": candles}
  31. def get_candles(symbol: str, timeframe: str = "5m", limit: int = 100) -> dict[str, Any]:
  32. return get_ohlcv(symbol, timeframe=timeframe, limit=limit)
  33. def get_last_candle(symbol: str, timeframe: str = "5m") -> dict[str, Any]:
  34. return {"symbol": symbol.upper(), "timeframe": timeframe, "candle": last_candle(DB_PATH, symbol.upper(), timeframe)}
  35. def get_indicator(symbol: str, indicator: str, timeframe: str = "5m", params: dict[str, Any] | None = None) -> dict[str, Any]:
  36. return {"symbol": symbol, "indicator": indicator, "timeframe": timeframe, "params": params or {}, "value": None, "status": "scaffolded"}
  37. def get_market_snapshot(symbol: str) -> dict[str, Any]:
  38. candle = last_candle(DB_PATH, symbol.upper(), "5m")
  39. price = candle["close"] if candle else None
  40. return {"symbol": symbol.upper(), "price": price, "trend_bias": "range", "status": "scaffolded"}
  41. def get_top_movers(limit: int = 10) -> dict[str, Any]:
  42. return {"limit": limit, "movers": [], "status": "scaffolded"}
  43. def get_regime(symbol: str, timeframe: str = "5m") -> dict[str, Any]:
  44. candles = latest_candles(DB_PATH, symbol.upper(), timeframe, 20)
  45. return {"symbol": symbol.upper(), "timeframe": timeframe, "candles": candles, "regime": None, "status": "scaffolded"}
  46. def get_health() -> dict[str, Any]:
  47. try:
  48. store = stats(DB_PATH)
  49. except Exception:
  50. store = {"ticks": 0, "candles": 0}
  51. return {"ok": True, "store": store}