Google ADK と MCP を使用して Looker とやり取りする AI エージェントを構築する

1. 概要

この Codelab では、Google Agent Development Kit(ADK)とデータベース向け MCP ツールボックスを使用して、Looker インスタンスとやり取りするインタラクティブな AI エージェントを構築します。

Google Agent Development Kit(ADK)で構築された Looker AI エージェントの統合スタック。オープンソースの MCP サーバーを介して接続し、基盤となる Looker API にアクセスします。

ADK と MCP とは

  • Google ADK(Agent Development Kit): Gemini を活用したマルチツール LLM エージェントの作成、テスト、オーケストレーションを行うための軽量フレームワーク。
  • Model Context Protocol(MCP): LLM がカスタム アダプタコードなしで外部ツール、データベース、API に安全に接続できるようにするオープン スタンダード。

作業内容

  • Gemini 3.8 Flash を活用するローカルの Python ベースのエージェント。
  • Looker に接続するための MCP ツールボックス統合。
  • インタラクティブな ADK ローカルウェブ UI でテストされた実行中のエージェント。

前提条件

  • API 認証情報を生成するための管理者権限またはデベロッパー権限を持つ Looker インスタンス。
  • Python 3.10 以降がインストールされている。
  • Go 1.21 以降がインストールされている(toolbox バイナリをビルド / インストールするため)。
  • Google Gemini API キー、または Vertex AI が有効になっている Google Cloud プロジェクト。

2. Looker API 認証情報を保護する

MCP サーバーが Looker インスタンスで認証できるようにするには、API 3 認証情報を取得する必要があります。

  1. Looker インスタンスにログインします。
  2. [Admin] > [Users](セルフサービス認証情報の生成が有効になっている場合は [Account Settings])に移動します。
  3. ユーザー アカウントを見つけて、[編集] を選択します。
  4. [API 3 Keys] セクションで、[New API Key] をクリックします。
  5. 生成されたクライアント ID とクライアント シークレットを保存します。

[警告] クライアント シークレットは安全に保管してください。.env ファイルをバージョン管理に commit しないでください。

3. 開発環境を設定して MCP ツールボックスをインストールする

プロジェクト ディレクトリを作成し、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 ツールボックス バイナリをインストールする

MCP 接続は、MCP ツールボックス バイナリ(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)の宣言型構成マニフェストです。Looker API とやり取りするためにカスタム ボイラープレートや手動 HTTP ハンドラを作成する代わりに、MCP ツールボックスは tools.yaml を使用して、Looker 機能を LLM エージェント用の標準化された Model Context Protocol(MCP)ツールとして動的に公開します。

これには、主に次の 2 つの目的があります。

  1. Looker インスタンスへの接続(エンドポイント URL、認証情報、プロジェクト、ロケーション、タイムアウト)を構成します。
  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/ の 1 つ上のディレクトリ)を介して 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.