|
@@ -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]:
|
|
def get_capabilities() -> dict[str, Any]:
|
|
|
return {
|
|
return {
|
|
|
"server": "metals-mcp",
|
|
"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]:
|
|
def get_price(symbol: str, counter_currency: str | None = None) -> dict[str, Any]:
|
|
|
pair = client.normalize_pair(symbol, counter_currency)
|
|
pair = client.normalize_pair(symbol, counter_currency)
|
|
|
if not client.pair_is_supported(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 {
|
|
return {
|
|
|
"symbol": symbol.upper(),
|
|
"symbol": symbol.upper(),
|
|
|
"counter_currency": (counter_currency or "USD").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)
|
|
quote = client.fetch_quote(pair)
|
|
|
if not quote:
|
|
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 {
|
|
return {
|
|
|
"symbol": symbol.upper(),
|
|
"symbol": symbol.upper(),
|
|
|
"counter_currency": (counter_currency or "USD").upper(),
|
|
"counter_currency": (counter_currency or "USD").upper(),
|