create_or_update_file
Create or update a file in a GitLab project by specifying the branch, file path, content, and commit message. Use for streamlined file management and version control.
Instructions
Create or update a single file in a GitLab project
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | No | ||
| commit_message | No | ||
| content | No | ||
| file_path | No | ||
| previous_path | No | ||
| project_id | No |
Input Schema (JSON Schema)
{
"properties": {
"branch": {
"type": "string"
},
"commit_message": {
"type": "string"
},
"content": {
"type": "string"
},
"file_path": {
"type": "string"
},
"previous_path": {
"type": "string"
},
"project_id": {
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/gitlab-api.ts:227-277 (handler)Core handler function that implements the create_or_update_file logic by calling GitLab repository/files API (POST/PUT based on existence).async createOrUpdateFile( projectId: string, filePath: string, content: string, commitMessage: string, branch: string, previousPath?: string ): Promise<GitLabCreateUpdateFileResponse> { const encodedPath = encodeURIComponent(filePath); const url = `${this.apiUrl}/projects/${encodeURIComponent(projectId)}/repository/files/${encodedPath}`; const body = { branch, content, commit_message: commitMessage, ...(previousPath ? { previous_path: previousPath } : {}) }; // Check if file exists let method = "POST"; try { await this.getFileContents(projectId, filePath, branch); method = "PUT"; } catch (error) { // File doesn't exist, use POST } const response = await fetch(url, { method, headers: { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" }, body: JSON.stringify(body) }); if (!response.ok) { throw new McpError( ErrorCode.InternalError, `GitLab API error: ${response.statusText}` ); } const responseData = await response.json() as Record<string, any>; return { file_path: filePath, branch: branch, commit_id: responseData.commit_id || responseData.id || "unknown", content: responseData.content }; }
- src/index.ts:364-375 (handler)MCP server tool dispatcher case that parses arguments using the schema and delegates to gitlabApi.createOrUpdateFile handler.case "create_or_update_file": { const args = CreateOrUpdateFileSchema.parse(request.params.arguments); const result = await gitlabApi.createOrUpdateFile( args.project_id, args.file_path, args.content, args.commit_message, args.branch, args.previous_path ); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; }
- src/index.ts:108-113 (registration)Tool registration in ALL_TOOLS array, defining name, description, input schema, and readOnly flag.{ name: "create_or_update_file", description: "Create or update a single file in a GitLab project", inputSchema: createJsonSchema(CreateOrUpdateFileSchema), readOnly: false },
- src/schemas.ts:392-399 (schema)Zod schema defining input parameters for the create_or_update_file tool.export const CreateOrUpdateFileSchema = z.object({ project_id: z.string(), file_path: z.string(), content: z.string(), commit_message: z.string(), branch: z.string(), previous_path: z.string().optional() });