Skip to main content
Glama
renjismzy

Smart Code Reviewer

by renjismzy

calculate_complexity

Analyze code complexity metrics like cyclomatic and cognitive complexity to identify maintainability issues and refactoring opportunities in multiple programming languages.

Instructions

计算代码复杂度指标(圈复杂度、认知复杂度等)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYes要分析的代码
languageYes编程语言

Implementation Reference

  • Core implementation of the calculate_complexity tool: computes cyclomatic complexity, cognitive complexity, Halstead metrics, maintainability index, and provides analysis, recommendations, and risk level.
    export async function calculateComplexity( code: string, language: string ): Promise<ComplexityResult> { const metrics: ComplexityMetrics = { cyclomaticComplexity: 0, cognitiveComplexity: 0, halsteadComplexity: { vocabulary: 0, length: 0, difficulty: 0, effort: 0, volume: 0, bugs: 0, time: 0 }, maintainabilityIndex: 0, linesOfCode: 0, logicalLinesOfCode: 0, commentLines: 0, blankLines: 0 }; // 基础指标计算 calculateBasicMetrics(code, metrics); // 圈复杂度计算 metrics.cyclomaticComplexity = calculateCyclomaticComplexity(code, language); // 认知复杂度计算 metrics.cognitiveComplexity = calculateCognitiveComplexity(code, language); // Halstead复杂度计算 metrics.halsteadComplexity = calculateHalsteadComplexity(code, language); // 可维护性指数计算 metrics.maintainabilityIndex = calculateMaintainabilityIndex(metrics); return { language, metrics, analysis: analyzeComplexity(metrics), recommendations: generateComplexityRecommendations(metrics), riskLevel: assessRiskLevel(metrics) }; }
  • MCP tool handler wrapper: validates input arguments using Zod and delegates to the core calculateComplexity function, formats result as MCP content.
    private async handleCalculateComplexity(args: any) { const schema = z.object({ code: z.string(), language: z.string() }); const { code, language } = schema.parse(args); const result = await calculateComplexity(code, language); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2) } ] }; }
  • src/index.ts:130-147 (registration)
    Tool registration in MCP server's listTools handler: defines name, description, and JSON input schema for calculate_complexity.
    { name: 'calculate_complexity', description: '计算代码复杂度指标(圈复杂度、认知复杂度等)', inputSchema: { type: 'object', properties: { code: { type: 'string', description: '要分析的代码' }, language: { type: 'string', description: '编程语言' } }, required: ['code', 'language'] } }
  • TypeScript interfaces defining the output structure (ComplexityResult, ComplexityMetrics, HalsteadMetrics) used by the tool.
    export interface ComplexityResult { language: string; metrics: ComplexityMetrics; analysis: string; recommendations: string[]; riskLevel: 'low' | 'medium' | 'high' | 'critical'; } export interface ComplexityMetrics { cyclomaticComplexity: number; cognitiveComplexity: number; halsteadComplexity: HalsteadMetrics; maintainabilityIndex: number; linesOfCode: number; logicalLinesOfCode: number; commentLines: number; blankLines: number; } export interface HalsteadMetrics { vocabulary: number; length: number; difficulty: number; volume: number; effort: number; bugs: number; time: number; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/renjismzy/mcp-code'

If you have feedback or need assistance with the MCP directory API, please join our Discord server