add_pr_comments
Add comments to GitHub pull requests to provide feedback, suggestions, or analysis during code review processes.
Instructions
Adds a comment to a specific pull request on GitHub. Args: repo_owner (str): The owner of the repository. repo_name (str): The name of the repository. pr_number (int): The pull request number to which the comment will be added. comment (str): The content of the comment to add. Returns: Dict[str, Any]: The JSON response from the GitHub API containing the comment data if successful. None: If an error occurs while adding the comment. Error Handling: Logs an error message and prints the traceback if the request fails or an exception is raised.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_owner | Yes | ||
| repo_name | Yes | ||
| pr_number | Yes | ||
| comment | Yes |
Implementation Reference
- The core handler implementation for the 'add_pr_comments' MCP tool. This method uses the GitHub API to post a general comment to the specified pull request.def add_pr_comments(self, repo_owner: str, repo_name: str, pr_number: int, comment: str) -> Dict[str, Any]: """ Adds a comment to a specific pull request on GitHub. Args: repo_owner (str): The owner of the repository. repo_name (str): The name of the repository. pr_number (int): The pull request number to which the comment will be added. comment (str): The content of the comment to add. Returns: Dict[str, Any]: The JSON response from the GitHub API containing the comment data if successful. None: If an error occurs while adding the comment. Error Handling: Logs an error message and prints the traceback if the request fails or an exception is raised. """ logging.info(f"Adding comment to PR {repo_owner}/{repo_name}#{pr_number}") # Construct the comments URL comments_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/{pr_number}/comments" try: # Add the comment response = requests.post(comments_url, headers=self._get_headers(), json={'body': comment}, timeout=TIMEOUT) response.raise_for_status() comment_data = response.json() logging.info("Comment added successfully") return comment_data except Exception as e: logging.error(f"Error adding comment: {str(e)}") traceback.print_exc() return {"status": "error", "message": str(e)}
- src/mcp_github/issues_pr_analyser.py:105-113 (registration)The registration logic that dynamically registers all public methods of the GitHubIntegration instance (including 'add_pr_comments') as MCP tools by calling mcp.add_tool on each qualifying method.def _register_tools(self): self.register_tools(self.gi) self.register_tools(self.ip) def register_tools(self, methods: Any = None) -> None: for name, method in inspect.getmembers(methods): if (inspect.isfunction(method) or inspect.ismethod(method)) and not name.startswith("_"): self.mcp.add_tool(method)
- src/mcp_github/issues_pr_analyser.py:67-70 (registration)Instantiation of the GitHubIntegration class instance used for tool registration.self.gi = GI() self.ip = IP() # Initialize MCP Server
- src/mcp_github/issues_pr_analyser.py:27-27 (registration)Import of the GitHubIntegration class containing the tool handler.from .github_integration import GitHubIntegration as GI