How to Attach Line-Specific Review Comments to Pull Requests
This guide explains how to add line-specific review comments to pull requests using the GitHub CLI (gh) API or mcp__github_inline_comment__create_inline_comment if it not available, similar to how the GitHub UI allows commenting on specific lines of code.
Preferred Approach: Using MCP GitHub Tools
If available, use the mcp__github_inline_comment__create_inline_comment MCP tool for posting line-specific inline comments on pull requests. This approach provides better integration with GitHub's UI and is the recommended method.
Fallback: If the MCP tool is not available, use the GitHub CLI (gh) API methods described below:
- For single comments: Use the
/commentsendpoint (see Adding a Single Line-Specific Comment) - For multiple comments: Use the
/reviewsendpoint (see Adding Multiple Line-Specific Comments Together)
Overview
While gh pr review provides basic review functionality (approve, request changes, general comments), it does not support line-specific comments directly. To add comments on specific lines of code, you must use the lower-level gh api command to call GitHub's REST API directly.
Prerequisites
-
GitHub CLI installed and authenticated:
-
Access to the repository and pull request you want to review
Understanding GitHub's Review Comment System
GitHub has two types of PR comments:
- Issue Comments - General comments on the PR conversation
- Review Comments - Line-specific comments on code changes
Review comments can be added in two ways:
- Single comment - Using the
/pulls/{pr}/commentsendpoint - Review with multiple comments - Using the
/pulls/{pr}/reviewsendpoint
Adding a Single Line-Specific Comment
Basic Syntax
Parameters Explained
Parameter Flags
-f(--field) - For string values-F(--field) - For integer values (note the capital F)
Complete Example
Understanding Line Numbers
The line parameter refers to the position in the diff, not the absolute line number in the file:
- For new files: Line numbers match the file's line numbers
- For modified files: Use the line number as it appears in the "Files changed" tab
- For multi-line comments: Use
start_lineandlineto specify the range
Response
On success, returns a JSON object with comment details:
Adding Multiple Line-Specific Comments Together
To add multiple comments across different files in a single review, use the /reviews endpoint with JSON input.
Why Use Reviews for Multiple Comments?
- Atomic operation - All comments are added together
- Single notification - Doesn't spam with multiple notifications
- Better UX - Appears as one cohesive review
- Same mechanism as GitHub UI - "Start a review" → "Finish review"
Basic Syntax
Review Event Types
Complete Example
Response
Common Issues and Solutions
Issue 1: "user_id can only have one pending review per pull request"
Error Message:
Cause: GitHub only allows one pending (unsubmitted) review per user per PR. If you previously started a review through the UI or API and didn't submit it, it blocks new review creation.
Solution 1: Submit the pending review
Solution 2: Use the single comment endpoint instead
Issue 2: Array syntax not working with --raw-field
Failed Attempt:
Error:
Solution: Use JSON input via heredoc:
Issue 3: Invalid line number
Error Message:
Cause: The line number doesn't exist in the diff for this file.
Solutions:
- Verify the file was actually changed in this PR
- Check the "Files changed" tab to see actual line numbers in the diff
- Ensure you're using the correct
commit_id(the latest commit in the PR)
Issue 4: Wrong commit_id
Error Message:
Solution: Get the latest commit SHA:
Best Practices
1. Get PR Information First
Before adding comments, gather necessary information:
2. Check for Pending Reviews
3. Use Meaningful Comment Text
- Be specific and constructive
- Reference documentation or best practices
- Suggest alternatives when requesting changes
- Use code blocks for code suggestions:
4. Batch Related Comments
Use the review endpoint to group related comments:
- All comments for a single file/area
- All comments for a specific concern (security, performance, etc.)
- Complete review session
5. Choose the Right Event Type
Workflow Examples
Example 1: Quick Single Comment
Example 2: Comprehensive Review
Example 3: Multi-line Comment
Helpful Helper Scripts
Get PR Files and Lines
Check Review Status
Related Documentation
- GitHub API: Pull Request Review Comments
- GitHub API: Pull Request Reviews
- GitHub CLI Manual
- Create PR Command
- Commit Command
API Reference
POST /repos/{owner}/{repo}/pulls/{pull_number}/comments
Creates a review comment on a specific line.
Endpoint: https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/comments
Parameters:
body(string, required): Comment textcommit_id(string, required): SHA of commitpath(string, required): Relative file pathline(integer, required): Line number in diffside(string, required): "LEFT" or "RIGHT"start_line(integer, optional): Start line for multi-linestart_side(string, optional): Start side for multi-line
POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews
Creates a review with optional line-specific comments.
Endpoint: https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/reviews
Parameters:
event(string, required): "APPROVE", "REQUEST_CHANGES", or "COMMENT"body(string, optional): Overall review commentcomments(array, optional): Array of comment objectscommit_id(string, optional): SHA of commit to review
Comment Object:
path(string, required): Relative file pathbody(string, required): Comment textline(integer, required): Line number in diffside(string, required): "LEFT" or "RIGHT"start_line(integer, optional): Start line for multi-linestart_side(string, optional): Start side for multi-line


