文件操作MCP服务器
一种模型上下文协议(MCP)服务器,为AI助手提供安全的文件操作工具。此服务器允许安全读取、写入和列出配置目录中的文件。
特性
- 读取文件:使用可自定义编码读取文件内容
- 写入文件:将内容写入具有编码选项的文件
- 列出目录:浏览包含可选隐藏文件的目录内容
- 文件存在性检查:验证文件或目录是否存在
- 安全:路径验证以防止目录遍历攻击
- TypeScript:完全打字,以获得更好的开发体验
可用工具
read_file
读取文件的内容。
参数:
path(string,必填):要读取的文件的路径encoding(字符串,可选):文件编码(默认值:'utf8')
write_file
将内容写入文件。
参数:
path(string,必填):要写入的文件的路径content(string,必填):要写入文件的内容encoding(字符串,可选):文件编码(默认值:'utf8')
list_directory
列出给定路径中的文件和目录。
参数:
path(string,必填):要列出的目录的路径includeHidden(布尔值,可选):包含隐藏文件(默认值:false)
file_exists
检查文件或目录是否存在。
参数:
path(字符串,必填):用于检查是否存在的路径
安装
- 克隆或下载此项目
- 安装依赖项:
npm install- 构建TypeScript代码:
npm run build用法
直接运行服务器
npm start发展模式
npm run dev添加到光标
要将此MCP服务器与Cursor一起使用,请将以下配置添加到MCP设置中:
- 打开光标设置(Cmd/Ctrl+,)
- 转到“功能”→ “模型上下文协议”
- 添加新的服务器配置:
{
"file-operations": {
"command": "node",
"args": ["/absolute/path/to/file-operations-mcp/build/index.js"],
"env": {}
}
}或添加到您的 ~/.cursor/mcp.json 文件:
{
"mcpServers": {
"file-operations": {
"command": "node",
"args": ["/absolute/path/to/file-operations-mcp/build/index.js"]
}
}
}安全
此服务器实施了多种安全措施:
- 路径验证:所有文件路径都经过验证,以防止目录遍历攻击
- 允许的目录:默认情况下,只能访问当前工作目录中的文件
- 安全路径解析:使用Node.js路径实用程序安全解析文件路径
自定义允许的目录
要修改服务器可以访问的目录,请编辑 ALLOWED_DIRECTORIES 数组in src/tools/file-tools.ts:
const ALLOWED_DIRECTORIES = [
process.cwd(), // Current working directory
'/path/to/your/safe/directory',
// Add more allowed directories as needed
];示例响应
成功读取文件
{
"success": true,
"message": "File read successfully: /path/to/file.txt",
"data": {
"content": "Hello, World!",
"path": "/path/to/file.txt"
}
}目录列表
{
"success": true,
"message": "Directory listed successfully: /path/to/directory",
"data": {
"files": [
{
"name": "example.txt",
"path": "/path/to/directory/example.txt",
"isDirectory": false,
"isFile": true,
"size": 1024,
"lastModified": "2024-01-15T10:30:00.000Z"
}
],
"count": 1
}
}错误响应
{
"success": false,
"message": "Failed to read file: ENOENT: no such file or directory",
"data": null
}发展
脚本
npm run build:将TypeScript编译为JavaScriptnpm start:运行已编译的服务器npm run dev:在一个命令中构建和运行npm run watch:注意更改并重新编译
项目结构
file-operations-mcp/
├── src/
│ ├── index.ts # Main server implementation
│ ├── tools/
│ │ └── file-tools.ts # File operation implementations
│ └── types/
│ └── index.ts # TypeScript type definitions
├── build/ # Compiled JavaScript (after build)
├── package.json
├── tsconfig.json
└── README.md需求
- Node.js>=18.0.0
- npm或yarn包管理器
许可证
MIT许可证-详见package.json。 通过Hüseyin Karacif
贡献
请随时打开问题或提交pull请求来改进此MCP服务器!
