Skip to main content
Glama
pdogra1299
by pdogra1299

delete_branch

Remove specific branches from Bitbucket repositories using workspace, repository, and branch name parameters. Optional force delete for unmerged branches.

Instructions

Delete a branch

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
branch_nameYesBranch name to delete
forceNoForce delete even if branch is not merged (optional, default: false)
repositoryYesRepository slug (e.g., "my-repo")
workspaceYesBitbucket workspace/project key (e.g., "PROJ")

Implementation Reference

  • The main handler function that executes the delete_branch tool logic, validating args with isDeleteBranchArgs, making appropriate Bitbucket API DELETE requests for Server (using branch-utils with endPoint) and Cloud, handling 204 responses, and returning success message or error.
    async handleDeleteBranch(args: any) {
      if (!isDeleteBranchArgs(args)) {
        throw new McpError(
          ErrorCode.InvalidParams,
          'Invalid arguments for delete_branch'
        );
      }
    
      const { workspace, repository, branch_name, force } = args;
    
      try {
        let apiPath: string;
    
        if (this.apiClient.getIsServer()) {
          // First, we need to get the branch details to find the latest commit
          const branchesPath = `/rest/api/latest/projects/${workspace}/repos/${repository}/branches`;
          const branchesResponse = await this.apiClient.makeRequest<any>('get', branchesPath, undefined, {
            params: {
              filterText: branch_name,
              limit: 100
            }
          });
          
          // Find the exact branch
          const branch = branchesResponse.values?.find((b: any) => b.displayId === branch_name);
          if (!branch) {
            throw new Error(`Branch '${branch_name}' not found`);
          }
          
          // Now delete using branch-utils endpoint with correct format
          apiPath = `/rest/branch-utils/latest/projects/${workspace}/repos/${repository}/branches`;
          
          try {
            await this.apiClient.makeRequest<any>('delete', apiPath, {
              name: branch_name,
              endPoint: branch.latestCommit
            });
          } catch (deleteError: any) {
            // If the error is about empty response but status is 204 (No Content), it's successful
            if (deleteError.originalError?.response?.status === 204 || 
                deleteError.message?.includes('No content to map')) {
              // Branch was deleted successfully
            } else {
              throw deleteError;
            }
          }
        } else {
          // Bitbucket Cloud API
          apiPath = `/repositories/${workspace}/${repository}/refs/branches/${branch_name}`;
          try {
            await this.apiClient.makeRequest<any>('delete', apiPath);
          } catch (deleteError: any) {
            // If the error is about empty response but status is 204 (No Content), it's successful
            if (deleteError.originalError?.response?.status === 204 || 
                deleteError.message?.includes('No content to map')) {
              // Branch was deleted successfully
            } else {
              throw deleteError;
            }
          }
        }
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                message: `Branch '${branch_name}' deleted successfully`,
                branch: branch_name,
                repository: `${workspace}/${repository}`
              }, null, 2),
            },
          ],
        };
      } catch (error) {
        return this.apiClient.handleApiError(error, `deleting branch '${branch_name}' in ${workspace}/${repository}`);
      }
    }
  • Type guard validating input arguments for delete_branch tool: requires workspace, repository, branch_name; optional force boolean.
    export const isDeleteBranchArgs = (
      args: any
    ): args is {
      workspace: string;
      repository: string;
      branch_name: string;
      force?: boolean;
    } =>
      typeof args === 'object' &&
      args !== null &&
      typeof args.workspace === 'string' &&
      typeof args.repository === 'string' &&
      typeof args.branch_name === 'string' &&
      (args.force === undefined || typeof args.force === 'boolean');
  • JSON schema definition for the delete_branch tool, including name, description, and inputSchema used for MCP tool listing and validation.
      name: 'delete_branch',
      description: 'Delete a branch',
      inputSchema: {
        type: 'object',
        properties: {
          workspace: {
            type: 'string',
            description: 'Bitbucket workspace/project key (e.g., "PROJ")',
          },
          repository: {
            type: 'string',
            description: 'Repository slug (e.g., "my-repo")',
          },
          branch_name: {
            type: 'string',
            description: 'Branch name to delete',
          },
          force: {
            type: 'boolean',
            description: 'Force delete even if branch is not merged (optional, default: false)',
          },
        },
        required: ['workspace', 'repository', 'branch_name'],
      },
    },
  • src/index.ts:114-115 (registration)
    Dispatches calls to the delete_branch tool to the BranchHandlers.handleDeleteBranch method in the main tool request handler switch statement.
    case 'delete_branch':
      return this.branchHandlers.handleDeleteBranch(request.params.arguments);

Latest Blog Posts

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/pdogra1299/bitbucket-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server