สร้าง AI Agent ด้วย Google ADK และ MCP เพื่อโต้ตอบกับ Looker

1. ภาพรวม

ใน Codelab นี้ คุณจะสร้าง AI Agent แบบอินเทอร์แอกทีฟโดยใช้ Google Agent Development Kit (ADK) และ MCP Toolbox สำหรับฐานข้อมูลเพื่อโต้ตอบกับอินสแตนซ์ Looker

สแต็กการผสานรวมของ AI Agent ของ Looker ที่สร้างขึ้นด้วย Google Agent Development Kit (ADK) ซึ่งเชื่อมต่อผ่านเซิร์ฟเวอร์ MCP แบบโอเพนซอร์สเพื่อเข้าถึง Looker API ที่เกี่ยวข้อง

ADK และ MCP คืออะไร

  • Google ADK (Agent Development Kit): เฟรมเวิร์กขนาดเล็กสำหรับการสร้าง ทดสอบ และจัดการ Agent LLM แบบหลายเครื่องมือที่ขับเคลื่อนโดย Gemini
  • Model Context Protocol (MCP): มาตรฐานแบบเปิดที่ช่วยให้ LLM เชื่อมต่อกับเครื่องมือ ฐานข้อมูล และ API ภายนอกได้อย่างปลอดภัยโดยไม่ต้องใช้โค้ดอะแดปเตอร์ที่กำหนดเอง

สิ่งที่คุณจะสร้าง

  • Agent ที่ใช้ Python ในเครื่องซึ่งขับเคลื่อนโดย Gemini 3.8 Flash
  • การผสานรวม MCP Toolbox เพื่อเชื่อมต่อกับ Looker
  • English: A running agent tested through the interactive ADK local web UI. Thai:

ข้อกำหนดเบื้องต้น

  • อินสแตนซ์ Looker ที่มีสิทธิ์ระดับผู้ดูแลระบบหรือนักพัฒนาซอฟต์แวร์ในการสร้างข้อมูลเข้าสู่ระบบ API
  • ติดตั้ง Python 3.10 ขึ้นไปแล้ว
  • ติดตั้ง Go 1.21 ขึ้นไป (เพื่อสร้าง/ติดตั้งไบนารี toolbox)
  • คีย์ Google Gemini API หรือโปรเจ็กต์ Google Cloud ที่เปิดใช้ Vertex AI

2. รักษาความปลอดภัยของข้อมูลเข้าสู่ระบบ Looker API

หากต้องการอนุญาตให้เซิร์ฟเวอร์ MCP ตรวจสอบสิทธิ์กับอินสแตนซ์ Looker คุณต้องได้รับข้อมูลเข้าสู่ระบบ API 3

  1. เข้าสู่ระบบอินสแตนซ์ Looker
  2. ไปที่ผู้ดูแลระบบ > ผู้ใช้ (หรือการตั้งค่าบัญชีหากเปิดใช้การสร้างข้อมูลเข้าสู่ระบบแบบบริการตนเอง)
  3. ค้นหาบัญชีผู้ใช้แล้วเลือกแก้ไข
  4. ในส่วนคีย์ API 3 ให้คลิกคีย์ API ใหม่
  5. บันทึกรหัสไคลเอ็นต์และรหัสลับไคลเอ็นต์ที่สร้างขึ้น

[คำเตือน] เก็บรักษา Client Secret ของคุณให้ปลอดภัย อย่าคอมมิตไฟล์ .env ไปยังการควบคุมเวอร์ชัน

3. ตั้งค่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์และติดตั้ง MCP Toolbox

สร้างไดเรกทอรีโปรเจ็กต์ ตั้งค่าสภาพแวดล้อมเสมือนของ Python และติดตั้งไบนารีของ MCP Toolbox ที่จำเป็นโดยใช้ Go

สร้างไดเรกทอรีโปรเจ็กต์และสภาพแวดล้อมเสมือน

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

ติดตั้งทรัพยากร Dependency ของ Python

ติดตั้ง Google ADK, MCP Client SDK และ Python Environment Manager โดยทำดังนี้

pip install google-adk mcp python-dotenv

ติดตั้งไบนารี MCP Toolbox โดยใช้ Go

การเชื่อมต่อ 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. กำหนดค่าตัวแปรสภาพแวดล้อม

สร้างไฟล์ .env ในรูทของ looker-mcp-agent/ ดังนี้

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

หมายเหตุ: คุณยังเชื่อมต่อกับ Looker ผ่าน OAuth ได้ด้วย

5. กำหนดเครื่องมือใน tools.yaml

tools.yaml คืออะไร

tools.yaml คือไฟล์ Manifest การกำหนดค่าเชิงประกาศสำหรับ MCP Toolbox (toolbox) แทนที่จะเขียนโค้ดสำเร็จรูปที่กำหนดเองหรือเครื่องจัดการ HTTP ด้วยตนเองเพื่อโต้ตอบกับ Looker API แต่ MCP Toolbox จะใช้ tools.yaml เพื่อแสดงความสามารถของ Looker เป็นเครื่องมือ Model Context Protocol (MCP) ที่ได้มาตรฐานสำหรับตัวแทน LLM ของคุณแบบไดนามิก

ซึ่งมีวัตถุประสงค์หลัก 2 ประการ ได้แก่

  1. กำหนดค่าการเชื่อมต่อกับอินสแตนซ์ Looker (URL ปลายทาง, ข้อมูลรับรองการตรวจสอบสิทธิ์, โปรเจ็กต์, ตำแหน่ง และระยะหมดเวลา)
  2. ระบุความสามารถของ Looker (เช่น การแสดงรายการการเชื่อมต่อ การสร้างแดชบอร์ด การเรียกใช้การค้นหา) ที่เซิร์ฟเวอร์ MCP จะแสดงต่อ Gemini พร้อมคำอธิบายภาษาธรรมชาติที่ 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 เอเจนต์จะแก้ไขการกำหนดค่าเซิร์ฟเวอร์ MCP ผ่าน ../tools.yaml (ไดเรกทอรีที่อยู่เหนือ basic_looker_agent/ 1 ระดับ) การเก็บ 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

สร้างไฟล์ชื่อ 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. เรียกใช้และทดสอบ Agent

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.