Skip to main content

MCP server for Google BigQuery — authenticate via service account JSON env var, run with uvx

Project description

BigQuery MCP Server

A Model Context Protocol (MCP) server that enables LLMs to interact with Google BigQuery. Built with FastMCP (Python) and designed for ephemeral cloud environments where only environment variables are available (no file-based credentials).


Features

Tool Description
bq_list_datasets List all datasets in the configured project
bq_list_tables List tables in a dataset with row counts and size in MB
bq_get_schema Get the full schema of a table (columns, types, descriptions)
bq_dry_run Validate SQL and estimate query cost (~$6.25/TB)
bq_run_query Execute SQL and return results (auto-appends LIMIT 1000 if missing)

Project Structure

bigquery-mcp-server/
├── src/
│   ├── server.py                 # Entry point — starts the MCP server
│   ├── constants.py              # Global constants (BigQuery config, limits)
│   ├── tools/
│   │   └── bigquery.py           # All 5 BigQuery tools
│   └── services/
│       ├── bigquery_client.py    # BigQuery client factory + row serialization
│       └── error_handler.py      # Centralized BigQuery error handling
├── main.py                       # Root-level entry point wrapper
├── Dockerfile                    # Multi-stage Docker build
├── docker-compose.yml            # Server + MCP Inspector
├── docker-compose.dev.yml        # Dev mode with hot-reload
├── mcp.json                      # MCP client configuration
├── pyproject.toml                # Project metadata and dependencies
└── requirements.txt              # Pip-compatible dependencies

Environment Variables

Variable Required Default Description
BIGQUERY_PROJECT_ID Yes GCP project ID (e.g. my-project-123)
BIGQUERY_SERVICE_ACCOUNT_JSON_CONTENT Yes Raw JSON string of the service account key
BIGQUERY_LOCATION No US BigQuery dataset region
TRANSPORT No stdio Transport mode: stdio or http
PORT No 8000 HTTP server port
HOST No 127.0.0.1 HTTP server bind address

Authentication

This server authenticates using a service account JSON provided directly via environment variable — no file paths, no gcloud CLI.

  1. Create a service account in the GCP Console with at least:
    • BigQuery Data Viewer (roles/bigquery.dataViewer)
    • BigQuery Job User (roles/bigquery.jobUser)
  2. Generate a JSON key for the service account
  3. Set the full JSON string as BIGQUERY_SERVICE_ACCOUNT_JSON_CONTENT
export BIGQUERY_PROJECT_ID="my-project-123"
export BIGQUERY_SERVICE_ACCOUNT_JSON_CONTENT='{"type":"service_account","project_id":"...","private_key":"...","client_email":"...","...":"..."}'

Quick Start

Option A — uvx (Recommended)

No manual install needed. uvx creates an isolated environment, installs dependencies, and runs the server in one command:

uvx --from /path/to/bigquery-mcp-server bigquery-mcp-server

For MCP clients (Claude Code, Cursor, etc.), configure the server in your client's MCP config:

{
  "mcpServers": {
    "bigquery-mcp": {
      "command": "uvx",
      "args": ["--from", "/path/to/bigquery-mcp-server", "bigquery-mcp-server"],
      "env": {
        "BIGQUERY_PROJECT_ID": "my-project-123",
        "BIGQUERY_SERVICE_ACCOUNT_JSON_CONTENT": "{ ... }"
      }
    }
  }
}

Note: When running via uvx, env vars must be passed explicitly (via the env block in your MCP client config). The .env file is only loaded during local development.

Option B — Local (virtualenv)

# Create virtual environment and install
python -m venv .venv
source .venv/bin/activate      # Windows: .venv\Scripts\activate
pip install -e .

# Set credentials (or use a .env file)
export BIGQUERY_PROJECT_ID="my-project-123"
export BIGQUERY_SERVICE_ACCOUNT_JSON_CONTENT='{ ... }'

# Run in stdio mode
python main.py

# Or HTTP mode (for MCP Inspector)
TRANSPORT=http python main.py

Option C — Docker

# Set env vars in a .env file or export them, then:
docker compose up --build

# Open MCP Inspector at http://localhost:6274
# Connect to: http://mcp-server:8000/mcp (Streamable HTTP)

Option D — Dev mode with hot-reload

docker compose -f docker-compose.dev.yml up --build

# In another terminal:
npx @modelcontextprotocol/inspector
# Connect to: http://localhost:8000/mcp (Streamable HTTP)

Testing with MCP Inspector CLI

# List all tools
npx @modelcontextprotocol/inspector --cli \
  --config mcp.json --server bigquery-mcp \
  --method tools/list

# List datasets
npx @modelcontextprotocol/inspector --cli \
  --config mcp.json --server bigquery-mcp \
  --method tools/call --tool-name bq_list_datasets

# List tables in a dataset
npx @modelcontextprotocol/inspector --cli \
  --config mcp.json --server bigquery-mcp \
  --method tools/call --tool-name bq_list_tables \
  --tool-arg 'params={"dataset": "my_dataset"}'

# Get table schema
npx @modelcontextprotocol/inspector --cli \
  --config mcp.json --server bigquery-mcp \
  --method tools/call --tool-name bq_get_schema \
  --tool-arg 'params={"dataset": "my_dataset", "table": "my_table"}'

# Dry run a query
npx @modelcontextprotocol/inspector --cli \
  --config mcp.json --server bigquery-mcp \
  --method tools/call --tool-name bq_dry_run \
  --tool-arg 'params={"sql": "SELECT * FROM my_dataset.my_table"}'

# Run a query
npx @modelcontextprotocol/inspector --cli \
  --config mcp.json --server bigquery-mcp \
  --method tools/call --tool-name bq_run_query \
  --tool-arg 'params={"sql": "SELECT * FROM my_dataset.my_table LIMIT 10"}'

Connect to Claude Code

Using uvx (recommended):

claude mcp add bigquery-mcp uvx -- --from /path/to/bigquery-mcp-server bigquery-mcp-server

Or add to ~/.claude/mcp.json:

{
  "mcpServers": {
    "bigquery-mcp": {
      "command": "uvx",
      "args": ["--from", "/path/to/bigquery-mcp-server", "bigquery-mcp-server"],
      "env": {
        "BIGQUERY_PROJECT_ID": "my-project-123",
        "BIGQUERY_SERVICE_ACCOUNT_JSON_CONTENT": "{ ... }"
      }
    }
  }
}

Using local Python (if developing):

claude mcp add bigquery-mcp python -- -m src.server

Transports

Transport How to activate When to use
stdio TRANSPORT=stdio python -m src.server Claude Code, Cursor, local integration
HTTP TRANSPORT=http python -m src.server MCP Inspector, remote servers

Safety Features

  • Auto LIMIT: bq_run_query automatically appends LIMIT 1000 if no LIMIT clause is detected, preventing accidental large data transfers
  • Dry run: bq_dry_run validates SQL and estimates cost before execution
  • Input validation: All tool inputs validated with Pydantic v2
  • Error handling: BigQuery exceptions (403, 404, 400, etc.) are caught and returned as human-readable strings — the server never crashes
  • Type serialization: Dates, datetimes, decimals, and bytes are automatically converted to JSON-safe types

References

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_server-0.1.0.tar.gz (11.0 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_server-0.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file bigquery_mcp_server-0.1.0.tar.gz.

File metadata

  • Download URL: bigquery_mcp_server-0.1.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for bigquery_mcp_server-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4d27aed4e52ea54928a9cb6d3750086d27514aca7f3c1a351c888c925c7eaefc
MD5 6ecde7949b862f41fc7293f90774f8e1
BLAKE2b-256 5058da2df7091566b86e065f462a94d0b66e01471ed90f56849c5b3994595d1f

See more details on using hashes here.

File details

Details for the file bigquery_mcp_server-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bigquery_mcp_server-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for bigquery_mcp_server-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83c12e806ef218e67594ec11780d62eb5dc3d65887a57fe960704b65e9ddc682
MD5 119108ed471bf24eb77f268b9e91042a
BLAKE2b-256 26418b3e727f5d1700f7178d9cfecf66984ca34407e3a237a89327d9e02741cd

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