File size: 2,855 Bytes
04bb755 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""MCP server registry and access grant models."""
import uuid
from datetime import datetime
from sqlalchemy import Boolean, DateTime, ForeignKey, JSON, String, Text, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from src.database import Base
class MCPServer(Base):
"""A registered MCP server that agents can use to access external tools."""
__tablename__ = "mcp_servers"
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
owner_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
url: Mapped[str] = mapped_column(String(2048), nullable=False)
description: Mapped[str] = mapped_column(Text, nullable=True)
auth_type: Mapped[str] = mapped_column(
String(50), nullable=False, default="none"
) # none, bearer, api_key, oauth2
auth_config: Mapped[dict] = mapped_column(JSON, nullable=True, default=dict)
tools_cached: Mapped[dict] = mapped_column(JSON, nullable=True, default=dict)
is_public: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
status: Mapped[str] = mapped_column(String(50), nullable=False, default="active")
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
# Relationships
grants = relationship("MCPGrant", back_populates="mcp_server", cascade="all, delete-orphan")
class MCPGrant(Base):
"""Authorization for an agent to access a specific MCP server."""
__tablename__ = "mcp_grants"
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
user_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
agent_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("agents.id", ondelete="CASCADE"), nullable=False, index=True
)
mcp_server_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("mcp_servers.id", ondelete="CASCADE"), nullable=False, index=True
)
scopes: Mapped[list] = mapped_column(
JSON, nullable=False, default=list
) # Tool names; empty = all tools
expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
# Relationships
mcp_server = relationship("MCPServer", back_populates="grants")
|