""" Server setup and initialization for Intervals.icu MCP Server. This module handles transport configuration and server startup logic. """ import os import logging import fastmcp from fastmcp import FastMCP from intervals_mcp_server.utils.types import TransportAliases logger = logging.getLogger("intervals_icu_mcp_server") def setup_transport() -> TransportAliases: """ Setup and validate the MCP transport configuration. Reads MCP_TRANSPORT environment variable and validates it against supported transport types. Returns: TransportAliases: The selected transport type. Raises: ValueError: If the transport type is not supported. """ transport_env = os.getenv("MCP_TRANSPORT", TransportAliases.STDIO.value).lower() try: transport_alias = TransportAliases(transport_env) except ValueError as exc: allowed = ", ".join(item.value for item in TransportAliases) raise ValueError(f"Unsupported MCP_TRANSPORT value. Use one of: {allowed}.") from exc # Map SSE and HTTP aliases to STREAMABLE_HTTP selected_transport = ( TransportAliases.STREAMABLE_HTTP if transport_alias in (TransportAliases.HTTP, TransportAliases.SSE) else transport_alias ) return selected_transport def start_server(mcp_instance: FastMCP, transport: TransportAliases) -> None: """ Start the MCP server with the specified transport. Args: mcp_instance (FastMCP): The FastMCP server instance to start. transport (TransportAliases): The transport type to use. """ if transport == TransportAliases.STDIO: logger.info("Starting MCP server with stdio transport.") mcp_instance.run() else: # STREAMABLE_HTTP # fastmcp reads host/port from its own global settings (FASTMCP_HOST/ # FASTMCP_PORT) only once, at process start, so read them fresh here # rather than relying on that cached default. host = os.getenv("FASTMCP_HOST", "127.0.0.1") port = int(os.getenv("FASTMCP_PORT", "8000")) logger.info( "Starting MCP server with Streamable HTTP transport at http://%s:%s%s.", host, port, fastmcp.settings.streamable_http_path, ) mcp_instance.run(transport="streamable-http", host=host, port=port)