MCP server exposing Bazel build/run/test/query as tools
Project description
bazel-mcp
Integrate Bazel with AI coding assistants via the Model Context Protocol
bazel-mcp is a Model Context Protocol server that exposes your Bazel workspace to AI assistants like Claude (in VS Code or Cursor). It enables AI assistants to understand and work with Bazel projects by providing direct access to build, test, run, and query capabilities.
Why bazel-mcp?
- 🤖 AI-Native Development: Let AI assistants build, test, and run Bazel targets directly
- 📦 Zero Configuration: Automatically discovers all Bazel targets in your workspace
- 🔄 Real-time Streaming: See build and test output as it happens
- 🎯 Smart Context: AI understands your build graph and dependencies
- ⚡ Fast Iteration: No context switching between AI chat and terminal
Installation
From PyPI (Recommended)
pip install bazel-mcp
From Source
git clone https://github.com/sandeepnmenon/py_bazel_mcp
cd py_bazel_mcp
pip install -e .
Quick Start
1. Configure Your Editor
VS Code (Claude Extension)
Create .vscode/mcp.json in your workspace:
{
"mcpServers": {
"bazel-mcp": {
"command": "bazel-mcp",
"args": ["--repo", "${workspaceFolder}"]
}
}
}
Cursor
Add as a custom MCP server:
- Settings → MCP Servers → Add Custom Server
- Command:
bazel-mcp - Args:
--repo /path/to/your/bazel/workspace
2. Verify Setup
Ask your AI assistant:
"Can you list all Bazel targets in this workspace?"
The assistant should use the bazel_list_targets tool and show your project's targets.
Features
🛠️ Available Tools
| Tool | Description |
|---|---|
bazel_list_targets |
List all discovered targets grouped by type |
bazel_query |
Run arbitrary Bazel query expressions |
bazel_build |
Build one or more targets with streaming output |
bazel_run |
Run a binary target with arguments |
bazel_test |
Run tests with optional filters |
repo_setup |
Run project setup scripts |
📚 Resources
bazel://targets: Returns a JSON snapshot of all discovered targets in your workspace
Usage Examples
Building and Testing
"Build the server binary"
# AI will use:
bazel_build(targets=["//apps:server"])
"Run all tests matching 'Vector' in the math library"
# AI will use:
bazel_test(
targets=["//lib/math:all"],
flags=["--test_filter=Vector.*"]
)
Dependency Analysis
"What depends on the database library?"
# AI will use:
bazel_query(expr="rdeps(//..., //lib:database)")
"Show me the build graph for the main app"
# AI will use:
bazel_query(expr="deps(//main:app)")
Running Binaries
"Run the CLI tool with port 8080"
# AI will use:
bazel_run(
target="//tools:cli",
args=["--port=8080"]
)
API Documentation
Tool Reference
bazel_list_targets - List all discovered targets
Parameters:
refresh(bool, optional): Force re-discovery of targets
Returns: JSON with targets grouped by kind (cc_library, cc_binary, py_library, py_binary, cc_test, py_test)
bazel_query - Run Bazel query expressions
Parameters:
expr(str, required): Bazel query expressionflags(list, optional): Additional Bazel flags
Example:
bazel_query(expr="deps(//main:app)")
bazel_query(expr="kind('cc_test', //...)", flags=["--output=label_kind"])
bazel_build - Build targets
Parameters:
targets(list, required): Target labels to buildflags(list, optional): Bazel build flags
Example:
bazel_build(targets=["//src:lib", "//apps:server"])
bazel_build(targets=["//..."], flags=["--config=debug"])
bazel_run - Run a binary target
Parameters:
target(str, required): Binary target labelargs(list, optional): Arguments for the binaryflags(list, optional): Bazel run flags
Example:
bazel_run(target="//tools:cli", args=["--port=8080"])
bazel_test - Run tests
Parameters:
targets(list, optional): Test targets (default://...)flags(list, optional): Test flags
Example:
bazel_test(targets=["//tests:unit_tests"])
bazel_test(targets=["//..."], flags=["--test_filter=MyTest.*"])
repo_setup - Run setup scripts
Parameters:
skipInstall(bool, optional): Skip install scripts
Runs (if present):
./tools/setup_cache.sh./install/install_all.sh
Advanced Configuration
Environment Variables
| Variable | Description | Default |
|---|---|---|
BAZEL_PATH |
Path to Bazel executable | bazel |
BAZELISK |
Use Bazelisk if available | auto-detect |
PYTHONPATH |
Python module search path | Required for source installs |
Using Bazelisk (Recommended)
Bazelisk automatically manages Bazel versions:
# Install Bazelisk
wget -O bazelisk https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64
chmod +x bazelisk
sudo mv bazelisk /usr/local/bin/bazel
# bazel-mcp will automatically detect and use it
Troubleshooting
| Issue | Solution |
|---|---|
| "No module named 'mcp'" | Install with pip install bazel-mcp |
| "bazel query failed" | Ensure you're in a Bazel workspace (has WORKSPACE or MODULE.bazel) |
| Editor not detecting server | Restart editor after configuration |
| Logs not streaming | Add --verbose_failures flag to see detailed output |
Development
Setting up for Development
git clone https://github.com/sandeepnmenon/py_bazel_mcp
cd py_bazel_mcp
python -m venv .venv
source .venv/bin/activate
pip install -e .
Running Tests
pytest tests/
Project Structure
py_bazel_mcp/
├── src/bazel_mcp/
│ ├── __init__.py
│ ├── server.py # MCP server implementation
│ ├── bazel.py # Bazel command wrappers
│ ├── targets.py # Target discovery & caching
│ └── util.py # Utility functions
├── tests/ # Unit tests
├── pyproject.toml # Package configuration
└── README.md # Documentation
Contributing
We welcome contributions! Please see our Contributing Guide for details.
How to Contribute
- 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
Reporting Issues
Please report issues on our GitHub Issues page. Include:
- Your environment (OS, Python version, Bazel version)
- Steps to reproduce
- Expected vs actual behavior
License
MIT License - see LICENSE file for details.
Acknowledgments
- Model Context Protocol - The protocol that makes this possible
- Bazel - The build system we're integrating with
- The MCP community for inspiration and examples
Links
- Documentation: GitHub Wiki
- PyPI Package: bazel-mcp
- Issue Tracker: GitHub Issues
- Discussions: GitHub Discussions
Built with ❤️ for developers using Bazel and AI assistants
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bazel_mcp-1.0.2.tar.gz.
File metadata
- Download URL: bazel_mcp-1.0.2.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa91327f9a4c9233046928348873f8cb796da0f83300f806f0813563887b4cb8
|
|
| MD5 |
d4566c14abb66dc3e7c6c8cd519fffcd
|
|
| BLAKE2b-256 |
9e76f26892798fa2606d5aec573680b7474172825329acba6e5c6ea6bc06f514
|
Provenance
The following attestation bundles were made for bazel_mcp-1.0.2.tar.gz:
Publisher:
workflow.yml on sandeepnmenon/py_bazel_mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bazel_mcp-1.0.2.tar.gz -
Subject digest:
fa91327f9a4c9233046928348873f8cb796da0f83300f806f0813563887b4cb8 - Sigstore transparency entry: 583719215
- Sigstore integration time:
-
Permalink:
sandeepnmenon/py_bazel_mcp@648377c3f419fadf606be7a6cdf938192cf7f23e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/sandeepnmenon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@648377c3f419fadf606be7a6cdf938192cf7f23e -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file bazel_mcp-1.0.2-py3-none-any.whl.
File metadata
- Download URL: bazel_mcp-1.0.2-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f40f55f230a81aeaa21f0c59cbd3c55d5d8bfa54e6e31986b0cb8120bbdb7430
|
|
| MD5 |
96d350c5e126ad9e6fc35f73a6adaedf
|
|
| BLAKE2b-256 |
490a098f17e9aae82d507affe9d319f4cb45424d84cba5f443516e6eba0cf929
|
Provenance
The following attestation bundles were made for bazel_mcp-1.0.2-py3-none-any.whl:
Publisher:
workflow.yml on sandeepnmenon/py_bazel_mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bazel_mcp-1.0.2-py3-none-any.whl -
Subject digest:
f40f55f230a81aeaa21f0c59cbd3c55d5d8bfa54e6e31986b0cb8120bbdb7430 - Sigstore transparency entry: 583719216
- Sigstore integration time:
-
Permalink:
sandeepnmenon/py_bazel_mcp@648377c3f419fadf606be7a6cdf938192cf7f23e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/sandeepnmenon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@648377c3f419fadf606be7a6cdf938192cf7f23e -
Trigger Event:
workflow_dispatch
-
Statement type: