File size: 8,695 Bytes
f44fd20 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
"""
Code Complexity Analyzer MCP Server
This MCP server analyzes Python code complexity and provides insights
about code quality, maintainability, and potential refactoring opportunities.
Tags: building-mcp-track-enterprise
"""
import ast
import re
from typing import Dict, List, Tuple
import gradio as gr
def calculate_cyclomatic_complexity(code: str) -> Dict[str, any]:
"""
Calculate cyclomatic complexity of Python code.
Args:
code: Python source code as a string
Returns:
Dictionary with complexity metrics
"""
try:
tree = ast.parse(code)
except SyntaxError as e:
return {"error": f"Syntax error in code: {str(e)}"}
complexity = 1 # Base complexity
# Count decision points that increase complexity
for node in ast.walk(tree):
if isinstance(node, (ast.If, ast.While, ast.For, ast.ExceptHandler)):
complexity += 1
elif isinstance(node, ast.BoolOp):
complexity += len(node.values) - 1
elif isinstance(node, (ast.ListComp, ast.DictComp, ast.SetComp, ast.GeneratorExp)):
complexity += 1
# Determine complexity level
if complexity <= 10:
level = "Low (Simple)"
recommendation = "Code is easy to understand and maintain."
elif complexity <= 20:
level = "Moderate"
recommendation = "Consider breaking into smaller functions."
elif complexity <= 50:
level = "High"
recommendation = "Refactoring strongly recommended. Break into smaller, focused functions."
else:
level = "Very High (Critical)"
recommendation = "Immediate refactoring required. Code is difficult to test and maintain."
return {
"cyclomatic_complexity": complexity,
"complexity_level": level,
"recommendation": recommendation
}
def analyze_code_metrics(code: str) -> Dict[str, any]:
"""
Analyze various code metrics including LOC, functions, classes, etc.
Args:
code: Python source code as a string
Returns:
Dictionary with code metrics
"""
try:
tree = ast.parse(code)
except SyntaxError as e:
return {"error": f"Syntax error in code: {str(e)}"}
lines = code.split('\n')
loc = len(lines)
# Count non-empty, non-comment lines
sloc = sum(1 for line in lines if line.strip() and not line.strip().startswith('#'))
# Count comments
comments = sum(1 for line in lines if line.strip().startswith('#'))
# Count functions and classes
functions = sum(1 for node in ast.walk(tree) if isinstance(node, ast.FunctionDef))
classes = sum(1 for node in ast.walk(tree) if isinstance(node, ast.ClassDef))
# Calculate comment ratio
comment_ratio = (comments / sloc * 100) if sloc > 0 else 0
return {
"lines_of_code": loc,
"source_lines_of_code": sloc,
"comment_lines": comments,
"comment_ratio_percent": round(comment_ratio, 2),
"functions": functions,
"classes": classes
}
def detect_code_smells(code: str) -> List[str]:
"""
Detect common code smells in Python code.
Args:
code: Python source code as a string
Returns:
List of detected code smells
"""
smells = []
try:
tree = ast.parse(code)
except SyntaxError:
return ["Syntax error prevents analysis"]
# Check for long functions (>50 lines)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
if hasattr(node, 'end_lineno') and hasattr(node, 'lineno'):
func_lines = node.end_lineno - node.lineno
if func_lines > 50:
smells.append(f"Long function '{node.name}' ({func_lines} lines)")
# Check for too many parameters
if isinstance(node, ast.FunctionDef):
param_count = len(node.args.args)
if param_count > 5:
smells.append(f"Function '{node.name}' has too many parameters ({param_count})")
# Check for deeply nested code (>4 levels)
if isinstance(node, (ast.If, ast.For, ast.While)):
depth = sum(1 for parent in ast.walk(tree)
if isinstance(parent, (ast.If, ast.For, ast.While)))
if depth > 4:
smells.append("Deeply nested code blocks detected")
break
# Check for duplicate code patterns (simple check)
lines = [line.strip() for line in code.split('\n') if line.strip()]
if len(lines) != len(set(lines)):
duplicate_count = len(lines) - len(set(lines))
if duplicate_count > 3:
smells.append(f"Possible duplicate code: {duplicate_count} duplicate lines")
if not smells:
smells.append("No obvious code smells detected!")
return smells
def full_code_analysis(code: str) -> str:
"""
Perform complete code analysis combining all metrics.
Args:
code: Python source code as a string
Returns:
Formatted analysis report
"""
if not code.strip():
return "Please provide Python code to analyze."
complexity = calculate_cyclomatic_complexity(code)
metrics = analyze_code_metrics(code)
smells = detect_code_smells(code)
# Build report
report = "# Code Analysis Report\n\n"
# Complexity section
report += "## Complexity Analysis\n"
if "error" in complexity:
report += f"Error: {complexity['error']}\n\n"
else:
report += f"- **Cyclomatic Complexity**: {complexity['cyclomatic_complexity']}\n"
report += f"- **Complexity Level**: {complexity['complexity_level']}\n"
report += f"- **Recommendation**: {complexity['recommendation']}\n\n"
# Metrics section
report += "## Code Metrics\n"
if "error" in metrics:
report += f"Error: {metrics['error']}\n\n"
else:
report += f"- **Total Lines**: {metrics['lines_of_code']}\n"
report += f"- **Source Lines**: {metrics['source_lines_of_code']}\n"
report += f"- **Comment Lines**: {metrics['comment_lines']}\n"
report += f"- **Comment Ratio**: {metrics['comment_ratio_percent']}%\n"
report += f"- **Functions**: {metrics['functions']}\n"
report += f"- **Classes**: {metrics['classes']}\n\n"
# Code smells section
report += "## Code Smells Detected\n"
for smell in smells:
report += f"- {smell}\n"
return report
# Create Gradio interface
with gr.Blocks(title="Code Complexity Analyzer MCP Server") as demo:
gr.Markdown("""
# Code Complexity Analyzer MCP Server
Analyze Python code complexity and quality metrics. This MCP server provides:
- Cyclomatic complexity calculation
- Code metrics (LOC, comments, functions, classes)
- Code smell detection
- Refactoring recommendations
**Category**: Enterprise MCP Server
**Tags**: building-mcp-track-enterprise
""")
with gr.Tab("Full Analysis"):
code_input = gr.Code(
label="Python Code",
language="python",
lines=20,
value="# Paste your Python code here\ndef example():\n pass"
)
analyze_btn = gr.Button("Analyze Code", variant="primary")
analysis_output = gr.Markdown(label="Analysis Report")
analyze_btn.click(
fn=full_code_analysis,
inputs=code_input,
outputs=analysis_output
)
with gr.Tab("Complexity Only"):
complexity_input = gr.Code(label="Python Code", language="python", lines=15)
complexity_btn = gr.Button("Calculate Complexity")
complexity_output = gr.JSON(label="Complexity Metrics")
complexity_btn.click(
fn=calculate_cyclomatic_complexity,
inputs=complexity_input,
outputs=complexity_output
)
with gr.Tab("Code Metrics"):
metrics_input = gr.Code(label="Python Code", language="python", lines=15)
metrics_btn = gr.Button("Analyze Metrics")
metrics_output = gr.JSON(label="Code Metrics")
metrics_btn.click(
fn=analyze_code_metrics,
inputs=metrics_input,
outputs=metrics_output
)
with gr.Tab("Code Smells"):
smells_input = gr.Code(label="Python Code", language="python", lines=15)
smells_btn = gr.Button("Detect Code Smells")
smells_output = gr.JSON(label="Detected Code Smells")
smells_btn.click(
fn=detect_code_smells,
inputs=smells_input,
outputs=smells_output
)
if __name__ == "__main__":
demo.launch(mcp_server=True, server_name="0.0.0.0", server_port=7860)
|