support cookies

This commit is contained in:
2025-02-25 02:29:16 +00:00
parent 3fd605b111
commit 2b652c5926
7 changed files with 90 additions and 22 deletions

View File

@@ -35,18 +35,22 @@ class AirflowClient:
self,
spec_path: Path | str | dict | bytes | BinaryIO | TextIO,
base_url: str,
auth_token: str,
auth_token: str | None = None,
cookie: str | None = None,
) -> None:
"""Initialize Airflow client.
Args:
spec_path: OpenAPI spec as file path, dict, bytes, or file object
base_url: Base URL for API
auth_token: Authentication token
auth_token: Authentication token (optional if cookie is provided)
cookie: Session cookie (optional if auth_token is provided)
Raises:
ValueError: If spec_path is invalid or spec cannot be loaded
ValueError: If spec_path is invalid or spec cannot be loaded or if neither auth_token nor cookie is provided
"""
if not auth_token and not cookie:
raise ValueError("Either auth_token or cookie must be provided")
try:
# Load and parse OpenAPI spec
if isinstance(spec_path, dict):
@@ -96,10 +100,13 @@ class AirflowClient:
# API configuration
self.base_url = base_url.rstrip("/")
self.headers = {
"Authorization": f"Basic {auth_token}",
"Accept": "application/json",
}
self.headers = {"Accept": "application/json"}
# Set authentication header based on what was provided
if auth_token:
self.headers["Authorization"] = f"Basic {auth_token}"
elif cookie:
self.headers["Cookie"] = cookie
except Exception as e:
logger.error("Failed to initialize AirflowClient: %s", e)

View File

@@ -22,9 +22,16 @@ logger = logging.getLogger(__name__)
async def serve() -> None:
"""Start MCP server."""
required_vars = ["AIRFLOW_BASE_URL", "AUTH_TOKEN"]
if not all(var in os.environ for var in required_vars):
raise ValueError(f"Missing required environment variables: {required_vars}")
# Check for AIRFLOW_BASE_URL which is always required
if "AIRFLOW_BASE_URL" not in os.environ:
raise ValueError("Missing required environment variable: AIRFLOW_BASE_URL")
# Check for either AUTH_TOKEN or COOKIE
has_auth_token = "AUTH_TOKEN" in os.environ
has_cookie = "COOKIE" in os.environ
if not has_auth_token and not has_cookie:
raise ValueError("Either AUTH_TOKEN or COOKIE environment variable must be provided")
server = Server("airflow-mcp-server")

View File

@@ -13,9 +13,16 @@ logger = logging.getLogger(__name__)
async def serve() -> None:
"""Start MCP server in safe mode (read-only operations)."""
required_vars = ["AIRFLOW_BASE_URL", "AUTH_TOKEN"]
if not all(var in os.environ for var in required_vars):
raise ValueError(f"Missing required environment variables: {required_vars}")
# Check for AIRFLOW_BASE_URL which is always required
if "AIRFLOW_BASE_URL" not in os.environ:
raise ValueError("Missing required environment variable: AIRFLOW_BASE_URL")
# Check for either AUTH_TOKEN or COOKIE
has_auth_token = "AUTH_TOKEN" in os.environ
has_cookie = "COOKIE" in os.environ
if not has_auth_token and not has_cookie:
raise ValueError("Either AUTH_TOKEN or COOKIE environment variable must be provided")
server = Server("airflow-mcp-server-safe")

View File

@@ -13,9 +13,16 @@ logger = logging.getLogger(__name__)
async def serve() -> None:
"""Start MCP server in unsafe mode (all operations)."""
required_vars = ["AIRFLOW_BASE_URL", "AUTH_TOKEN"]
if not all(var in os.environ for var in required_vars):
raise ValueError(f"Missing required environment variables: {required_vars}")
# Check for AIRFLOW_BASE_URL which is always required
if "AIRFLOW_BASE_URL" not in os.environ:
raise ValueError("Missing required environment variable: AIRFLOW_BASE_URL")
# Check for either AUTH_TOKEN or COOKIE
has_auth_token = "AUTH_TOKEN" in os.environ
has_cookie = "COOKIE" in os.environ
if not has_auth_token and not has_cookie:
raise ValueError("Either AUTH_TOKEN or COOKIE environment variable must be provided")
server = Server("airflow-mcp-server-unsafe")

View File

@@ -32,12 +32,26 @@ def _initialize_client() -> AirflowClient:
except Exception as e:
raise ValueError("Default OpenAPI spec not found in package resources") from e
required_vars = ["AIRFLOW_BASE_URL", "AUTH_TOKEN"]
missing_vars = [var for var in required_vars if var not in os.environ]
if missing_vars:
raise ValueError(f"Missing required environment variables: {missing_vars}")
# Check for base URL
if "AIRFLOW_BASE_URL" not in os.environ:
raise ValueError("Missing required environment variable: AIRFLOW_BASE_URL")
return AirflowClient(spec_path=spec_path, base_url=os.environ["AIRFLOW_BASE_URL"], auth_token=os.environ["AUTH_TOKEN"])
# Check for either AUTH_TOKEN or COOKIE
has_auth_token = "AUTH_TOKEN" in os.environ
has_cookie = "COOKIE" in os.environ
if not has_auth_token and not has_cookie:
raise ValueError("Either AUTH_TOKEN or COOKIE environment variable must be provided")
# Initialize client with appropriate authentication method
client_args = {"spec_path": spec_path, "base_url": os.environ["AIRFLOW_BASE_URL"]}
if has_auth_token:
client_args["auth_token"] = os.environ["AUTH_TOKEN"]
elif has_cookie:
client_args["cookie"] = os.environ["COOKIE"]
return AirflowClient(**client_args)
async def _initialize_tools() -> None: