# -*- coding: utf-8 -*-
"""Location: ./mcpgateway/transports/context.py
Copyright 2026
SPDX-License-Identifier: Apache-2.0
Authors: Mihai Criveti

Request-scoped context variables shared across transports and services.
These ``ContextVar``s are populated by the transport layer (primarily
``streamablehttp_transport``) and read by service-layer code that needs
request-scoped metadata without taking a dependency on the transport module.
Keeping them in a neutral module breaks the cycle that otherwise exists
between ``mcpgateway.services.*`` and
``mcpgateway.transports.streamablehttp_transport``.
"""

# Future
from __future__ import annotations

# Standard
import contextvars
from typing import Any, Dict, Optional

# Third-Party
from cpex.framework import UserContext

# Per-request HTTP headers. Set by the streamable-http ASGI layer before
# dispatching into business logic; read by anything that needs the caller's
# downstream ``Mcp-Session-Id``, passthrough headers, etc.
request_headers_var: contextvars.ContextVar[Dict[str, Any]] = contextvars.ContextVar("request_headers", default={})

# Authenticated user context for the current request. Mirrors the headers
# ContextVar — transport layer fills it, service layer reads it.
user_context_var: contextvars.ContextVar[Dict[str, Any]] = contextvars.ContextVar("user_context", default={})

# UserContext is now defined in cpex.framework (single source of truth) and
# re-exported here so that existing imports of the form
# ``from mcpgateway.transports.context import UserContext`` keep working.
__all__ = ["UserContext", "request_headers_var", "user_context_var", "user_identity_var"]


user_identity_var: contextvars.ContextVar[Optional[UserContext]] = contextvars.ContextVar("user_identity", default=None)
