Skip to main content
Glama
renjismzy

Smart Code Reviewer

by renjismzy

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
NameRequiredDescriptionDefault
codeYes要分析的代码
languageYes编程语言

Implementation Reference

  • 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) }; }
  • 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'] } }
  • Input schema definition for the calculate_complexity tool.
    inputSchema: { type: 'object', properties: { code: { type: 'string', description: '要分析的代码' }, language: { type: 'string', description: '编程语言' } },

Other Tools

Related Tools

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