feat(stock-server): update stock server to use FastMCP for better performance

pull/819/head
Pavan Kumar 3 months ago committed by ascender1729
parent 07f4c65bde
commit 702d9f2a9e

@ -1,33 +1,39 @@
from fastmcp import Server
from fastmcp.messages import Request, Response
from fastmcp import FastMCP
from typing import Dict, Union
# Create MCP server
mcp = Server("Stock-Mock-Server")
# Create FastMCP server
mcp = FastMCP("Stock-Mock-Server")
# Define stock price lookup handler
@mcp.handler("get_stock_price")
async def get_stock_price(request: Request) -> Response:
# Mock stock price data
stock_prices = {
@mcp.tool()
def get_stock_price(symbol: str) -> Dict[str, Union[float, str]]:
"""Get the current price of a stock"""
prices = {
"AAPL": 150.0,
"GOOGL": 2800.0,
"MSFT": 300.0
"MSFT": 300.0,
"AMZN": 3300.0
}
if symbol not in prices:
return {"error": f"Stock {symbol} not found"}
return {"price": prices[symbol]}
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")
@mcp.tool()
def calculate_moving_average(prices: list[float], window: int) -> Dict[str, Union[list[float], str]]:
"""Calculate moving average of stock prices"""
if not isinstance(prices, list) or not all(isinstance(x, (int, float)) for x in prices):
return {"error": "Invalid price data"}
if not isinstance(window, int) or window <= 0:
return {"error": "Invalid window size"}
if len(prices) < window:
return {"error": "Not enough price points"}
avgs = []
for i in range(len(prices) - window + 1):
avg = sum(prices[i:i+window]) / window
avgs.append(round(avg, 2))
return {"averages": avgs}
if __name__ == "__main__":
print("Starting Mock Stock Server on port 8001...")
# Run server with SSE transport on specified host/port
mcp.run(
transport="sse",
transport_kwargs={
"host": "0.0.0.0",
"port": 8001
}
)
mcp.run(transport="sse", host="0.0.0.0", port=8001)

Loading…
Cancel
Save