"""Per-request context shared between the access-gate middleware and the mounted MCP server. The middleware authenticates every request (cookie or bearer token) and stows the resolved user here; MCP tools — which run inside the mounted Streamable HTTP sub-app and don't see FastAPI's ``request.state`` directly — read it back to act as the authenticated operator. """ from __future__ import annotations from contextvars import ContextVar from typing import Any current_user_ctx: ContextVar[Any | None] = ContextVar("current_user", default=None)