calculate_complexity
Analyze code complexity metrics such as cyclomatic and cognitive complexity to assess maintainability and identify potential refactoring needs using Smart Code Reviewer.
Instructions
计算代码复杂度指标(圈复杂度、认知复杂度等)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | 要分析的代码 | |
| language | Yes | 编程语言 |
Implementation Reference
- src/tools/complexityCalculator.ts:6-51 (handler)Core handler function implementing the calculate_complexity tool logic, computing cyclomatic complexity, cognitive complexity, Halstead metrics, maintainability index, and providing analysis/recommendations.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) }; }
- src/index.ts:264-281 (handler)MCP server handler for 'calculate_complexity' tool that validates input using Zod and delegates to calculateComplexity function.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)Registration of the 'calculate_complexity' tool in the MCP server's tool list, including name, description, and input schema.{ name: 'calculate_complexity', description: '计算代码复杂度指标(圈复杂度、认知复杂度等)', inputSchema: { type: 'object', properties: { code: { type: 'string', description: '要分析的代码' }, language: { type: 'string', description: '编程语言' } }, required: ['code', 'language'] } }
- src/index.ts:133-144 (schema)Input schema definition for the calculate_complexity tool.inputSchema: { type: 'object', properties: { code: { type: 'string', description: '要分析的代码' }, language: { type: 'string', description: '编程语言' } },