MCP server for knip.dev integration to help AI agents identify and clean up unused code
npm install knip-mcp-serverA Model Control Protocol (MCP) server that integrates with knip.dev to help AI agents identify and clean up unused files, imports, exports, and dependencies in JavaScript/TypeScript projects.
This MCP server provides AI agents with powerful code cleanup capabilities through knip.dev integration. It's designed to work seamlessly with Cursor AI and other MCP-compatible tools to maintain clean, efficient codebases.
- Code Analysis: Run comprehensive knip analysis to find unused code
- Unused File Detection: Identify files that are no longer referenced
- Import/Export Cleanup: Find and remove unused imports and exports
- Dependency Management: Detect unused npm dependencies and devDependencies
- Safe Operations: Built-in safety checks and backup mechanisms
- Workspace Support: Full support for monorepo and workspace setups
- Dry Run Mode: Preview changes before applying them
``bash`
npm install -g @genar/knip-mcp-server
Or in your project:
`bash`
npm install @genar/knip-mcp-server
Run the server directly:
`bash`
knip-mcp-server
Or use npx:
`bash`
npx @genar/knip-mcp-server
Configure via environment variables:
`bash`
export KNIP_PROJECT_ROOT="/path/to/your/project"
export KNIP_CONFIG_PATH="/path/to/knip.json"
export KNIP_BACKUP_DIR="/path/to/backups"
export KNIP_LOG_LEVEL="info"
export KNIP_SAFE_MODE="true"
#### Option 1: MCP Server Configuration
1. In Cursor AI settings, navigate to MCP configuration
2. Add a new MCP server:
- Name: "Knip Code Cleanup"
- Command: npx @genar/knip-mcp-server
3. Save and restart Cursor AI
#### Option 2: Cursor Rules Configuration
You can also integrate this MCP server through Cursor's rules system by creating a .cursor/rules directory in your project root and adding the following configuration:
1. Create the rules directory structure:
`bash`
mkdir -p .cursor/rules
2. Create a knip rule file (.cursor/rules/knip-mcp.md):
`markdownKnip Code Cleanup Rules
You have access to the Knip MCP server for code cleanup tasks. Use these tools proactively to maintain clean code:
- knip_scan - Run comprehensive code analysisknip_get_unused_files
- - Find unused filesknip_get_unused_exports
- - Find unused exportsknip_get_unused_imports
- - Find unused imports knip_get_unused_dependencies
- - Find unused dependenciesknip_remove_unused_files
- - Safely remove unused filesknip_remove_unused_imports
- - Clean up unused importsknip_fix_issues
- - Auto-fix common issuesknip_get_config
- - View knip configurationknip_validate_config
- - Validate knip config
1. After implementing features: Always run knip_scan to identify cleanup opportunitiesknip_fix_issues
2. Before commits: Use with dryRun: true to preview cleanupknip_get_unused_dependencies
3. Regular maintenance: Run to audit packages
4. Safety first: Always use dry-run mode first, create backups for destructive operations
When asked to implement features:
1. Complete the implementation
2. Run knip_scan to analyze unused codeknip_remove_unused_imports
3. Use to clean importsknip_get_unused_files
4. Check for orphaned files
5. Apply fixes with appropriate safety measures
- All destructive operations default to dry-run mode
- Backups are created automatically before modifications
- File limits prevent accidental bulk deletions
- Safe mode protects critical files (package.json, etc.)
Remember: Always review suggestions before applying changes to ensure they don't break functionality.
`
3. Create a main rules file (.cursor/rules/main.md) that includes knip rules:
`markdownProject Development Rules
Include code cleanup and maintenance guidelines.
@knip-mcp.md
- Use the Knip MCP server for regular code cleanup
- Maintain clean imports and remove unused dependencies
- Follow the knip workflow after feature implementation
`
4. Optional: Add to your .gitignore if you want to keep rules private:`bash`
echo ".cursor/" >> .gitignore
#### Option 3: Per-Project MCP Configuration
Create a local MCP configuration file in your project root (.mcp-config.json):
`json`
{
"servers": {
"knip": {
"command": "npx",
"args": ["@genar/knip-mcp-server"],
"env": {
"KNIP_PROJECT_ROOT": ".",
"KNIP_LOG_LEVEL": "info",
"KNIP_SAFE_MODE": "true"
}
}
}
}
Then reference this configuration in Cursor's MCP settings.
Parameters:
- projectPath (optional): Project path to analyzeworkspace
- (optional): Specific workspace for monoreposincludePaths
- (optional): Array of paths to includeexcludePaths
- (optional): Array of paths to excludedryRun
- (default: true): Run without making changes
Parameters:
- workspace (optional): Specific workspace to analyze
Parameters:
- workspace (optional): Specific workspace to analyzefilePath
- (optional): Get exports for specific file only
Parameters:
- workspace (optional): Specific workspace to analyzefilePath
- (optional): Get imports for specific file only
Parameters:
- workspace (optional): Specific workspace to analyzetype
- (optional): 'dependencies', 'devDependencies', or 'all'
Parameters:
- files (optional): Specific files to remove, or all if emptydryRun
- (default: true): Preview modecreateBackup
- (default: true): Create backups before removalbackupDir
- (optional): Custom backup directorymaxFiles
- (default: 50): Safety limit
Parameters:
- filePath (required): File to clean upimports
- (optional): Specific imports to removedryRun
- (default: true): Preview modecreateBackup
- (default: true): Create backup before changes
Parameters:
- issueTypes (default: ['imports']): Types of issues to fixdryRun
- (default: true): Preview modecreateBackup
- (default: true): Create backupsmaxFiles
- (default: 25): Safety limit
Parameters:
- projectPath (optional): Project pathconfigPath
- (optional): Specific config file path
Parameters:
- projectPath (optional): Project pathconfigPath
- (optional): Specific config file pathconfig
- (optional): Configuration object to validate
`
AI Agent: I just implemented a new authentication feature. Let me clean up any unused code.
1. Run knip_scan to get overview of unused code
2. Use knip_get_unused_imports to find unused imports
3. Apply knip_remove_unused_imports to clean up imports
4. Check knip_get_unused_files for any orphaned files
5. Safely remove files using knip_remove_unused_files
`
`
AI Agent: Let me audit our dependencies for unused packages.
1. Run knip_get_unused_dependencies
2. Review the list of unused dependencies
3. Generate recommendation report for package.json cleanup
`
`
AI Agent: Let me analyze the overall health of this codebase.
1. Run knip_scan for comprehensive analysis
2. Use knip_validate_config to check configuration
3. Generate health report with recommendations
`
- Dry Run Mode: All destructive operations default to dry-run mode
- Backup Creation: Automatic backups before file modifications
- Safe Mode: Prevents removal of critical files (package.json, etc.)
- File Limits: Configurable limits on batch operations
- Error Handling: Comprehensive error reporting and recovery
The server supports knip configuration files:
- knip.jsonknip.jsonc
- .knip.json
- .knip.jsonc
- knip.config.js
- knip.config.ts
-
`bashClone the repository
git clone https://github.com/genar/galleries.git
cd galleries/packages/knip-mcp-server
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests
5. Submit a pull request
MIT License - see LICENSE file for details.
- GitHub Issues: Report bugs or request features
- Documentation: Full documentation
- knip.dev - The underlying code analysis tool
- MCP SDK - Model Context Protocol implementation
- Cursor AI - AI-powered code editor with MCP support