Bazel MCP server

Bridges AI with Bazel build system, enabling target building, dependency querying, test running, and workspace management even in environments with misconfigured shells.
Back to servers
Provider
nacgarg
Release date
Mar 07, 2025
Language
TypeScript
Stats
3 stars

This MCP server enables AI agents to interact with the Bazel build system through the Model Context Protocol, providing a seamless interface when traditional command-line tools are unavailable or when shell environments are misconfigured.

Installation Options

Using with Cursor

Add the following to your .cursor/mcp.json file:

{
  "mcpServers": {
    "bazel": {
      "command": "npx",
      "args": [
        "-y",
        "github:nacgarg/bazel-mcp-server",
        
        // Optional: specify the bazel binary path
        "--bazel_path", 
        "/absolute/path/to/your/bazel/binary",
        
        // Optional: specify the workspace path
        "--workspace_path",
        "/absolute/path/to/your/bazel/workspace"
      ]
    }
  }
}

Note: This configuration won't work with Cursor in Remote SSH sessions as the MCP server runs locally.

Using with Claude Desktop

The same configuration used for Cursor works with Claude Desktop.

Running Standalone

# Run directly from GitHub (no installation needed)
npx -y github:nacgarg/bazel-mcp-server

# From source
git clone https://github.com/nacgarg/bazel-mcp-server.git
cd bazel-mcp-server
npm install
npm run build
dist/index.js

Configuration

The MCP server can be configured using several methods, applied in this order:

  1. Command line arguments
  2. Environment variables
  3. Configuration file

Configuration Options

CLI Argument Environment Variable Config File Key Description
--bazel_path MCP_BAZEL_PATH bazel_path Path to Bazel binary
--workspace_path MCP_WORKSPACE_PATH workspace_path Path to Bazel workspace
--workspace_config MCP_WORKSPACE_CONFIG workspace_config Workspace configuration (defaults to .bazelrc)
--log_path MCP_LOG_PATH log_path Path for server logs

Available Tools

The Bazel MCP Server provides these tools:

  • bazel_build_target: Build specified Bazel targets
  • bazel_query_target: Query dependency graph for targets matching a pattern
  • bazel_test_target: Run tests for specified targets
  • bazel_list_targets: List all available targets in the workspace (requires path parameter, use "//" for all targets)
  • bazel_fetch_dependencies: Fetch external dependencies
  • bazel_set_workspace_path: Change the Bazel workspace path at runtime

Usage Examples

Building Targets

bazel_build_target //path/to:target

Running Tests

bazel_test_target //path/to:test_target --additionalArgs="--test_output=all"

Querying Dependencies

bazel_query_target "deps(//path/to:target)"

Listing All Targets

bazel_list_targets "//"

Setting Workspace Path

bazel_set_workspace_path /path/to/workspace

Using Additional Arguments

Most commands support an optional additionalArgs parameter for passing extra flags:

bazel_build_target //path/to:target --additionalArgs="--verbose_failures --keep_going"

Debugging

For verbose logging to the console:

DEBUG=true npx github:nacgarg/bazel-mcp-server

Specify a log path for clients that don't print logs to console:

npx github:nacgarg/bazel-mcp-server --log_path=/path/to/logs.txt

How to add this MCP server to Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later