create_branch
Creates a new branch in a GitHub repository, specifying the owner, repository name, and desired branch, with an option to base it on an existing branch.
Instructions
Create a new branch in a GitHub repository
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | Yes | Name for the new branch | |
| from_branch | No | Optional: source branch to create from (defaults to the repository's default branch) | |
| owner | Yes | Repository owner (username or organization) | |
| repo | Yes | Repository name |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"branch": {
"description": "Name for the new branch",
"type": "string"
},
"from_branch": {
"description": "Optional: source branch to create from (defaults to the repository's default branch)",
"type": "string"
},
"owner": {
"description": "Repository owner (username or organization)",
"type": "string"
},
"repo": {
"description": "Repository name",
"type": "string"
}
},
"required": [
"owner",
"repo",
"branch"
],
"type": "object"
}
Implementation Reference
- index.ts:177-188 (handler)Main handler for the 'create_branch' tool in the MCP server. Parses input arguments using CreateBranchSchema and delegates to branches.createBranchFromRef to perform the branch creation.case "create_branch": { const args = branches.CreateBranchSchema.parse(request.params.arguments); const branch = await branches.createBranchFromRef( args.owner, args.repo, args.branch, args.from_branch ); return { content: [{ type: "text", text: JSON.stringify(branch, null, 2) }], }; }
- operations/branches.ts:11-16 (schema)Zod schema for validating input parameters of the create_branch tool: owner, repo, branch, and optional from_branch.export const CreateBranchSchema = z.object({ owner: z.string().describe("Repository owner (username or organization)"), repo: z.string().describe("Repository name"), branch: z.string().describe("Name for the new branch"), from_branch: z.string().optional().describe("Optional: source branch to create from (defaults to the repository's default branch)"), });
- index.ts:113-117 (registration)Tool registration in the ListTools response, defining name, description, and input schema for 'create_branch'.{ name: "create_branch", description: "Create a new branch in a GitHub repository", inputSchema: zodToJsonSchema(branches.CreateBranchSchema), },
- operations/branches.ts:75-92 (helper)Core helper function called by the handler to create a branch from a reference (either specified from_branch or default branch), resolving SHA and delegating to createBranch.export async function createBranchFromRef( owner: string, repo: string, newBranch: string, fromBranch?: string ): Promise<z.infer<typeof GitHubReferenceSchema>> { let sha: string; if (fromBranch) { sha = await getBranchSHA(owner, repo, fromBranch); } else { sha = await getDefaultBranchSHA(owner, repo); } return createBranch(owner, repo, { ref: newBranch, sha, }); }
- operations/branches.ts:41-60 (helper)Low-level helper that performs the actual GitHub API POST request to create a git ref (branch).export async function createBranch( owner: string, repo: string, options: CreateBranchOptions ): Promise<z.infer<typeof GitHubReferenceSchema>> { const fullRef = `refs/heads/${options.ref}`; const response = await githubRequest( `https://api.github.com/repos/${owner}/${repo}/git/refs`, { method: "POST", body: { ref: fullRef, sha: options.sha, }, } ); return GitHubReferenceSchema.parse(response); }