get_github_pr_content
Fetch the detailed content of a GitHub pull request by specifying the repository owner, name, and PR number. Use this tool to analyze pull request information for better context in repository management.
Instructions
First use get_pr_diff to fetch the diff of a specific pull request from a GitHub repository.
Then, if you still need more context use get_pr_content to fetch the content of the pull request.
Fetches the content of a GitHub pull request for a given repository and PR number.
Args:
repo_owner (str): The owner of the GitHub repository.
repo_name (str): The name of the GitHub repository.
pr_number (int): The pull request number to fetch.
Returns:
Dict[str, Any]: A dictionary containing the pull request information if successful, or an empty dictionary if no information is found or an error occurs.
Error Handling:
Logs an error message and prints the traceback to stderr if an exception is raised during the fetch operation. Returns an empty dictionary in case of errors.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pr_number | Yes | ||
| repo_name | Yes | ||
| repo_owner | Yes |
Implementation Reference
- The core handler function implementing the logic to fetch GitHub pull request content (title, body, author, dates, state) via the GitHub API. This is the main execution logic for the tool.def get_pr_content(self, repo_owner: str, repo_name: str, pr_number: int) -> Dict[str, Any]: """ Fetches the content/details of a specific pull request from a GitHub repository. Args: repo_owner (str): The owner of the repository. repo_name (str): The name of the repository. pr_number (int): The pull request number. Returns: Dict[str, Any]: A dictionary containing the pull request's title, description, author, creation and update timestamps, and state. Returns None if an error occurs during the fetch operation. Error Handling: Logs an error message and prints the traceback if the request fails or an exception is raised during processing. """ logging.info(f"Fetching PR content for {repo_owner}/{repo_name}#{pr_number}") # Construct the PR URL pr_url = self._get_pr_url(repo_owner, repo_name, pr_number) try: # Fetch PR details response = requests.get(pr_url, headers=self._get_headers(), timeout=TIMEOUT) response.raise_for_status() pr_data = response.json() # Extract relevant information pr_info = { 'title': pr_data['title'], 'description': pr_data['body'], 'author': pr_data['user']['login'], 'created_at': pr_data['created_at'], 'updated_at': pr_data['updated_at'], 'state': pr_data['state'] } logging.info("Successfully fetched PR content") return pr_info except Exception as e: logging.error(f"Error fetching PR content: {str(e)}") traceback.print_exc() return {"status": "error", "message": str(e)}
- src/mcp_github/issues_pr_analyser.py:105-108 (registration)Registers all public methods of the GitHubIntegration instance (including get_pr_content) as MCP tools by calling register_tools(self.gi).def _register_tools(self): self.register_tools(self.gi) self.register_tools(self.ip)
- src/mcp_github/issues_pr_analyser.py:109-113 (registration)Generic tool registration function that iterates over object methods and adds non-private ones as MCP tools using FastMCP's add_tool, naming them after the method names (e.g., 'get_pr_content').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)
- Helper function to construct the GitHub API URL for fetching PR details, used by get_pr_content.def _get_pr_url(self, repo_owner: str, repo_name: str, pr_number: int) -> str: """ Construct the GitHub API URL for a specific pull request. Args: repo_owner (str): The owner of the GitHub repository. repo_name (str): The name of the GitHub repository. pr_number (int): The pull request number. Returns: str: The formatted GitHub API URL for the specified pull request. Raises: ValueError: If any of the arguments are empty or if pr_number is not a positive integer. """ url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls/{pr_number}" return url
- Helper function to generate HTTP headers with GitHub token authentication, used in API requests by get_pr_content.def _get_headers(self): """ Constructs the HTTP headers required for GitHub API requests, including the authorization token. Returns: dict: A dictionary containing the required HTTP headers. Error Handling: Raises ValueError if the GitHub token is not set. """ if not self.github_token: raise ValueError("GitHub token is missing for API requests") headers = { 'Authorization': f'token {self.github_token}', 'Accept': 'application/vnd.github.v3+json' } return headers