mcp-file-scanner
v1.2.0
Published
递归遍历指定文件夹下所有文件,并返回文件详细信息
Readme
MCP 文件扫描器
一个简单的命令行工具,用于递归遍历指定文件夹下所有文件,并返回详细的文件信息。
功能特点
- 递归扫描指定目录下的所有文件
- 收集每个文件的详细信息:
- 创建时间
- 修改时间
- 文件名称
- 文件大小
- 文件扩展名
- 支持多种输出格式(表格、JSON)
- 支持文件名过滤
- 支持限制扫描深度
- 支持将结果保存到文件
- 支持文件统计分析
- 提供标准MCP接口,方便其他程序调用
- 支持@modelcontextprotocol/sdk标准
安装
无需安装,可以直接通过npx使用:
npx mcp-file-scanner [目录路径] [选项]或者全局安装:
npm install -g mcp-file-scanner
mcp-file-scanner [目录路径] [选项]使用方法
基本用法
# 显示帮助信息
npx mcp-file-scanner
# 扫描当前目录
npx mcp-file-scanner scan
# 扫描指定目录
npx mcp-file-scanner scan /path/to/directory
# 显示文件统计信息
npx mcp-file-scanner stats命令
Commands:
scan [options] [directory] 扫描文件并列出详细信息
stats [options] [directory] 显示文件统计信息
server 启动MCP服务器
help [command] 显示命令帮助选项
扫描命令 (scan) 选项:
Options:
-o, --output <type> 输出格式 (json, table) (默认: "table")
-d, --depth <number> 扫描深度 (0表示无限制) (默认: "0")
-f, --filter <pattern> 文件名过滤模式 (支持glob模式) (默认: "*")
-s, --save <path> 将结果保存到文件
-h, --help 显示帮助信息统计命令 (stats) 选项:
Options:
-d, --depth <number> 扫描深度 (0表示无限制) (默认: "0")
-f, --filter <pattern> 文件名过滤模式 (支持glob模式) (默认: "*")
-s, --save <path> 将结果保存到文件
-h, --help 显示帮助信息示例
# 以JSON格式输出结果
npx mcp-file-scanner scan -o json
# 只扫描一级目录
npx mcp-file-scanner scan -d 1
# 只扫描JS文件
npx mcp-file-scanner scan -f "*.js"
# 将结果保存到文件
npx mcp-file-scanner scan -s result.json
# 组合使用
npx mcp-file-scanner scan /path/to/directory -o json -d 2 -f "*.txt" -s result.json
# 显示文件统计信息
npx mcp-file-scanner stats
# 显示特定目录的文件统计信息
npx mcp-file-scanner stats /path/to/directory
# 将统计信息保存到文件
npx mcp-file-scanner stats -s stats.json
# 启动MCP服务器
npx mcp-file-scanner server在代码中使用
基本用法
const { scanDirectory } = require('mcp-file-scanner');
async function main() {
const files = await scanDirectory('./src', {
depth: 2,
filter: '*.js',
outputFormat: 'json',
silent: true // 不在控制台输出结果
});
console.log(`找到 ${files.length} 个文件`);
}
main().catch(console.error);MCP接口
传统接口
const { mcpScanFiles, mcpGetFileStats } = require('mcp-file-scanner');
async function main() {
// 扫描文件
const result = await mcpScanFiles({
directory: './src',
depth: 2,
filter: '*.js'
});
if (result.success) {
console.log(`找到 ${result.count} 个文件`);
console.log(result.data);
} else {
console.error(`扫描失败: ${result.error}`);
}
// 获取文件统计信息
const statsResult = await mcpGetFileStats({
directory: './src',
depth: 2,
filter: '*.js'
});
if (statsResult.success) {
const stats = statsResult.data;
console.log(`总文件数: ${stats.totalFiles}`);
console.log(`总大小: ${stats.totalSize} 字节`);
console.log(`平均大小: ${stats.averageSize} 字节`);
// 显示文件类型分布
Object.entries(stats.extensionStats).forEach(([ext, info]) => {
console.log(`${ext}: ${info.count} 个文件, 总大小 ${info.totalSize} 字节`);
});
}
}
main().catch(console.error);标准MCP接口
本工具支持标准的MCP协议,使用@modelcontextprotocol/sdk包实现。你可以通过以下方式启动MCP服务器:
const { startMcpServer } = require('mcp-file-scanner');
// 启动MCP服务器
startMcpServer()
.then(() => {
console.log('MCP服务器已启动');
})
.catch(error => {
console.error('启动MCP服务器失败:', error);
});或者直接使用命令行工具启动:
npx mcp-file-scanner serverMCP服务器提供以下工具:
scanFiles- 递归扫描指定目录下的所有文件,并返回详细信息getFileStats- 获取指定目录下文件的统计信息
这些工具可以通过任何支持MCP协议的客户端调用。
许可证
MIT
