Files
core/homeassistant/components/mcp/application_credentials.py
T
epenetGitHubcopilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>frenck
d766aae436 Remove import annotations from components (#169536)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: frenck <195327+frenck@users.noreply.github.com>
2026-04-30 21:14:48 +02:00

34 lines
1.1 KiB
Python

"""Application credentials platform for Model Context Protocol."""
from collections.abc import Generator
from contextlib import contextmanager
import contextvars
from homeassistant.components.application_credentials import AuthorizationServer
from homeassistant.core import HomeAssistant
CONF_ACTIVE_AUTHORIZATION_SERVER = "active_authorization_server"
_mcp_context: contextvars.ContextVar[AuthorizationServer] = contextvars.ContextVar(
"mcp_authorization_server_context"
)
@contextmanager
def authorization_server_context(
authorization_server: AuthorizationServer,
) -> Generator[None]:
"""Context manager for setting the active authorization server."""
token = _mcp_context.set(authorization_server)
try:
yield
finally:
_mcp_context.reset(token)
async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer:
"""Return authorization server, for the default auth implementation."""
if _mcp_context.get() is None:
raise RuntimeError("No MCP authorization server set in context")
return _mcp_context.get()