Bitbucket Cloud MCP
by Mrsmithz
README.md
# Bitbucket Cloud MCP
A Rust MCP server for Bitbucket Cloud repositories, pull requests, and snippets over stdio. The optional Pi extension asks for fresh consent before approving a pull request or withdrawing an approval.
## Requirements
- Rust 1.88 or later with Rust edition 2024 support.
- A Bitbucket Cloud account, an API token, and a workspace. Bitbucket Server and Data Center are not supported.
- Node.js and npm only if you use or test the [Pi consent extension](#pi-consent-extension).
Grant the API token only the scopes you need:
| Scope | Use |
| --- | --- |
| `read:repository:bitbucket` | Discover repositories and read diffs. |
| `read:pullrequest:bitbucket` | Read pull requests and review activity. |
| `write:pullrequest:bitbucket` | Create pull requests, post comments, and manage your approvals. |
| `read:snippet:bitbucket` | List and read snippets. Also required for snippet writes. |
| `write:snippet:bitbucket` | Create and update snippets. Does not grant read access. |
## Setup
### 1. Build the server
```sh
cargo build --locked --release
```
### 2. Set the server environment
Use your secret manager or MCP host to supply credentials. See [`.env.example`](.env.example) for reference. The server does not load dotenv files. Never commit credentials or put a real token in a shell command.
| Variable | Required | Value |
| --- | --- | --- |
| `BITBUCKET_EMAIL` | Yes | Email address for the token owner. |
| `BITBUCKET_API_TOKEN` | Yes | Bitbucket Cloud API token. |
| `BITBUCKET_WORKSPACE` | Yes | Workspace slug. All tools use this workspace; no tool accepts a workspace override. |
| `BITBUCKET_BASE_URL` | No | Defaults to `https://api.bitbucket.org/2.0`. A trusted Cloud-compatible proxy can use a path prefix. |
| `BITBUCKET_TLS_VERIFY` | No | Defaults to `true`. Set to `false` only for a trusted test or proxy whose CA you cannot install. |
The server accepts HTTPS and allows HTTP only on loopback for local tests. The base URL cannot contain credentials, a query string, or a fragment. Disabling TLS verification applies to every request and prints a warning to stderr. Do not use it to bypass certificate errors against Bitbucket Cloud.
### 3. Configure your MCP client
Point the client at the built `target/release/mcp-bitbucket` binary and use stdio. Adapt the environment variable syntax to your client:
```json
{
"mcpServers": {
"bitbucket": {
"command": "/absolute/path/to/mcp-bitbucket",
"env": {
"BITBUCKET_EMAIL": "${BITBUCKET_EMAIL}",
"BITBUCKET_API_TOKEN": "${BITBUCKET_API_TOKEN}",
"BITBUCKET_WORKSPACE": "${BITBUCKET_WORKSPACE}"
}
}
}
}
```
## Tools
### Repositories
| Tool | Description |
| --- | --- |
| `search_repositories` | Find repositories whose name or slug contains a literal, case-insensitive substring. |
### Pull requests
| Tool | Description |
| --- | --- |
| `list_pull_requests` | List pull requests in a repository. |
| `get_pull_request` | Read a pull request, including its description. |
| `get_pull_request_diff` | Read the bounded pull-request diff. |
| `list_pull_request_files` | List changed paths, statuses, and available line counts. Returns up to 50 files per page. |
| `get_pull_request_file_diff` | Read Bitbucket's patch for one repository-relative path. Returns up to 500 KB and reports truncation. |
| `create_pull_request` | Create a non-draft pull request between two explicit branches in the same repository. Optional reviewers use Bitbucket UUIDs. |
### Review
| Tool | Description |
| --- | --- |
| `list_pull_request_comments` | List comments on a pull request. |
| `create_pull_request_comment` | Post a general comment, reply to a comment, or comment on one line of the current diff. |
| `approve_pull_request` | Approve a pull request as the token owner. |
| `withdraw_pull_request_approval` | Withdraw the token owner's approval. |
| `get_pull_request_approvals` | List current approvers and count their approvals. Does not check whether the pull request can merge. |
### Snippets
| Tool | Description |
| --- | --- |
| `list_snippets` | List visible snippets. Optionally filter by `role`: `owner`, `contributor`, or `member`. |
| `get_snippet` | Read a snippet's title, privacy, and filenames. Returns the current revision when the file links agree on it. |
| `get_snippet_file` | Read a UTF-8 file by exact relative path, including nested directories. Reads the current revision by default or accepts a full commit SHA. |
| `create_snippet` | Create a private snippet with a title and one or more text files. Returns only `{ "id": "…" }`. |
| `update_snippet` | Change the title or add or replace files in a private snippet. File changes require the current revision. |
Use the opaque snippet ID, not the numeric API ID or a URL. Reads reject partial, oversized, and non-text file responses. A file response can be at most 1,000,000 bytes. Snippet requests do not follow redirects.
Titles must be nonblank and at most 500 bytes. Uploads accept up to 10 files with unique basenames, up to 256,000 bytes each and 500,000 bytes total. Omitted files stay unchanged. The tools do not delete snippets, remove or rename files, or change visibility.
The server checks privacy before every update, but Bitbucket does not offer a way to make that check atomic with the write. Public snippets and snippets whose privacy cannot be confirmed are not editable. Title-only updates do not require a revision.
### Pagination
List tools return `values`, `next_page`, and `truncated`. Pass `next_page` back as `page` on the same tool. Keep the original filters and page size when the tool accepts them. Stop when there is no next page, not when `values` is empty.
Repository search accepts `query`, `page`, and `pagelen`. It reads one upstream page per call. Reuse its cursor with the same query and page size.
Omit `role` on the first snippet page to list all snippets visible to the token owner. See [Bitbucket's snippet role definitions](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-snippets/#api-snippets-workspace-get) for filtered listings. To continue, omit `role` or repeat the original value.
Repository and snippet cursors belong to the running server. Restart the listing if the server restarted or evicted the cursor.
### Diffs and inline comments
For a large pull request, list changed files first with `{"repo_slug":"demo","pull_request_id":42,"pagelen":25}`. Then read one patch with `{"repo_slug":"demo","pull_request_id":42,"path":"src/lib.rs"}`. Bitbucket controls filtering for renamed files and may not match both names. Fetch the patch again if the pull request changes between requests.
Inline comments require a path and a line on the selected side of the current diff. The server rejects placement when the whole diff is truncated, the format does not support placement, or the selected line is absent. Renames and binary diffs can still be read as bounded raw diffs. Bitbucket can change the diff between validation and posting, so validation does not guarantee atomic placement.
### Write errors
The server never redirects or automatically retries writes. `outcome_unknown` means Bitbucket might have applied the write. Check Bitbucket before retrying. `upstream_rejected` reports a known upstream rejection.
The server bounds response bodies as it reads them, omits upstream error bodies, and redacts known token and Basic-auth echoes in successful string fields. Where the server follows GET redirects, it accepts at most three hops on the same origin and within the configured API path prefix.
## Pi consent extension
1. Run `npm ci` to install the pinned dependencies. Configure Pi `1.5.0` to use `pi-mcp-adapter` `7.0.1`. Pi's built-in MCP integration does not use this broker.
2. Trust [`.pi/extensions/bitbucket-consent.ts`](.pi/extensions/bitbucket-consent.ts). Add the MCP configuration to `.pi/mcp.json` and run `/reload` in Pi. If you copy the extension to another project, make `pi-mcp-adapter` available to its module resolver.
The extension prompts for **Allow once** on every `approve_pull_request` and `withdraw_pull_request_approval`, even when the adapter has a session grant. It shows the action, server, repository, and pull-request ID. The broker does not supply the server workspace. Invalid requests, cancellation, UI errors, headless sessions, and session changes deny the request.
The extra prompt applies only to approval and withdrawal through the Pi adapter. It does not cover direct MCP calls, shell commands, or other code with access to the token. The Rust server acts as the token owner and cannot infer human consent.
## Verification
### Rust server
```sh
cargo fmt --check
cargo clippy --locked --all-targets -- -D warnings
cargo test --locked
```
### Local stdio and HTTP contracts
Use Python 3 in a POSIX environment. The repository contract check also requires OpenSSL for its self-signed TLS fixture.
```sh
cargo build --locked
python3 scripts/test_smoke_regressions.py
python3 scripts/smoke_stdio.py target/debug/mcp-bitbucket
python3 scripts/check_file_diff_contract.py target/debug/mcp-bitbucket
python3 scripts/check_repository_search_contract.py target/debug/mcp-bitbucket
```
The stdio smoke calls every tool on the built server against a local Bitbucket mock. The other checks cover file filters, pagination, streaming limits, repository queries, and TLS verification. None require a Bitbucket account. The snippet tools have not been tested against a live Cloud workspace. The per-file tools also passed a read-only Bitbucket Cloud check for changed-file listing, a selected patch, and an empty result for a nonexistent path.
### Pi extension
```sh
npm ci --ignore-scripts
npm run typecheck
npm test
PATH="$PWD/node_modules/.bin:$PATH" python3 scripts/smoke_pi_consent.py
```
The Node tests exercise the real adapter broker and permission caches. The Pi smoke drives the consent dialog offline without model requests or account credentials.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessOutdated data