Skip to main content

BigQuery MCP server optimized for quick navigation of larger projects and datasets.

Project description

🗂️ BigQuery MCP Server

Practical MCP server for navigating BigQuery datasets and tables by LLMs. Designed for larger projects with many datasets/tables, optimized to keep BigQuery spend low and LLM context small while staying fast and safe.

  • Minimal by default: list datasets and tables names; fetch details only when asked
  • Navigate larger projects: filter by name, request detailed metadata/schemas on demand
  • Quick table insight: optional schema, column descriptions and fill-rate to help an agent decide relevance fast
  • Safe to run: read-only query execution with guardrails (SELECT/WITH only, comment stripping)
  • Cost-bounded by design: metadata-first discovery, a dry_run_query cost estimator, and a hard per-query bytes-billed cap
  • Supports vector search: Use bigquery as your vector store. See Vector Search section for full setup instructions.

🎯 Optimization priority

Every tool and default in this server is designed around one explicit ordering. When choices conflict, earlier goals win:

  1. Minimize BigQuery cost first — bytes scanned is what you pay for. Discovery (list_dataset_ids, get_dataset_info, list_table_ids, get_table_info) is metadata-only and scans zero bytes. dry_run_query estimates a query's bytes without running it. Every real query (execute_sql, get_table_info sampling, vector_search) is capped by maximum_bytes_billed (default ~USD 0.50/query). Tool descriptions steer the model to filter on partition/cluster columns, select only needed columns, and use LIMIT.
  2. Then minimize LLM (token) cost — list tools return names only by default, switching to full metadata only when detailed=true. Responses are compact, structured JSON so the agent spends few tokens deciding what's relevant before paying for a scan.
  3. Then minimize latency — metadata calls run in threads and time out fast; list+search uses a bounded fetch multiplier; embedding-table discovery is cached. Latency is optimized only where it doesn't increase BigQuery or token cost.

See ARCHITECTURE.md for the mechanisms behind each level.

🧭 Tool naming

Tool names follow Google's BigQuery MCP / MCP Toolbox conventions so agents already trained on Google's surface feel at home: execute_sql, list_dataset_ids, get_dataset_info, list_table_ids, get_table_info. Tools that Google does not provide are this project's own additions: dry_run_query (pre-flight cost estimate) and vector_search.

Two implementations, one contract. This is the Python server. A standalone Node/TypeScript server, bigquery-mcp-js, exposes the same tools. Both implement a shared contract (contract/tools.json); pick whichever fits your runtime. See ARCHITECTURE.md.

🧠 Bundled agent skill

Both packages ship a portable Agent Skill that teaches an agent how to drive these tools cost-first (BigQuery bytes → tokens → latency). It captures the decision procedure for choosing the right tool, query-shaping rules, and anti-patterns. The single canonical copy lives at .agents/skills/bigquery-cost-first-querying/SKILL.md and is bundled into both distributions:

  • PyPI (bigquery-mcp-python): bigquery_mcp/skills/bigquery-cost-first-querying/SKILL.md
  • npm (bigquery-mcp-js): dist/skills/bigquery-cost-first-querying/SKILL.md

Point your agent runtime at the file (or copy it into your project's skills directory) to load the cost-first guidance.

Quick Start

Prerequisites: Python 3.10+ and uv package manager

🚀 Quick Setup

Option 1: Direct from PyPI (Recommended)

# 1. Authenticate
gcloud auth application-default login

# 2. Run server
uvx bigquery-mcp-python --project YOUR_PROJECT --location US

Option 2: Clone locally (development setup)

# 1. Clone and setup
git clone https://github.com/pvoo/bigquery-mcp.git
cd bigquery-mcp

# 2. Configure environment
cp .env.example .env
# Edit .env with your project and location

# 3. Run or inspect
make run      # Start server
make inspect  # Open MCP inspector

🔧 MCP Client Configuration

Option 1: PyPI package (Recommended) Simplest setup using the published PyPI package:

{
  "mcpServers": {
    "bigquery": {
      "command": "uvx",
      "args": [
        "bigquery-mcp-python",
        "--project", "your-project-id",
        "--location", "US"
     ]
    }
  }
}

Option 2: Local clone (for development)

# Clone first
git clone https://github.com/pvoo/bigquery-mcp.git
{
  "mcpServers": {
    "bigquery": {
      "command": "uv",
      "args": ["--directory", "/absolute/path/to/bigquery-mcp", "run", "bigquery-mcp"],
      "env": {
        "GCP_PROJECT_ID": "your-project-id",
        "BIGQUERY_LOCATION": "US"
      }
    }
  }
}

🧪 Test Your Setup

# Test with MCP inspector
npx @modelcontextprotocol/inspector uvx bigquery-mcp-python --project YOUR_PROJECT --location US

🔧 Configuration Options

All configuration can be set via CLI arguments or environment variables. CLI arguments take precedence.

Required Parameters

--project YOUR_PROJECT    # Google Cloud project ID
--location US             # BigQuery location (US, EU, etc.)

Optional Parameters

# Dataset Access Control
--datasets dataset1 dataset2    # Restrict to specific datasets (default: all datasets)

# Query & Result Limits
--list-max-results 500          # Max results for basic list operations (default: 500)
--detailed-list-max 25          # Max results for detailed list operations (default: 25)
--max-bytes-billed 109951162777  # Max bytes billed per query job (~USD 0.50/query)

# Table Analysis
--sample-rows 3                 # Sample data rows returned in get_table_info (default: 3)
--stats-sample-size 500         # Rows sampled for column fill rate calculations (default: 500)

# Authentication
--key-file /path/to/key.json    # Service account key file (default: ADC)

Environment Variables

All CLI options have corresponding environment variables:

export GCP_PROJECT_ID=your-project
export BIGQUERY_LOCATION=US
export BIGQUERY_ALLOWED_DATASETS=dataset1,dataset2
export BIGQUERY_LIST_MAX_RESULTS=500
export BIGQUERY_LIST_MAX_RESULTS_DETAILED=25
export BIGQUERY_MAX_BYTES_BILLED=109951162777
export BIGQUERY_SAMPLE_ROWS=3
export BIGQUERY_SAMPLE_ROWS_FOR_STATS=500
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json

Vector Search Configuration

See Vector Search section for full setup instructions.

--embedding-model project.dataset.model
--embedding-tables dataset.table1 dataset.table2
--distance-type COSINE

🛠️ Tools Overview

This MCP server provides 7 BigQuery tools, ordered below cheapest-first (no BigQuery cost → bounded cost). Names follow Google's BigQuery MCP conventions.

📊 Discovery — metadata only, scans zero bytes

  • list_dataset_ids - List dataset names in the project. Dual mode: names only (default) vs detailed=true for descriptions + table counts.
  • get_dataset_info - Metadata for one dataset (description, location, labels, table count).
  • list_table_ids - List table names in a dataset. Dual mode: names only (default) vs detailed=true for row counts + sizes.
  • get_table_info - Schema, column descriptions, per-column fill rates, and a few sample rows so an agent can judge relevance. The fill-rate/sample probes scan only a small bounded sample (capped by maximum_bytes_billed).

🔍 Querying — cost-bounded

  • dry_run_query - Estimate the bytes a query would scan without running it (zero cost). Run before execute_sql on large tables.
  • execute_sql - Execute SELECT/WITH queries only, with cost tracking, safety validation, and a default per-query billing cap of about USD 0.50. The description steers the model to filter on partitions, avoid SELECT *, and use LIMIT.

🔮 Vector Search (Optional)

  • vector_search - Dual-mode tool: discover embedding tables (no query_text) or perform semantic similarity search (with query_text)

Key Features:

  • Cost-first - Discovery scans zero bytes; dry_run_query previews cost; every query is capped by maximum_bytes_billed
  • Minimal by default - names-only list mode means ~70% fewer tokens before you commit to a scan
  • Safe queries only - Blocks all write operations (SELECT/WITH only)
  • LLM-optimized - Returns structured data perfect for AI analysis
  • Cost transparent - Shows bytes processed for each query
  • Google-aligned naming - Matches the Google BigQuery MCP toolset; own tools added only where Google has no equivalent

🔮 Vector Search (Optional)

Enable semantic similarity search using BigQuery vector embeddings.

Prerequisites: Setting Up Embeddings in BigQuery

Before using vector search, you need an embedding model and tables with embeddings:

Step 1: Create a Vertex AI connection (one-time setup)

-- In BigQuery console or bq command line
-- This creates a connection to Vertex AI for generating embeddings
CREATE EXTERNAL CONNECTION `your-project.your-region.vertex-ai`
  OPTIONS (
    endpoint = 'https://your-region-aiplatform.googleapis.com',
    type = 'CLOUD_RESOURCE'
  );

Step 2: Create the embedding model

CREATE OR REPLACE MODEL `your-project.your_dataset.text_embedding_model`
REMOTE WITH CONNECTION `your-project.your-region.vertex-ai`
OPTIONS (ENDPOINT = 'text-embedding-005');

Step 3: Add embeddings to your table

-- Add embedding column to existing table
ALTER TABLE `your-project.your_dataset.products`
ADD COLUMN IF NOT EXISTS embedding ARRAY<FLOAT64>;

-- Generate embeddings for your text data
UPDATE `your-project.your_dataset.products` t
SET embedding = (
  SELECT ml_generate_embedding_result
  FROM ML.GENERATE_EMBEDDING(
    MODEL `your-project.your_dataset.text_embedding_model`,
    (SELECT t.name AS content),
    STRUCT(TRUE AS flatten_json_output)
  )
)
WHERE embedding IS NULL;

See BigQuery text embeddings documentation for detailed setup instructions and connection permissions.

MCP Configuration for Vector Search

Once you have embeddings set up, configure the MCP server:

{
  "mcpServers": {
    "bigquery": {
      "command": "uvx",
      "args": [
        "bigquery-mcp-python",
        "--project", "your-project",
        "--location", "US",
        "--embedding-model", "your-project.your_dataset.text_embedding_model",
        "--embedding-tables", "your_dataset.products", "your_dataset.documents"
      ]
    }
  }
}

Configuration Reference

CLI Argument Environment Variable Default Description
--embedding-model BIGQUERY_EMBEDDING_MODEL - Required. Full path to embedding model (project.dataset.model). Validated on startup.
--embedding-tables BIGQUERY_EMBEDDING_TABLES - Tables with embedding columns (skips auto-discovery)
--vector-column-contains BIGQUERY_EMBEDDING_COLUMN_CONTAINS embedding Pattern for finding embedding columns (column name must contain this)
--distance-type BIGQUERY_DISTANCE_TYPE COSINE Distance metric: COSINE, EUCLIDEAN, DOT_PRODUCT
--no-vector-search BIGQUERY_VECTOR_SEARCH_ENABLED=false enabled Disable vector search tools

Usage Examples

Discovery mode - find tables with embeddings:

{
  "query_text": ""
}

Search mode - semantic similarity search:

{
  "query_text": "solenoid valve for water",
  "table_path": "my_dataset.products",
  "top_k": "10",
  "select_columns": "name,description,price"
}

Required Permissions

Role Purpose
roles/bigquery.dataViewer Read tables and models
roles/bigquery.jobUser Run BigQuery jobs
roles/bigquery.metadataViewer Auto-discover embedding tables (optional)

🏗️ Development Setup

Local Development

# Clone and setup
git clone https://github.com/pvoo/bigquery-mcp.git
cd bigquery-mcp
make install  # Setup environment + pre-commit hooks

# Development workflow
make run      # Start server
make test     # Run test suite
make check    # Lint + format + typecheck
make inspect  # Launch MCP inspector

Testing & Quality

make test                    # Full test suite
pytest tests/test_safety.py  # SQL safety validation tests
pytest tests/test_server.py  # Core server functionality tests
make check                   # Run all quality checks

🔐 Authentication & Permissions

Authentication Methods:

  1. Application Default Credentials (recommended): gcloud auth application-default login
  2. Service Account Key: Use --key-file or set GOOGLE_APPLICATION_CREDENTIALS

Required BigQuery Permissions:

  • bigquery.datasets.get, bigquery.datasets.list
  • bigquery.tables.list, bigquery.tables.get
  • bigquery.jobs.create, bigquery.data.get

🚨 Troubleshooting

Authentication Issues:

# Check current auth
gcloud auth application-default print-access-token

# Re-authenticate
gcloud auth application-default login

# Enable BigQuery API
gcloud services enable bigquery.googleapis.com

MCP Connection Issues:

  • Ensure absolute paths in MCP config
  • Test server manually: make run
  • Check that project and location environment variables or args are set correctly

Performance Issues:

  • Use {"detailed": false} for faster responses
  • Add search filters: {"search": "pattern"}
  • Reduce max_results for large datasets

💡 Usage Examples

📊 SQL Query Example

-- Query public datasets
SELECT
    EXTRACT(YEAR FROM pickup_datetime) as year,
    COUNT(*) as trips,
    ROUND(AVG(fare_amount), 2) as avg_fare
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2020`
WHERE pickup_datetime BETWEEN '2020-01-01' AND '2020-12-31'
GROUP BY year
LIMIT 20

🤖 Example: Usage with Claude Code subagent

Scenario: Use the specialized BigQuery Table Analyst agent in Claude Code to automatically explore your data warehouse, analyze table relationships, and provide structured insights. By using the subagent you can take the context used for analyzing the tables out of the main thread and return actionable insights into the main agent thread for writing SQL or analyzing.

Setup:

# 1. Clone and configure
git clone https://github.com/pvoo/bigquery-mcp.git
cd bigquery-mcp

# 2. Setup environment
export GCP_PROJECT_ID="your-project-id"
export BIGQUERY_LOCATION="US"
gcloud auth application-default login

# 3. Launch Claude Code
claude-code

Example Usage:

💬 You: "I need to understand our sales data structure and find tables related to customer orders"

🤖 Claude: I'll use the BigQuery Table Analyst agent to explore your sales datasets and identify relevant tables with their relationships.

[Agent automatically:]
- Lists all datasets to identify sales-related ones
- Explores table schemas with detailed metadata
- Shows actual sample data from key tables
- Discovers join relationships between tables
- Provides ready-to-use SQL queries

What the Agent Returns:

  • Table schemas with column descriptions and types
  • Sample data showing actual values (not placeholders)
  • Join relationships with working SQL examples
  • Data quality insights (null rates, freshness, etc.)
  • Actionable SQL queries you can immediately execute

🤝 Contributing

We welcome contributions! Looking forward to your feedback for improvements.

Quick Start:

# Fork on GitHub, then:
git clone https://github.com/yourusername/bigquery-mcp.git
cd bigquery-mcp
make install  # Setup dev environment
make check    # Verify everything works

# Make changes, then:
make test     # Run tests
make check    # Quality checks
# Submit PR!

Development Guidelines:

  • Add tests for new features
  • Update documentation
  • Follow existing code style (enforced by pre-commit hooks)
  • Ensure all quality checks pass

Found an issue or have a feature request?


🌟 Star this repo if it helps you!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bigquery_mcp_python-0.1.2.tar.gz (196.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bigquery_mcp_python-0.1.2-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

Details for the file bigquery_mcp_python-0.1.2.tar.gz.

File metadata

  • Download URL: bigquery_mcp_python-0.1.2.tar.gz
  • Upload date:
  • Size: 196.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bigquery_mcp_python-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a405a3821a71194b014a23c46281715660f1dc0b0fd37e3f1b78954ccd2c82ce
MD5 f59b3aa044dcecdb2832fe6b4a74db93
BLAKE2b-256 fd992c43b3fdb9abf1c1d045c1b2b42d60ee570b4d6eb1cc8e05cbb0acc9e7f7

See more details on using hashes here.

File details

Details for the file bigquery_mcp_python-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for bigquery_mcp_python-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ef1e5dc64e2ce027a32f34bccad86d4dc4b84643af840ff31556601d50d78f2e
MD5 9303a32211695abcd1833b47550e383f
BLAKE2b-256 56e9525b118e02d96169d1b897d11d2ee0907c197bc06d9295cb5dd3dd0bf9aa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page