from fastapi import FastAPI, Request, Query, Depends from fastapi.responses import StreamingResponse, JSONResponse, HTMLResponse from fastapi.responses import RedirectResponse from fastapi.exceptions import HTTPException from fastapi.templating import Jinja2Templates from datetime import datetime, timedelta import asyncio import os import aiofiles import logging import json from fastapi_mcp import FastApiMCP from app.request_model import RequestModel, parse_query from app.utils import ( validate_cve_id, is_safe_path, report_exists, _report_base_path, _report_path, _lock_path, load_report, write_report, generate_openai_stream, generate_openai_json, generate_openai_report, get_cve_info, ghsa_to_cve, ) # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) app = FastAPI() templates = Jinja2Templates(directory="app/templates") from fastapi.responses import PlainTextResponse, HTMLResponse, FileResponse from fastapi.staticfiles import StaticFiles # Mount static files app.mount("/static", StaticFiles(directory="app/static"), name="static") # # Add a helper function to validate CVE-ID # def validate_cve_id(cve_id: str): # parts = cve_id.upper().split("-") # if len(parts) != 3 or not parts[2].isdigit(): # raise HTTPException(status_code=400, detail="Invalid CVE ID format. Expected format: CVE-YYYY-NNNNN.") @app.get("/cached", operation_id="get_cve_report_from_cache") async def cached_report(cve_id: str, refresh: bool = Query(False)): if not is_safe_path(_report_base_path(), _report_path(cve_id)): raise HTTPException(status_code=400, detail="Unsafe path access detected; Got path: " + _report_path(cve_id)) if refresh: try: os.remove(_report_path(cve_id, suffix=".md")) except FileNotFoundError: pass if await report_exists(cve_id): content = await load_report(cve_id) return PlainTextResponse(content, media_type="text/markdown") return PlainTextResponse("Report not found", status_code=404) @app.get("/viewer", operation_id="viewer") async def viewer(request: Request, cve_id: str, refresh: bool = Query(False)): # validate_cve_id(cve_id) # Validate CVE-ID return templates.TemplateResponse("index.html", { "request": request, "cve_id": cve_id, "refresh": refresh }) @app.get("/stream", operation_id="stream") async def stream(cve_id: str, refresh: bool = Query(False)): if not is_safe_path(_report_base_path(), _report_path(cve_id)): raise HTTPException(status_code=400, detail="Unsafe path access detected; Got path: " + _report_path(cve_id)) report_path = _report_path(cve_id) lock_path = _lock_path(cve_id) if os.path.exists(lock_path): async def busy_wait(): yield f"data: Report is currently being generated, please wait or retry...\n\n" return StreamingResponse(busy_wait(), media_type="text/event-stream") if not refresh and await report_exists(cve_id): async def yield_cached(): content = await load_report(cve_id) yield f"event: end\ndata: {content}\n\n" return StreamingResponse(yield_cached(), media_type="text/event-stream") if refresh: try: os.remove(_report_path(cve_id, suffix=".md")) except FileNotFoundError: pass open(lock_path, "w").close() async def yield_live(): full_text = "" logger.info(f"Starting OpenAI stream for {cve_id}") try: async for chunk in generate_openai_stream(cve_id): logger.debug(f"Got chunk: {repr(chunk)}") full_text += chunk for line in chunk.splitlines(): yield f"data: {line}\n" yield "\n" logger.info(f"Completed OpenAI stream for {cve_id}, preview: {repr(full_text[:200])}") await write_report(cve_id, full_text) yield "event: end\ndata: [done]\n\n" except Exception as e: logger.exception(f"Error during OpenAI stream: {e}") yield f"data: ❌ Error generating report: {str(e)}\n\n" finally: os.remove(_lock_path(cve_id)) return StreamingResponse(yield_live(), media_type="text/event-stream") @app.get("/json", operation_id="get_cve_report_as_json") async def json_analysis(cve_id: str, refresh: bool = Query(False)): if not is_safe_path(_report_base_path(), _report_path(cve_id)): raise HTTPException(status_code=400, detail="Unsafe path access detected; Got path: " + _report_path(cve_id)) if refresh: try: os.remove(_report_path(cve_id, suffix=".json")) except FileNotFoundError: pass logger.info(f"Received JSON analysis request for cve_id={cve_id}, refresh={refresh}") if not refresh and await report_exists(cve_id, suffix=".json"): logger.info(f"JSON report exists for cve_id={cve_id}, loading report") content = await load_report(cve_id, suffix=".json") try: return JSONResponse(content=json.loads(content)) except json.JSONDecodeError: return JSONResponse(content={"error": "Invalid JSON format in cached report."}, status_code=500) logger.info(f"Generating new JSON report for cve_id={cve_id}") try: result = await generate_openai_json(cve_id) parsed_result = json.loads(result) await write_report(cve_id, result, suffix=".json") return JSONResponse(content=parsed_result) except ValueError as e: logger.error(f"Error generating JSON for cve_id={cve_id}: {e}") return JSONResponse(content={"error": str(e)}, status_code=500) except json.JSONDecodeError: logger.error(f"Generated result is not valid JSON for cve_id={cve_id}") return JSONResponse(content={"error": "Generated result is not valid JSON."}, status_code=500) @app.get("/raw", operation_id="get_cve_report") async def raw_report(cve_id: str, refresh: bool = Query(False)): if not is_safe_path(_report_base_path(), _report_path(cve_id)): raise HTTPException(status_code=400, detail="Unsafe path access detected; Got path: " + _report_path(cve_id)) if refresh: try: os.remove(_report_path(cve_id, suffix=".md")) except FileNotFoundError: pass if await report_exists(cve_id): content = await load_report(cve_id) return PlainTextResponse(content, media_type="text/plain") # Generate the report using OpenAI if it doesn't exist logger.info(f"Generating new raw report for cve_id={cve_id}") full_report = "" async for chunk in generate_openai_stream(cve_id): full_report += chunk await write_report(cve_id, full_report, suffix=".md") return PlainTextResponse(full_report, media_type="text/plain") @app.get("/", operation_id="redirect_to_viewer") async def root_redirect(cve_id: str, refresh = Query(False)): return RedirectResponse(f"/viewer?cve_id={cve_id}&refresh={refresh}") @app.get("/healthz", operation_id="healthz") def healthz(): return {"status": "ok"} @app.get("/cve_info", operation_id="get_cve_info") async def cve_info(cve_id: str): """ Endpoint to fetch CVE information. Args: cve_id (str): The CVE identifier, e.g., "CVE-2025-1234". Returns: JSONResponse: Parsed CVE data or error message. """ try: cve_data = None if validate_cve_id(cve_id): # Validate CVE-ID cve_data = await get_cve_info(cve_id) else: raise HTTPException(status_code=400, detail="Invalid CVE ID format. Expected format: CVE-YYYY-NNNNN.") if cve_data is None: raise HTTPException(status_code=404, detail=f"CVE {cve_id} not found.") return JSONResponse(content=cve_data) except HTTPException as http_exc: raise http_exc except Exception as e: logger.exception(f"Unexpected error while fetching CVE info for {cve_id}: {e}") raise HTTPException(status_code=500, detail="Internal server error.") @app.get("/ghsa_to_cve", operation_id="ghsa_to_cve") async def ghsa_to_cve_endpoint(ghsa_id: str): """ Endpoint to convert a GHSA ID to a CVE ID. Args: ghsa_id (str): The GHSA identifier, e.g., "GHSA-1234-5678-9abc". Returns: JSONResponse: The corresponding CVE ID or an error message. """ try: cve_id = await ghsa_to_cve(ghsa_id) return {"ghsa_id": ghsa_id, "cve_id": cve_id} except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) except Exception as e: logger.exception(f"Unexpected error while converting GHSA to CVE: {e}") raise HTTPException(status_code=500, detail="Internal server error.") @app.get("/report", operation_id="report") async def report_endpoint(model: RequestModel = Depends(parse_query)): """ Endpoint to get a markdown report. Args: model: RequestModel Returns: PlainTextResponse """ try: full_report = await generate_openai_report(model.model_dump(), 'str') except (KeyError, ValueError) as e: logger.exception(f"Exception while generating markdown report. {e}") raise HTTPException(status_code=400, detail=str(e)) except Exception as e: logger.exception(f"Unexpected exception while generating markdown report. {e}") raise HTTPException(status_code=500, detail=f"Internal server error.") return PlainTextResponse(full_report, media_type="text/plain") @app.post("/report", operation_id="post_report", response_class=PlainTextResponse) async def post_report(model: RequestModel): """ POST endpoint to get a markdown report from a JSON body. Accepts a RequestModel as JSON and returns the markdown report as plain text. The schema for RequestModel will be shown in Swagger. """ try: full_report = await generate_openai_report(model.model_dump(), 'str') except (KeyError, ValueError) as e: logger.exception(f"Exception while generating markdown report. {e}") raise HTTPException(status_code=400, detail=str(e)) except Exception as e: logger.exception(f"Unexpected exception while generating markdown report. {e}") raise HTTPException(status_code=500, detail=f"Internal server error.") return full_report @app.post("/report_json", operation_id="post_report_json", response_class=JSONResponse) async def post_report_json(model: RequestModel): """ POST endpoint to get a JSON report from a JSON body. Accepts a RequestModel as JSON and returns the report as JSON. The schema for RequestModel will be shown in Swagger. """ try: full_report = await generate_openai_report(model.model_dump()) if not full_report or not full_report.strip(): logger.error("Empty response from generate_openai_report") raise HTTPException(status_code=500, detail="Empty response from report generator.") if model.prompt.source == 'prompt': detail = "Warning! JSON format is used without specifying prompt_name" content = {"body": full_report, "detail": detail} logger.warning(detail) else: try: content = {"body": json.loads(full_report)} except json.JSONDecodeError as e: logger.error(f"JSON decode error: {e}. Raw output: {repr(full_report)}") raise HTTPException( status_code=500, detail=f"Report generator did not return valid JSON. Raw output: {repr(full_report)}" ) except (KeyError, ValueError) as e: logger.exception(f"Exception while generating JSON report. {e}") raise HTTPException(status_code=400, detail=str(e)) except Exception as e: logger.exception(f"Unexpected exception while generating JSON report. {e}") raise HTTPException(status_code=500, detail=f"Internal server error.") return JSONResponse(content=content) @app.get("/report_json", operation_id="report_json") async def report_endpoint(model: RequestModel = Depends(parse_query)): """ Endpoint to get a json report. Args: model: RequestModel Returns: JSONResponse """ try: full_report = await generate_openai_report(model.model_dump()) if model.prompt.source == 'prompt': detail = "Warning! JSON format is used without specifying prompt_name" content = {"body": full_report, "detail": detail} logger.warning(detail) else: content = {"body": json.loads(full_report)} except (KeyError, ValueError) as e: logger.exception(f"Exception while generating JSON report. {e}") raise HTTPException(status_code=400, detail=str(e)) except Exception as e: logger.exception(f"Unexpected exception while generating JSON report. {e}") raise HTTPException(status_code=500, detail=f"Internal server error.") return JSONResponse(content=content) @app.get("/favicon.ico", include_in_schema=False) def favicon(): """ Endpoint to serve the favicon. Returns: FileResponse: The favicon image file. """ return FileResponse("app/templates/favicon.png") mcp = FastApiMCP( app, name="Get CVE Info", # Name for your MCP server description="MCP server getting CVE raw info or full report", # Description include_operations=["get_cve_info", "get_cve_report"] ) # Mount the MCP server to your FastAPI app mcp.mount() #if __name__ == "__main__": # import uvicorn # uvicorn.run(app, host="0.0.0.0", port=8000)