Skip to main content
In this lesson we build a churn-detection agent that uses the ADK MCP Toolbox to query BigQuery. The Toolbox runs as a helper subprocess (via stdio), publishes BigQuery tools (for example execute_sql, get_table_info, list_table_ids), and the agent calls those tools while composing its own SQL. Below you’ll find a clear, step-by-step walkthrough covering:
  • imports and environment bootstrap
  • model initialization
  • configuring the MCP Toolbox bridge to BigQuery
  • agent instructions and schema guidance
  • the runner loop that sends a user query and streams the agent’s final reply
This pattern is reusable for other data sources (Databricks, Snowflake) with minor adjustments.

Imports and environment bootstrap

Load environment variables and suppress the ADK experimental warning before importing ADK modules. The ADK issues a one-time warning for experimental features (MCP support), so filter warnings before ADK imports:
Then import ADK, MCP, and helper types used by the runner and content model:
Quick reference:
  • InMemoryRunner runs the agent and maintains session state.
  • types and types.Part let you construct multimodal messages (text, files, audio, etc.).
  • McpToolset, StdioConnectionParams, and StdioServerParameters create the MCP/stdio bridge to the Toolbox subprocess.

.env example

Copy .env.example to .env and fill in the actual keys. Never commit a file containing secrets.
Copy .env.example to .env and fill in your actual keys. Never commit .env.
Environment variables summary:

Model initialization

Create a LiteLlm model instance using your KodeKey/OpenAI-compatible endpoint and model name. Replace the model name with the model available on your endpoint.
Grab the Google Cloud project and optional toolbox path from the environment:

Configure the MCP Toolbox bridge to BigQuery

The ADK launches the Toolbox binary in stdio mode; Toolbox connects to BigQuery and exposes a set of tools.
Notes:
  • args uses --prebuilt bigquery --stdio. Toolbox connects to BigQuery using Application Default Credentials (run gcloud auth application-default login if needed).
  • env ensures Toolbox sees BIGQUERY_PROJECT, so it can set the project for queries.
  • tool_filter restricts the model to only the tools it needs for safety.
Toolbox uses Application Default Credentials (ADC). Run gcloud auth application-default login and verify ADC access before starting the agent. Always close Toolbox from the same task that started it to avoid noisy shutdown tracebacks.
Toolset parameter summary:

Agent definition and instructions

Create an LlmAgent named churn_agent. Provide the task instruction plus the BigQuery table schema so the model can reason without querying the churned label:
Why include the schema in the instruction?
  • It enables the model to reason about which columns are available without repeatedly querying schema.
  • It explicitly forbids using the churned label so the agent cannot return ground-truth labels.

Preview of the BigQuery table (for context)

Below is a preview screenshot of analytics.customer_features from BigQuery. This demonstrates the table layout and the churned column (which the agent must not query):
A screenshot of the Google Cloud BigQuery console showing the "customer_features" table in preview mode. It lists customer records with columns like customer_id, region, usage metrics, plan_tier, tenure_months, and churned.
The schema is also visible here:
Screenshot of the Google Cloud BigQuery console showing the schema for a "customer_features" table. The table fields (customer_id, region, upgrade_eligible, usage_last_30d, usage_delta_30d, support_tickets_90d, plan_tier, tenure_months, churned) and the Explorer sidebar are visible.

Main runner and session loop

The main function runs the agent with InMemoryRunner, opens a session, sends a user question, and streams events until the agent emits a final response. It also ensures the Toolbox subprocess is closed cleanly.
Why types.Part? ADK’s content parts support multimodal inputs (text, images, audio, etc.), so the typed Part makes messages explicit and extensible.

Example run and sample output

Run the script:
Sample output (formatted for clarity). The agent uses the BigQuery tools to read the customer’s features (without querying the churned column), reasons over signals, and returns a risk assessment and one recommended retention action:
The agent cites the signals (usage metrics and support tickets) and suggests a single concrete retention action, following the instructions.

Closing notes and best practices

  • Always close the MCP Toolbox subprocess from the same task that started it to avoid noisy cancel-scope tracebacks at interpreter shutdown.
  • Minimize the tool_filter to only the tools the agent actually needs — principle of least privilege for safety.
  • Use get_table_info when you want the agent to fetch schema information programmatically instead of embedding the schema in the instruction.
  • Provide clear instructions and a schema in the agent prompt so the model can reason without accidentally leaking labels like churned.
This completes the churn-agent-with-BigQuery lesson. The same approach — using MCP Toolbox to expose a small, audited set of database tools — can be applied to other backends such as Databricks or Snowflake.

Watch Video