|
| 1 | +"""Structural MCP tools (T4-T8). |
| 2 | +
|
| 3 | +These tools wrap the existing ``Project`` / ``Graph`` / ``AsyncGraphQuery`` |
| 4 | +operations so MCP-capable agents (Claude Code, Cursor, Copilot, Cline) |
| 5 | +can drive code-graph over the standard stdio transport. |
| 6 | +
|
| 7 | +Conventions shared by all tools in this module: |
| 8 | +
|
| 9 | +* Every tool accepts an optional ``branch`` so the agent can scope queries |
| 10 | + to a specific per-branch graph (see T17, issue #651). When omitted the |
| 11 | + branch is either auto-detected from a local checkout (``index_repo``) |
| 12 | + or defaults to ``_default``. |
| 13 | +* Long-running synchronous operations are pushed into a thread via |
| 14 | + ``asyncio.get_running_loop().run_in_executor`` so the MCP event loop |
| 15 | + stays responsive. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import asyncio |
| 21 | +import logging |
| 22 | +import os |
| 23 | +from pathlib import Path |
| 24 | +from typing import Any, Optional |
| 25 | + |
| 26 | +from ..server import app |
| 27 | + |
| 28 | + |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +# --------------------------------------------------------------------------- |
| 33 | +# Helpers |
| 34 | +# --------------------------------------------------------------------------- |
| 35 | + |
| 36 | + |
| 37 | +def _looks_like_url(spec: str) -> bool: |
| 38 | + """Return True for HTTP(S) / git URLs, False for local paths.""" |
| 39 | + return spec.startswith(("http://", "https://", "git@", "ssh://", "git://")) |
| 40 | + |
| 41 | + |
| 42 | +def _languages_detected(graph) -> list[str]: |
| 43 | + """Best-effort enumeration of distinct ``File.ext`` values. |
| 44 | +
|
| 45 | + Returns a sorted list of extension strings (without the leading dot). |
| 46 | + Empty when no files were indexed. |
| 47 | + """ |
| 48 | + try: |
| 49 | + rows = graph.g.query( |
| 50 | + "MATCH (f:File) RETURN DISTINCT f.ext AS ext" |
| 51 | + ).result_set |
| 52 | + except Exception as e: # pragma: no cover — defensive |
| 53 | + logger.warning("languages_detected query failed: %s", e) |
| 54 | + return [] |
| 55 | + seen: set[str] = set() |
| 56 | + for row in rows or []: |
| 57 | + ext = (row[0] or "").lstrip(".") |
| 58 | + if ext: |
| 59 | + seen.add(ext) |
| 60 | + return sorted(seen) |
| 61 | + |
| 62 | + |
| 63 | +def _count(graph, label: str) -> int: |
| 64 | + try: |
| 65 | + rows = graph.g.query( |
| 66 | + f"MATCH (n:{label}) RETURN count(n) AS c" |
| 67 | + ).result_set |
| 68 | + return int(rows[0][0]) if rows else 0 |
| 69 | + except Exception: |
| 70 | + return 0 |
| 71 | + |
| 72 | + |
| 73 | +def _count_edges(graph) -> int: |
| 74 | + try: |
| 75 | + rows = graph.g.query("MATCH ()-[r]->() RETURN count(r) AS c").result_set |
| 76 | + return int(rows[0][0]) if rows else 0 |
| 77 | + except Exception: |
| 78 | + return 0 |
| 79 | + |
| 80 | + |
| 81 | +# --------------------------------------------------------------------------- |
| 82 | +# T4 — index_repo |
| 83 | +# --------------------------------------------------------------------------- |
| 84 | + |
| 85 | + |
| 86 | +@app.tool( |
| 87 | + name="index_repo", |
| 88 | + description=( |
| 89 | + "Index a code repository into code-graph for subsequent navigation. " |
| 90 | + "Accepts a local path or a git URL. When `branch` is omitted, " |
| 91 | + "auto-detects the current branch from the local checkout (defaults " |
| 92 | + "to '_default' for non-git folders). Returns the indexed graph's " |
| 93 | + "node/edge counts, detected languages, and the (project, branch) " |
| 94 | + "identity callers should pass to other code-graph tools." |
| 95 | + ), |
| 96 | +) |
| 97 | +async def index_repo( |
| 98 | + path_or_url: str, |
| 99 | + branch: Optional[str] = None, |
| 100 | + incremental: bool = True, # accepted now, fully honored once T18 lands |
| 101 | + ignore: Optional[list[str]] = None, |
| 102 | +) -> dict[str, Any]: |
| 103 | + """Implementation for the ``index_repo`` MCP tool. |
| 104 | +
|
| 105 | + Args: |
| 106 | + path_or_url: Filesystem path to a local repository **or** a clonable |
| 107 | + git URL (``https://...``, ``git@host:...``, ``ssh://...``). |
| 108 | + branch: Branch identity for the indexed graph. When ``None``: |
| 109 | + auto-detect from the checkout via ``git rev-parse --abbrev-ref |
| 110 | + HEAD``; falls back to ``_default`` if not a git checkout. |
| 111 | + incremental: Accepted for forward-compatibility with T18; the |
| 112 | + current full-reindex path ignores it. |
| 113 | + ignore: List of relative paths to skip during analysis. |
| 114 | + """ |
| 115 | + |
| 116 | + from api.project import Project, detect_branch |
| 117 | + |
| 118 | + if ignore is None: |
| 119 | + ignore = [] |
| 120 | + |
| 121 | + loop = asyncio.get_running_loop() |
| 122 | + |
| 123 | + def _do_index() -> dict[str, Any]: |
| 124 | + if _looks_like_url(path_or_url): |
| 125 | + project = Project.from_git_repository(path_or_url, branch=branch) |
| 126 | + else: |
| 127 | + local_path = Path(path_or_url).expanduser().resolve() |
| 128 | + if not local_path.exists(): |
| 129 | + raise ValueError(f"path does not exist: {local_path}") |
| 130 | + |
| 131 | + # Reject paths outside the allow-list when one is configured. |
| 132 | + allowed_root = os.getenv("ALLOWED_ANALYSIS_DIR") |
| 133 | + if allowed_root: |
| 134 | + allowed = Path(allowed_root).expanduser().resolve() |
| 135 | + try: |
| 136 | + local_path.relative_to(allowed) |
| 137 | + except ValueError as e: |
| 138 | + raise ValueError( |
| 139 | + f"path {local_path} is outside ALLOWED_ANALYSIS_DIR={allowed}" |
| 140 | + ) from e |
| 141 | + |
| 142 | + # Use Project for git-repo paths so commit metadata is saved, |
| 143 | + # otherwise drive SourceAnalyzer directly so non-git folders work. |
| 144 | + if (local_path / ".git").is_dir(): |
| 145 | + project = Project.from_local_repository(local_path, branch=branch) |
| 146 | + else: |
| 147 | + # Synthesize a Project-like object so the return shape is uniform. |
| 148 | + from api.analyzers.source_analyzer import SourceAnalyzer |
| 149 | + from api.graph import Graph |
| 150 | + |
| 151 | + detected = branch if branch is not None else detect_branch(local_path) |
| 152 | + graph = Graph(local_path.name, branch=detected) |
| 153 | + analyzer = SourceAnalyzer() |
| 154 | + analyzer.analyze_local_folder(str(local_path), graph, ignore) |
| 155 | + |
| 156 | + class _Synth: # tiny shim to mirror Project's surface |
| 157 | + name = local_path.name |
| 158 | + |
| 159 | + def __init__(self, g, b): |
| 160 | + self.graph = g |
| 161 | + self.branch = b |
| 162 | + |
| 163 | + return _payload(_Synth(graph, detected)) |
| 164 | + |
| 165 | + project.analyze_sources(ignore) |
| 166 | + return _payload(project) |
| 167 | + |
| 168 | + def _payload(project) -> dict[str, Any]: |
| 169 | + g = project.graph |
| 170 | + return { |
| 171 | + "project_name": project.name, |
| 172 | + "branch": getattr(project, "branch", None), |
| 173 | + "graph_name": g.name, |
| 174 | + "num_nodes": ( |
| 175 | + _count(g, "File") + _count(g, "Class") + _count(g, "Function") |
| 176 | + ), |
| 177 | + "num_edges": _count_edges(g), |
| 178 | + "languages_detected": _languages_detected(g), |
| 179 | + # T18 will flip this to "incremental" when only changed files |
| 180 | + # were re-analyzed. |
| 181 | + "mode": "full", |
| 182 | + } |
| 183 | + |
| 184 | + return await loop.run_in_executor(None, _do_index) |
0 commit comments