MCP文件管理器
一种模型上下文协议(MCP)服务器,为AI模型提供文件系统访问。支持本机MCP协议和REST API。 \
\
概述
MCP文件管理器使ChatGPT和Claude等AI模型能够直接与您的文件系统交互。人工智能助手可以以编程方式读取、写入、搜索和管理文件,而不是手动复制文件内容或上传文档。 关键能力:
- 读写文件(文本和二进制)
- 按模式或内容搜索和查找文件
- 使用自动压缩处理图像
- 复制、移动和组织文件
- 计算哈希值并创建存档
- 列出目录内容和树结构\
双协议支持:
- 本地MCP:与Claude Desktop和其他MCP客户端直接集成
- REST API:ChatGPT自定义操作和web客户端的HTTP端点
特性
文件操作
- 读取、写入、附加和删除文件
- 支持文本和二进制文件
- 图像和二进制数据的Base64编码
- 多种编码支持(UTF-8、UTF-16、Latin-1等)
目录操作
- 使用递归支持列出目录内容
- 创建和删除目录
- 显示目录树结构
- 模式匹配(通配符)
搜索与发现
- 在文件中搜索文本
- 按模式查找文件
- 基于内容的文件发现
- 区分大小写/不区分大小写的搜索
图像支持
- 读取视觉AI模型的图像
- 大文件的自动图像压缩
- 用于AI处理的Base64编码
- 在目录中列出图像
- 支持JPG、PNG、GIF、BMP、WebP
高级功能
- 文件复制和移动
- 文件哈希计算(MD5、SHA256、SHA512)
- 文件压缩(ZIP)
- 批量操作
- OneDrive和符号链接解析
安全
- 系统关键文件保护
- 危险文件警告
- 路径遍历预防
- 可配置的文件大小限制
- 综合运行日志
快速开始
需求
- Python 3.10或更高版本
- pip包管理器
设置
git clone
cd mcp-file-manager
pip install -r requirements.txt
pip install -e .用法
适用于Claude Desktop(原生MCP)
编辑您的Claude Desktop配置文件:
- 视窗:
%APPDATA%\Claude\claude_desktop_config.json - macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Linux:
~/.config/Claude/claude_desktop_config.json
添加此配置:
{
"mcpServers": {
"file-manager": {
"command": "python",
"args": ["-m", "file_manager_server.server"],
"env": {
"LOG_LEVEL": "INFO",
"MAX_FILE_SIZE_MB": "100"
}
}
}
}编辑后重新启动Claude Desktop。
对于ChatGPT和Web客户端(REST API)
运行服务器:
python mcp_sse_server.py服务器运行于 http://localhost:8080\ 使用隧道暴露于互联网:
选项A——Cloudflare隧道
cloudflared tunnel --url http://localhost:8080选项B--ngrok
ngrok http 8080然后,在ChatGPT中→ 设置→ 行动→ 添加新操作\ 粘贴以下内容 gpt_actions.json 并将URL替换为您的隧道。
示例用例
代码分析
读取所有Python文件 src 目录并查找任何TODO注释。自动文档
创建一个 CHANGELOG.md 通过分析git提交和代码更改来创建文件。图像处理
在“我的图片”文件夹中查找所有大于2MB的图片并创建缩略图。
批处理文件操作
全部重命名.jpeg文件到.jpg在下载文件夹中。
项目设置
创建一个新的Python项目结构src/,tests/,以及docs/文件夹。
API 参考
基本URL: http://localhost:8080/api
{
"openapi": "3.1.0",
"info": {
"title": "MCP File Manager",
"description": "Complete file system operations with image support",
"version": "2.0.0"
},
"servers": [
{
"url": "https://attachment-gene-missed-yen.trycloudflare.com"
}
],
"paths": {
"/api/read": {
"post": {
"operationId": "readFile",
"summary": "Read file content",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Full file path"
},
"encoding": {
"type": "string",
"default": "utf-8"
}
},
"required": ["path"]
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/list": {
"post": {
"operationId": "listDirectory",
"summary": "List directory contents",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory path"
},
"recursive": {
"type": "boolean",
"default": false
},
"pattern": {
"type": "string",
"description": "File pattern (e.g. *.py)"
}
},
"required": ["path"]
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/write": {
"post": {
"operationId": "writeFile",
"summary": "Write or create file",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "File path"
},
"content": {
"type": "string",
"description": "File content (text or base64)"
},
"encoding": {
"type": "string",
"default": "utf-8"
},
"is_base64": {
"type": "boolean",
"default": false,
"description": "Set true for binary files (images)"
}
},
"required": ["path", "content"]
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/search": {
"post": {
"operationId": "searchInFiles",
"summary": "Search text in files",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory to search"
},
"text": {
"type": "string",
"description": "Text to search for"
},
"pattern": {
"type": "string",
"description": "File pattern (e.g. *.txt)"
}
},
"required": ["path", "text"]
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/find": {
"post": {
"operationId": "findFiles",
"summary": "Find files by pattern",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory to search"
},
"pattern": {
"type": "string",
"description": "File pattern (e.g. *.jpg)"
}
},
"required": ["path", "pattern"]
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/tree": {
"post": {
"operationId": "directoryTree",
"summary": "Get directory tree structure",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory path"
},
"max_depth": {
"type": "integer",
"default": 3
}
},
"required": ["path"]
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/image": {
"post": {
"operationId": "getImage",
"summary": "Get image for GPT Vision",
"description": "Returns image with auto-compression",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Image file path"
}
},
"required": ["path"]
}
}
}
},
"responses": {
"200": {
"description": "Image file",
"content": {
"image/jpeg": {},
"image/png": {},
"image/gif": {}
}
}
}
}
},
"/api/image/base64": {
"post": {
"operationId": "getImageBase64",
"summary": "Get image as base64",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Image file path"
}
},
"required": ["path"]
}
}
}
},
"responses": {
"200": {
"description": "Base64 encoded image"
}
}
}
},
"/api/images": {
"post": {
"operationId": "listImages",
"summary": "List images in directory",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory path"
}
},
"required": ["path"]
}
}
}
},
"responses": {
"200": {
"description": "List of images"
}
}
}
},
"/api/copy": {
"post": {
"operationId": "copyFile",
"summary": "Copy file",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"source": {
"type": "string",
"description": "Source file path"
},
"destination": {
"type": "string",
"description": "Destination path"
},
"overwrite": {
"type": "boolean",
"default": false
}
},
"required": ["source", "destination"]
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/delete": {
"post": {
"operationId": "deleteFile",
"summary": "Delete file",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "File path to delete"
}
},
"required": ["path"]
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
}高级
POST /api/copy
{"source": "/source/file.txt", "destination": "/dest/file.txt", "overwrite": false}配置
| 变量 | 默认值 | 描述 |
|---|---|---|
LOG_LEVEL | 信息 | 日志记录级别 |
MAX_FILE_SIZE_MB | 100 | 最大文件大小 |
MAX_IMAGE_SIZE_MB | 15 | 最大图像大小 |
ENABLE_COMPRESSION | true | 启用ZIP压缩 |
ENABLE_HASHING | true | 启用哈希计算 |
ENABLE_DANGEROUS_OPERATIONS | false | 允许删除可执行文件 |
项目结构
mcp-file-manager/
├── src/
│ └── file_manager_server/
│ ├── server.py
│ ├── rest_api.py
│ ├── config.py
│ ├── handlers/
│ └── utils/
├── mcp_sse_server.py
├── gpt_actions.json
├── requirements.txt
└── tests/测试
pytest
pytest --cov=file_manager_server --cov-report=html安全
此服务器提供具有多个安全层的文件系统访问。\ 保护机制
- 阻止系统关键路径上的操作
- 防止路径遍历和危险删除
- 可配置的文件大小限制
- 综合运行日志\
推荐做法
- 从非关键目录开始
- 定期监控日志
- 避免在没有身份验证的情况下暴露公共隧道
许可证
MIT许可证——见 许可证
致谢
- 人为——模型上下文协议(MCP)
- OpenAI——支持ChatGPT自定义操作
- 开源社区❤️
