Lukas Goldschmidt преди 1 седмица
родител
ревизия
1b460c2dc0
променени са 2 файла, в които са добавени 30 реда и са изтрити 1 реда
  1. 1 1
      src/metals_mcp/config.py
  2. 29 0
      src/metals_mcp/mcp_tools.py

+ 1 - 1
src/metals_mcp/config.py

@@ -9,6 +9,6 @@ PORT = int(os.getenv("METALS_PORT", "8515"))
 DATA_DIR = Path(os.getenv("METALS_DATA_DIR", BASE_DIR / "data"))
 LOG_DIR = Path(os.getenv("METALS_LOG_DIR", BASE_DIR / "logs"))
 DB_PATH = Path(os.getenv("METALS_DB_PATH", DATA_DIR / "metals.sqlite3"))
-POLL_INTERVAL_SECONDS = float(os.getenv("SWISSQUOTE_POLL_INTERVAL_SECONDS", "0.2"))
+POLL_INTERVAL_SECONDS = float(os.getenv("SWISSQUOTE_POLL_INTERVAL_SECONDS", "2.0"))
 METALS_PAIRS = [p.strip().upper() for p in os.getenv("METALS_PAIRS", "XAU/USD,XAG/USD,XPT/USD,XPD/USD,EUR/USD,USD/JPY").split(",") if p.strip()]
 METALS_CANDLE_RETENTION_DAYS = int(os.getenv("METALS_CANDLE_RETENTION_DAYS", "30"))

+ 29 - 0
src/metals_mcp/mcp_tools.py

@@ -170,6 +170,13 @@ def _symbol_snapshot(symbol: str, timeframe: str = "5m", limit: int = 20) -> dic
     }
 
 
+def _cached_last_price(pair: str) -> tuple[float | None, int | None]:
+    candle = last_candle(DB_PATH, pair, "5m")
+    if candle is None:
+        return None, None
+    return _to_float(candle.get("close")), candle.get("end_ts")
+
+
 def get_capabilities() -> dict[str, Any]:
     return {
         "server": "metals-mcp",
@@ -192,6 +199,17 @@ def get_capabilities() -> dict[str, Any]:
 def get_price(symbol: str, counter_currency: str | None = None) -> dict[str, Any]:
     pair = client.normalize_pair(symbol, counter_currency)
     if not client.pair_is_supported(symbol, counter_currency):
+        cached_price, cached_timestamp = _cached_last_price(pair)
+        if cached_price is not None:
+            return {
+                "symbol": symbol.upper(),
+                "counter_currency": (counter_currency or "USD").upper(),
+                "pair": pair,
+                "price": cached_price,
+                "timestamp": cached_timestamp,
+                "source": "sqlite-cache",
+                "status": "cached",
+            }
         return {
             "symbol": symbol.upper(),
             "counter_currency": (counter_currency or "USD").upper(),
@@ -204,6 +222,17 @@ def get_price(symbol: str, counter_currency: str | None = None) -> dict[str, Any
 
     quote = client.fetch_quote(pair)
     if not quote:
+        cached_price, cached_timestamp = _cached_last_price(pair)
+        if cached_price is not None:
+            return {
+                "symbol": symbol.upper(),
+                "counter_currency": (counter_currency or "USD").upper(),
+                "pair": pair,
+                "price": cached_price,
+                "timestamp": cached_timestamp,
+                "source": "sqlite-cache",
+                "status": "cached",
+            }
         return {
             "symbol": symbol.upper(),
             "counter_currency": (counter_currency or "USD").upper(),