app.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from __future__ import annotations
  2. import logging
  3. import os
  4. import uvicorn
  5. from .db import check_database_connection
  6. from .mcp_app import mcp
  7. # Import tool modules for registration side effects.
  8. from . import bacnet_server as bacnet_server # noqa: F401
  9. from . import common_server as common_server # noqa: F401
  10. from . import modbus_server as modbus_server # noqa: F401
  11. from . import s7_server as s7_server # noqa: F401
  12. logger = logging.getLogger(__name__)
  13. def build_mcp_http_app(path: str | None = None):
  14. effective_path = str(path or os.getenv("MCP_PATH", "/mcp")).strip() or "/mcp"
  15. return mcp.http_app(path=effective_path, transport="http", stateless_http=True)
  16. def main() -> None:
  17. logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"))
  18. try:
  19. check_database_connection()
  20. except Exception:
  21. logger.exception("Database startup check failed")
  22. raise SystemExit(1)
  23. host = os.getenv("MCP_HOST", "0.0.0.0").strip() or "0.0.0.0"
  24. port = int(os.getenv("MCP_PORT", "8501"))
  25. path = os.getenv("MCP_PATH", "/mcp").strip() or "/mcp"
  26. app = build_mcp_http_app(path)
  27. print(f"Using FastMCP app '{mcp.name}' on http://{host}:{port}{path}")
  28. uvicorn.run(
  29. app,
  30. host=host,
  31. port=port,
  32. lifespan="on",
  33. timeout_graceful_shutdown=2,
  34. ws="websockets-sansio",
  35. )