Skip to main content
Glama
renjismzy

Smart Code Reviewer

by renjismzy

analyze_code_quality

Evaluate code quality by identifying issues, suggesting improvements, and detecting vulnerabilities across multiple programming languages to enhance software reliability and maintainability.

Instructions

分析代码质量,检测潜在问题和改进建议

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYes要分析的代码内容
filenameNo文件名(可选)
languageYes编程语言 (javascript, typescript, python, java, etc.)

Implementation Reference

  • Core handler function implementing the analyze_code_quality tool logic: calculates metrics, detects issues across multiple languages (JS/TS, Python, Java), and generates recommendations.
    export async function analyzeCodeQuality( code: string, language: string, filename?: string ): Promise<CodeAnalysisResult> { const issues: CodeIssue[] = []; const metrics: QualityMetrics = { linesOfCode: 0, complexity: 0, maintainabilityIndex: 0, duplicateLines: 0, testCoverage: 0 }; // 基础指标计算 const lines = code.split('\n'); metrics.linesOfCode = lines.filter(line => line.trim().length > 0).length; // 通用代码问题检测 await detectCommonIssues(code, language, issues); // 语言特定分析 switch (language.toLowerCase()) { case 'javascript': case 'typescript': await analyzeJavaScript(code, issues, metrics); break; case 'python': await analyzePython(code, issues, metrics); break; case 'java': await analyzeJava(code, issues, metrics); break; default: await analyzeGeneric(code, issues, metrics); } // 计算可维护性指数 metrics.maintainabilityIndex = calculateMaintainabilityIndex(metrics, issues.length); return { filename: filename || 'unknown', language, metrics, issues, overallScore: calculateOverallScore(metrics, issues), recommendations: generateRecommendations(issues, metrics) }; }
  • TypeScript interfaces defining the input parameters, output structure (CodeAnalysisResult, QualityMetrics, CodeIssue) for the analyze_code_quality tool.
    export interface CodeAnalysisResult { filename: string; language: string; metrics: QualityMetrics; issues: CodeIssue[]; overallScore: number; recommendations: string[]; } export interface QualityMetrics { linesOfCode: number; complexity: number; maintainabilityIndex: number; duplicateLines: number; testCoverage: number; } export interface CodeIssue { type: 'style' | 'maintainability' | 'performance' | 'error-handling' | 'security'; severity: 'error' | 'warning' | 'info'; message: string; line: number; column: number; rule: string; suggestion?: string; }
  • src/index.ts:44-64 (registration)
    MCP tool registration in listToolsRequest handler: defines name 'analyze_code_quality', description, and JSON schema for input validation.
    { name: 'analyze_code_quality', description: '分析代码质量,检测潜在问题和改进建议', inputSchema: { type: 'object', properties: { code: { type: 'string', description: '要分析的代码内容' }, language: { type: 'string', description: '编程语言 (javascript, typescript, python, java, etc.)' }, filename: { type: 'string', description: '文件名(可选)' } }, required: ['code', 'language'] }
  • MCP CallToolRequest dispatch handler for 'analyze_code_quality': validates arguments using Zod, calls core analyzeCodeQuality function, formats response.
    private async handleAnalyzeCodeQuality(args: any) { const schema = z.object({ code: z.string(), language: z.string(), filename: z.string().optional() }); const { code, language, filename } = schema.parse(args); const result = await analyzeCodeQuality(code, language, filename); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2) } ] }; }
  • Helper utility for calculating cyclomatic complexity metric, used in quality analysis for all languages.
    function calculateCyclomaticComplexity(code: string): number { let complexity = 1; // 基础复杂度 // 计算决策点 const decisionKeywords = [ 'if', 'else if', 'elif', 'while', 'for', 'foreach', 'switch', 'case', 'catch', 'and', 'or', '&&', '||', '?', 'try' ]; decisionKeywords.forEach(keyword => { const escapedKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const regex = new RegExp(`\\b${escapedKeyword}\\b`, 'gi'); const matches = code.match(regex); if (matches) { complexity += matches.length; } }); return complexity; }

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