create-branch
Create a new branch in a GitHub repository by specifying the owner, repository name, and branch name, with optional source branch selection.
Instructions
Create a new branch in a GitHub repository
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | Yes | Name for the new branch | |
| from_branch | No | Source branch to create from (defaults to the repository's default branch) | |
| owner | Yes | Repository owner | |
| repo | Yes | Repository name |
Implementation Reference
- src/tools/repository.ts:187-239 (handler)The core handler function that executes the create-branch tool. Validates inputs using CreateBranchSchema, checks if the branch exists, determines the source branch, fetches its SHA, and creates the new branch ref via GitHub API.export async function createBranch(args: unknown): Promise<any> { const { owner, repo, branch, from_branch } = CreateBranchSchema.parse(args); const github = getGitHubApi(); return tryCatchAsync(async () => { // Check if branch already exists try { await github.getOctokit().git.getRef({ owner, repo, ref: `heads/${branch}`, }); return { success: false, message: `Branch '${branch}' already exists in ${owner}/${repo}`, }; } catch (error: any) { // If error is not 404 (not found), rethrow it if (error.status !== 404) { throw error; } } // Get the SHA of the latest commit on the source branch const sourceBranch = from_branch || await github.getDefaultBranch(owner, repo); const { data: refData } = await github.getOctokit().git.getRef({ owner, repo, ref: `heads/${sourceBranch}`, }); // Create a new branch from the source branch const { data } = await github.getOctokit().git.createRef({ owner, repo, ref: `refs/heads/${branch}`, sha: refData.object.sha, }); return { success: true, ref: data.ref, url: data.url, object: { sha: data.object.sha, type: data.object.type, url: data.object.url, }, message: `Branch '${branch}' created from '${sourceBranch}' in ${owner}/${repo}`, }; }, 'Failed to create branch'); }
- src/utils/validation.ts:71-74 (schema)Zod validation schema used in the handler to parse and validate the input arguments for create-branch.export const CreateBranchSchema = OwnerRepoSchema.extend({ branch: z.string().min(1, 'Branch name is required'), from_branch: z.string().optional(), });
- src/server.ts:1175-1177 (registration)Switch case in the MCP tool call handler that routes 'create-branch' invocations to the createBranch function.case 'create-branch': result = await createBranch(parsedArgs); break;
- src/server.ts:222-248 (schema)Input schema definition for the create-branch tool in the MCP server's listTools response.{ name: 'create-branch', description: 'Create a new branch in a GitHub repository', inputSchema: { type: 'object', properties: { owner: { type: 'string', description: 'Repository owner', }, repo: { type: 'string', description: 'Repository name', }, branch: { type: 'string', description: 'Name for the new branch', }, from_branch: { type: 'string', description: 'Source branch to create from (defaults to the repository\'s default branch)', }, }, required: ['owner', 'repo', 'branch'], additionalProperties: false, }, },