Skip to content

AI Models API Package

nautobot_ai_models.api

REST API module for nautobot_ai_models app.

serializers

API serializers for nautobot_ai_models.

This is how a consumer outside Nautobot reads the registries, so the MCP tool serializer carries the whole advertised definition - both schemas included - rather than a summary.

AIModelSerializer

Bases: NautobotModelSerializer

AI Model Serializer.

Source code in nautobot_ai_models/api/serializers.py
class AIModelSerializer(NautobotModelSerializer):  # pylint: disable=too-many-ancestors
    """AI Model Serializer."""

    class Meta:
        """Meta attributes."""

        model = models.AIModel
        fields = "__all__"
Meta

Meta attributes.

Source code in nautobot_ai_models/api/serializers.py
class Meta:
    """Meta attributes."""

    model = models.AIModel
    fields = "__all__"

AIProviderSerializer

Bases: NautobotModelSerializer

AI Provider Serializer.

Source code in nautobot_ai_models/api/serializers.py
class AIProviderSerializer(NautobotModelSerializer):  # pylint: disable=too-many-ancestors
    """AI Provider Serializer."""

    class Meta:
        """Meta attributes."""

        model = models.AIProvider
        fields = "__all__"
Meta

Meta attributes.

Source code in nautobot_ai_models/api/serializers.py
class Meta:
    """Meta attributes."""

    model = models.AIProvider
    fields = "__all__"

MCPServerSerializer

Bases: NautobotModelSerializer, TaggedModelSerializerMixin

MCPServer Serializer.

Source code in nautobot_ai_models/api/serializers.py
class MCPServerSerializer(NautobotModelSerializer, TaggedModelSerializerMixin):  # pylint: disable=too-many-ancestors
    """MCPServer Serializer."""

    tool_count = drf_serializers.SerializerMethodField()

    class Meta:
        """Meta attributes."""

        model = models.MCPServer
        fields = "__all__"

        # Written by the discovery job from what the server said about itself. A client that could
        # PATCH these could make the registry claim a server reported something it never did.
        read_only_fields = list(MCP_SERVER_DISCOVERED_FIELDS)

    def get_tool_count(self, obj):
        """How many tools this server offers.

        Read off the viewset's annotation when there is one, and counted otherwise. A serializer
        also renders objects that never came from that queryset - the one just created by a POST,
        for instance - and reading a missing annotation would fail there.
        """
        count = getattr(obj, "tool_count", None)
        return obj.tools.count() if count is None else count
Meta

Meta attributes.

Source code in nautobot_ai_models/api/serializers.py
class Meta:
    """Meta attributes."""

    model = models.MCPServer
    fields = "__all__"

    # Written by the discovery job from what the server said about itself. A client that could
    # PATCH these could make the registry claim a server reported something it never did.
    read_only_fields = list(MCP_SERVER_DISCOVERED_FIELDS)
get_tool_count(obj)

How many tools this server offers.

Read off the viewset's annotation when there is one, and counted otherwise. A serializer also renders objects that never came from that queryset - the one just created by a POST, for instance - and reading a missing annotation would fail there.

Source code in nautobot_ai_models/api/serializers.py
def get_tool_count(self, obj):
    """How many tools this server offers.

    Read off the viewset's annotation when there is one, and counted otherwise. A serializer
    also renders objects that never came from that queryset - the one just created by a POST,
    for instance - and reading a missing annotation would fail there.
    """
    count = getattr(obj, "tool_count", None)
    return obj.tools.count() if count is None else count

MCPToolSerializer

Bases: NautobotModelSerializer

MCPTool Serializer.

Source code in nautobot_ai_models/api/serializers.py
class MCPToolSerializer(NautobotModelSerializer):  # pylint: disable=too-many-ancestors
    """MCPTool Serializer."""

    is_available = drf_serializers.BooleanField(read_only=True)

    class Meta:
        """Meta attributes."""

        model = models.MCPTool
        fields = "__all__"

        # Discovery's own evidence of what it saw and when. A client that could rewrite either
        # could make the registry claim a review is current when it is not.
        #
        # The rest of what discovery writes - title, description, both schemas, and
        # advertised_read_only - stays writable on purpose. A stdio server cannot be reached
        # from a worker, so its tools are entered and corrected by hand.
        read_only_fields = [
            "definition_fingerprint",
            "last_seen_at",
        ]
Meta

Meta attributes.

Source code in nautobot_ai_models/api/serializers.py
class Meta:
    """Meta attributes."""

    model = models.MCPTool
    fields = "__all__"

    # Discovery's own evidence of what it saw and when. A client that could rewrite either
    # could make the registry claim a review is current when it is not.
    #
    # The rest of what discovery writes - title, description, both schemas, and
    # advertised_read_only - stays writable on purpose. A stdio server cannot be reached
    # from a worker, so its tools are entered and corrected by hand.
    read_only_fields = [
        "definition_fingerprint",
        "last_seen_at",
    ]

urls

Django API urlpatterns declaration for nautobot_ai_models app.

views

API views for nautobot_ai_models.

AIModelViewSet

Bases: NautobotModelViewSet

AI Model viewset.

Source code in nautobot_ai_models/api/views.py
class AIModelViewSet(NautobotModelViewSet):  # pylint: disable=too-many-ancestors
    """AI Model viewset."""

    queryset = models.AIModel.objects.select_related("provider")
    serializer_class = serializers.AIModelSerializer
    filterset_class = filters.AIModelFilterSet

AIProviderViewSet

Bases: NautobotModelViewSet

AI Provider viewset.

Source code in nautobot_ai_models/api/views.py
class AIProviderViewSet(NautobotModelViewSet):  # pylint: disable=too-many-ancestors
    """AI Provider viewset."""

    queryset = models.AIProvider.objects.select_related("external_integration")
    serializer_class = serializers.AIProviderSerializer
    filterset_class = filters.AIProviderFilterSet

MCPServerViewSet

Bases: NautobotModelViewSet

MCPServer viewset.

Source code in nautobot_ai_models/api/views.py
class MCPServerViewSet(NautobotModelViewSet):  # pylint: disable=too-many-ancestors
    """MCPServer viewset."""

    queryset = models.MCPServer.objects.select_related("external_integration", "tenant").annotate(
        tool_count=count_related(models.MCPTool, "mcp_server")
    )
    serializer_class = serializers.MCPServerSerializer
    filterset_class = filters.MCPServerFilterSet

MCPToolViewSet

Bases: NautobotModelViewSet

MCPTool viewset.

Source code in nautobot_ai_models/api/views.py
class MCPToolViewSet(NautobotModelViewSet):  # pylint: disable=too-many-ancestors
    """MCPTool viewset."""

    queryset = models.MCPTool.objects.select_related("mcp_server")
    serializer_class = serializers.MCPToolSerializer
    filterset_class = filters.MCPToolFilterSet