"""External MCP server connections (RLS forced; migration 0010).""" from __future__ import annotations import uuid from datetime import datetime from typing import Any from sqlalchemy import Boolean, CheckConstraint, DateTime, String, UniqueConstraint, func from sqlalchemy.dialects.postgresql import JSONB, UUID from sqlalchemy.orm import Mapped, mapped_column from core.db.base import Base, TenantMixin, TimestampMixin, new_id class McpConnection(TenantMixin, TimestampMixin, Base): """``url`` is the server's Streamable HTTP endpoint; ``secret_ref`` names the bearer token (or OAuth access token kept fresh by the secret store) — never the value.""" __tablename__ = "mcp_connections" __table_args__ = ( UniqueConstraint("tenant_id", "name", name="uq_mcp_connections_tenant_name"), CheckConstraint("auth_type IN ('none', 'bearer')", name="auth_type"), CheckConstraint("status IN ('new', 'ok', 'changed', 'error')", name="status"), ) id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id) name: Mapped[str] = mapped_column(String(40), nullable=False) url: Mapped[str] = mapped_column(String(500), nullable=False) auth_type: Mapped[str] = mapped_column(String(20), nullable=False, default="none") secret_ref: Mapped[str | None] = mapped_column(String(120), nullable=True) enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True) # Phase 15: the server can move money. Its tools are tagged ``payments`` and the gateway # holds them for a human when the run was started by untrusted inbound content # (email/WhatsApp/webhook): payment servers are isolated from inbox-reading agents. payment_capable: Mapped[bool] = mapped_column( Boolean, nullable=False, default=False, server_default="false" ) status: Mapped[str] = mapped_column(String(20), nullable=False, default="new") last_refreshed_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True ) last_diff: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False, default=dict) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False )