"""3tears-mcp: shared Model Context Protocol framework.

per-product MCP servers compose this
framework instead of reimplementing stdio transport, JWT auth, error
mapping, and per-tool RBAC.

modules:

- :mod:`threetears.mcp.server` -- :class:`McpServer` (RBAC-gated
  wrapper over the official ``mcp.server.Server``; v1 stdio +
  v2 Streamable-HTTP entry points)
- :mod:`threetears.mcp.http_server` -- v2 Streamable-HTTP transport
  (:func:`build_mcp_http_app`, :func:`serve_mcp_http`,
  :func:`current_bearer_token`) running the SAME wired SDK server over
  HTTP
- :mod:`threetears.mcp.tool` -- :class:`McpTool`, :class:`ToolRegistry`,
  :func:`register_tool` decorator
- :mod:`threetears.mcp.http_client` -- :class:`PlatformHttpClient`
  (typed httpx with JWT login + refresh-on-401; used by both MCP
  servers and CLI scripts)
- :mod:`threetears.mcp.auth` -- :class:`Identity`,
  :class:`IdentityProvider` Protocol + :class:`EnvVarIdentityProvider`
  (v1 stdio) + :class:`BearerTokenIdentityProvider` (v2 HTTP),
  :class:`Authorizer` Protocol + :class:`LocalGrantAuthorizer`
- :mod:`threetears.mcp.rbac` -- :class:`McpToolGrantCollection` over
  the ``mcp_tool_grants`` table
- :mod:`threetears.mcp.migrations` -- platform-scope migration that
  creates ``mcp_tool_grants``
"""

from __future__ import annotations

# Version derived from pyproject.toml so the metadata is the single
# source of truth -- a future release that bumps pyproject without
# updating ``__init__.py`` can't drift the runtime ``__version__``.
# The except guard handles the rare case where the package isn't
# installed via importlib.metadata (e.g. running directly from a
# checked-out source tree without ``uv sync``); the fallback keeps
# imports working but reports ``unknown`` rather than crashing.
from importlib.metadata import PackageNotFoundError as _PackageNotFoundError
from importlib.metadata import version as _version

try:
    __version__ = _version("3tears-mcp")
except _PackageNotFoundError:  # pragma: no cover - dev fallback
    __version__ = "unknown"

from threetears.mcp.auth import (
    Authorizer,
    BearerTokenIdentityProvider,
    BearerTokenResolver,
    EnvVarIdentityProvider,
    Identity,
    IdentityProvider,
    LocalGrantAuthorizer,
    PrincipalType,
    TokenSource,
)
from threetears.mcp.http_client import PlatformHttpClient, PlatformHttpError
from threetears.mcp.http_server import (
    build_mcp_http_app,
    current_bearer_token,
    serve_mcp_http,
)
from threetears.mcp.rbac import (
    McpToolGrantCollection,
    McpToolGrantEntity,
    mcp_tool_grants_table,
)
from threetears.mcp.server import McpServer
from threetears.mcp.tool import (
    McpTool,
    ToolHandler,
    ToolRegistry,
    register_tool,
)

__all__ = [
    "Authorizer",
    "BearerTokenIdentityProvider",
    "BearerTokenResolver",
    "EnvVarIdentityProvider",
    "Identity",
    "IdentityProvider",
    "LocalGrantAuthorizer",
    "McpServer",
    "McpTool",
    "McpToolGrantCollection",
    "McpToolGrantEntity",
    "PlatformHttpClient",
    "PlatformHttpError",
    "PrincipalType",
    "TokenSource",
    "ToolHandler",
    "ToolRegistry",
    "build_mcp_http_app",
    "current_bearer_token",
    "mcp_tool_grants_table",
    "register_tool",
    "serve_mcp_http",
]
