Skip to main content
Glama

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

TableJSON Schema
NameRequiredDescriptionDefault
branchNo
commit_messageNo
contentNo
file_pathNo
previous_pathNo
project_idNo

Implementation Reference

  • 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
      };
    }
  • 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
    },
  • 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()
    });

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/yoda-digital/mcp-gitlab-server'

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