chore: add a sample BigQuery agent using BigQuery MCP tools

PiperOrigin-RevId: 856400285
This commit is contained in:
Google Team Member
2026-01-14 15:49:23 -08:00
committed by Copybara-Service
parent 38d52b2476
commit 672b57f1b7
5 changed files with 126 additions and 7 deletions
+5 -5
View File
@@ -24,11 +24,11 @@ distributed via the `google.adk.tools.bigquery` module. These tools include:
5. `get_job_info`
Fetches metadata about a BigQuery job.
5. `execute_sql`
6. `execute_sql`
Runs or dry-runs a SQL query in BigQuery.
6. `ask_data_insights`
7. `ask_data_insights`
Natural language-in, natural language-out tool that answers questions
about structured data in BigQuery. Provides a one-stop solution for generating
@@ -38,18 +38,18 @@ distributed via the `google.adk.tools.bigquery` module. These tools include:
the official [Conversational Analytics API documentation](https://cloud.google.com/gemini/docs/conversational-analytics-api/overview)
for instructions.
7. `forecast`
8. `forecast`
Perform time series forecasting using BigQuery's `AI.FORECAST` function,
leveraging the TimesFM 2.0 model.
8. `analyze_contribution`
9. `analyze_contribution`
Perform contribution analysis in BigQuery by creating a temporary
`CONTRIBUTION_ANALYSIS` model and then querying it with
`ML.GET_INSIGHTS` to find top contributors for a given metric.
9. `detect_anomalies`
10. `detect_anomalies`
Perform time series anomaly detection in BigQuery by creating a temporary
`ARIMA_PLUS` model and then querying it with
@@ -0,0 +1,55 @@
# BigQuery MCP Toolset Sample
## Introduction
This sample agent demonstrates using ADK's `McpToolset` to interact with
BigQuery's official MCP endpoint, allowing an agent to access and execute
toole by leveraging the Model Context Protocol (MCP). These tools include:
1. `list_dataset_ids`
Fetches BigQuery dataset ids present in a GCP project.
2. `get_dataset_info`
Fetches metadata about a BigQuery dataset.
3. `list_table_ids`
Fetches table ids present in a BigQuery dataset.
4. `get_table_info`
Fetches metadata about a BigQuery table.
5. `execute_sql`
Runs or dry-runs a SQL query in BigQuery.
## How to use
Set up your project and local authentication by following the guide
[Use the BigQuery remote MCP server](https://docs.cloud.google.com/bigquery/docs/use-bigquery-mcp).
This agent uses Application Default Credentials (ADC) to authenticate with the
BigQuery MCP endpoint.
Set up environment variables in your `.env` file for using
[Google AI Studio](https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-ai-studio)
or
[Google Cloud Vertex AI](https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-cloud-vertex-ai)
for the LLM service for your agent. For example, for using Google AI Studio you
would set:
* GOOGLE_GENAI_USE_VERTEXAI=FALSE
* GOOGLE_API_KEY={your api key}
Then run the agent using `adk run .` or `adk web .` in this directory.
## Sample prompts
* which weather datasets exist in bigquery public data?
* tell me more about noaa_lightning
* which tables exist in the ml_datasets dataset?
* show more details about the penguins table
* compute penguins population per island.
@@ -0,0 +1,15 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from . import agent
@@ -0,0 +1,51 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from google.adk.agents.llm_agent import LlmAgent
from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
import google.auth
BIGQUERY_AGENT_NAME = "adk_sample_bigquery_mcp_agent"
BIGQUERY_MCP_ENDPOINT = "https://bigquery.googleapis.com/mcp"
BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery"
# Initialize the tools to use the application default credentials.
# https://cloud.google.com/docs/authentication/provide-credentials-adc
credentials, project_id = google.auth.default(scopes=[BIGQUERY_SCOPE])
credentials.refresh(google.auth.transport.requests.Request())
oauth_token = credentials.token
bigquery_mcp_toolset = McpToolset(
connection_params=StreamableHTTPConnectionParams(
url=BIGQUERY_MCP_ENDPOINT,
headers={"Authorization": f"Bearer {oauth_token}"},
)
)
# The variable name `root_agent` determines what your root agent is for the
# debug CLI
root_agent = LlmAgent(
model="gemini-2.5-flash",
name=BIGQUERY_AGENT_NAME,
description=(
"Agent to answer questions about BigQuery data and models and execute"
" SQL queries using MCP."
),
instruction="""\
You are a data science agent with access to several BigQuery tools provided via MCP.
Make use of those tools to answer the user's questions.
""",
tools=[bigquery_mcp_toolset],
)
-2
View File
@@ -1291,7 +1291,6 @@ def cli_web(
host=host,
port=port,
reload=reload,
log_level=log_level.lower(),
)
server = uvicorn.Server(config)
@@ -1368,7 +1367,6 @@ def cli_api_server(
host=host,
port=port,
reload=reload,
log_level=log_level.lower(),
)
server = uvicorn.Server(config)
server.run()