使用 Google ADK 和 MCP 构建可与 Looker 交互的 AI 智能体

1. 概览

在此 Codelab 中,您将使用 Google 智能体开发套件 (ADK) 和 MCP Toolbox for Databases 构建一个交互式 AI 智能体,以便与 Looker 实例进行交互。

使用 Google 智能体开发套件 (ADK) 构建的 Looker AI 智能体的集成堆栈,通过开源 MCP 服务器连接,以访问底层 Looker API。

什么是 ADK 和 MCP?

  • Google ADK(智能体开发套件):一个轻量级框架,用于创建、测试和编排由 Gemini 提供支持的多工具 LLM 智能体。
  • Model Context Protocol (MCP):一种开放标准,可让 LLM 安全地连接到外部工具、数据库和 API,而无需自定义适配器代码。

构建内容

  • 由 Gemini 3.8 Flash 提供支持的本地 Python 智能体。
  • 用于连接到 Looker 的 MCP Toolbox 集成。
  • 通过交互式 ADK 本地 Web 界面测试运行中的智能体。

前提条件

  • 具有管理员或开发者权限的 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 工具箱二进制文件。

创建项目目录和虚拟环境

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"

验证 .env 是否已在 .gitignore 文件中列出。

注意:您还可以通过 OAuth 连接到 Looker

5. 在 tools.yaml 中定义工具

什么是 tools.yaml?

tools.yaml 是 MCP Toolbox (toolbox) 的声明式配置清单。MCP Toolbox 无需编写自定义样板或手动 HTTP 处理程序来与 Looker API 交互,而是使用 tools.yaml 将 Looker 功能动态公开为 LLM 智能体的标准化 Model Context Protocol (MCP) 工具。

它主要有两个用途:

  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 Web 服务器

从项目目录中运行以下命令:

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.