repo_get_branch_by_name
Retrieve a specific branch from an Azure DevOps repository by providing the repository ID and branch name. Simplifies branch management using PAT authentication.
Instructions
Get a branch by its name.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branchName | Yes | The name of the branch to retrieve, e.g., 'main' or 'feature-branch'. | |
| repositoryId | Yes | The ID of the repository where the branch is located. |
Implementation Reference
- src/tools/repos.ts:554-572 (handler)Handler function that retrieves all branches (refs) from the specified repository using the Azure DevOps Git API, finds the one matching the provided branch name (prefixed with 'refs/heads/'), and returns the full branch details or a not-found message.async ({ repositoryId, branchName }) => { const connection = await connectionProvider(); const gitApi = await connection.getGitApi(); const branches = await gitApi.getRefs(repositoryId); const branch = branches?.find((branch) => branch.name === `refs/heads/${branchName}`); if (!branch) { return { content: [ { type: "text", text: `Branch ${branchName} not found in repository ${repositoryId}`, }, ], }; } return { content: [{ type: "text", text: JSON.stringify(branch, null, 2) }], }; }
- src/tools/repos.ts:550-553 (schema)Zod input schema defining the required parameters: repositoryId (string, ID of the repo) and branchName (string, name like 'main').{ repositoryId: z.string().describe("The ID of the repository where the branch is located."), branchName: z.string().describe("The name of the branch to retrieve, e.g., 'main' or 'feature-branch'."), },
- src/tools/repos.ts:547-573 (registration)Registration of the tool 'repo_get_branch_by_name' using McpServer.tool(), referencing REPO_TOOLS.get_branch_by_name, with description, input schema, and inline handler function.server.tool( REPO_TOOLS.get_branch_by_name, "Get a branch by its name.", { repositoryId: z.string().describe("The ID of the repository where the branch is located."), branchName: z.string().describe("The name of the branch to retrieve, e.g., 'main' or 'feature-branch'."), }, async ({ repositoryId, branchName }) => { const connection = await connectionProvider(); const gitApi = await connection.getGitApi(); const branches = await gitApi.getRefs(repositoryId); const branch = branches?.find((branch) => branch.name === `refs/heads/${branchName}`); if (!branch) { return { content: [ { type: "text", text: `Branch ${branchName} not found in repository ${repositoryId}`, }, ], }; } return { content: [{ type: "text", text: JSON.stringify(branch, null, 2) }], }; } );
- src/tools/repos.ts:25-44 (registration)REPO_TOOLS constant object that maps the internal handler identifier 'get_branch_by_name' to the tool name 'repo_get_branch_by_name' used in registration.const REPO_TOOLS = { list_repos_by_project: "repo_list_repos_by_project", list_pull_requests_by_repo: "repo_list_pull_requests_by_repo", list_pull_requests_by_project: "repo_list_pull_requests_by_project", list_branches_by_repo: "repo_list_branches_by_repo", list_my_branches_by_repo: "repo_list_my_branches_by_repo", list_pull_request_threads: "repo_list_pull_request_threads", list_pull_request_thread_comments: "repo_list_pull_request_thread_comments", get_repo_by_name_or_id: "repo_get_repo_by_name_or_id", get_branch_by_name: "repo_get_branch_by_name", get_pull_request_by_id: "repo_get_pull_request_by_id", create_pull_request: "repo_create_pull_request", update_pull_request: "repo_update_pull_request", update_pull_request_reviewers: "repo_update_pull_request_reviewers", reply_to_comment: "repo_reply_to_comment", create_pull_request_thread: "repo_create_pull_request_thread", resolve_comment: "repo_resolve_comment", search_commits: "repo_search_commits", list_pull_requests_by_commits: "repo_list_pull_requests_by_commits", };