使用 Google ADK 和 MCP 建構 AI 代理,與 Looker 互動

1. 總覽

在本程式碼實驗室中,您將使用 Google Agent Development Kit (ADK) 和 MCP Toolbox for Databases 建構互動式 AI 代理,與 Looker 執行個體互動。

Looker AI 代理的整合堆疊,使用 Google Agent Development Kit (ADK) 建構,透過開放原始碼 MCP 伺服器連線,存取底層 Looker API。

什麼是 ADK 和 MCP?

  • Google ADK (Agent Development Kit):輕量級框架,用於建立、測試及自動調度管理由 Gemini 驅動的多工具 LLM 代理。
  • Model Context Protocol (MCP):這項開放標準可讓 LLM 安全連結外部工具、資料庫和 API,不需自訂轉接器程式碼。

建構目標

  • 採用 Gemini 3.8 Flash 的本機 Python 代理。
  • MCP 工具箱整合,可連線至 Looker。
  • 透過互動式 ADK 本機網頁 UI 測試執行中的代理。

必要條件

  • 具備管理員或開發人員權限的 Looker 執行個體,可產生 API 憑證。
  • 已安裝 Python 3.10 以上版本。
  • 已安裝 Go 1.21 以上版本 (用於建構/安裝 toolbox 二進位檔)。
  • Google Gemini API 金鑰,或已啟用 Vertex AI 的 Google Cloud 專案。

2. 保護 Looker API 憑證

如要允許 MCP 伺服器向 Looker 執行個體驗證身分,您必須取得 API 3 憑證。

  1. 登入 Looker 執行個體。
  2. 依序前往「管理」>「使用者」(如果已啟用自助憑證產生功能,則前往「帳戶設定」)。
  3. 找到您的使用者帳戶,然後選取「編輯」。
  4. 在「API 3 金鑰」部分,點按「新增 API 金鑰」。
  5. 儲存系統產生的用戶端 ID 和用戶端密鑰。

[警告] 請妥善保管用戶端密鑰。請勿將 .env 檔案提交至版本管控系統。

3. 設定開發環境並安裝 MCP Toolbox

建立專案目錄、設定 Python 虛擬環境,並使用 Go 安裝必要的 MCP Toolbox 二進位檔。

建立專案目錄和虛擬環境

mkdir looker-mcp-agent && cd looker-mcp-agent
python3 -m venv .venv
source .venv/bin/activate

安裝 Python 依附元件

安裝 Google ADK、MCP 用戶端 SDK 和 Python 環境管理工具:

pip install google-adk mcp python-dotenv

使用 Go 安裝 MCP Toolbox 二進位檔

MCP 連線仰賴 MCP Toolbox 二進位檔 (toolbox)。您可以使用 go install 直接安裝:

# Install the MCP toolbox binary from source
go install github.com/googleapis/genai-toolbox/cmd/toolbox@latest

確認二進位檔已安裝到 $(go env GOPATH)/bin 目錄:

# Check if MCP toolbox is in your Go bin
ls $(go env GOPATH)/bin/toolbox

為方便指令碼參照,請確認 $(go env GOPATH)/bin 位於 $PATH,或將二進位檔複製/符號連結至專案目錄:

# Symlink or copy the MCP toolbox into your current project directory
cp $(go env GOPATH)/bin/toolbox ./toolbox
chmod +x ./toolbox

4. 設定環境變數

在 looker-mcp-agent/ 的根目錄中建立 .env 檔案:

LOOKER_BASE_URL="https://your-looker-instance.com"
LOOKER_CLIENT_ID="your_client_id"
LOOKER_CLIENT_SECRET="your_client_secret"
GEMINI_API_KEY="your_gemini_api_key"

確認 .gitignore 檔案中列有 .env。

注意:您也可以透過 OAuth 連結至 Looker

5. 在 tools.yaml 中定義工具

什麼是 tools.yaml?

tools.yaml 是 MCP Toolbox (toolbox) 的宣告式設定資訊清單。MCP Toolbox 不會編寫自訂樣板或手動 HTTP 處理常式來與 Looker API 互動,而是使用 tools.yaml,以標準化 Model Context Protocol (MCP) 工具的形式,為 LLM 代理動態公開 Looker 功能。

主要用途有兩個:

  1. 設定 Looker 執行個體的連線 (端點網址、驗證憑證、專案、位置和逾時)。
  2. 指定 MCP 伺服器要向 Gemini 公開哪些 Looker 功能 (例如列出連線、建立資訊主頁、執行查詢),並提供自然語言說明,讓 Gemini 用於選取工具及生成引數。

檔案位置:將 tools.yaml 放在專案根目錄

tools.yaml 檔案必須位於專案根目錄 (basic_looker_agent/ 的上層目錄),而非 basic_looker_agent/ 子目錄中。

以下是預期的專案目錄結構:

looker-mcp-agent/                 <-- Project root directory
├── tools.yaml                    <-- Place tools.yaml here!
├── toolbox                       <-- MCP Toolbox binary executable
└── basic_looker_agent/           <-- Agent package directory created by ADK
    ├── .env                      <-- Local environment variables (keys & credentials)
    ├── __init__.py
    └── agent.py                  <-- Agent code (references ../tools.yaml)

為什麼要放在專案根目錄?在 agent.py 中,代理會透過 ../tools.yaml (basic_looker_agent/ 的上一層目錄) 解析 MCP 伺服器設定。將 tools.yaml 放在專案根目錄,可清楚區分 MCP 工具定義、資料來源和 Python 代理程式碼。如果將 tools.yaml 放在 basic_looker_agent/ 內,代理程式將無法找到該檔案。

sources:
  my-looker-source:
    kind: looker
    base_url: https://your-looker-instance.com
    client_id: $LOOKER_CLIENT_ID
    client_secret: $LOOKER_CLIENT_SECRET
    project: your-looker-project
    location: us-east1
    verify_ssl: true
    timeout: 600s

tools:
    get_connections:
        kind: looker-get-connections
        source: my-looker-source
        description: |
          This tool retrieves a list of all database connections configured in the Looker system.

          Parameters:
          This tool takes no parameters.

          Output:
          A JSON array of objects, each representing a database connection.
    create_dashboard:
        kind: looker-make-dashboard
        source: my-looker-source
        description: |
          This tool creates a dashboard from a prompt.

注意:$LOOKER_CLIENT_ID 和 $LOOKER_CLIENT_SECRET 預留位置會自動從傳遞至 MCP 伺服器程序的環境中解析。

6. 導入代理

建立名為 agent.py 的檔案。這個指令碼會將 MCP 伺服器程序連至 ADK LlmAgent。

import os
from dotenv import load_dotenv
from mcp import StdioServerParameters
from google.adk.agents import LlmAgent
from google.adk.tools import MCPToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams

# Load environment variables
load_dotenv()

# Configure MCP Stdio server parameters
base_dir = os.path.dirname(os.path.abspath(__file__))
# Reference the toolbox binary in your project root or from GOPATH
toolbox_path = os.path.abspath(os.path.join(base_dir, "toolbox"))
tools_path = os.path.abspath(os.path.join(base_dir, "tools.yaml"))

looker_server = StdioServerParameters(
    command=toolbox_path,
    args=["--stdio", "--tools-file", tools_path],
    env={
        "LOOKER_BASE_URL": os.getenv("LOOKER_BASE_URL"),
        "LOOKER_CLIENT_ID": os.getenv("LOOKER_CLIENT_ID"),
        "LOOKER_CLIENT_SECRET": os.getenv("LOOKER_CLIENT_SECRET"),
    },
)

# Create the ADK MCP Toolset
looker_toolset = MCPToolset(
    connection_params=StdioConnectionParams(server_params=looker_server)
)

# Define the root LLM Agent
root_agent = LlmAgent(
    model="gemini-3.8-flash",
    name="looker_pro",
    description="A helpful assistant that queries Looker and uses mcp-toolbox-for-databases.",
    instruction="You are a helpful assistant that retrieves Looker connection information and assists users with available Looker tools.",
    tools=[looker_toolset],
)

7. 執行及測試代理

ADK 內建開發網頁介面。

啟動 ADK 網路伺服器

從專案目錄執行下列指令:

adk web
``` This will spin up a lightweight locally hosted server to
review your agent.

### Interact with Your Agent

1.  Open the URL printed in the terminal (typically `http://localhost:8080` or
    `http://127.0.0.1:8000`).
2.  Test tool discovery by prompting the agent:

可以利用哪些工具?

    inspect its toolset and report the `get_connections` and the
    `create_dashboard` tool.

3.  Test tool execution:

可以顯示 Looker 中的資料庫連線嗎?

" class="language->">    Response: The agent will invoke `get_connections` over MCP and list your
    instance's active database connections.

---

## Congratulations & Next Steps

Duration: 1:00

You have built an AI agent using Google ADK connected to Looker via the Model
Context Protocol (MCP).

### What you learned:

*   How to install and configure `toolbox` using `go install`.
*   How to configure Looker API credentials for external integrations.
*   How to define MCP sources and tools in `tools.yaml`.
*   How to link an MCP toolset to an ADK `LlmAgent`.
*   How to test tool invocation in the ADK web interface.

### Next Steps:

*   Create a dashboard with the agent defined in the codelab.
*   Add more [Looker tools](https://mcp-toolbox.dev/integrations/looker/source/)
    to `tools.yaml` (such as `looker-query`, `looker-run-look`).
*   Check out [Google ADK Documentation](https://github.com/google/adk) for
    multi-agent routing and state persistence.