这是一个用于文件和目录操作的MCP(Meta Computing Protocol)服务器,提供了基本文件操作、目录管理、变更跟踪等功能,能高效处理文件系统相关任务。
npx -y @smithery/cli install @bsmi021/mcp-file-operations-server --client claude
npm install
npm start
开发模式(自动重载):
npm run dev
copy_file
:复制文件到新位置read_file
:读取文件内容write_file
:写入内容到文件move_file
:移动/重命名文件delete_file
:删除文件append_file
:追加内容到文件make_directory
:创建目录remove_directory
:删除目录copy_directory
:递归复制目录(带进度报告)watch_directory
:开始监控目录变化unwatch_directory
:停止监控目录get_changes
:获取记录的变更列表clear_changes
:清除所有变更记录file:///recent-changes
:最近文件系统变更列表file://{path}
:访问文件内容metadata://{path}
:访问文件元数据directory://{path}
:列出目录内容服务器实现速率限制以防止滥用:
速率限制错误包含重试-after时间段信息。
所有文件路径均经过验证以防止目录遍历攻击:
../
)长时间运行的操作(如目录复制)提供进度更新:
interface ProgressUpdate {
token: string | number;
message: string;
percentage: number;
}
通过操作结果中的进度令牌跟踪进度。
服务器内置文件操作缓存,可提高重复操作的效率。默认启用。
支持多线程和并发操作以提升性能,具体取决于系统负载和配置。
// 复制文件
await fileOperations.copyFile({
source: 'source.txt',
destination: 'destination.txt',
overwrite: false
});
// 监控目录
await fileOperations.watchDirectory({
path: './watched-dir',
recursive: true
});
// 通过资源访问文件内容
const resource = await mcp.readResource('file:///path/to/file.txt');
console.log(resource.contents[0].text);
// 带进度跟踪复制目录
const result = await fileOperations.copyDirectory({
source: './source-dir',
destination: './dest-dir',
overwrite: false
});
// 通过结果中的进度令牌跟踪进度
console.log(result.progressToken);
可以通过环境变量或配置文件设置以下选项:
PORT
:服务器监听端口,默认为3000LOG_LEVEL
:日志级别,可选值为debug
, info
, warning
, error
, 默认为info
MAX_FILE_SIZE
:允许的最大文件大小,默认为1MB{
enableChangeTracking: true // 默认为true
}
{
copyConcurrency: 5 // 复制操作默认并发数为5
}
Error: File not found
Error: Permission denied
Error: Directory traversal attempt detected
可以通过注册全局错误处理器捕获和处理错误:
mcp errorHandler(error) {
console.error('An error occurred:', error);
// 自定义处理逻辑
}
默认的日志格式为:
[timestamp] - [level] - message
例如:
2023-10-05 12:34:56 - info - File operation completed successfully.
支持以下日志级别:
debug
:调试信息info
:一般信息warning
:警告消息error
:错误信息如何查看当前日志?
mcp logs
命令查看实时日志。能否禁用变更跟踪?
enableChangeTracking: false
可以禁用变更跟踪功能。服务器支持哪些协议?
该服务器基于MCP协议构建,在实现文件和目录操作功能时,采用了路径验证机制防止目录遍历攻击,通过速率限制避免滥用,内置缓存机制和支持并行处理提升性能。同时,对各类操作和错误进行了详细的日志记录,方便后续排查和维护。
文档未提及相关信息。
以上是项目的完整文档,包含安装、配置、使用说明以及常见问题解答。如果有任何疑问或需要进一步帮助,请随时联系项目维护团队。