"""MCP gateway: scoped bearer tokens for external AI agents Revision ID: a8b9c0d1e2f3 Revises: f7a8b9c0d1e2 Create Date: 2026-10-14 12:00:00.000000 Owned by Agent 1 (PLAN-v2 §6). Only the token hash and a display prefix are stored; the raw token is shown once at issue time. """ from __future__ import annotations from collections.abc import Sequence import sqlalchemy as sa from alembic import op revision: str = "a8b9c0d1e2f3" down_revision: str | None = "f7a8b9c0d1e2" branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None def upgrade() -> None: op.create_table( "mcp_tokens", sa.Column("id", sa.String(length=32), primary_key=True), sa.Column( "organization_id", sa.String(length=32), sa.ForeignKey("organizations.id", ondelete="CASCADE"), nullable=False, ), sa.Column("name", sa.String(length=120), nullable=False), # ``unique=True`` carries the index (``ix_mcp_tokens_token_hash``), so no # separate create_index is needed — a second one would drift. sa.Column("token_hash", sa.String(length=128), nullable=False, unique=True), sa.Column("prefix", sa.String(length=16), nullable=False), sa.Column("scopes", sa.String(length=255), nullable=False, server_default=""), sa.Column("created_by_user_id", sa.String(length=32), sa.ForeignKey("users.id")), sa.Column("expires_at", sa.DateTime(timezone=True), nullable=True), sa.Column("last_used_at", sa.DateTime(timezone=True), nullable=True), sa.Column("revoked_at", sa.DateTime(timezone=True), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), ) op.create_index("ix_mcp_tokens_organization_id", "mcp_tokens", ["organization_id"]) def downgrade() -> None: op.drop_index("ix_mcp_tokens_organization_id", table_name="mcp_tokens") op.drop_table("mcp_tokens")