"""
REST API Server as Lambda Function
FastAPI application wrapped for Lambda deployment using Mangum
"""
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict, Any
from mangum import Mangum
app = FastAPI(title="MCP REST API Tools", version="1.0.0")
class StockPriceRequest(BaseModel):
symbol: str
class StockPriceResponse(BaseModel):
symbol: str
price: float
currency: str
change: float
change_percent: float
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy"}
@app.post("/stock/price", response_model=StockPriceResponse)
async def get_stock_price(request: StockPriceRequest):
"""
Get stock price information
This endpoint can be wrapped as an MCP tool
"""
# Mock stock data (in production, call actual stock API)
mock_stocks = {
"AAPL": {"price": 175.50, "change": 2.30, "change_percent": 1.33},
"GOOGL": {"price": 142.20, "change": -1.10, "change_percent": -0.77},
"MSFT": {"price": 378.85, "change": 5.20, "change_percent": 1.39},
}
symbol = request.symbol.upper()
if symbol not in mock_stocks:
raise HTTPException(status_code=404, detail=f"Stock symbol {symbol} not found")
stock_data = mock_stocks[symbol]
return StockPriceResponse(
symbol=symbol,
price=stock_data["price"],
currency="USD",
change=stock_data["change"],
change_percent=stock_data["change_percent"]
)
@app.get("/time/current")
async def get_current_time():
"""
Get current time
This endpoint can be wrapped as an MCP tool
"""
from datetime import datetime, timezone
utc_now = datetime.now(timezone.utc)
return {
"utc_time": utc_now.isoformat(),
"unix_timestamp": int(utc_now.timestamp()),
"timezone": "UTC"
}
@app.post("/text/summarize")
async def summarize_text(text: Dict[str, str]):
"""
Summarize text
This endpoint can be wrapped as an MCP tool
"""
content = text.get("text", "")
if not content:
raise HTTPException(status_code=400, detail="Text field is required")
# Simple mock summarization (in production, use actual NLP model)
words = content.split()
summary = " ".join(words[:20]) + "..." if len(words) > 20 else content
return {
"original_length": len(content),
"summary_length": len(summary),
"summary": summary,
"word_count": len(words)
}
# Lambda handler
handler = Mangum(app, lifespan="off")
def lambda_handler(event, context):
"""AWS Lambda handler for REST API"""
return handler(event, context)