create_branch
Create a new branch in a GitLab project by specifying the branch name, project ID, and reference. Integrates with the GitLab MCP Server for efficient project management.
Instructions
Create a new branch in a GitLab project
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | No | ||
| project_id | No | ||
| ref | No |
Input Schema (JSON Schema)
{
"properties": {
"branch": {
"type": "string"
},
"project_id": {
"type": "string"
},
"ref": {
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/index.ts:331-344 (handler)Main MCP tool handler for 'create_branch': parses input schema, fetches default branch ref if not provided, calls GitLab API via gitlabApi.createBranch, and returns the branch details as JSON.case "create_branch": { const args = CreateBranchSchema.parse(request.params.arguments); let ref = args.ref; if (!ref) { ref = await gitlabApi.getDefaultBranchRef(args.project_id); } const branch = await gitlabApi.createBranch(args.project_id, { name: args.branch, ref }); return { content: [{ type: "text", text: JSON.stringify(branch, null, 2) }] }; }
- src/index.ts:156-161 (registration)Tool registration in ALL_TOOLS array, defining name, description, input schema, and readOnly flag.{ name: "create_branch", description: "Create a new branch in a GitLab project", inputSchema: createJsonSchema(CreateBranchSchema), readOnly: false },
- src/schemas.ts:435-439 (schema)Zod schema for 'create_branch' tool input validation: requires project_id and branch, optional ref.export const CreateBranchSchema = z.object({ project_id: z.string(), branch: z.string(), ref: z.string().optional() });
- src/gitlab-api.ts:119-146 (helper)GitLabApi helper method that performs the actual POST request to create a branch, handles response parsing and error throwing.async createBranch( projectId: string, options: z.infer<typeof CreateBranchOptionsSchema> ): Promise<GitLabReference> { const response = await fetch( `${this.apiUrl}/projects/${encodeURIComponent(projectId)}/repository/branches`, { method: "POST", headers: { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" }, body: JSON.stringify({ branch: options.name, ref: options.ref }) } ); if (!response.ok) { throw new McpError( ErrorCode.InternalError, `GitLab API error: ${response.statusText}` ); } return GitLabReferenceSchema.parse(await response.json()); }