← Files Dataiku HeadlessARCHIVED FILE

dataiku_mcp/tools/instances.py

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

"""Instance management tools for switching between Dataiku instances."""

import asyncio
import secrets
from dataclasses import asdict
from typing import Annotated

from fastmcp import Context
from pydantic import Field

from ..instance_policy import INSTANCE_MANAGEMENT_POLICY, SUPPORTED_DSS_INSTANCE_TYPES
from ..auth import get_dss_client
from ..config import request, stdio
from ..executors import run_blocking
from ..server import DSS_INDEPENDENT_TOOL_TAG, mcp
from ..setup_server import SESSION_LIFETIME_SECONDS, SetupSession, start_setup_server
from .utils.errors import safe_error_text
from dataiku_mcp.utils.serialization import columnar, compact_json, omit_empty

InstanceName = Annotated[
    str, Field(description="A configured instance name, from list_instances.")
]


@mcp.tool(
    meta=INSTANCE_MANAGEMENT_POLICY,
    title="List Dataiku Instances",
    annotations={
        "readOnlyHint": True,
        "destructiveHint": False,
        "openWorldHint": False,
    },
    tags={DSS_INDEPENDENT_TOOL_TAG},
)
async def list_instances(ctx: Context) -> str:
    """See which Dataiku instances are configured and which one is active."""
    instances = request.get_instances()
    try:
        current_instance_name = request.get_pinned_instance().name
    except ValueError:
        current_instance_name = ""

    # Never include instance credentials in tool responses.
    result = []
    for name, inst in instances.items():
        result.append(
            {
                "name": name,
                "url": inst.url,
                "description": inst.description,
                "active": name == current_instance_name,
                "instance_type": inst.instance_type,
            }
        )
    return compact_json(
        columnar(result, ["name", "url", "description", "active", "instance_type"])
    )


@mcp.tool(
    meta=INSTANCE_MANAGEMENT_POLICY,
    title="Switch Dataiku Instance",
    annotations={
        "readOnlyHint": False,
        "destructiveHint": False,
        "idempotentHint": True,
        "openWorldHint": False,
    },
    tags={DSS_INDEPENDENT_TOOL_TAG},
)
async def switch_instance(name: InstanceName, ctx: Context) -> str:
    """Retarget every later tool call at a different configured instance."""
    await ctx.info(f"Switching to instance '{name}'...")
    info = await run_blocking(request.set_current_instance, name)
    return compact_json(info)


@mcp.tool(
    meta=INSTANCE_MANAGEMENT_POLICY,
    title="Delete Dataiku Instance",
    annotations={
        "readOnlyHint": False,
        "destructiveHint": True,
        "idempotentHint": False,
        "openWorldHint": False,
    },
    tags={DSS_INDEPENDENT_TOOL_TAG},
)
async def delete_instance(name: InstanceName, ctx: Context) -> str:
    """Forget a stored instance's local config; one set via DKU_DSS_URL cannot be deleted."""
    if request.is_http_request():
        raise ValueError(
            "Instances are platform-managed in HTTP mode and cannot be deleted."
        )
    await ctx.info(f"Deleting instance '{name}'...")
    info = await run_blocking(stdio.delete_instance_from_config, name)
    return compact_json(info)


@mcp.tool(
    meta=INSTANCE_MANAGEMENT_POLICY,
    title="Get Current Instance",
    annotations={
        "readOnlyHint": True,
        "destructiveHint": False,
        "openWorldHint": False,
    },
)
async def get_current_instance(ctx: Context) -> str:
    """Inspect the active profile, DSS connection and failure details, or an unsupported node type."""

    # Strip both credential types from the response, including on failure.
    current_instance = asdict(request.get_pinned_instance())
    current_instance.pop("api_key", None)
    current_instance.pop("api_ticket", None)
    current_instance.pop("encrypted_rpc_cert_path", None)
    if current_instance["instance_type"] not in SUPPORTED_DSS_INSTANCE_TYPES:
        current_instance["connection_status"] = "unsupported"
        return compact_json(omit_empty(current_instance))
    current_instance["connection_status"] = "failed"
    try:
        version = await run_blocking(
            lambda: get_dss_client().get_instance_info().raw.get("dssVersion") or ""
        )
    except Exception as exc:
        current_instance["error"] = safe_error_text(exc)
    else:
        current_instance["connection_status"] = "connected"
        current_instance["dataiku_version"] = version

    result = omit_empty(current_instance)
    return compact_json(result)


async def _start_connection_manager(*, open_browser: bool = True) -> SetupSession:
    # Executor work keeps running after cancellation. Retain its result so the
    # newly created manager can be closed before propagating cancellation.
    startup = asyncio.create_task(
        run_blocking(lambda: start_setup_server(open_browser=open_browser))
    )
    try:
        return await asyncio.shield(startup)
    except asyncio.CancelledError:

        async def cleanup():
            session = await startup
            await run_blocking(session.close)

        closing = asyncio.create_task(cleanup())
        # MCP cancellation scopes can cancel each subsequent await as well.
        # Keep cleanup independent until it has reclaimed the server.
        while not closing.done():
            try:
                await asyncio.shield(closing)
            except asyncio.CancelledError:
                pass
        closing.result()
        raise


@mcp.tool(
    meta=INSTANCE_MANAGEMENT_POLICY,
    title="Manage Dataiku Instances",
    annotations={
        "readOnlyHint": False,
        "destructiveHint": True,
        "idempotentHint": False,
        "openWorldHint": False,
    },
    tags={DSS_INDEPENDENT_TOOL_TAG},
)
async def configure_instance(ctx: Context) -> str:
    """Open the local browser to add, edit, delete, or select saved Dataiku instances."""
    if request.is_http_request():
        raise ValueError(
            "Instances are platform-managed in HTTP mode and cannot be configured."
        )
    client_params = ctx.session.client_params
    elicitation_capability = (
        client_params.capabilities.elicitation if client_params else None
    )
    supports_url_elicitation = bool(
        elicitation_capability and elicitation_capability.url is not None
    )

    if not supports_url_elicitation:
        await ctx.info(
            "This MCP client does not advertise URL elicitation; opening the "
            "local configuration page directly."
        )
        session = await _start_connection_manager()
        return compact_json(
            {
                "interaction_mode": "direct_local_url",
                "setup_url": session.url,
                "browser_opened": session.browser_opened,
                "expires_in_seconds": SESSION_LIFETIME_SECONDS,
                "next_step": "Open setup_url if needed. Manage instances, then select Done.",
            }
        )

    await ctx.info("Requesting permission to open the Dataiku configuration page...")
    session = await _start_connection_manager(open_browser=False)
    elicitation_id = secrets.token_urlsafe(18)
    try:
        elicitation = await ctx.session.elicit_url(
            message=(
                "Open the local Dataiku instance manager to manage saved instances, "
                "then select Done to return to your agent."
            ),
            url=session.url,
            elicitation_id=elicitation_id,
            related_request_id=ctx.request_id,
        )
    except BaseException:
        await run_blocking(session.close)
        raise

    if elicitation.action != "accept":
        # Some clients advertise URL elicitation but decline the request because
        # the capability is gated behind an unreleased feature flag (e.g. Codex,
        # openai/codex#29344). Fall back to opening the local setup page directly.
        fallback = await _start_connection_manager()
        return compact_json(
            {
                "interaction_mode": "direct_local_url",
                "elicitation_status": elicitation.action,
                "setup_url": fallback.url,
                "browser_opened": fallback.browser_opened,
                "expires_in_seconds": SESSION_LIFETIME_SECONDS,
                "next_step": (
                    "The in-app prompt was declined or is unsupported by this "
                    "client; open setup_url to manage instances, then select Done."
                ),
            }
        )

    try:
        await asyncio.to_thread(
            session.completed.wait,
            SESSION_LIFETIME_SECONDS + 1,
        )
        if session.result is None:
            return compact_json(
                {
                    "interaction_mode": "mcp_url_elicitation",
                    "status": "expired" if session.expired else "incomplete",
                }
            )

        await ctx.session.send_elicit_complete(
            elicitation_id,
            related_request_id=ctx.request_id,
        )
        return compact_json(
            {
                "interaction_mode": "mcp_url_elicitation",
                "status": "completed",
                "state": session.result,
            }
        )
    finally:
        await run_blocking(session.close)

SHA-256: 4c9b5491bffd1aad3a23659706ad289511a56ef63a53c629e5450d42e52ef54d