""" Main MCP server entry point for TiDB Observability Server. This server provides tools for querying Prometheus metrics and analyzing time-series data. """ import asyncio import logging import os import src.tools.query_range # noqa: F401 # register query_range tool (side effect) from src.services.auth import bootstrap_server_auth from src.tools import ( # noqa: F401,I001 get_loki_label_values, # noqa: F401 get_o11y_delta_path, # noqa: F401 list_loki_labels, # noqa: F401 list_loki_series, # noqa: F401 list_metric_label_values, # noqa: F401 list_metric_labels, # noqa: F401 list_metrics, # noqa: F401 query_instant, # noqa: F401 query_loki_instant, # noqa: F401 query_loki_range, # noqa: F401 query_topsql, # noqa: F401 read_delta, # noqa: F401 ) from src.tools.query_range import mcp from src.utils.config import get_auth_service_url, get_github_token from src.utils.logging_utils import get_logger logger = get_logger("startup") LOG_FILE = "/tmp/o11y-mcp-server.log" DEBUG_ENABLED = bool(os.environ.get("DEBUG")) root_logger = logging.getLogger() root_logger.setLevel(logging.INFO) console_handler = logging.StreamHandler() console_handler.setLevel(logging.INFO) console_handler.setFormatter( logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") ) root_logger.addHandler(console_handler) if DEBUG_ENABLED: file_handler = logging.FileHandler(LOG_FILE) file_handler.setLevel(logging.DEBUG) file_handler.setFormatter( logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") ) root_logger.addHandler(file_handler) logging.info(f"Logging to file: {LOG_FILE}") root_logger.setLevel(logging.DEBUG) console_handler.setLevel(logging.DEBUG) logging.getLogger("src").setLevel(logging.DEBUG) def main() -> None: logger.info("server_startup_begin") has_github_token = bool(get_github_token()) has_auth_service_url = bool(get_auth_service_url()) logger.info( "server_auth_bootstrap_config has_github_token=%s has_auth_service_url=%s", has_github_token, has_auth_service_url, ) if has_github_token and has_auth_service_url: logger.info("server_auth_bootstrap_begin") try: asyncio.run(bootstrap_server_auth()) logger.info("server_auth_bootstrap_success") except Exception: logger.exception("server_auth_bootstrap_failed") raise else: logger.info("server_auth_bootstrap_skipped") logger.info("server_mcp_run_begin") # Run the server # FastMCP automatically handles JSON-RPC over stdin/stdout # All output must go through JSON-RPC protocol - no print() or stdout writes mcp.run() __all__ = ["mcp", "main"] if __name__ == "__main__": main()