← Files Dataiku HeadlessARCHIVED FILE

dataiku_mcp/tools/cross_project_sharing.py

2.5 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.

"""Cross-project sharing inspection for Dataiku shared objects."""

from dataikuapi.utils import DataikuException
from fastmcp import Context

from ..instance_policy import PROJECT_READ_POLICY
from ..server import mcp
from ..auth import get_dss_client
from ..executors import run_blocking
from dataiku_mcp.utils.serialization import columnar, compact_json
from dataiku_mcp.utils.validation import (
    require_non_empty_string as _require_non_empty_string,
)


def _raise_if_forbidden(project_key: str, exc: DataikuException) -> None:
    msg = str(exc)
    if "forbidden" in msg.lower() or "unauthorized" in msg.lower():
        raise PermissionError(
            f"Forbidden on source '{project_key}'. "
            f"Needs `Read project conf` + `Write project conf`."
        ) from exc


@mcp.tool(
    meta=PROJECT_READ_POLICY,
    title="List Shared Objects",
    annotations={
        "readOnlyHint": True,
        "destructiveHint": False,
        "openWorldHint": False,
    },
)
async def list_shared_objects(project_key: str, ctx: Context) -> str:
    """Find which of a project's objects are exposed to which other projects."""
    project_key = _require_non_empty_string(project_key, "project_key")
    await ctx.info(f"Listing shared objects in {project_key}...")

    def _run():
        try:
            settings = get_dss_client().get_project(project_key).get_settings()
        except DataikuException as exc:
            _raise_if_forbidden(project_key, exc)
            raise
        return [
            {
                "type": obj.get("type"),
                "local_name": obj.get("localName"),
                "target_projects": [
                    rule.get("targetProject") for rule in obj.get("rules", [])
                ],
            }
            for obj in settings.get_raw().get("exposedObjects", {}).get("objects", [])
        ]

    rows = await run_blocking(_run)
    return compact_json(columnar(rows, ["type", "local_name", "target_projects"]))

SHA-256: 3a3bb2f8891de230627e268636dbede844a888d58a20dd3c249226c08736112c