关于 Knip Mcp Server
Knip Mcp Server 将 knip.dev 的代码分析能力提供给 JavaScript 和 TypeScript 项目的 AI Agent,用于发现并清理未使用代码。它支持工作区,并以预览、备份和安全限制保护修改操作。
主要功能
- 01扫描未使用文件、导入和导出
- 02检测未使用的 npm 依赖
- 03支持 monorepo 工作区分析
- 04预览修改并在操作前备份
- 05校验和读取 Knip 配置
适用场景
- 01功能开发后清理闲置代码
- 02审查项目中的未使用依赖
- 03检查 monorepo 工作区的代码状况
MCP 相关
该 MCP 服务向兼容客户端提供 knip 分析、配置检查和清理工具,可扫描项目并执行带预览和备份选项的修改。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
@genar/knip-mcp-server
一个 Model Control Protocol (MCP) 服务,可与 knip.dev 集成,帮助 AI Agent 在 JavaScript/TypeScript 项目中识别并清理未使用的文件、导入、导出和依赖项。
概述
此 MCP 服务通过与 knip.dev 集成,为 AI Agent 提供强大的代码清理能力。它旨在与 Cursor AI 和其他兼容 MCP 的工具无缝协作,从而保持代码库整洁、高效。
功能
- 代码分析:运行全面的 knip 分析,查找未使用的代码
- 未使用文件检测:识别不再被引用的文件
- 导入/导出清理:查找并移除未使用的导入和导出
- 依赖项管理:检测未使用的 npm 依赖项和 devDependencies
- 安全操作:内置安全检查和备份机制
- 工作区支持:全面支持 monorepo 和工作区配置
- 试运行模式:在应用更改前预览
安装
npm install -g @genar/knip-mcp-server
或在你的项目中安装:
npm install @genar/knip-mcp-server
用法
作为 MCP 服务
直接运行服务:
knip-mcp-server
或使用 npx:
npx @genar/knip-mcp-server
配置
通过环境变量进行配置:
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"
与 Cursor AI 集成
选项 1:MCP 服务配置
- 在 Cursor AI 设置中,进入 MCP 配置
- 添加新的 MCP 服务:
- 名称:"Knip Code Cleanup"
- 命令:
npx @genar/knip-mcp-server
- 保存并重启 Cursor AI
选项 2:Cursor 规则配置
你也可以通过 Cursor 的规则系统集成此 MCP 服务:在项目根目录中创建 .cursor/rules 目录,并添加以下配置:
- 创建规则目录结构:
mkdir -p .cursor/rules
- 创建 knip 规则文件(
.cursor/rules/knip-mcp.md):
# Knip Code Cleanup Rules
You have access to the Knip MCP server for code cleanup tasks. Use these tools proactively to maintain clean code:
## Available Tools
- `knip_scan` - Run comprehensive code analysis
- `knip_get_unused_files` - Find unused files
- `knip_get_unused_exports` - Find unused exports
- `knip_get_unused_imports` - Find unused imports
- `knip_get_unused_dependencies` - Find unused dependencies
- `knip_remove_unused_files` - Safely remove unused files
- `knip_remove_unused_imports` - Clean up unused imports
- `knip_fix_issues` - Auto-fix common issues
- `knip_get_config` - View knip configuration
- `knip_validate_config` - Validate knip config
## Usage Guidelines
1. **After implementing features**: Always run `knip_scan` to identify cleanup opportunities
2. **Before commits**: Use `knip_fix_issues` with `dryRun: true` to preview cleanup
3. **Regular maintenance**: Run `knip_get_unused_dependencies` to audit packages
4. **Safety first**: Always use dry-run mode first, create backups for destructive operations
## Workflow Integration
When asked to implement features:
1. Complete the implementation
2. Run `knip_scan` to analyze unused code
3. Use `knip_remove_unused_imports` to clean imports
4. Check `knip_get_unused_files` for orphaned files
5. Apply fixes with appropriate safety measures
## 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.
- 创建包含 knip 规则的主规则文件(
.cursor/rules/main.md):
# Project Development Rules
Include code cleanup and maintenance guidelines.
@knip-mcp.md
## Code Quality Standards
- Use the Knip MCP server for regular code cleanup
- Maintain clean imports and remove unused dependencies
- Follow the knip workflow after feature implementation
- 可选:如果想让规则保持私有,可将其添加到
.gitignore:
echo ".cursor/" >> .gitignore
选项 3:项目级 MCP 配置
在项目根目录中创建本地 MCP 配置文件(.mcp-config.json):
{
"servers": {
"knip": {
"command": "npx",
"args": ["@genar/knip-mcp-server"],
"env": {
"KNIP_PROJECT_ROOT": ".",
"KNIP_LOG_LEVEL": "info",
"KNIP_SAFE_MODE": "true"
}
}
}
}
然后在 Cursor 的 MCP 设置中引用此配置。
可用工具
knip_scan
对你的项目运行全面的 knip 分析。
参数:
projectPath(可选):要分析的项目路径workspace(可选):monorepo 中的特定工作区includePaths(可选):要包含的路径数组excludePaths(可选):要排除的路径数组dryRun(默认值:true):运行但不进行更改
knip_get_unused_files
获取项目中未使用文件的列表。
参数:
workspace(可选):要分析的特定工作区
knip_get_unused_exports
按文件整理未使用的导出。
参数:
workspace(可选):要分析的特定工作区filePath(可选):仅获取特定文件的导出
knip_get_unused_imports
按文件整理未使用的导入。
参数:
workspace(可选):要分析的特定工作区filePath(可选):仅获取特定文件的导入
knip_get_unused_dependencies
获取未使用的 npm 依赖项和 devDependencies。
参数:
workspace(可选):要分析的特定工作区type(可选):'dependencies'、'devDependencies' 或 'all'
knip_remove_unused_files
安全移除未使用的文件,并提供备份选项。
参数:
files(可选):要移除的特定文件;为空时移除所有文件dryRun(默认值:true):预览模式createBackup(默认值:true):移除前创建备份backupDir(可选):自定义备份目录maxFiles(默认值:50):安全上限
knip_remove_unused_imports
从特定文件中移除未使用的导入。
参数:
filePath(必需):要清理的文件imports(可选):要移除的特定导入dryRun(默认值:true):预览模式createBackup(默认值:true):更改前创建备份
knip_fix_issues
自动修复常见的 knip 问题。
参数:
issueTypes(默认值:['imports']):要修复的问题类型dryRun(默认值:true):预览模式createBackup(默认值:true):创建备份maxFiles(默认值:25):安全限制
knip_get_config
获取当前的 knip 配置。
参数:
projectPath(可选):项目路径configPath(可选):特定配置文件路径
knip_validate_config
验证 knip 配置是否正确。
参数:
projectPath(可选):项目路径configPath(可选):特定配置文件路径config(可选):要验证的配置对象
AI Agent 工作流示例
1. 功能实现后的代码清理
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
2. 依赖项审计
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
3. 项目健康检查
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
安全功能
- 试运行模式:所有破坏性操作默认采用试运行模式
- 创建备份:修改文件前自动创建备份
- 安全模式:防止移除关键文件(如 package.json 等)
- 文件数量限制:可配置批量操作的数量限制
- 错误处理:全面的错误报告与恢复机制
配置
此服务支持以下 knip 配置文件:
knip.jsonknip.jsonc.knip.json.knip.jsoncknip.config.jsknip.config.ts
开发
# Clone the repository
git clone https://github.com/gtrias/knip-mcp-server.git
# Install dependencies
npm install
# Build the project
npm run build
# Run in development mode
npm run dev
# Run tests
npm test
贡献
- Fork 此仓库
- 创建功能分支
- 进行更改
- 添加测试
- 提交拉取请求
许可证
MIT 许可证,详情请参阅 LICENSE 文件。
支持
相关项目
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。