Skip to main content
Glama

create_branch

Generate a new branch in a GitHub repository by specifying the owner, repository name, and branch name, with an option to base it on an existing branch.

Instructions

Create a new branch in a GitHub repository

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
branchYesName for the new branch
from_branchNoOptional: source branch to create from (defaults to the repository's default branch)
ownerYesRepository owner (username or organization)
repoYesRepository name

Implementation Reference

  • MCP CallToolRequest handler for 'create_branch': validates input schema, calls createBranchFromRef, and returns JSON response.
    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) }],
      };
    }
  • Zod input schema definition for the create_branch tool, used for validation in handler and JSON schema conversion.
    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:111-113 (registration)
    Tool registration in the ListTools response, providing name, description, and input schema.
    name: "create_branch",
    description: "Create a new branch in a GitHub repository",
    inputSchema: zodToJsonSchema(branches.CreateBranchSchema),
  • Primary handler logic: determines source SHA (from specified branch or default) and invokes 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,
      });
    }
  • Core utility: Makes POST request to GitHub Git Refs API to create the new branch reference.
    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);
    }

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/tuanle96/mcp-github'

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