feat(mcp): codegraph_review_context — structured PR-review context from a diff#110
Open
andreinknv wants to merge 1 commit into
Open
feat(mcp): codegraph_review_context — structured PR-review context from a diff#110andreinknv wants to merge 1 commit into
andreinknv wants to merge 1 commit into
Conversation
…om a diff Adds a new MCP tool that takes a unified diff and returns structured review context for an LLM-driven PR reviewer. Codegraph becomes a substrate for Greptile/CodeRabbit-style products without itself doing the synthesis. ## What it returns Per changed file: - status (added / modified / deleted / renamed) - hunks (line ranges) - affected symbols (line-range overlap with hunks) - tests covering the file (via PR colbymchenry#106 `tests` edges; graceful no-op if those edges aren't present) Per affected symbol: - signature, docstring - top-N callers (PR colbymchenry#108's batch lookups make this fast) - top-N callees - impact-radius node count For deleted files: - brokenIncomingRefs — every distinct caller of a vanishing symbol (deduped by source name + file + line) Co-change warnings (the genuinely novel signal): - For each changed file, surface up to N historical co-changers that were NOT touched in this PR (catches "you changed schema.sql but didn't update migrations.ts" coupling violations) - Graceful degrade if PR colbymchenry#105's co_changes table isn't present Output is JSON; the LLM consumer does the synthesis (writes review comments, decides severity, posts to GitHub). ## Components - src/review/diff-parser.ts — pure unified-diff parser. Handles modified, added (/dev/null in ---), deleted (/dev/null in +++), renamed (rename from/to), multi-hunk files, single-line hunks (no comma in @@), C-style-quoted paths with spaces, and hunk-less rename / mode-change files mid-diff. - src/review/index.ts — buildReviewContext(diff, queries, traverser). Iterates files, maps hunks to symbols, attaches per-symbol context, surfaces co-change warnings. - src/index.ts — CodeGraph.buildReviewContext(diff, options) public API. - src/mcp/tools.ts — codegraph_review_context tool definition + handler. JSON output runs through serializeReviewContextWithinCap which progressively trims (docstrings → signatures → caller/callee caps → drop callers/callees → drop oldest files → summary-only) so the result stays valid JSON even when it would exceed MAX_OUTPUT_LENGTH. ## Files changed | File | Change | |---|---| | src/review/diff-parser.ts (NEW) | Pure unified-diff parser | | src/review/index.ts (NEW) | buildReviewContext + co-change warnings | | src/index.ts | Add CodeGraph.buildReviewContext public API | | src/mcp/tools.ts | New tool + handler + JSON-safe truncation | | __tests__/review-context.test.ts (NEW) | 25 regression tests | ## Test plan - [x] npm test: 405/405 pass on macOS - [x] npx tsc --noEmit clean - [x] Independent reviewer pass before pushing — addressed four findings: - Mid-diff hunk-less rename / mode-change was silently dropped (request_changes → fixed; flushHunkless on every header transition, flags reset after consumption to prevent phantom emits) - truncateOutput sliced JSON mid-string producing invalid JSON (request_changes → tier-and-trim helper, regression test asserts output stays parseable) - brokenIncomingRefs could double-list the same caller with multiple edge types (info → dedup by name|filePath|line, regression test added) - C-style-quoted paths in `diff --git "a/x" "b/y"` headers were not stripped (info → regex now matches both shapes, regression test added) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced Apr 27, 2026
Contributor
Author
|
Heads-up: this branch has been force-pushed. This PR's contents have been rebased to use the per-thing file layout established by refactor PRs #116-#119. Functionality is identical to the original; the internal file shape now matches the post-refactor architecture so this PR can be merged after the refactors with zero conflicts. The current diff against main looks larger than the feature alone because it transitively includes the 4 refactor commits + any prerequisite PRs. That diff auto-shrinks as the prerequisites merge in the order documented in #120. See #120 (comment) for the full rebase summary. |
3 tasks
andreinknv
added a commit
to andreinknv/codegraph
that referenced
this pull request
Apr 28, 2026
…kers Two new tools landed in colbymchenry#124 and colbymchenry#125 that this playbook should route the agent to instead of falling back to "read the source": - codegraph_biomarkers (PR colbymchenry#125): structured static-analysis signals (Code Health, cyclomatic, nesting, length) so an agent can ask "is this function risky to change?" without reading the source. - codegraph_coverage (PR colbymchenry#124): per-symbol coverage from lcov so an agent can ask "is this function tested?" with a structured answer. Updates: - "When to use which tool" map gains two entries. - Refactor-planning chain expanded to call both tools before callers/impact -- and points at the killer cross-tool query (high-centrality + warning-severity findings). - Tier table places biomarkers in tier 1 (always available after colbymchenry#125 lands) and coverage in tier 2 (conditional on a prior `codegraph coverage <lcov>` ingestion). Both references are forward-compatible: agents that try to call a not-yet-merged tool get a graceful "unknown tool" error, same pattern the existing playbook already uses for colbymchenry#110, colbymchenry#111, etc. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
andreinknv
added a commit
to andreinknv/codegraph
that referenced
this pull request
Apr 28, 2026
The MCP `initialize` response can include an `instructions` field that clients (Claude Code, Cursor, opencode, LangChain, OpenAI Agent SDK, etc.) surface in the agent's system prompt automatically. Today codegraph emits an empty initialize response — agents only see individual tool descriptions, no overall guidance on how to compose them. This adds the missing playbook: - **Tool selection by intent** — quick map from "what is X" / "how does X work" / "what would changing X break" to the right tool. - **Common chains** — onboarding (context first), PR review (review_context), refactor planning (search → callers → impact), debugging a regression. - **Tier discipline** — start at the cheap deterministic tier (search, context, callers, callees, impact, node, explore, files, status), escalate to conditional tools only when their data exists, and only reach for LLM-mediated tools when the cheap path doesn't suffice. - **Agent-bridge tier** — explicit recipe for projects without a local LLM where the agent itself summarizes via codegraph_pending_summaries + codegraph_save_summaries. - **Anti-patterns** — don't grep when search exists, don't chain search+node when context covers it, don't query the index immediately after a write. Lives in src/mcp/server-instructions.ts so it's easy to update without touching the JSON-RPC dispatch in src/mcp/index.ts. Single-file, no schema changes, no migrations, no test changes needed. References tools that exist on `main` today; doesn't presume any of the in-flight feature PRs (colbymchenry#110, colbymchenry#112-115, colbymchenry#111) have landed. After those merge, the relevant sections of this guidance start applying without needing a follow-up edit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
andreinknv
added a commit
to andreinknv/codegraph
that referenced
this pull request
Apr 28, 2026
…kers Two new tools landed in colbymchenry#124 and colbymchenry#125 that this playbook should route the agent to instead of falling back to "read the source": - codegraph_biomarkers (PR colbymchenry#125): structured static-analysis signals (Code Health, cyclomatic, nesting, length) so an agent can ask "is this function risky to change?" without reading the source. - codegraph_coverage (PR colbymchenry#124): per-symbol coverage from lcov so an agent can ask "is this function tested?" with a structured answer. Updates: - "When to use which tool" map gains two entries. - Refactor-planning chain expanded to call both tools before callers/impact -- and points at the killer cross-tool query (high-centrality + warning-severity findings). - Tier table places biomarkers in tier 1 (always available after colbymchenry#125 lands) and coverage in tier 2 (conditional on a prior `codegraph coverage <lcov>` ingestion). Both references are forward-compatible: agents that try to call a not-yet-merged tool get a graceful "unknown tool" error, same pattern the existing playbook already uses for colbymchenry#110, colbymchenry#111, etc. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
b481d10 to
c7ea248
Compare
This was referenced May 6, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new MCP tool
codegraph_review_contextthat takes a unified diff and returns structured review context for an LLM-driven PR reviewer. Codegraph becomes a substrate for Greptile/CodeRabbit-style products without itself doing the synthesis — the LLM consumer (Claude Code, ultrareview, etc.) writes the review comments, decides severity, and posts to GitHub.What it returns (JSON)
Per changed file:
testsedges; graceful no-op if those edges aren't present)Per affected symbol:
For deleted files:
brokenIncomingRefs— every distinct caller of a vanishing symbol (deduped by source name + file + line)Co-change warnings (the genuinely novel signal):
schema.sqlbut didn't updatemigrations.ts" coupling violations that no static analyzer can seeco_changestable isn't presentComponents
src/review/diff-parser.ts— pure unified-diff parser. Handles modified, added (/dev/nullin---), deleted (/dev/nullin+++), renamed (rename from/to), multi-hunk, single-line hunks, C-style-quoted paths with spaces, and hunk-less rename / mode-change files mid-diff.src/review/index.ts—buildReviewContext(diff, queries, traverser, options). Iterates files, maps hunks to symbols, attaches per-symbol context, surfaces co-change warnings.CodeGraph.buildReviewContext(diff, options)— public API method.MCP tool in
src/mcp/tools.ts— JSON output runs throughserializeReviewContextWithinCapwhich progressively trims (docstrings → signatures → caller/callee caps → drop callers/callees → drop oldest files → summary-only) so the result stays valid JSON even when it would exceedMAX_OUTPUT_LENGTH. (Naive mid-string truncation would corrupt JSON parsing.)Files changed
src/review/diff-parser.ts(NEW)src/review/index.ts(NEW)buildReviewContext+ co-change warningssrc/index.tsCodeGraph.buildReviewContextpublic APIsrc/mcp/tools.ts__tests__/review-context.test.ts(NEW)Example flow
Test plan
npm test: 405/405 pass on macOSnpx tsc --noEmitcleanflushHunklesson every header transition, flags reset after consumption to prevent phantom emits)truncateOutputsliced JSON mid-string producing invalid JSON (request_changes → tier-and-trim helper, regression test asserts output stays parseable even with 200-symbol over-cap context)brokenIncomingRefscould double-list the same caller with multiple edge types (info → dedup byname|filePath|line)diff --git "a/x" "b/y"headers were not stripped (info → regex now matches both shapes;unquotehelper handles the value)Forward-compat with sibling PRs
This PR works on a clean upstream/main install today. When PRs #105 (co-change graph) and #106 (tests-as-edges) land, the output gets richer automatically — no further code changes required.
🤖 Generated with Claude Code