# Connecting Databricks Genie Code to Treasure AI

[Databricks Genie Code CLI](https://docs.databricks.com/aws/en/genie-code/genie-code-cli) (`genie`) is Databricks' terminal coding agent. Connect it to Treasure AI through the Model Context Protocol (MCP) to query databases, inspect tables, manage parent segments and journeys, or run [Treasure AI Studio](/products/ai-studio/overview) Actions and chats from your terminal.

You can connect Databricks Genie Code CLI to Treasure AI using either or both of two MCP integration patterns:

- **[MCP Server (Platform)](/getting-started/ai/mcp-server-platform) (`tdx mcp`)**: Runs the [Treasure Code (`tdx`) CLI](/treasure-code/) locally over `stdio`. Genie calls `tdx_search` and `tdx_run` to query databases, inspect schemas, and manage segments, journeys, and workflows directly against Treasure AI APIs.
- **[Treasure AI Studio MCP Endpoint](/products/ai-studio/agent-channels/mcp-endpoints)**: Connects over Streamable HTTP to the Treasure AI Studio Agent Gateway. Genie delegates tasks to a shared work folder in Treasure AI Studio, runs saved Actions, and reads back the agent's answers.


Scope of this guide
This guide covers the standalone terminal CLI started with `genie`. The `databricks genie` subcommand group (which manages Genie spaces) and the in-workspace Genie Code Unity Catalog MCP setup are separate interfaces.

## Which MCP Connection Should You Use?

| Aspect  | MCP Server (Platform) (`tdx mcp`)  | Treasure AI Studio MCP Endpoint  |
|  --- | --- | --- |
| **Best for** | Engineers querying tables, validating segment or journey YAML, and running CLI workflows from the terminal | Teams running saved Studio Actions, shared reports, and multi-turn Studio chats from Genie Code CLI |
| **Transport** | Local `stdio` process (`tdx mcp`) started by Genie Code CLI | Remote Streamable HTTP (`https://<Agent Gateway>/v1/mcp/<endpoint ID>`) |
| **Authentication** | Dedicated `tdx` profile passed via environment variable (`TDX_PROFILE=genie`) | Static Gateway Token read from an environment variable (`--bearer-token-env-var TAS_TOKEN`) |
| **Runs as** | The Treasure AI user authenticated in your `genie` profile | The MCP endpoint's designated executor in one shared work folder |
| **AI token usage** | Databricks model access only (Treasure AI serves standard API requests) | Databricks model access (for Genie) + Treasure AI Credits (for Studio chats and Actions) |
| **Tools exposed** | `tdx_run`, `tdx_search` | `list_work_folders`, `list_actions`, `run_action`, `start_chat`, `continue_chat`, `get_chat_result` |


You can register both MCP servers in Databricks Genie Code CLI at the same time.

```mermaid
flowchart LR
    subgraph local["Option 1: MCP Server (Platform) — Local stdio"]
        G1["Databricks Genie Code CLI"] -->|"stdio (TDX_PROFILE=genie)"| TDX["tdx mcp"]
        TDX -->|"HTTPS (Keychain credentials)"| API["Treasure AI APIs<br/>Databases, Segments, Journeys, Workflows"]
    end
    subgraph remote["Option 2: Treasure AI Studio MCP — Remote HTTP"]
        G2["Databricks Genie Code CLI"] -->|"Streamable HTTP<br/>Bearer $TAS_TOKEN"| GW["AI Studio Agent Gateway"]
        GW -->|"Runs as executor"| WF["Shared Work Folder<br/>Saved Actions & Studio Chats"]
    end
```

## Prerequisites

| Requirement  | Details  |
|  --- | --- |
| **Databricks workspace** | Unity Catalog enabled in a supported Unity Gateway region |
| **Databricks Genie Code CLI** | [Databricks Genie Code CLI](https://docs.databricks.com/aws/en/genie-code/genie-code-cli) (`genie`) and [Databricks CLI](https://docs.databricks.com/aws/en/dev-tools/cli/install) (version 1.0.0 or later) installed and authenticated with your Databricks workspace |
| **Node.js** *(for `tdx mcp`)* | Version 22.12 or later (see the [tdx Installation Guide](/treasure-code/guide/installation) for OS-specific setup) |
| **Treasure AI access** | For `tdx mcp`: API key or SSO access to the databases, segments, journeys, and workflows you plan to use. For a Studio MCP Endpoint: **Manage** access to a non-archived [shared work folder](/products/ai-studio/shared-folders) and permission under your account's endpoint creation policy |


## Option 1: Connect MCP Server (Platform) (`tdx mcp`)

Use this setup to run `tdx` commands from Genie Code CLI using a dedicated `tdx` profile.

### Step 1: Install and Authenticate `tdx`

Install `@treasuredata/tdx` globally (requires Node.js 22.12 or later) and create a dedicated `genie` profile for your Treasure AI site:

```bash
npm install -g @treasuredata/tdx
tdx profile create genie
tdx --profile genie auth
tdx status --profile genie
```

- Credentials are stored in your system credential store (macOS Keychain, Windows Credential Manager, or Linux Secret Service) rather than in plain-text configuration files.


### Step 2: Set the `TDX_PROFILE` Environment Variable

Genie Code CLI passes its shell environment to `tdx` and does not accept `-e` environment flags on `tdx mcp`. Set `TDX_PROFILE` in the terminal session where you launch `genie`:

macOS / Linux / WSL
```bash
export TDX_PROFILE=genie
```

Add the `export` line to `~/.zshrc` or `~/.bashrc` to persist it across terminal sessions.

Windows (PowerShell)
```powershell
$env:TDX_PROFILE = "genie"
```

### Step 3: Register `tdx` in Genie Code CLI

Register `tdx mcp` using either the `genie` CLI or by editing `~/.genie/config.toml`.

Option A: CLI Command
```bash
genie mcp add tdx -- tdx mcp
```

Do not pass -e flags after tdx mcp
The `--` separator divides Genie's options from the server command. Put nothing after `tdx mcp`—passing `-e TDX_PROFILE=genie` passes `-e` directly to `tdx mcp`, which fails with `error: unknown option '-e'`. Use `export TDX_PROFILE=genie` in your shell instead.

Option B: Edit config.toml
Add the `[mcp_servers.tdx]` section to `~/.genie/config.toml`:

```toml
[mcp_servers.tdx]
command = "tdx"
args = ["mcp"]
```

### Step 4: Verify and Run Prompts

Confirm the saved server entry lists only `mcp` under `args`, then start `genie` from a terminal where `TDX_PROFILE=genie` is exported:

```bash
genie mcp get tdx
genie
```

Inside the Genie session, run `/mcp` to confirm `tdx` shows as `connected (2 tools)`:

Ask Genie to query or inspect your Treasure AI environment:

- *"Use tdx to list my Treasure AI databases."*
- *"Show the tables in sample_datasets and describe the tables."*
- *"Run a Trino query that counts rows in sample_datasets.www_access."*
- *"List my parent segments and the child segments under one of them."*


## Option 2: Connect a Treasure AI Studio MCP Endpoint

Use this setup to let Genie Code CLI list your shared work folder, trigger saved [Actions](/products/ai-studio/skills/actions), and run [Treasure AI Studio](/products/ai-studio/overview) chats over Streamable HTTP.

### Step 1: Create a Static-Token MCP Endpoint in Treasure AI Studio

Create an endpoint for the shared work folder you want Genie Code CLI to use. You must hold **Manage** permissions on a non-archived [shared work folder](/products/ai-studio/shared-folders). For full details on folder sharing and endpoint policies, see [Static-Token MCP Endpoints](/products/ai-studio/agent-channels/mcp-endpoints).

1. In Treasure AI Studio (Web or Desktop), open **Settings → Agent Channels** and select **New MCP Endpoint**.
2. Keep **Static token · shared executor** selected, enter a name (for example, `Databricks Genie`), and select **Next**.
3. Select the **Shared work folder** to publish and choose a token **Expiration** (7, 30, or 90 days, custom date, or no expiration).
4. Select **Create endpoint & issue first token**.
5. Copy both the **Connection URL** (`https://agent-gateway.<region>.treasure.ai/v1/mcp/<endpoint_id>`) and the **Authentication header** (`Authorization: Bearer tasgwt_<secret>`), check **I have stored the values**, and select **Done**.


Store the token immediately
The `tasgwt_` token is displayed only once. Store it in a password manager or your system credential store before closing the dialog. If you lose it, revoke the token and issue a new one on the endpoint's detail page.

### Step 2: Set the Token Environment Variable

Set the `TAS_TOKEN` environment variable in your shell so the token never appears in `~/.genie/config.toml`.

macOS / Linux / WSL
```bash
export TAS_TOKEN="tasgwt_<YOUR_TOKEN>"
```

Persist the variable in your shell profile (`~/.zshrc` or `~/.bashrc`), or load it dynamically from your system credential manager.

Windows (PowerShell)
```powershell
$env:TAS_TOKEN = "tasgwt_<YOUR_TOKEN>"
```

### Step 3: Register the Studio Endpoint in Genie Code CLI

Pass the exact **Connection URL** copied from Treasure AI Studio (`https://agent-gateway.<REGION>.treasure.ai/v1/mcp/<YOUR_ENDPOINT_ID>`, where `<REGION>` is `us01`, `ap01`, `eu01`, or `ap02`) and the variable name `TAS_TOKEN` (not `$TAS_TOKEN`) so Genie reads the secret from the environment at runtime.

Option A: CLI Command
```bash
genie mcp add treasure-studio --url "https://agent-gateway.<REGION>.treasure.ai/v1/mcp/<YOUR_ENDPOINT_ID>" --bearer-token-env-var TAS_TOKEN
```

Pass the variable name, not the secret
Pass `TAS_TOKEN` (without a leading `$`) to `--bearer-token-env-var`. Writing `$TAS_TOKEN` expands the secret itself as the environment variable name.

Option B: Edit config.toml
Add the `[mcp_servers.treasure-studio]` section to `~/.genie/config.toml`:

```toml
[mcp_servers.treasure-studio]
url = "https://agent-gateway.<REGION>.treasure.ai/v1/mcp/<YOUR_ENDPOINT_ID>"
bearer_token_env_var = "TAS_TOKEN"
```

### Step 4: Verify and Run Studio Actions

Check the saved configuration to confirm `transport: streamable_http` and `bearer_token_env_var: TAS_TOKEN`:

```bash
genie mcp list
genie mcp get treasure-studio
```

Start `genie` from a terminal where `TAS_TOKEN` is exported and test the connection with a prompt:

- *"Use treasure-studio to list my work folders."*
- *"List the Actions in my shared work folder in treasure-studio."*
- *"Run the `<action_name>` Action in treasure-studio. If it returns a running status, poll get_chat_result until it finishes."*
- *"Start a Studio chat that summarizes last week's campaign performance as a table."*


Each tool call waits up to 200 seconds. If a Studio task takes longer than 200 seconds, the tool returns `status: "running"` and a `chat_id`. Ask Genie to poll `get_chat_result` with that `chat_id` instead of starting a new run. For the complete tool schema and status codes, see [Connecting an MCP Client](/products/ai-studio/agent-channels/mcp-clients#the-tools).

## Guardrails and Operational Limits

Commands with production side effects (tdx mcp)
`tdx_run` can execute any `tdx` CLI command that your profile's user has permission to run, including commands that modify live CDP resources or trigger audience activations:

- **`tdx wf run`**: Starts a workflow attempt immediately.
- **`tdx journey resume`**: Resumes a paused journey and starts firing stage activations.
- **`tdx sg push` / `tdx journey push` / `tdx ps push`**: Updates live segment or journey configurations.


Always ask Genie to run mutating commands with `--dry-run` first, or use read-only validation commands (`tdx wf validate`, `tdx sg validate`, `tdx journey validate`). Point the `genie` profile at a sandbox account whenever possible.

- **Blocked `tdx` commands**: For security, `tdx mcp` blocks authentication (`tdx auth`), profile management (`tdx profile`), and recursive `tdx mcp` commands.
- **Studio MCP Endpoint limits**: A static-token endpoint publishes one shared work folder read-only, runs all chats in a temporary workspace that is discarded after the chat, allows up to 30 new chats per minute (`1` to `32,768` characters per message), and automatically declines clarifying questions from the agent (include all required inputs in your prompt).


## Troubleshooting

| Symptom  | Likely Cause  | Fix  |
|  --- | --- | --- |
| `tdx` shows as `failed` in `/mcp`, and `genie mcp get tdx` lists `-e TDX_PROFILE=genie` in `args` | The server was added with `-e` after `tdx mcp`, which `tdx mcp` rejects as an unknown option | Run `genie mcp remove tdx`, export `TDX_PROFILE=genie` in your shell, and re-run `genie mcp add tdx -- tdx mcp` |
| `treasure-studio` shows a command instead of a URL in `genie mcp list` | The entry was registered without the `--url` flag | Run `genie mcp remove treasure-studio` and add it again using `--url "$TAS_MCP_URL" --bearer-token-env-var TAS_TOKEN` |
| `tdx` tools or Studio authentication fail after starting `genie` | `genie` was started in a terminal where `TDX_PROFILE` or `TAS_TOKEN` was not set | Set `TDX_PROFILE=genie` and `TAS_TOKEN` in your shell environment, then restart `genie` |
| `The Databricks CLI v1.0.0 or newer is required to run Genie` | The Databricks CLI is missing or older than 1.0.0 | Upgrade or install the Databricks CLI, open a new terminal, and verify with `databricks -v` |
| Genie cannot get model access or reports a Databricks auth error | The profile in `[model_providers.databricks.auth]` in `~/.genie/config.toml` is missing or expired | Run `databricks auth profiles` and sign in again with `databricks auth login --profile <YOUR_PROFILE>` |
| `401 Unauthorized` or `403 Forbidden` from the Studio endpoint | The `tasgwt_` token is expired/revoked (`401`), or the endpoint is disabled or its executor lost folder access (`403`) | Check the endpoint and issue a new token or restore folder access in **Settings → Agent Channels** |


## Next Steps

- [MCP Server (Platform)](/getting-started/ai/mcp-server-platform) — reference for `tdx_run` and `tdx_search`
- [tdx mcp command reference](/treasure-code/commands/mcp) — full tool reference and blocked commands
- [Static-Token MCP Endpoints](/products/ai-studio/agent-channels/mcp-endpoints) — create and govern Treasure AI Studio endpoints
- [Connecting an MCP Client to Treasure AI Studio](/products/ai-studio/agent-channels/mcp-clients) — full reference for the six Studio MCP tools
- Connect a different tool: [Snowflake Cortex Code](/getting-started/ai/snowflake-cortex-code) · [Snowflake CoWork](/getting-started/ai/snowflake-cowork) · [Claude Code](/getting-started/ai/claude-code) · [OpenAI Codex](/getting-started/ai/openai-codex) · [VS Code and GitHub Copilot](/getting-started/ai/vscode-github-copilot) · [Cursor](/getting-started/ai/cursor)