app.py 1.3 KB

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