get_pr_content
Fetch pull request details from GitHub repositories to analyze content, author information, and status for review or integration workflows.
Instructions
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.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_owner | Yes | ||
| repo_name | Yes | ||
| pr_number | Yes |
Implementation Reference
- The handler function that executes the get_pr_content tool logic. Fetches PR details (title, body, author, created_at, updated_at, state) from GitHub API endpoint /repos/{owner}/{repo}/pulls/{pr_number}, extracts key fields, handles errors by returning error dict.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:106-113 (registration)Registers all public methods of GitHubIntegration instance (self.gi) as MCP tools using FastMCP.add_tool in a loop over inspect.getmembers. This automatically registers get_pr_content since it does not start with underscore.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)
- Helper method used by get_pr_content to construct the GitHub API URL: https://api.github.com/repos/{repo_owner}/{repo_name}/pulls/{pr_number}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 method used by get_pr_content to generate HTTP headers with GitHub token authorization and v3 API accept header.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
- src/mcp_github/issues_pr_analyser.py:67-67 (registration)Instantiates the GitHubIntegration instance (self.gi = GI()) whose methods, including get_pr_content, will be registered as MCP tools.self.gi = GI()