PR: Add detect_cycles MCP Tool for Circular Dependency Detection#61
Merged
userFRM merged 2 commits intouserFRM:mainfrom Feb 26, 2026
Merged
PR: Add detect_cycles MCP Tool for Circular Dependency Detection#61userFRM merged 2 commits intouserFRM:mainfrom
userFRM merged 2 commits intouserFRM:mainfrom
Conversation
Implements a new MCP tool that identifies circular dependencies in codebases
using a funnel design pattern with TOON-optimized output.
## New Features
- Cycle detection using DFS algorithm with deduplication
- TOON-optimized output format for LLM efficiency
- Funnel design: summary first, then filtered details on follow-up calls
- Area breakdown showing cycles per hierarchy area
- Cross-file and cross-area cycle filtering
- Respects .rpgignore patterns
## Parameters (all optional)
- area: Filter by hierarchy area(s), comma-separated
- max_cycles: Limit cycles returned
- min_cycle_length: Skip trivial 2-cycles
- cross_file_only: Only multi-file cycles
- cross_area_only: Only multi-area cycles
- sort_by: length/file_count/entity_count
- include_files: Include file paths
- ignore_rpgignore: Include ignored files
## Output
First call returns summary + area breakdown + next_step guidance.
Subsequent calls with filters show specific cycle details.
Example summary output:
cycles: {total: 512, entities: 467, files: 46, areas: 8, cross_file: 152}
area_breakdown[N]{area,cycles,len2,len3,len4+,files}:
## Testing
- 16 unit tests covering all filter paths
- MCP integration tests for edge cases
- Bug fixes: area breakdown counts, files_in_cycles computation
## Files
- crates/rpg-nav/src/cycles.rs (new) - Core algorithm
- crates/rpg-mcp/src/tools.rs - Tool handler
- crates/rpg-mcp/src/params.rs - Parameters
- crates/rpg-mcp/src/prompts/server_instructions.md - Docs
…ies using a funnel design pattern.
7 tasks
userFRM
approved these changes
Feb 26, 2026
Owner
userFRM
left a comment
There was a problem hiding this comment.
Code Review
Verified locally: clean fast-forward merge, fmt, clippy (default + --no-default-features), all workspace tests pass.
What's good
- Solid DFS algorithm: Modified DFS with recursion stack tracking, lexicographic normalization for dedup. Correctly excludes
Containsedges. - Good funnel design: First call returns summary + area breakdown +
next_stepprompt. Filtered calls return detailed cycles. The TOON output is compact and LLM-friendly. - Thorough filtering: area, cross-file, cross-area, min/max length, sort options. All tested independently.
- 16 unit tests covering: linear chains (no cycles), 2-node/3-node/multi-cycle graphs, all filter paths, deterministic output, edge case (empty graph),
include_files=falsecorrectness. - Stats computed from entity data, not cycle.files — correct even when
include_files=false. - Area breakdown counts cycles, not entities — explicitly tested with a dedicated regression test.
- Custom glob_match avoids a crate dep for the .rpgignore filtering.
- MCP integration: params, tool handler, server_instructions.md all consistent.
Minor observations (non-blocking)
- The algorithm notes honestly state it doesn't find ALL simple cycles (Johnson's would be needed) — good transparency. Representative cycles are the right trade-off for architectural analysis.
- The
max_cycle_lengthdefault of 20 is a reasonable safety valve against exponential blowup.
LGTM. Merging.
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
Implement a new MCP tool
detect_cyclesthat identifies circular dependencies in codebases using a funnel design pattern with TOON-optimized output.Changes
New Files
crates/rpg-nav/src/cycles.rs- Core cycle detection algorithm using DFSModified Files
crates/rpg-nav/src/lib.rs- Export cycles modulecrates/rpg-mcp/src/params.rs- Add DetectCyclesParamscrates/rpg-mcp/src/tools.rs- Tool handler with TOON-optimized outputcrates/rpg-mcp/src/prompts/server_instructions.md- DocumentationFeatures
Parameters (all optional)
area"Navigation")max_cycles10)min_cycle_length3+)max_cycle_lengthcross_file_onlycross_area_onlysort_by"length","file_count","entity_count"include_filesignore_rpgignoreOutput Examples
No Parameters (Summary)
With Filters
Agent Guidance (Funnel Design)
The tool is designed with a funnel pattern to guide agents:
First call (no params): Returns summary statistics, length distribution, area breakdown, and available areas. Shows
next_stepprompting to call again with filters.Subsequent calls: Agent uses filters to get specific cycle details.
The
next_stepline in every response guides the agent:This is documented in server_instructions.md so the LLM knows how to use the tool effectively.
Testing
Unit Tests (16 tests, all pass)
MCP Integration Tests
{}{"area": "Navigation"}{"cross_file_only": true}{"cross_area_only": true}{"cross_file": true, "cross_area": true}{"max_cycles": 5}{"max_cycles": 0}{"include_files": false}Algorithm Notes
Containsedge kinds (only structural).rpgignorepatternsRelated
analyze_healthtool