← Files Dataiku HeadlessARCHIVED FILE

dataiku_mcp/instance_policy.py

2.39 KB · Nov 27, 2026 · 19:32 UTC

↓ Download file

See the change to this file →

# Copyright 2026 Dataiku SAS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Declared instance policies, enforced before acquiring backend credentials."""

from typing import get_args

from .config.models import InstanceType

# Instance types currently supported through DSSClient.
SUPPORTED_DSS_INSTANCE_TYPES = frozenset({"design", "automation", "deployer"})

# These are @mcp.tool metadata policies. V1 tools expose individual actions;
# project authoring still routes through Cobuild and its fixed direct exceptions.
PROJECT_READ_POLICY = {"allowed_instance_types": ["design", "automation"]}
PROJECT_EXECUTION_POLICY = {"allowed_instance_types": ["design", "automation"]}
ADMIN_POLICY = {"allowed_instance_types": ["design", "automation"]}
PROJECT_EDIT_POLICY = {"allowed_instance_types": ["design"]}
API_OPERATIONS_POLICY = {"allowed_instance_types": ["deployer"]}
INSTANCE_MANAGEMENT_POLICY = {"allowed_instance_types": list(get_args(InstanceType))}


def require_instance_type(
    tool_name: str, instance_type: InstanceType | None, metadata: dict | None
) -> None:
    allowed = (
        metadata.get("allowed_instance_types") if isinstance(metadata, dict) else None
    )
    if (
        not isinstance(allowed, list)
        or not allowed
        or any(
            not isinstance(value, str) or value not in get_args(InstanceType)
            for value in allowed
        )
        or len(allowed) != len(set(allowed))
    ):
        raise ValueError(f"Tool '{tool_name}' has an invalid instance-type policy.")
    # Only the connection-management group can run before a profile is selected.
    if instance_type is None and metadata == INSTANCE_MANAGEMENT_POLICY:
        return
    if instance_type not in allowed:
        raise ValueError(
            f"Tool '{tool_name}' is unavailable for instance type '{instance_type}'. Allowed types: {', '.join(allowed)}. Use switch_instance to choose a supported instance."
        )

SHA-256: 97a9ba539c13720847bf723c4b4ff1da1c5fbb9f0df7aa20ac7648739c0da210