fix(runtime): prefer uvicorn ASGI app path, fallback to programmatic mcp.run
This commit is contained in:
36
server.py
36
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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user