mattckrause/python-mcp-bigquery
If you are the rightful owner of python-mcp-bigquery and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
The MCP BigQuery Server is a Model Context Protocol server designed for seamless integration with Google BigQuery, offering secure access to datasets through stdio or a REST API.
MCP BigQuery Server
A Model Context Protocol (MCP) server for Google BigQuery integration, providing secure access to BigQuery datasets via stdio or a simple REST API. Built with Python and FastAPI and deployable to Azure Container Apps.
๐ Features
- Dual transport: stdio (for MCP clients) and HTTP (REST)
- BigQuery integration: query datasets and explore table schemas
- Azure Container Apps deployment with auto-scaling
- Authentication: API key via header (optional, dev-friendly toggle)
- OpenAPI docs:
/docs
and an OpenAPI 3.0.4 YAML at/openapi.yaml
- Health checks at
/health
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ MCP Client โโโโโโ MCP BigQuery โโโโโโ Google Cloud โ
โ (stdio/HTTP) โ โ Server โ โ BigQuery โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโ
โ Azure Container โ
โ Apps โ
โโโโโโโโโโโโโโโโโโโโ
Components:
- MCP server implementing the Model Context Protocol
- FastAPI-based REST layer for simple HTTP access
- Google Cloud BigQuery Python SDK client
- Docker container for Azure Container Apps
- Authentication layer: optional API key header
๐ Prerequisites
For Local Development
- Python 3.8+
- Google Cloud Project with BigQuery API enabled
- Service Account with BigQuery permissions (Job User, Data Viewer)
For Azure Deployment
- Azure CLI (
az
) installed and authenticated - Azure Developer CLI (
azd
) installed - Docker Desktop (for container building)
- GitHub Account (if using CI/CD)
๐ ๏ธ Installation & Setup
1. Clone and Setup Environment
# Clone the repository
git clone https://github.com/mattckrause/python-mcp-bigquery.git
cd python-mcp-bigquery
# Create virtual environment
python -m venv venv
.\venv\Scripts\Activate.ps1 # Windows
# source venv/bin/activate # Linux/macOS
# Install dependencies (project uses standard Python packages)
pip install -r requirements.txt
2. Google Cloud Configuration
-
Enable BigQuery API:
gcloud services enable bigquery.googleapis.com
-
Create Service Account:
gcloud iam service-accounts create mcp-bigquery-server \ --description="Service account for MCP BigQuery Server" \ --display-name="MCP BigQuery Server"
-
Assign Permissions:
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:mcp-bigquery-server@YOUR_PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/bigquery.jobUser" gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:mcp-bigquery-server@YOUR_PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/bigquery.dataViewer"
-
Download Credentials:
gcloud iam service-accounts keys create credentials.json \ --iam-account=mcp-bigquery-server@YOUR_PROJECT_ID.iam.gserviceaccount.com
3. Environment Configuration
# Copy environment template
Copy-Item .env.sample .env
# Edit .env with your values
notepad .env
Required Configuration:
## Google Cloud
# Project ID used by the server
GOOGLE_CLOUD_PROJECT=your-project-id
# Service account credentials (JSON string) all one line.
GOOGLE_SERVICE_ACCOUNT_CREDENTIALS={"type":"service_account",...}
## Azure (for deployment)
AZURE_ENV_NAME=mcp-bigquery
AZURE_LOCATION=centralus
## Authentication (optional)
# When true, all REST endpoints (except /health) require X-API-Key
ENABLE_AUTH=false
# Comma-separated list, e.g. key1,key2
API_KEYS=your-api-key-here
๐โโ๏ธ Running the Server
Local Development (stdio)
# Run as MCP stdio server
python src/mcp_bigquery_server/main.py --project-id <project_id> --key-file <path_to_service_account_keyfile>
Local HTTP Server (REST)
# Run HTTP server (reads GOOGLE_SERVICE_ACCOUNT_CREDENTIALS when set)
python src/mcp_bigquery_server/main.py --project-id <project_id> --http --port 8000
# Server runs on http://localhost:8000
# Docs at http://localhost:8000/docs
Docker (Local)
# Build container
docker build -t mcp-bigquery-server .
# Run container
docker run -p 8000:8000 --env-file .env mcp-bigquery-server
โ๏ธ Azure Deployment
# Run preprovision script to set up Azure variables
./preprovision.ps1
# Initialize Azure environment (first time only)
azd init
# Deploy to Azure
azd up
# you need to select the Azure Subscription and Resource Group as part of the deployment.
The deployment will:
- Create Azure resources (Key Vault, ACR, Container Apps)
- Build and push the Docker image
- Deploy the Container App with environment variables and secrets
Accessing Your Deployed API
After deployment, azd
will provide your Container Apps URL similar to:
https://your-app.kindriverbank-12345678.centralus.azurecontainerapps.io
REST Endpoints:
GET /health
- Health check (public)POST /query
- Execute read-only BigQuery SQLGET /resources
- List BigQuery resources (datasets/tables)GET /resources/read?uri=bigquery://project/dataset/table/schema
- Read schema/contentGET /docs
- Interactive API documentation (Swagger UI)GET /openapi.yaml
- OpenAPI 3.0.4 specification (authoritative)
๐ง Configuration Options
Environment Variables
Variable | Required | Description | Default |
---|---|---|---|
GOOGLE_CLOUD_PROJECT | Yes | Google Cloud project ID | - |
GOOGLE_SERVICE_ACCOUNT_CREDENTIALS | Yes | Service account JSON or base64 of JSON | - |
AZURE_ENV_NAME | For deployment | Azure environment name | mcp-bigquery |
AZURE_LOCATION | For deployment | Azure region | centralus |
ENABLE_AUTH | No | Enable API key authentication | false |
API_KEYS | If auth enabled | Comma-separated API keys | - |
PORT | No | HTTP server port | 8000 |
HOST | No | HTTP server host | 0.0.0.0 |
Authentication
No Authentication (Development)
ENABLE_AUTH=false
API Key Authentication (header only)
ENABLE_AUTH=true
API_KEYS=key1,key2,key3
Usage
$headers = @{ "X-API-Key" = "your-api-key"; "Content-Type" = "application/json" }
$body = '{"sql":"SELECT 1 AS x"}'
Invoke-RestMethod -Uri "https://your-app.azurecontainerapps.io/query" -Method POST -Headers $headers -Body $body
๐ API Usage Examples (REST)
Health Check
# Check server health
$response = Invoke-RestMethod -Uri "https://your-app.azurecontainerapps.io/health" -Method GET
Write-Output $response
# Output: {"status": "healthy", "timestamp": "2025-01-15T10:30:00Z"}
Run a SQL Query
$headers = @{ "X-API-Key" = "your-api-key"; "Content-Type" = "application/json" }
$body = '{"sql":"SELECT 1 AS x"}'
Invoke-RestMethod -Uri "https://your-app.azurecontainerapps.io/query" -Method POST -Headers $headers -Body $body
List Resources
$headers = @{ "X-API-Key" = "your-api-key" }
Invoke-RestMethod -Uri "https://your-app.azurecontainerapps.io/resources" -Method GET -Headers $headers
Read a Resource (Schema)
$headers = @{ "X-API-Key" = "your-api-key" }
$uri = "https://your-app.azurecontainerapps.io/resources/read?uri=bigquery://your-project/your_dataset/your_table/schema"
Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
๐ Monitoring & Troubleshooting
Azure Logs
# View application logs
azd logs
# Stream live logs
azd logs --follow
# View specific container logs
az containerapp logs show --name your-app --resource-group rg-mcp-bigquery
Health Monitoring
The /health
endpoint provides detailed status information:
{
"status": "healthy",
"timestamp": "2025-01-15T10:30:00Z",
"bigquery_connection": "ok",
"auth_enabled": false,
"version": "0.1.0"
}
Common Issues
-
BigQuery Permission Errors:
- Verify service account has correct IAM roles
- Check project ID matches your Google Cloud project
-
Authentication Failures:
- Ensure API keys are properly formatted
- Verify JWT secret is set if using JWT tokens
-
Container Startup Issues:
- Check environment variables are properly set
- Verify JSON credentials are formatted as single line
-
Network Connectivity:
- Ensure BigQuery API is enabled
- Check Azure Container Apps networking configuration
๐งช Development
Running Tests
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run with coverage
pytest --cov=mcp_bigquery_server
Code Quality
# Format code
black src/
# Lint code
flake8 src/
# Type checking
mypy src/
Local Development (HTTP)
python src/mcp_bigquery_server/main.py --project-id <project_id> --http --port 8000
๐ Documentation
- API documentation:
/docs
- OpenAPI specification (3.0.4 YAML):
/openapi.yaml
- Model Context Protocol: MCP Specification
- Google BigQuery API: BigQuery Documentation
- Azure Container Apps: Container Apps Documentation
๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the file for details.
๐ Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: See
/docs
endpoint when running the server