async function createInlineComment()

in src/mcp/github-inline-comment-server.ts [110:174]


async function createInlineComment(
    config: ServerConfig,
    params: InlineCommentParams
): Promise<CommentResult> {
    try {
        const octokit = new Octokit({
            auth: config.token,
            baseUrl: config.apiUrl,
        });

        // Prepare the comment request
        const requestParams: any = {
            owner: config.owner,
            repo: config.repo,
            pull_number: config.prNumber,
            body: params.commentBody,
            path: params.filePath,
            commit_id: config.commitSha,
            side: params.diffSide || "RIGHT",
        };

        // Determine if this is a single-line or multi-line comment
        const isMultiLine = params.startLineNumber !== undefined;

        if (isMultiLine) {
            // Multi-line comment
            requestParams.start_line = params.startLineNumber;
            requestParams.start_side = params.diffSide || "RIGHT";
            requestParams.line = params.lineNumber;
        } else {
            // Single-line comment
            requestParams.line = params.lineNumber;
        }

        const response = await octokit.rest.pulls.createReviewComment(requestParams);

        return {
            success: true,
            commentId: response.data.id,
            url: response.data.html_url,
        };
    } catch (error: any) {
        let errorMsg = "Failed to create inline comment";
        let details = "";

        if (error.message) {
            errorMsg = error.message;
        }

        // Provide helpful context for common errors
        if (errorMsg.includes("Validation Failed")) {
            details = "The specified line may not exist in the diff, or the file path may be incorrect. Ensure you're commenting on lines that are part of the PR changes.";
        } else if (errorMsg.includes("Not Found")) {
            details = "Could not find the specified PR, repository, or file. Verify that the PR number and file path are correct.";
        } else if (error.status === 403) {
            details = "Permission denied. The GitHub token may lack required permissions for creating PR comments.";
        }

        return {
            success: false,
            error: errorMsg,
            details,
        };
    }
}