|
| 1 | +""" |
| 2 | +GitHub MCP Resources - Exposes GitHub data for Claude's context. |
| 3 | +
|
| 4 | +Resources are automatically available in Claude's context when connected. |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +from typing import Any, Dict, Optional |
| 9 | + |
| 10 | +import yaml |
| 11 | +from fastmcp import FastMCP |
| 12 | + |
| 13 | +from mcp_server.auth import get_credential_store, get_github_pat |
| 14 | +from mcp_server.auth.credentials import _find_project_root, _parse_env_file |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +def _load_issue_templates_config() -> Optional[Dict[str, Any]]: |
| 20 | + """ |
| 21 | + Load issue templates from ISSUE_TEMPLATE_PATH in .quickcall.env. |
| 22 | + Returns None if not configured or file doesn't exist. |
| 23 | + """ |
| 24 | + import os |
| 25 | + from pathlib import Path |
| 26 | + |
| 27 | + template_path = os.getenv("ISSUE_TEMPLATE_PATH") |
| 28 | + |
| 29 | + # Check .quickcall.env in project root |
| 30 | + if not template_path: |
| 31 | + project_root = _find_project_root() |
| 32 | + if project_root: |
| 33 | + config_path = project_root / ".quickcall.env" |
| 34 | + if config_path.exists(): |
| 35 | + env_vars = _parse_env_file(config_path) |
| 36 | + if "ISSUE_TEMPLATE_PATH" in env_vars: |
| 37 | + template_path = env_vars["ISSUE_TEMPLATE_PATH"] |
| 38 | + if not Path(template_path).is_absolute(): |
| 39 | + template_path = str(project_root / template_path) |
| 40 | + |
| 41 | + if not template_path: |
| 42 | + return None |
| 43 | + |
| 44 | + try: |
| 45 | + with open(template_path) as f: |
| 46 | + return yaml.safe_load(f) or {} |
| 47 | + except Exception as e: |
| 48 | + logger.warning(f"Failed to load issue templates: {e}") |
| 49 | + return None |
| 50 | + |
| 51 | + |
| 52 | +def create_github_resources(mcp: FastMCP) -> None: |
| 53 | + """Add GitHub resources to the MCP server.""" |
| 54 | + |
| 55 | + @mcp.resource("github://repositories") |
| 56 | + def get_github_repositories() -> str: |
| 57 | + """ |
| 58 | + List of GitHub repositories the user has access to. |
| 59 | +
|
| 60 | + Use these when working with GitHub operations. |
| 61 | + """ |
| 62 | + store = get_credential_store() |
| 63 | + |
| 64 | + # Check if authenticated via PAT or QuickCall |
| 65 | + pat_token, pat_source = get_github_pat() |
| 66 | + has_pat = pat_token is not None |
| 67 | + |
| 68 | + if not has_pat and not store.is_authenticated(): |
| 69 | + return "GitHub not connected. Options:\n- Run connect_github_via_pat with a Personal Access Token\n- Run connect_quickcall to use QuickCall" |
| 70 | + |
| 71 | + # Check QuickCall GitHub App connection |
| 72 | + has_app = False |
| 73 | + if store.is_authenticated(): |
| 74 | + creds = store.get_api_credentials() |
| 75 | + if creds and creds.github_connected and creds.github_token: |
| 76 | + has_app = True |
| 77 | + |
| 78 | + if not has_pat and not has_app: |
| 79 | + return "GitHub not connected. Connect at quickcall.dev/assistant or use connect_github_via_pat." |
| 80 | + |
| 81 | + try: |
| 82 | + # Import here to avoid circular imports |
| 83 | + from mcp_server.tools.github_tools import _get_client |
| 84 | + |
| 85 | + client = _get_client() |
| 86 | + repos = client.list_repos(limit=50) |
| 87 | + |
| 88 | + # Determine auth mode for display |
| 89 | + auth_mode = "PAT" if has_pat else "GitHub App" |
| 90 | + |
| 91 | + lines = [f"GitHub Repositories (via {auth_mode}):", ""] |
| 92 | + for repo in repos: |
| 93 | + visibility = "private" if repo.private else "public" |
| 94 | + lines.append(f"- {repo.full_name} ({visibility})") |
| 95 | + |
| 96 | + if len(repos) >= 50: |
| 97 | + lines.append("") |
| 98 | + lines.append("(Showing first 50 repos)") |
| 99 | + |
| 100 | + return "\n".join(lines) |
| 101 | + except Exception as e: |
| 102 | + logger.error(f"Failed to fetch GitHub repositories: {e}") |
| 103 | + return f"Error fetching repositories: {str(e)}" |
| 104 | + |
| 105 | + @mcp.resource("github://issue-templates") |
| 106 | + def get_issue_templates() -> str: |
| 107 | + """ |
| 108 | + Available issue templates from project configuration. |
| 109 | +
|
| 110 | + Use template names when creating issues with manage_issues. |
| 111 | + """ |
| 112 | + config = _load_issue_templates_config() |
| 113 | + |
| 114 | + if not config: |
| 115 | + return "No issue templates configured.\n\nTo configure:\n1. Create a YAML file with your templates\n2. Add ISSUE_TEMPLATE_PATH=/path/to/templates.yaml to .quickcall.env" |
| 116 | + |
| 117 | + templates = config.get("templates", {}) |
| 118 | + defaults = config.get("defaults", {}) |
| 119 | + |
| 120 | + if not templates: |
| 121 | + lines = ["Issue Templates:", ""] |
| 122 | + if defaults: |
| 123 | + labels = defaults.get("labels", []) |
| 124 | + lines.append("Default template:") |
| 125 | + if labels: |
| 126 | + lines.append(f" Labels: {', '.join(labels)}") |
| 127 | + if defaults.get("body"): |
| 128 | + lines.append(f" Body template: {defaults['body'][:100]}...") |
| 129 | + return "\n".join(lines) |
| 130 | + |
| 131 | + lines = ["Available Issue Templates:", ""] |
| 132 | + |
| 133 | + for name, template in templates.items(): |
| 134 | + labels = template.get("labels", []) |
| 135 | + body_preview = template.get("body", "")[:80] |
| 136 | + lines.append(f"- {name}") |
| 137 | + if labels: |
| 138 | + lines.append(f" Labels: {', '.join(labels)}") |
| 139 | + if body_preview: |
| 140 | + lines.append(f" Body: {body_preview}...") |
| 141 | + |
| 142 | + lines.append("") |
| 143 | + lines.append( |
| 144 | + "Usage: manage_issues(action='create', title='...', template='<name>')" |
| 145 | + ) |
| 146 | + |
| 147 | + return "\n".join(lines) |
0 commit comments