Skip to main content
Glama
linting.ts7.39 kB
import { z } from "zod"; /** * Check Imports Tool * Validates import statements for correctness */ export const checkImportsSchema = { name: "check_imports", description: "Checks import statements in code for common issues like circular dependencies, unused imports, and missing modules.", inputSchema: z.object({ code: z.string().describe("The code to check imports for"), language: z.string().describe("Programming language (javascript, typescript, python)") }) }; export function checkImportsHandler(args: any) { const { code, language } = args; const lines = code.split("\n"); const issues: string[] = []; const imports: string[] = []; // Extract imports based on language if (language === "javascript" || language === "typescript") { lines.forEach((line: string, i: number) => { if (line.includes("import ") || line.includes("require(")) { imports.push(line.trim()); // Check for common issues if (line.includes("* as") && !line.includes("from")) { issues.push(`Line ${i + 1}: Invalid wildcard import syntax`); } if (line.includes("..")) { // Potentially deep relative import const depth = (line.match(/\.\.\//g) || []).length; if (depth > 3) { issues.push(`Line ${i + 1}: Deep relative import (${depth} levels) - consider absolute imports`); } } } }); } else if (language === "python") { lines.forEach((line: string, i: number) => { if (line.startsWith("import ") || line.startsWith("from ")) { imports.push(line.trim()); if (line.includes("import *")) { issues.push(`Line ${i + 1}: Wildcard import detected - avoid 'import *'`); } } }); } const result = `# Import Analysis: ${language} ## Imports Found (${imports.length}) ${imports.map(i => `- \`${i}\``).join("\n") || "No imports found"} ## Issues Detected (${issues.length}) ${issues.length > 0 ? issues.map(i => `- ⚠️ ${i}`).join("\n") : "✅ No issues detected"} ## Recommendations - Keep imports organized (external, internal, relative) - Remove unused imports - Prefer named imports over wildcards - Use absolute imports for clarity `; return { content: [{ type: "text", text: result }] }; } /** * Lint Code Tool * Provides linting feedback for code */ export const lintCodeSchema = { name: "lint_code", description: "Analyzes code for style issues, potential bugs, and best practice violations.", inputSchema: z.object({ code: z.string().describe("The code to lint"), language: z.string().describe("Programming language"), rules: z.array(z.string()).optional().describe("Specific rules to check") }) }; export function lintCodeHandler(args: any) { const { code, language, rules = [] } = args; const lines = code.split("\n"); const warnings: string[] = []; const errors: string[] = []; lines.forEach((line: string, i: number) => { const lineNum = i + 1; // Universal checks if (line.length > 120) { warnings.push(`Line ${lineNum}: Line exceeds 120 characters (${line.length})`); } if (line.includes("\t")) { warnings.push(`Line ${lineNum}: Tab character detected - prefer spaces`); } if (line.endsWith(" ")) { warnings.push(`Line ${lineNum}: Trailing whitespace`); } // Language-specific checks if (language === "javascript" || language === "typescript") { if (line.includes("var ")) { errors.push(`Line ${lineNum}: Use 'let' or 'const' instead of 'var'`); } if (line.includes("== ") && !line.includes("=== ")) { warnings.push(`Line ${lineNum}: Use strict equality (===) instead of loose equality (==)`); } if (line.includes("console.log")) { warnings.push(`Line ${lineNum}: console.log found - remove before production`); } if (line.includes("any")) { warnings.push(`Line ${lineNum}: 'any' type detected - consider using specific type`); } } if (language === "python") { if (line.includes("except:") && !line.includes("except ")) { errors.push(`Line ${lineNum}: Bare except clause - specify exception type`); } if (line.includes("print(") && !line.includes("# debug")) { warnings.push(`Line ${lineNum}: print() found - use logging instead`); } } }); const result = `# Lint Report: ${language} ## Summary - **Errors**: ${errors.length} - **Warnings**: ${warnings.length} - **Lines Analyzed**: ${lines.length} ## Errors ${errors.length > 0 ? errors.map(e => `- ❌ ${e}`).join("\n") : "✅ No errors"} ## Warnings ${warnings.length > 0 ? warnings.map(w => `- ⚠️ ${w}`).join("\n") : "✅ No warnings"} ## Score ${errors.length === 0 && warnings.length < 3 ? "🌟 Excellent!" : errors.length === 0 ? "👍 Good" : "⚠️ Needs improvement"} `; return { content: [{ type: "text", text: result }] }; } /** * Format Code Tool * Provides formatting suggestions */ export const formatCodeSchema = { name: "format_code", description: "Provides formatted version of code following language-specific style guides.", inputSchema: z.object({ code: z.string().describe("The code to format"), language: z.string().describe("Programming language"), indent: z.number().optional().describe("Indentation size (default: 2 for JS, 4 for Python)") }) }; export function formatCodeHandler(args: any) { const { code, language, indent } = args; const defaultIndent = language === "python" ? 4 : 2; const indentSize = indent || defaultIndent; const indentStr = " ".repeat(indentSize); let formatted = code; const suggestions: string[] = []; // Basic formatting formatted = formatted.replace(/\t/g, indentStr); formatted = formatted.replace(/[ \t]+$/gm, ""); // Remove trailing whitespace // Language-specific suggestions if (language === "javascript" || language === "typescript") { if (!formatted.endsWith("\n")) { suggestions.push("Add newline at end of file"); } if (formatted.includes("{ }")) { suggestions.push("Empty blocks should be on separate lines"); } } if (language === "python") { if (formatted.includes(" #")) { suggestions.push("Add two spaces before inline comments"); } } const result = `# Format Analysis: ${language} ## Formatting Applied - Converted tabs to ${indentSize} spaces - Removed trailing whitespace - Normalized line endings ## Additional Suggestions ${suggestions.length > 0 ? suggestions.map(s => `- ${s}`).join("\n") : "✅ Code follows formatting guidelines"} ## Style Guide Reference - ${language === "python" ? "PEP 8" : language === "javascript" || language === "typescript" ? "Prettier/ESLint" : "Standard conventions"} `; return { content: [{ type: "text", text: result }] }; }

Implementation Reference

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/millsydotdev/Code-MCP'

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