Spaces:
Running
Running
File size: 2,355 Bytes
889e080 13e3e7d 889e080 c5917f4 889e080 c5917f4 889e080 13e3e7d 889e080 13e3e7d 889e080 13e3e7d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | """
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)
|