Update server.py
Browse files
server.py
CHANGED
|
@@ -2,7 +2,8 @@
|
|
| 2 |
import sys, json, asyncio
|
| 3 |
import yfinance as yf
|
| 4 |
from statsmodels.tsa.holtwinters import ExponentialSmoothing
|
| 5 |
-
from langchain_core.tools import
|
|
|
|
| 6 |
|
| 7 |
# --- Compact MCP Server Logic ---
|
| 8 |
class MCPToolServer:
|
|
@@ -39,7 +40,10 @@ class MCPToolServer:
|
|
| 39 |
# --- Compact Tool Definitions ---
|
| 40 |
COMMODITY_TICKERS = {"gold": "GC=F", "silver": "SI=F"}
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
async def get_current_price(commodity_name: str) -> str:
|
| 44 |
"""Gets the most recent 'live' price for gold or silver."""
|
| 45 |
ticker = COMMODITY_TICKERS.get(commodity_name.lower())
|
|
@@ -49,19 +53,28 @@ async def get_current_price(commodity_name: str) -> str:
|
|
| 49 |
return f"The current price of {commodity_name} is approx. ${price:.2f} USD."
|
| 50 |
except Exception as e: return f"Error fetching price: {e}"
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
async def get_price_forecast(commodity_name: str, forecast_days: int) -> str:
|
| 54 |
"""Generates a 3 to 5 day price forecast for gold or silver."""
|
| 55 |
ticker = COMMODITY_TICKERS.get(commodity_name.lower())
|
| 56 |
-
if not ticker:
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
try:
|
| 59 |
data = yf.download(ticker, period="6mo", progress=False)['Close']
|
| 60 |
-
if data.empty:
|
|
|
|
| 61 |
forecast = ExponentialSmoothing(data, trend='add').fit().forecast(steps=forecast_days)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
# --- Main Server Entrypoint ---
|
| 67 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import sys, json, asyncio
|
| 3 |
import yfinance as yf
|
| 4 |
from statsmodels.tsa.holtwinters import ExponentialSmoothing
|
| 5 |
+
from langchain_core.tools import StructuredTool
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
|
| 8 |
# --- Compact MCP Server Logic ---
|
| 9 |
class MCPToolServer:
|
|
|
|
| 40 |
# --- Compact Tool Definitions ---
|
| 41 |
COMMODITY_TICKERS = {"gold": "GC=F", "silver": "SI=F"}
|
| 42 |
|
| 43 |
+
class PriceInput(BaseModel):
|
| 44 |
+
commodity_name: str
|
| 45 |
+
|
| 46 |
+
@tool(args_schema=PriceInput)
|
| 47 |
async def get_current_price(commodity_name: str) -> str:
|
| 48 |
"""Gets the most recent 'live' price for gold or silver."""
|
| 49 |
ticker = COMMODITY_TICKERS.get(commodity_name.lower())
|
|
|
|
| 53 |
return f"The current price of {commodity_name} is approx. ${price:.2f} USD."
|
| 54 |
except Exception as e: return f"Error fetching price: {e}"
|
| 55 |
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class ForecastInput(BaseModel):
|
| 59 |
+
commodity_name: str
|
| 60 |
+
forecast_days: int
|
| 61 |
+
|
| 62 |
+
@tool(args_schema=ForecastInput)
|
| 63 |
async def get_price_forecast(commodity_name: str, forecast_days: int) -> str:
|
| 64 |
"""Generates a 3 to 5 day price forecast for gold or silver."""
|
| 65 |
ticker = COMMODITY_TICKERS.get(commodity_name.lower())
|
| 66 |
+
if not ticker:
|
| 67 |
+
return f"Error: '{commodity_name}' is not supported. Use 'gold' or 'silver'."
|
| 68 |
+
if not 3 <= forecast_days <= 5:
|
| 69 |
+
return "Error: Forecast must be for 3, 4, or 5 days."
|
| 70 |
try:
|
| 71 |
data = yf.download(ticker, period="6mo", progress=False)['Close']
|
| 72 |
+
if data.empty:
|
| 73 |
+
return "Not enough data to forecast."
|
| 74 |
forecast = ExponentialSmoothing(data, trend='add').fit().forecast(steps=forecast_days)
|
| 75 |
+
return "\n".join([f"Day {i+1}: ${val:.2f} USD" for i, val in enumerate(forecast)])
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"Error during forecast: {e}"
|
| 78 |
|
| 79 |
# --- Main Server Entrypoint ---
|
| 80 |
if __name__ == "__main__":
|