Skip to content

feat(mcp/cli): add codegraph_check for circular import detection#889

Open
goutham80808 wants to merge 5 commits into
colbymchenry:mainfrom
goutham80808:feat/codegraph-check-circular-imports
Open

feat(mcp/cli): add codegraph_check for circular import detection#889
goutham80808 wants to merge 5 commits into
colbymchenry:mainfrom
goutham80808:feat/codegraph-check-circular-imports

Conversation

@goutham80808

@goutham80808 goutham80808 commented Jun 14, 2026

Copy link
Copy Markdown

Summary

Surfaces the existing findCircularDependencies() DFS detector (src/graph/queries.ts:225) on two channels:

  • MCP tool (codegraph_check) — callable by AI agents, ships functional but NOT in DEFAULT_MCP_TOOLS (deliberate surface-trim per CLAUDE.md eval evidence)
  • CLI subcommand (codegraph check [path]) — deterministic, git-hook ready (exits non-zero on cycles)

Closes #888

What Changed

5 files, 341 insertions, 0 deletions:

  • src/mcp/tools.ts — tool def (~line 600) + dispatch case (~line 1123) + handleCheck handler (~line 3256)
  • src/bin/codegraph.tscodegraph check [path] subcommand (~line 1859), process.exitCode = 1 on cycles
  • __tests__/mcp-check-circular-imports.test.ts (new) — 4 MCP tests
  • __tests__/cli-check.test.ts (new) — 2 CLI tests
  • CHANGELOG.md[Unreleased] entry

Why

The DFS detector at src/graph/queries.ts:225 was fully implemented with zero callers — dead code. Circular imports cause build failures, slow bundlers, and create maintenance traps. This PR completes the feature by wiring the existing algorithm to both agent and developer workflows.

The CLI channel is the primary path (deterministic, user-invoked). The MCP channel is correctly low-salience — DEFAULT_MCP_TOOLS remains unchanged per CLAUDE.md's evidence that agents under-pick new tools.

Design Decisions

Decision Rationale
codegraph_check NOT in DEFAULT_MCP_TOOLS CLAUDE.md eval evidence: agents under-pick new tools; adding unproven tools dilutes selection
CLI exits non-zero on cycles Git-hook ready (codegraph check || exit 1); --json flag available for scripts
Both channels sort deterministically Byte-stable output across runs; identical comparator in MCP handler + CLI action
CLI indexes via library API Avoids interactive prompts from codegraph init --index; sets CODEGRAPH_NO_DAEMON=1

Verification

✓ tsc --noEmit → exit 0
✓ npm run build → dist verified (tools.js has codegraph_check, bin has check cmd)
✓ 6/6 new tests pass (4 MCP + 2 CLI)
✓ 46/46 MCP-dispatch tests pass (no regression)
✓ Full suite: 15 pre-existing Windows failures only (all in untouched modules, CLAUDE.md-documented)

Test Coverage

Test Asserts
MCP: cycle detection Detects a two-file import cycle and names both files in output
MCP: no cycles Reports clean success message (not an error) when there are no cycles
MCP: multi-cycle Reports each cycle separately when more than one exists
MCP: surface trim Is callable directly but NOT listed in the default tool surface
CLI: acyclic Exits 0 and reports no cycles on an acyclic project
CLI: cyclic Exits non-zero and names both files when a cycle exists

Migration

None. codegraph_check is a new MCP tool (additive). codegraph check is a new CLI subcommand (additive). No schema changes. No breaking changes.

To enable codegraph_check for agents, add check to CODEGRAPH_MCP_TOOLS env var. To run as a git hook: codegraph check || exit 1 in .git/hooks/pre-commit.

Wires the existing (previously uncalled) CodeGraph.findCircularDependencies() DFS detector into the MCP tool surface. No new algorithm; the handler formats the string[][] result as markdown. Tool is callable but not added to DEFAULT_MCP_TOOLS -- respects the deliberate 4-tool surface trim.
Prints file-level import cycles and exits non-zero when any are found, so it works as a git pre-commit hook. Backed by the same findCircularDependencies() DFS as the codegraph_check MCP tool.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: circular import detection via MCP tool + CLI subcommand

1 participant