Skip to main content
Glama

gitlab_add_comment_to_merge_request

Add comments to GitLab merge requests for general feedback, discussion replies, or inline code reviews.

Instructions

Adds a comment to a GitLab Merge Request. Can be a general comment, a reply to an existing discussion, or an inline comment on a specific line.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
mrUrlYesThe URL of the GitLab Merge Request.
commentBodyYesThe content of the comment.
discussionIdNoOptional: The ID of an existing discussion to reply to.
positionNoOptional: Position object for inline comments, specifying file paths, SHAs, and line numbers.

Implementation Reference

  • src/index.ts:109-147 (registration)
    Tool registration and input schema definition for gitlab_add_comment_to_merge_request
    {
      name: 'gitlab_add_comment_to_merge_request',
      description:
        'Adds a comment to a GitLab Merge Request. Can be a general comment, a reply to an existing discussion, or an inline comment on a specific line.',
      inputSchema: {
        type: 'object',
        properties: {
          mrUrl: {
            type: 'string',
            description: 'The URL of the GitLab Merge Request.',
          },
          commentBody: {
            type: 'string',
            description: 'The content of the comment.',
          },
          discussionId: {
            type: 'string',
            description:
              'Optional: The ID of an existing discussion to reply to.',
          },
          position: {
            type: 'object',
            description:
              'Optional: Position object for inline comments, specifying file paths, SHAs, and line numbers.',
            properties: {
              base_sha: { type: 'string' },
              start_sha: { type: 'string' },
              head_sha: { type: 'string' },
              position_type: { type: 'string' },
              old_path: { type: 'string' },
              new_path: { type: 'string' },
              new_line: { type: 'number' },
              old_line: { type: 'number' },
            },
          },
        },
        required: ['mrUrl', 'commentBody'],
      },
    },
  • MCP tool call handler that invokes the GitLab service method to add the comment
    case 'gitlab_add_comment_to_merge_request': {
      if (!gitlabService) {
        throw new Error('GitLab service is not initialized.');
      }
      const { mrUrl, commentBody, discussionId, position } = args as {
        mrUrl: string;
        commentBody: string;
        discussionId?: string;
        position?: GitLabPosition;
      };
      const result = await gitlabService.addCommentToMergeRequestFromUrl(
        mrUrl,
        commentBody,
        discussionId,
        position,
      );
      return {
        content: [
          {
            type: 'text',
            text: `Comment added successfully: ${JSON.stringify(result)}`,
          },
        ],
      };
    }
  • Convenience wrapper that parses MR URL and delegates to core addCommentToMergeRequest method
    async addCommentToMergeRequestFromUrl(
      mrUrl: string,
      commentBody: string,
      discussionId?: string,
      position?: GitLabPosition,
    ): Promise<any> {
      const { projectPath, mrIid } = this.parseMrUrl(mrUrl, this.config.url);
      return this.addCommentToMergeRequest(
        projectPath,
        mrIid,
        discussionId,
        commentBody,
        position,
      );
    }
  • Core handler logic in GitLabService that makes the API calls to GitLab for adding comments (general, reply, or inline) to a merge request
    async addCommentToMergeRequest(
      projectPath: string,
      mrIid: number,
      discussionId: string | undefined,
      commentBody: string,
      position: GitLabPosition | undefined,
    ): Promise<any> {
      const encodedProjectPath = encodeURIComponent(projectPath);
    
      if (discussionId) {
        // Reply to an existing discussion
        return this.callGitLabApi(
          `projects/${encodedProjectPath}/merge_requests/${mrIid}/discussions/${discussionId}/notes`,
          'POST',
          { body: commentBody },
        );
      } else if (position) {
        // Add a new comment with a position (inline comment)
        return this.callGitLabApi(
          `projects/${encodedProjectPath}/merge_requests/${mrIid}/notes`,
          'POST',
          {
            body: commentBody,
            noteable_type: 'MergeRequest',
            noteable_id: mrIid,
            position: {
              base_sha: position.base_sha,
              start_sha: position.start_sha,
              head_sha: position.head_sha,
              position_type: position.position_type,
              old_path: position.old_path,
              new_path: position.new_path,
              new_line: position.new_line,
              old_line: position.old_line,
            },
          },
        );
      } else {
        // Add a general comment
        return this.callGitLabApi(
          `projects/${encodedProjectPath}/merge_requests/${mrIid}/notes`,
          'POST',
          { body: commentBody },
        );
      }
    }
  • Type definition for GitLabPosition used in inline comment positions
    export interface GitLabPosition {
      base_sha: string;
      start_sha: string;
      head_sha: string;
      position_type: 'text';
      old_path: string;
      new_path: string;
      new_line?: number;
      old_line?: number;
    }

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/HainanZhao/mcp-gitlab-jira'

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