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}"
|
return f"Query failed: {e}"
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import uvicorn
|
||||||
logging.info("Starting knowledge-mcp server via embedded FastMCP runner...")
|
|
||||||
|
|
||||||
# Force FastMCP.run() to start SSE listener on all interfaces.
|
logging.info("Starting knowledge-mcp server via python entrypoint...")
|
||||||
# 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}")
|
|
||||||
|
|
||||||
|
# Strategy 1 (preferred): build an ASGI app and run via uvicorn.
|
||||||
|
# This avoids FastMCP CLI argument parsing ambiguity.
|
||||||
|
app = None
|
||||||
try:
|
try:
|
||||||
mcp.run()
|
if hasattr(mcp, "sse_app"):
|
||||||
except BaseException as e:
|
app = mcp.sse_app()
|
||||||
logging.critical(f"Server crashed: {e}", exc_info=True)
|
logging.info("Using mcp.sse_app() with uvicorn")
|
||||||
raise
|
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