parent
c16448f0c1
commit
fd723e712d
@ -1,30 +1,33 @@
|
||||
|
||||
from fastmcp import FastMCP
|
||||
import time
|
||||
from fastmcp import Server
|
||||
from fastmcp.messages import Request, Response
|
||||
|
||||
# Initialize MCP server
|
||||
mcp = FastMCP("Stock-Mock-Server")
|
||||
# Create MCP server
|
||||
mcp = Server("Stock-Mock-Server")
|
||||
|
||||
@mcp.tool()
|
||||
def calculate_simple_moving_average(prices: list[float], period: int) -> float:
|
||||
"""Calculate Simple Moving Average"""
|
||||
try:
|
||||
time.sleep(0.1) # Simulate processing time
|
||||
if len(prices) < period:
|
||||
return {"error": "Not enough data points"}
|
||||
return sum(prices[-period:]) / period
|
||||
except Exception as e:
|
||||
return {"error": f"Error calculating SMA: {str(e)}"}
|
||||
# Define stock price lookup handler
|
||||
@mcp.handler("get_stock_price")
|
||||
async def get_stock_price(request: Request) -> Response:
|
||||
# Mock stock price data
|
||||
stock_prices = {
|
||||
"AAPL": 150.0,
|
||||
"GOOGL": 2800.0,
|
||||
"MSFT": 300.0
|
||||
}
|
||||
|
||||
@mcp.tool()
|
||||
def calculate_percentage_change(old_value: float, new_value: float) -> float:
|
||||
"""Calculate percentage change between two values"""
|
||||
try:
|
||||
time.sleep(0.1) # Simulate processing time
|
||||
return ((new_value - old_value) / old_value) * 100
|
||||
except Exception as e:
|
||||
return {"error": f"Error calculating percentage change: {str(e)}"}
|
||||
symbol = request.data.get("symbol")
|
||||
if symbol in stock_prices:
|
||||
return Response(data={"price": stock_prices[symbol]})
|
||||
else:
|
||||
return Response(error=f"Stock {symbol} not found")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Starting Mock Stock Server on port 8001...")
|
||||
mcp.run(transport="sse", port=8001)
|
||||
# Run server with SSE transport on specified host/port
|
||||
mcp.run(
|
||||
transport="sse",
|
||||
transport_kwargs={
|
||||
"host": "0.0.0.0",
|
||||
"port": 8001
|
||||
}
|
||||
)
|
||||
|
Loading…
Reference in new issue