From ab5041d4ecd85f5673523025b813b057b189e233 Mon Sep 17 00:00:00 2001 From: ClawdBot Date: Thu, 19 Feb 2026 09:40:55 +1100 Subject: [PATCH] fix(runtime): prefer uvicorn ASGI app path, fallback to programmatic mcp.run --- server.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/server.py b/server.py index b598749..0d8ebcd 100644 --- a/server.py +++ b/server.py @@ -210,17 +210,33 @@ def query_notebook(notebook: str, query: str, limit: int = 5) -> str: return f"Query failed: {e}" if __name__ == "__main__": - import sys - logging.info("Starting knowledge-mcp server via embedded FastMCP runner...") + import uvicorn - # Force FastMCP.run() to start SSE listener on all interfaces. - # We intentionally reset argv so container/entrypoint args cannot break startup. - sys.argv = [sys.argv[0], "sse", "--host", "0.0.0.0", "--port", "8000"] - logging.info(f"Forced argv: {sys.argv}") + logging.info("Starting knowledge-mcp server via python entrypoint...") + # Strategy 1 (preferred): build an ASGI app and run via uvicorn. + # This avoids FastMCP CLI argument parsing ambiguity. + app = None try: - mcp.run() - except BaseException as e: - logging.critical(f"Server crashed: {e}", exc_info=True) - raise + if hasattr(mcp, "sse_app"): + app = mcp.sse_app() + logging.info("Using mcp.sse_app() with uvicorn") + elif hasattr(mcp, "http_app"): + app = mcp.http_app(path="/sse") + logging.info("Using mcp.http_app(path='/sse') with uvicorn") + except Exception as e: + logging.warning(f"ASGI app construction failed, will fallback to mcp.run(): {e}") + + if app is not None: + uvicorn.run(app, host="0.0.0.0", port=8000) + else: + # Strategy 2: programmatic FastMCP run with explicit settings + # (works on newer FastMCP APIs). + try: + logging.info("Falling back to mcp.run(transport='sse', host='0.0.0.0', port=8000)") + mcp.run(transport="sse", host="0.0.0.0", port=8000) + except TypeError: + # Final fallback for older signatures. + logging.info("Fallback signature without host/port") + mcp.run(transport="sse")