Skip to main content

Minimal MCP server for BigQuery SQL validation and dry-run analysis

Project description

mcp-bigquery-dryrun

Minimal MCP server for BigQuery SQL validation and dry-run analysis. This server provides exactly two tools for validating and analyzing BigQuery SQL queries without executing them.

** IMPORTANT: This server does NOT execute queries. All operations are dry-run only. Cost estimates are approximations based on bytes processed.**

Features

  • SQL Validation: Check BigQuery SQL syntax without running queries
  • Dry-Run Analysis: Get cost estimates, referenced tables, and schema preview
  • Parameter Support: Validate parameterized queries
  • Cost Estimation: Calculate USD estimates based on bytes processed

Quick Start

Prerequisites

  • Python 3.9+
  • Google Cloud SDK with BigQuery API enabled
  • Application Default Credentials configured

Installation

From PyPI (Recommended)

# Install from PyPI
pip install mcp-bigquery-dryrun

# Or with uv
uv pip install mcp-bigquery-dryrun

From Source

# Clone the repository
git clone https://github.com/caron14/mcp-bigquery-dryrun.git
cd mcp-bigquery-dryrun

# Install with uv (recommended)
uv pip install -e .

# Or install with pip
pip install -e .

Authentication

Set up Application Default Credentials:

gcloud auth application-default login

Or use a service account key:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json

Configuration

Environment Variables

Variable Description Default
BQ_PROJECT GCP project ID From ADC
BQ_LOCATION BigQuery location (e.g., US, EU, asia-northeast1) None
SAFE_PRICE_PER_TIB Default price per TiB for cost estimation 5.0

Claude Code Integration

Add to your Claude Code configuration:

{
  "mcpServers": {
    "bq-dryrun": {
      "command": "mcp-bigquery-dryrun",
      "env": {
        "BQ_PROJECT": "your-gcp-project",
        "BQ_LOCATION": "asia-northeast1",
        "SAFE_PRICE_PER_TIB": "5.0"
      }
    }
  }
}

Or if installed from source:

{
  "mcpServers": {
    "bq-dryrun": {
      "command": "python",
      "args": ["-m", "mcp_bigquery_dryrun"],
      "env": {
        "BQ_PROJECT": "your-gcp-project",
        "BQ_LOCATION": "asia-northeast1",
        "SAFE_PRICE_PER_TIB": "5.0"
      }
    }
  }
}

Tools

bq.validate_sql

Validate BigQuery SQL syntax without executing the query.

Input:

{
  "sql": "SELECT * FROM dataset.table WHERE id = @id",
  "params": {"id": "123"}  // Optional
}

Success Response:

{
  "isValid": true
}

Error Response:

{
  "isValid": false,
  "error": {
    "code": "INVALID_SQL",
    "message": "Syntax error at [3:15]",
    "location": {
      "line": 3,
      "column": 15
    },
    "details": [...]  // Optional
  }
}

bq.dry_run_sql

Perform a dry-run to get cost estimates and metadata without executing the query.

Input:

{
  "sql": "SELECT * FROM dataset.table",
  "params": {"id": "123"},  // Optional
  "pricePerTiB": 6.0  // Optional, overrides default
}

Success Response:

{
  "totalBytesProcessed": 1073741824,
  "usdEstimate": 0.005,
  "referencedTables": [
    {
      "project": "my-project",
      "dataset": "my_dataset",
      "table": "my_table"
    }
  ],
  "schemaPreview": [
    {
      "name": "id",
      "type": "STRING",
      "mode": "NULLABLE"
    },
    {
      "name": "created_at",
      "type": "TIMESTAMP",
      "mode": "REQUIRED"
    }
  ]
}

Error Response:

{
  "error": {
    "code": "INVALID_SQL",
    "message": "Table not found: dataset.table",
    "details": [...]  // Optional
  }
}

Examples

Validate a Simple Query

# Tool: bq.validate_sql
{
  "sql": "SELECT 1"
}
# Returns: {"isValid": true}

Validate with Parameters

# Tool: bq.validate_sql
{
  "sql": "SELECT * FROM users WHERE name = @name AND age > @age",
  "params": {
    "name": "Alice",
    "age": 25
  }
}

Get Cost Estimate

# Tool: bq.dry_run_sql
{
  "sql": "SELECT * FROM `bigquery-public-data.samples.shakespeare`",
  "pricePerTiB": 5.0
}
# Returns bytes processed, USD estimate, and schema

Analyze Complex Query

# Tool: bq.dry_run_sql
{
  "sql": """
    WITH user_stats AS (
      SELECT user_id, COUNT(*) as order_count
      FROM orders
      GROUP BY user_id
    )
    SELECT * FROM user_stats WHERE order_count > 10
  """
}

Testing

Run tests with pytest:

# Run all tests (requires BigQuery credentials)
pytest tests/

# Run only tests that don't require credentials
pytest tests/test_min.py::TestWithoutCredentials

Development

# Install development dependencies
uv pip install -e ".[dev]"

# Run the server locally
python -m mcp_bigquery_dryrun

# Or using the console script
mcp-bigquery-dryrun

Limitations

  • No Query Execution: This server only performs dry-runs and validation
  • Cost Estimates: USD estimates are approximations based on bytes processed
  • Parameter Types: Initial implementation treats all parameters as STRING type
  • Cache Disabled: Queries always run with use_query_cache=False for accurate estimates

License

Apache-2.0

Changelog

0.1.0 (2024-08-12)

  • Initial release

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

mcp_bigquery_dryrun-0.1.0b0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

mcp_bigquery_dryrun-0.1.0b0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file mcp_bigquery_dryrun-0.1.0b0.tar.gz.

File metadata

  • Download URL: mcp_bigquery_dryrun-0.1.0b0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for mcp_bigquery_dryrun-0.1.0b0.tar.gz
Algorithm Hash digest
SHA256 73894a3c706dc3682cbd51ea616e48087807b53c6592024054c40a8ef8e37c9c
MD5 58fa4d6d00664773d0e9cac894ba75c1
BLAKE2b-256 08232bb2b96cd97fce2c4be82e0dd8404faa76b711ca8b387381ae9ccb21d4cd

See more details on using hashes here.

File details

Details for the file mcp_bigquery_dryrun-0.1.0b0-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_bigquery_dryrun-0.1.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad32c3f5c028d1e5dd31ed9c3c0f2c2c18742cc34b47a45b39000ad229d82f52
MD5 9266bdcbacf0943fa341a6b3586b346e
BLAKE2b-256 fc1513e96b10791b72a410a37813ca87033de7ab80a0bcf493b8062e6eb0e93f

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