Tools

analyze_quality

Complexity metrics and code health analysis

The analyze_quality tool measures code quality through multiple dimensions: complexity scores, circular dependency detection, and unused code identification. It's essential for understanding code health and identifying areas that need attention.

How It Works

Stellarion uses the ComplexityAnalyzer engine with tree-sitter AST parsing to calculate accurate complexity metrics. It respects .gitignore patterns and excludes common directories like node_modules, build artifacts, and the .stellarion data directory.

When to Use

  • Measuring code complexity: Find functions and files that are hard to understand or maintain
  • Finding circular dependencies: Detect import cycles that cause issues
  • Detecting unused code: Identify dead code that can be removed
  • Assessing overall code health: Get a comprehensive view of project quality
  • Planning refactoring: Prioritize improvements based on severity

Parameters

ParameterTypeRequiredDefaultDescription
typestringNocomplexityAnalysis type: complexity, circular_deps, or unused_code
pathstringNo.Directory or file to analyze
thresholdnumberNo10Complexity threshold for filtering results
metricstringNocyclomaticMetric to use: cyclomatic, cognitive, or both
groupBySeveritybooleanNotrueGroup results by severity level
includeTestsbooleanNofalseInclude test files in analysis
limitnumberNo50Maximum results to return
summaryOnlybooleanNofalseReturn only statistics without detailed file lists

MCP Command Syntax

mcp__stellarion__analyze_quality type:complexity path:src/ threshold:15 groupBySeverity:true

Analysis Types

Complexity Analysis

Measure cyclomatic and cognitive complexity of your code.

Natural Language:

Find all complex functions in my codebase

Direct MCP Call:

mcp__stellarion__analyze_quality type:complexity path:src/ threshold:15 metric:both

Returns: Functions exceeding the threshold, grouped by severity


Circular Dependencies

Detect circular import chains that can cause issues.

Natural Language:

Are there any circular dependencies in this project?

Direct MCP Call:

mcp__stellarion__analyze_quality type:circular_deps path:src/

Returns: All circular dependency chains with severity classification


Unused Code Detection

Find potentially unused functions, classes, and variables.

Natural Language:

What code in this project might be unused?

Direct MCP Call:

mcp__stellarion__analyze_quality type:unused_code path:src/ includeTests:false

Returns: Potentially unused elements grouped by type

Examples

Find Complex Functions

Natural Language:

Show me all functions with cyclomatic complexity over 20

Direct MCP Call:

mcp__stellarion__analyze_quality type:complexity threshold:20 metric:cyclomatic groupBySeverity:true

Get Cognitive Complexity

Natural Language:

Which functions are hardest to understand (high cognitive complexity)?

Direct MCP Call:

mcp__stellarion__analyze_quality type:complexity threshold:15 metric:cognitive

Check Specific Directory

Natural Language:

Analyze complexity in the services directory

Direct MCP Call:

mcp__stellarion__analyze_quality type:complexity path:src/services/ threshold:10

Get Summary Statistics Only

Natural Language:

Give me a high-level summary of code complexity, not detailed files

Direct MCP Call:

mcp__stellarion__analyze_quality type:complexity summaryOnly:true

Find All Circular Dependencies

Natural Language:

Find circular dependencies and show me the severity

Direct MCP Call:

mcp__stellarion__analyze_quality type:circular_deps minCycleLength:2

Identify Dead Code

Natural Language:

What functions and classes might be unused?

Direct MCP Call:

mcp__stellarion__analyze_quality type:unused_code groupByType:true

Output Format

Complexity Results

Each function/file includes:

FieldDescription
Function nameName of the function or method
File pathLocation in the codebase
Line numbersStart and end lines
Cyclomatic complexityNumber of independent paths through the code
Cognitive complexityMeasure of how hard the code is to understand
Maintainability indexScore from 0-100 (higher is better)
SeverityCritical, high, medium, or low

Circular Dependency Results

FieldDescription
Files in cycleList of files involved
Cycle lengthNumber of files in the chain
SeverityBased on cycle length and file importance

Unused Code Results

FieldDescription
Element nameFunction, class, or variable name
Element typeWhat kind of element it is
File locationWhere it's defined
ConfidenceHow confident the detection is

Understanding Complexity Metrics

Cyclomatic Complexity

Measures the number of independent paths through the code. Each decision point (if, for, while, case, catch, etc.) increases the count.

ScoreInterpretationRecommendation
1-10SimpleGenerally acceptable
11-20ModerateConsider simplification
21-30ComplexShould be refactored
31+Very complexHigh priority for refactoring

Cognitive Complexity

Measures how difficult code is to understand, accounting for nesting depth and break in linear flow.

ScoreInterpretationRecommendation
0-5Easy to understandExcellent
6-10Moderate effortAcceptable
11-15Hard to understandNeeds attention
16+Very difficultRefactor urgently

Maintainability Index

A composite score from 0-100 based on complexity, lines of code, and Halstead metrics.

ScoreInterpretation
65-100Highly maintainable
35-64Moderately maintainable
0-34Difficult to maintain

Severity Levels

LevelCyclomaticDescriptionAction
Critical> 30Extremely complexImmediate refactoring needed
High20-30Very complexPlan refactoring soon
Medium15-20Moderately complexMonitor and consider improvements
Low10-15Acceptable complexityNo immediate action needed

Tips for Quality Analysis

  1. Start with a threshold of 15: This catches significant issues without too much noise
  2. Use groupBySeverity: true: Makes it easy to prioritize fixes
  3. Check circular deps when you have import issues: They often cause mysterious bugs
  4. Run unused code detection periodically: Remove dead code to reduce maintenance burden
  5. Use summaryOnly: true for quick health checks: Get the big picture without details

Combining with Other Tools

  1. Quality + Structure: High complexity in highly-coupled modules is a red flag
  2. Quality + Refactoring: Use complexity results to guide refactoring priorities
  3. Quality + Reports: Generate quality reports for tracking over time