get_imports
Analyze JavaScript/TypeScript code by extracting import statements to audit dependencies, identify heavy or suspicious packages, and prepare for refactoring or license compliance.
Instructions
Get all import statements with detailed module and specifier information. Essential for dependency analysis.
Examples: • Dependency audit: get_imports() to see all external dependencies • Bundle analysis: get_imports() to identify heavy imports • Security audit: get_imports() to check for suspicious packages • TypeScript analysis: get_imports({includeTypeImports: false}) to focus on runtime imports • Refactoring prep: get_imports() to understand module structure before changes • License compliance: get_imports() to generate dependency list
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| includeTypeImports | No | Include TypeScript type-only imports (default: true). Set false for runtime dependency analysis. |
Input Schema (JSON Schema)
Implementation Reference
- src/index.ts:703-751 (handler)The core handler function for the 'get_imports' tool. It retrieves import statements from the current AST, optionally filters out type-only imports, processes them with helpers to extract details, updates analysis state, and returns formatted results.private async getImports(args: { includeTypeImports?: boolean }) { if (!this.currentAST) { return { content: [{ type: "text", text: "No AST loaded. Please use parse_code first.", }], isError: true, }; } try { let imports = this.currentAST.tree.imports(); if (args.includeTypeImports === false) { imports = imports.filter(imp => !imp.text.includes('type ')); } const importData = imports.map(imp => ({ module: this.extractModuleName(imp.text), specifiers: this.extractImportSpecifiers(imp.text), line: imp.line, column: imp.column, isTypeOnly: imp.text.includes('type '), text: imp.text, })); this.lastAnalysis = { ...this.lastAnalysis, imports: importData, timestamp: new Date(), } as AnalysisResult; return { content: [{ type: "text", text: `Found ${importData.length} imports:\n${JSON.stringify(importData, null, 2)}`, }], }; } catch (error) { return { content: [{ type: "text", text: `Error getting imports: ${error instanceof Error ? error.message : String(error)}`, }], isError: true, }; } }
- src/index.ts:268-280 (registration)Tool registration in the ListToolsRequestSchema response, defining the name, description, and input schema for 'get_imports'.{ name: "get_imports", description: "Get all import statements with detailed module and specifier information. Essential for dependency analysis.\n\nExamples:\n• Dependency audit: get_imports() to see all external dependencies\n• Bundle analysis: get_imports() to identify heavy imports\n• Security audit: get_imports() to check for suspicious packages\n• TypeScript analysis: get_imports({includeTypeImports: false}) to focus on runtime imports\n• Refactoring prep: get_imports() to understand module structure before changes\n• License compliance: get_imports() to generate dependency list", inputSchema: { type: "object", properties: { includeTypeImports: { type: "boolean", description: "Include TypeScript type-only imports (default: true). Set false for runtime dependency analysis." } }, }, },
- src/index.ts:428-429 (registration)Dispatch logic in CallToolRequestSchema handler that maps tool calls for 'get_imports' to the handler method.case "get_imports": return await this.getImports(args as { includeTypeImports?: boolean });
- src/index.ts:753-756 (helper)Helper function that extracts the module specifier from an import statement using regex matching on 'from' clause.private extractModuleName(importText: string): string { const match = importText.match(/from\s+['"]([^'"]+)['"]/); return match ? match[1] : 'unknown'; }
- src/index.ts:758-779 (helper)Helper function that parses various import specifier types (default, named, namespace) from the import statement text using regex.private extractImportSpecifiers(importText: string): string[] { const defaultMatch = importText.match(/import\s+(\w+)(?=\s*[,{]|\s+from)/); const namedMatch = importText.match(/import\s*(?:\w+\s*,\s*)?{([^}]+)}/); const namespaceMatch = importText.match(/import\s*\*\s*as\s+(\w+)/); const specifiers: string[] = []; if (defaultMatch) { specifiers.push(defaultMatch[1]); } if (namedMatch) { const named = namedMatch[1].split(',').map(s => s.trim().split(' as ')[0].trim()); specifiers.push(...named); } if (namespaceMatch) { specifiers.push(`* as ${namespaceMatch[1]}`); } return specifiers; }