1. 簡介
在本程式碼研究室中,您將使用 MCP Toolbox for Databases,讓 BigQuery 資料集可供使用。
在本程式碼研究室中,您將逐步完成下列步驟:
- 從公開 BigQuery 資料集計畫中找出特定 BigQuery 資料集 (「Google Cloud 發行說明」)。
- 設定 MCP Toolbox for Databases,並連結至 BigQuery 資料集。
- 使用 Agent Development Kit (ADK) 開發代理,利用 MCP Toolbox 回答使用者有關 Google Cloud 版本資訊的查詢
執行步驟
- 設定 MCP Toolbox for Databases,將 Google Cloud 版本資訊和公開 BigQuery 資料集公開為其他 MCP 用戶端 (IDE、工具等) 的 MCP 介面。
課程內容
- 探索 BigQuery 公開資料集,然後選擇特定資料集。
- 為要提供給 MCP 用戶的 BigQuery 公開資料集,設定 MCP 資料庫工具箱。
- 使用 Agent Development Kit (ADK) 設計及開發代理,回答使用者查詢。
- 在本機環境中測試代理程式和 MCP Toolbox for Databases。
軟硬體需求
- Chrome 網路瀏覽器。
- 本機 Python 開發環境。
2. 事前準備
建立專案
- 在 Google Cloud 控制台的專案選取器頁面中,選取或建立 Google Cloud 專案。
- 確認 Cloud 專案已啟用計費功能。瞭解如何檢查專案是否已啟用計費功能。
- 您將使用 Cloud Shell,這是 Google Cloud 中執行的指令列環境,且已預先載入 bq。按一下 Google Cloud 控制台上方的「啟用 Cloud Shell」。
- 連線至 Cloud Shell 後,請使用下列指令檢查您是否已通過驗證,且專案已設為您的專案 ID:
gcloud auth list
- 在 Cloud Shell 中執行下列指令,確認 gcloud 指令知道您的專案。
gcloud config list project
- 如果未設定專案,請使用下列指令來設定:
gcloud config set project <YOUR_PROJECT_ID>
如要瞭解 gcloud 指令和用法,請參閱說明文件。
3. Google 版本資訊資料集和 MCP 用戶端
首先,請前往官方 Google Cloud 版本資訊網頁,查看定期更新的 Google Cloud 版本資訊。以下是該網頁的螢幕截圖:
你可能會訂閱動態消息網址,但如果我們可以在 Agent Chat 中詢問這些版本說明,會怎麼樣呢?例如「Google Cloud 版本資訊的最新消息」。
4. MCP Toolbox for Databases
MCP Toolbox for Databases 是資料庫專用的開放原始碼 MCP 伺服器,設計時考量了企業級和生產品質。這項服務可處理連線集區和驗證等複雜作業,讓您更輕鬆、快速且安全地開發工具。
工具箱可協助您建構生成式 AI 工具,讓代理程式存取資料庫中的資料。工具箱提供:
- 簡化開發作業:整合工具至代理程式時,程式碼行數少於 10 行;在多個代理程式或架構之間重複使用工具;更輕鬆地部署新版工具。
- 提升效能:連線集區、驗證等最佳做法。
- 強化安全性:整合式驗證機制,可更安全地存取資料
- 端對端監控:內建 OpenTelemetry 支援,可直接使用指標和追蹤功能。
- 有了 Toolbox,您可以輕鬆將資料庫連結至任何支援 MCP 的 AI 助理,包括 IDE 中的助理。
Toolbox 位於應用程式的協調架構和資料庫之間,提供用於修改、發布或叫用工具的控制平面。這項功能提供集中式位置來儲存及更新工具,方便您管理工具,並在代理程式和應用程式之間共用工具,以及更新工具,不必重新部署應用程式。
簡單來說:
- MCP Toolbox 提供二進位檔和容器映像檔,您也可以從來源建構。
- 這項服務會公開一組工具,您可透過 tools.yaml 檔案進行設定。這些工具可視為連結資料來源。您可以查看支援的各種資料來源:AlloyDB、BigQuery 等。
- 由於這個工具箱現在支援 MCP,您會自動擁有 MCP 伺服器端點,可供代理程式 (IDE) 使用,或在您使用各種架構 (例如 Agent Development Kit (ADK)) 開發代理程式應用程式時使用。
本網誌文章將著重於以下重點區域:
總而言之,我們將在 MCP Toolbox for Databases 中建立設定,瞭解如何連線至 BigQuery 資料集。接著,我們將使用 Agent Development Kit (ADK) 開發代理,與 MCP Toolbox 端點整合,並傳送自然查詢來詢問資料集。您可以將其視為您開發的代理程式應用程式,這類應用程式知道如何與 BigQuery 資料集通訊,並執行一些查詢。
5. Google Cloud 版本資訊的 BigQuery 資料集
Google Cloud 公開資料集計畫提供一系列資料集,供您在應用程式中使用。其中一個資料集是 Google Cloud 版本資訊資料庫。這個資料集提供的資訊與官方 Google Cloud 版本資訊網頁相同,而且可公開查詢。
做為測試,我只需執行下列簡單查詢,即可驗證資料集:
SELECT
product_name,description,published_at
FROM
`bigquery-public-data`.`google_cloud_release_notes`.`release_notes`
WHERE
DATE(published_at) >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)
GROUP BY product_name,description,published_at
ORDER BY published_at DESC
這樣一來,我就可以取得過去 7 天內發布的「發行說明」資料集記錄清單。
您可以將此資料集替換為任何其他資料集,以及您想使用的相關查詢和參數。現在只要在 MCP Toolbox for Databases 中,將其設為資料來源和工具即可。我們來看看如何操作。
6. 安裝 MCP Toolbox for Databases
在本機上開啟終端機,並建立名為 mcp-toolbox
的資料夾。
mkdir mcp-toolbox
透過下列指令前往 mcp-toolbox
資料夾:
cd mcp-toolbox
透過下列指令碼安裝 MCP Toolbox for Databases 的二進位版本。下列指令適用於 Linux,但如果您使用 Mac 或 Windows,請務必下載正確的二進位檔。請前往作業系統和架構的發布頁面,下載正確的二進位檔。
export VERSION=0.13.0
curl -O https://storage.googleapis.com/genai-toolbox/v$VERSION/linux/amd64/toolbox
chmod +x toolbox
現在我們已準備好工具箱的二進位版本,下一步是使用資料來源和其他設定來設定工具箱。
7. 設定 MCP Toolbox for Databases
現在,我們需要在 MCP Toolbox for Database tools.yaml
檔案中定義 BigQuery 資料集和工具。tools.yaml
檔案是設定 Toolbox 的主要方式。
在同一個資料夾 (即 mcp-toolbox
) 中建立名為 tools.yaml
的檔案,內容如下所示。
您可以使用 Cloud Shell 提供的 nano 編輯器。nano 指令如下:「nano tools.yaml
」。
請記得將 YOUR_PROJECT_ID
值替換為您的 Google Cloud 專案 ID。
sources:
my-bq-source:
kind: bigquery
project: YOUR_PROJECT_ID
tools:
search_release_notes_bq:
kind: bigquery-sql
source: my-bq-source
statement: |
SELECT
product_name,description,published_at
FROM
`bigquery-public-data`.`google_cloud_release_notes`.`release_notes`
WHERE
DATE(published_at) >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)
GROUP BY product_name,description,published_at
ORDER BY published_at DESC
description: |
Use this tool to get information on Google Cloud Release Notes.
toolsets:
my_bq_toolset:
- search_release_notes_bq
讓我們簡要瞭解檔案:
- 來源代表工具可互動的不同資料來源。來源代表工具可互動的資料來源。您可以在 tools.yaml 檔案的來源區段中,將來源定義為對應。一般來說,來源設定會包含連線及與資料庫互動所需的任何資訊。在本例中,我們已定義 BigQuery 來源
my-bq-source
,您需要提供 Google Cloud 專案 ID。詳情請參閱「來源」參考資料。 - 工具會定義代理程式可執行的動作,例如讀取及寫入來源。工具代表代理程式可執行的動作,例如執行 SQL 陳述式。您可以在 tools.yaml 檔案的 tools 區段中,將工具定義為對應。一般來說,工具需要來源才能執行動作。在本例中,我們定義單一工具
search_release_notes_bq
。這會參照我們在第一個步驟中定義的 BigQuery 來源my-bq-source
。此外,當中也包含 AI 代理程式用戶端會使用的陳述內容和指令。詳情請參閱「工具」參考資料。 - 最後是「工具集」,可讓您定義要一起載入的工具群組。這項功能有助於根據代理程式或應用程式定義不同群組。在本例中,我們有一個工具集定義,目前只定義了一個現有工具
search_release_notes_bq
。你可以有多個工具集,每個工具集都包含不同的工具組合。
因此目前我們只定義了一項工具,可根據查詢取得過去 7 天的發行說明。但您也可以使用參數進行各種組合。
如要進一步瞭解設定詳情 ( 來源、工具),請參閱 MCP Toolbox for Databases 中的 BigQuery 資料來源設定。
8. 測試 MCP Toolbox for Databases
我們已下載並設定 Toolbox,並將 tools.yaml
檔案放在 mcp-toolbox
資料夾中。請先在本機執行。
執行下列指令:
./toolbox --tools-file="tools.yaml"
執行成功後,您應該會看到伺服器啟動,並顯示類似下方的輸出範例:
2025-09-08T03:56:45.772489914Z INFO "Initialized 1 sources."
2025-09-08T03:56:45.772682659Z INFO "Initialized 0 authServices."
2025-09-08T03:56:45.772735103Z INFO "Initialized 1 tools."
2025-09-08T03:56:45.772774639Z INFO "Initialized 2 toolsets."
2025-09-08T03:56:45.777711644Z INFO "Server ready to serve!"
MCP Toolbox 伺服器預設會在通訊埠 5000
上執行。如果發現通訊埠 5000
已在使用中,請隨意使用其他通訊埠 (例如 7000
),如下列指令所示。請在後續指令中使用 7000
,而非 5000
連接埠。
./toolbox --tools-file "tools.yaml" --port 7000
我們將使用 Cloud Shell 測試這項功能。
在 Cloud Shell 中按一下「網頁預覽」,如下所示:
按一下「變更通訊埠」,將通訊埠設為 5000,如下所示,然後按一下「變更並預覽」。
這時應該會輸出下列內容:
在瀏覽器網址中,於網址末端加入下列內容:
/api/toolset
這時應該會顯示目前設定的工具。輸出範例如下所示:
{
"serverVersion": "0.13.0+binary.linux.amd64.1a6dfe8d37d0f42fb3fd3f75c50988534dbc1b85",
"tools": {
"search_release_notes_bq": {
"description": "Use this tool to get information on Google Cloud Release Notes.\n",
"parameters": [],
"authRequired": []
}
}
}
透過 MCP Toolbox for Databases UI 測試工具
工具箱提供視覺化介面 (工具箱使用者介面),可直接與工具互動,方法是在簡單的網頁使用者介面中修改參數、管理標頭及執行呼叫。
如要測試,請執行先前用來啟動 Toolbox 伺服器的指令,並加上 --ui
選項。
如要這麼做,請關閉先前可能正在執行的 MCP Toolbox for Databases Server 執行個體,然後輸入下列指令:
./toolbox --tools-file "tools.yaml" --ui
理想情況下,您應該會看到伺服器已連線至資料來源,並載入工具集和工具的輸出內容。以下是輸出範例,您會發現其中提到 Toolbox UI 正在運作。
2025-09-08T03:59:34.856476253Z INFO "Initialized 1 sources."
2025-09-08T03:59:34.856546526Z INFO "Initialized 0 authServices."
2025-09-08T03:59:34.856577586Z INFO "Initialized 1 tools."
2025-09-08T03:59:34.856641568Z INFO "Initialized 2 toolsets."
2025-09-08T03:59:34.86133344Z INFO "Server ready to serve!"
2025-09-08T03:59:34.861366205Z INFO "Toolbox UI is up and running at: http://localhost:5000/ui"
按一下使用者介面網址,確認網址結尾有 /ui
。系統會顯示如下所示的 UI:
按一下左側的「工具」選項,即可查看已設定的工具。在本例中,應該只有一個工具,即 search_release_notes_bq
,如下所示:
只要按一下工具 (search_release_notes_bq
),系統就會顯示頁面,供您測試工具。由於不需要提供任何參數,只要按一下「執行工具」即可查看結果。以下是執行範例:
資料庫適用的 MCP 工具包也說明瞭如何以 Python 方式驗證及測試工具,詳情請參閱這篇文章。我們將略過這個步驟,直接進入下一節的 Agent 開發套件 (ADK),並使用這些工具。
9. 使用 Agent Development Kit (ADK) 撰寫代理程式
安裝 Agent Development Kit (ADK)
在 Cloud Shell 中開啟新的終端機分頁,然後建立名為 my-agents
的資料夾,如下所示。前往 my-agents
資料夾。
mkdir my-agents
cd my-agents
現在,讓我們使用 venv
建立虛擬 Python 環境,如下所示:
python -m venv .venv
按照下列步驟啟用虛擬環境:
source .venv/bin/activate
安裝 ADK 和 MCP Toolbox for Databases 套件,以及 langchain 依附元件,如下所示:
pip install google-adk toolbox-core
現在可以按照下列方式叫用 adk
公用程式。
adk
系統會顯示指令清單。
$ adk
Usage: adk [OPTIONS] COMMAND [ARGS]...
Agent Development Kit CLI tools.
Options:
--help Show this message and exit.
Commands:
api_server Starts a FastAPI server for agents.
create Creates a new app in the current folder with prepopulated agent template.
deploy Deploys agent to hosted environments.
eval Evaluates an agent given the eval sets.
run Runs an interactive CLI for a certain agent.
web Starts a FastAPI server with Web UI for agents.
建立第一個代理程式應用程式
我們現在要使用 adk
,透過 adk
create
指令,為 Google Cloud 版本資訊代理程式應用程式建立架構,並使用以下指定的應用程式名稱 **(gcp-releasenotes-agent-app)
**。
adk create gcp-releasenotes-agent-app
按照步驟選取下列項目:
- Gemini 模型,用於選擇根代理程式的模型。
- 選擇 Vertex AI 做為後端。
- 系統會顯示預設的 Google 專案 ID 和區域。選取預設值本身。
Choose a model for the root agent:
1. gemini-2.0-flash-001
2. Other models (fill later)
Choose model (1, 2): 1
1. Google AI
2. Vertex AI
Choose a backend (1, 2): 2
You need an existing Google Cloud account and project, check out this link for details:
https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-cloud-vertex-ai
Enter Google Cloud project ID [YOUR_GOOGLE_PROJECT_ID]:
Enter Google Cloud region [us-central1]:
Agent created in ../my-agents/gcp-releasenotes-agent-app:
- .env
- __init__.py
- agent.py
觀察系統為 Agent 建立預設範本和必要檔案的資料夾。
首先是 .env
檔案。內容如下所示:
GOOGLE_GENAI_USE_VERTEXAI=1
GOOGLE_CLOUD_PROJECT=YOUR_GOOGLE_PROJECT_ID
GOOGLE_CLOUD_LOCATION=YOUR_GOOGLE_PROJECT_REGION
這些值表示我們將透過 Vertex AI 使用 Gemini,以及 Google Cloud 專案 ID 和位置的相應值。
接著是 __init__.py
檔案,這個檔案會將資料夾標示為模組,並包含從 agent.py
檔案匯入代理程式的單一陳述式。
from . import agent
最後,我們來看看 agent.py
檔案。內容如下所示:
from google.adk.agents import Agent
root_agent = Agent(
model='gemini-2.0-flash-001',
name='root_agent',
description='A helpful assistant for user questions.',
instruction='Answer user questions to the best of your knowledge',
)
這是您可以使用 ADK 撰寫的最簡單代理程式。根據 ADK 文件頁面,代理程式是獨立的執行單元,可自主運作以達成特定目標。代理程式可以執行工作、與使用者互動、使用外部工具,以及與其他代理程式協調運作。
具體來說,LLMAgent (通常別名為 Agent) 會以大型語言模型 (LLM) 做為核心引擎,理解自然語言、推論、規劃、生成回覆,並動態決定後續步驟或要使用的工具,因此非常適合以語言為中心、需要彈性的工作。如要進一步瞭解 LLM 代理程式,請參閱這個頁面。
這樣就完成了基本架構,可使用 Agent Development Kit (ADK) 產生基本代理。接下來,我們要將 Agent 連線至 MCP Toolbox,讓 Agent 可以使用該工具回答使用者的查詢 (在本例中,查詢內容為 Google Cloud 版本資訊)。
10. 將 Agent 連結至工具
我們現在要將這個 Agent 連結至工具。在 ADK 中,工具代表提供給 AI 代理的特定功能,可讓代理執行動作,並與核心文字生成和推理能力以外的世界互動。
在本例中,我們現在要為 Agent 配備在 MCP Toolbox for Databases 中設定的工具。
使用下列程式碼修改 agent.py
檔案。請注意,我們在程式碼中使用預設通訊埠 5000,但如果您使用其他通訊埠號碼,請使用該號碼。
from google.adk.agents import Agent
from toolbox_core import ToolboxSyncClient
toolbox = ToolboxSyncClient("http://127.0.0.1:5000")
# Load all the tools
tools = toolbox.load_toolset('my_bq_toolset')
root_agent = Agent(
name="gcp_releasenotes_agent",
model="gemini-2.0-flash",
description=(
"Agent to answer questions about Google Cloud Release notes."
),
instruction=(
"You are a helpful agent who can answer user questions about the Google Cloud Release notes. Use the tools to answer the question"
),
tools=tools,
)
現在可以測試 Agent,從已透過 MCP Toolbox for Databases 設定的 BigQuery 資料集擷取實際資料。
如要執行這項操作,請依序完成下列步驟:
在 Cloud Shell 的其中一個終端機中,啟動 MCP Toolbox for Databases。如先前測試時一樣,您可能已在本機的 5000 埠執行這項服務。如果沒有,請執行下列指令 (從 mcp-toolbox
資料夾) 啟動伺服器:
./toolbox --tools_file "tools.yaml"
理想情況下,您應該會看到伺服器已連線至資料來源,並載入工具集和工具的輸出內容。輸出範例如下所示:
./toolbox --tools-file "tools.yaml"
2025-06-17T07:48:52.989710733Z INFO "Initialized 1 sources."
2025-06-17T07:48:52.989805642Z INFO "Initialized 0 authServices."
2025-06-17T07:48:52.989847035Z INFO "Initialized 1 tools."
2025-06-17T07:48:52.989889742Z INFO "Initialized 2 toolsets."
2025-06-17T07:48:52.990357879Z INFO "Server ready to serve!"
MCP 伺服器成功啟動後,請在另一個終端機中,透過下方顯示的 adk run
(位於 my-agents
資料夾中) 指令啟動代理程式。如有需要,您也可以使用 adk web
指令。
$ adk run gcp-releasenotes-agent-app/
Log setup complete: /tmp/agents_log/agent.20250423_170001.log
To access latest log: tail -F /tmp/agents_log/agent.latest.log
Running agent gcp_releasenotes_agent, type exit to exit.
[user]: get me the google cloud release notes
[gcp_releasenotes_agent]: Here are the Google Cloud Release Notes.
Google SecOps SOAR: Release 6.3.49 is being rolled out to the first phase of regions. This release contains internal and customer bug fixes. Published: 2025-06-14
Compute Engine: Dynamic NICs let you add or remove network interfaces to or from an instance without having to restart or recreate the instance. You can also use Dynamic NICs when you need more network interfaces. The maximum number of vNICs for most machine types in Google Cloud is 10; however, you can configure up to 16 total interfaces by using Dynamic NICs. Published: 2025-06-13
Compute Engine: General purpose C4D machine types, powered by the fifth generation AMD EPYC processors (Turin) and Google Titanium, are generally available. Published: 2025-06-13
Google Agentspace: Google Agentspace Enterprise: App-level feature management. As an Agentspace administrator, you can choose to turn the following features on or off for your end users in the web app: Agents gallery, Prompt gallery, No-code agent, NotebookLM Enterprise. Published: 2025-06-13
Cloud Load Balancing: Cloud Load Balancing supports load balancing to multi-NIC instances that use Dynamic NICs. This capability is in Preview. Published: 2025-06-13
Virtual Private Cloud: Dynamic Network Interfaces (NICs) are available in Preview. Dynamic NICs let you update an instance to add or remove network interfaces without having to restart or recreate the instance. Published: 2025-06-13
Security Command Center: The following Event Threat Detection detectors for Vertex AI have been released to Preview:
- `Persistence: New Geography for AI Service`
- `Privilege Escalation: Anomalous Multistep Service Account Delegation for AI Admin Activity`
- `Privilege Escalation: Anomalous Multistep Service Account Delegation for AI Data Access`
- `Privilege Escalation: Anomalous Service Account Impersonator for AI Admin Activity`
- `Privilege Escalation: Anomalous Service Account Impersonator for AI Data Access`
- `Privilege Escalation: Anomalous Impersonation of Service Account for AI Admin Activity`
- `Persistence: New AI API Method`
......
......
請注意,代理程式會使用我們在 MCP Toolbox for Databases (search_release_notes_bq
) 中設定的工具,從 BigQuery 資料集擷取資料,並據此設定回應格式。
11. 恭喜
恭喜!您已成功設定資料庫適用的 MCP 工具箱,並設定 BigQuery 資料集,以便在 MCP 用戶端中存取。