BigQuery Validator
mcp-bigquery
通过 Model Context Protocol 安全探索 BigQuery
概述
mcp-bigquery 是一个 Model Context Protocol (MCP) 服务器,使 AI 助手(如 Claude)能够安全地与 Google BigQuery 交互。
主要特性
安全执行:所有操作严格限制为 dry-run 验证。服务器从不执行会修改数据或产生执行成本的查询。
成本透明:在执行前提供查询成本和处理字节数的估算。
静态分析:分析查询依赖关系并验证 SQL 语法。
模式探索:浏览数据集、表和列。
商业价值
问题 | 使用 mcp-bigquery 的解决方案 |
意外执行高成本查询 | 执行前成本估算 |
因 SQL 语法错误导致开发延迟 | 早期语法错误检测 |
对模式结构缺乏可见性 | 安全的模式元数据发现 |
AI 未经授权修改数据的风险 | 强制 dry-run 约束 |
Related MCP server: mcp-bigquery-dryrun
快速开始
步骤 1:安装
通过 pip 安装包:
pip install mcp-bigquery步骤 2:身份验证
设置 Google Cloud Platform 身份验证:
# For user account authentication
gcloud auth application-default login
# For service account authentication
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json步骤 3:Claude Desktop 配置
在 Claude Desktop 配置文件中配置服务器:
macOS:
~/Library/Application Support/Claude/claude_desktop_config.jsonWindows:
%APPDATA%\Claude\claude_desktop_config.json
添加以下条目:
{
"mcpServers": {
"mcp-bigquery": {
"command": "mcp-bigquery",
"env": {
"BQ_PROJECT": "your-gcp-project-id"
}
}
}
}步骤 4:验证
重启 Claude Desktop 并运行以下查询以验证设置:
"我的 BigQuery 项目中有哪些数据集?"
"你能估算一下这个查询的成本吗:SELECT * FROM dataset.table"
"显示 users 表的模式"
可用工具
SQL 验证与分析
工具 | 用途 | 主要使用场景 |
bq_validate_sql | 检查 SQL 语法 | 查询执行前的验证 |
bq_dry_run_sql | 获取成本估算和元数据 | 执行前成本评估 |
bq_extract_dependencies | 映射表依赖关系 | 血缘和依赖映射 |
bq_validate_query_syntax | 详细语法分析 | 调试复杂 SQL 查询 |
模式发现
工具 | 用途 | 主要使用场景 |
bq_list_datasets | 列出项目中的所有数据集 | 初始项目发现 |
bq_list_tables | 列出带有分区元数据的表 | 数据集结构浏览 |
bq_describe_table | 获取详细的模式信息 | 列级验证 |
bq_get_table_info | 获取全面的元数据 | 表统计分析 |
bq_preview_table | 预览表数据(免费) | 检查样本记录而无需数据扫描成本 |
[!重要] bq_preview_table 工具使用
client.list_rows(API:tabledata.list)直接获取样本行,因此扫描字节数为零,且无执行成本。为防止敏感信息(如 PII)意外暴露给 LLM,此工具默认禁用。您必须通过在环境配置中设置MCP_BQ_ENABLE_PREVIEW=true来明确选择启用。
配置
环境变量
变量 | 用途 | 默认值 |
| 目标 GCP 项目 ID | 通过 ADC 确定 |
| 目标 BigQuery 区域 | 未设置 |
| 成本估算的每 TiB 价格 | 5.0 |
| 日志详细程度(DEBUG、INFO、WARNING、ERROR、CRITICAL) | WARNING |
| 启用 bq_preview_table 工具(true/false) | false |
示例 .env 文件
对于本地测试或开发环境,您可以在 .env 文件中定义这些变量:
BQ_PROJECT=your-gcp-project-id
BQ_LOCATION=asia-northeast1
SAFE_PRICE_PER_TIB=5.0
LOG_LEVEL=WARNING
MCP_BQ_ENABLE_PREVIEW=true完整的 Claude Desktop 配置示例
{
"mcpServers": {
"mcp-bigquery": {
"command": "mcp-bigquery",
"env": {
"BQ_PROJECT": "my-production-project",
"BQ_LOCATION": "asia-northeast1",
"SAFE_PRICE_PER_TIB": "6.0",
"LOG_LEVEL": "WARNING",
"MCP_BQ_ENABLE_PREVIEW": "true"
}
}
}
}故障排除
映射的错误及解决方案
身份验证错误
Error: Could not automatically determine credentials解决方案:使用命令行重新进行身份验证:
gcloud auth application-default login
权限被拒绝
Error: User does not have bigquery.tables.get permission解决方案:为目标身份授予
BigQuery Data Viewer角色:gcloud projects add-iam-policy-binding YOUR_PROJECT \ --member="user:your-email@example.com" \ --role="roles/bigquery.dataViewer"
项目 ID 缺失
Error: Project ID is required解决方案:确保在配置中正确设置
BQ_PROJECT变量。
使用示例
示例 1:运行前检查成本
# Before running an expensive query...
query = "SELECT * FROM `bigquery-public-data.github_repos.commits`"
# First, check the cost
result = bq_dry_run_sql(sql=query)
print(f"Estimated cost: ${result['usdEstimate']}")
print(f"Data processed: {result['totalBytesProcessed'] / 1e9:.2f} GB")
# Output:
# Estimated cost: $12.50
# Data processed: 2500.00 GB示例 2:了解表结构
# Check table schema
result = bq_describe_table(
dataset_id="your_dataset",
table_id="users"
)
# Output:
# ├── user_id (INTEGER, REQUIRED)
# ├── email (STRING, NULLABLE)
# ├── created_at (TIMESTAMP, REQUIRED)
# └── profile (RECORD, REPEATED)
# ├── name (STRING)
# └── age (INTEGER)示例 3:跟踪数据依赖关系
# Understand query dependencies
query = """
WITH user_stats AS (
SELECT user_id, COUNT(*) as order_count
FROM orders
GROUP BY user_id
)
SELECT u.name, s.order_count
FROM users u
JOIN user_stats s ON u.id = s.user_id
"""
result = bq_extract_dependencies(sql=query)
# Output:
# Tables: ['orders', 'users']
# Columns: ['user_id', 'name', 'id']
# Dependency Graph:
# orders → user_stats → final_result
# users → final_result项目状态和版本历史
版本 | 发布日期 | 变更摘要 |
v0.7.1 | 2026-08-17 | 优化了 mcp 依赖约束并精简了 wiki 文档 |
v0.7.0 | 2026-06-21 | 添加了免费表预览工具( |
v0.6.0 | 2026-06-21 | 线程安全缓存、递归 AST 查询、退避重试和 Google API 异常映射 |
v0.5.0 | 2026-01-02 | 整合了格式化器、客户端缓存和统一日志控制 |
v0.4.2 | 2025-12-08 | 模块化模式探索器和统一客户端/日志控制 |
v0.4.1 | 2025-01-22 | 错误处理和调试日志改进 |
v0.4.0 | 2025-01-22 | 添加了模式发现工具 |
v0.3.0 | 2025-01-17 | 集成了 SQL 静态分析引擎 |
v0.2.0 | 2025-01-16 | 初始版本,支持基本验证和 dry-run 查询 |
开发和贡献
有关本地开发设置和贡献政策的说明,请参阅 CONTRIBUTING.md 指南。
# Clone the repository
git clone https://github.com/caron14/mcp-bigquery.git
cd mcp-bigquery
# Install development dependencies
pip install -e ".[dev]"
# Execute the test suite
pytest tests/许可证
本项目根据 MIT 许可证授权。有关详细信息,请参阅 LICENSE。
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- AlicenseNot gradedqualityBmaintenanceA read-only BigQuery MCP server with auto-LIMIT injection, dry-run cost guard, and ADC authentication. Allows safe SQL querying of BigQuery by LLMs without risk of data modification or unexpected costs.1MIT
- AlicenseAqualityFmaintenanceValidates BigQuery SQL syntax and performs dry-run analysis without executing queries, providing cost estimates, referenced tables, and schema previews.2Apache 2.0
- AlicenseNot gradedqualityBmaintenanceEnables LLMs to explore BigQuery datasets and tables, run safe read-only queries, and optionally perform vector search using BigQuery embeddings.9MIT
- AlicenseNot gradedqualityCmaintenanceEnables AI assistants to query and analyze Google BigQuery data, including schema browsing, running queries, and comparing datasets through natural language.MIT
Related MCP Connectors
Deterministic validation for AI-generated artifacts: JSON Schema, OpenAPI response, SQL syntax.
Run SOQL queries to explore and retrieve Salesforce data. Inspect records, fields, and relationshi…
Run SOQL queries against your Salesforce org to explore and retrieve data. Quickly iterate on filt…
Appeared in Searches
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/caron14/mcp-bigquery'
If you have feedback or need assistance with the MCP directory API, please join our Discord server