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
| Name | Required | Description | Default |
|---|---|---|---|
| mrUrl | Yes | The URL of the GitLab Merge Request. | |
| commentBody | Yes | The content of the comment. | |
| discussionId | No | Optional: The ID of an existing discussion to reply to. | |
| position | No | Optional: 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'], }, },
- src/index.ts:1255-1279 (handler)MCP tool call handler that invokes the GitLab service method to add the commentcase '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)}`, }, ], }; }
- src/gitlab.service.ts:330-344 (helper)Convenience wrapper that parses MR URL and delegates to core addCommentToMergeRequest methodasync 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, ); }
- src/gitlab.service.ts:282-327 (handler)Core handler logic in GitLabService that makes the API calls to GitLab for adding comments (general, reply, or inline) to a merge requestasync 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 }, ); } }
- src/gitlab.ts:10-19 (schema)Type definition for GitLabPosition used in inline comment positionsexport 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; }