下载文件夹管理工具

一个基于Python的智能下载文件夹管理工具,提供文件扫描、智能分类、去重、自动解压等功能。

  • by Rivka445
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 Rivka445/Downloads-Warden-MCP

使用说明

📥 下载管理员--MCP服务器

MCP服务器,用于使用FastMCP和Python 3.10+进行智能下载文件夹管理。


🏗 建筑

src/
├── server.py               # MCP entry point — exposes 7 tools, wires DI in main()
├── interfaces.py           # IDownloadsService — abstract interface
├── exceptions.py           # Custom exceptions hierarchy
├── services/
│   └── downloads_service.py  # Business logic — implements IDownloadsService
├── models/
│   ├── file_info.py          # FileInfo dataclass
│   ├── scan_result.py        # ScanResult, CategoryStats dataclasses
│   └── operation_result.py   # OperationResult, LargeFilesResult dataclasses
└── utils/
    └── file_utils.py         # get_file_category, calculate_file_hash, get_file_size_mb

tests/
├── test_downloads_service.py  # Unit tests for DownloadsService
├── test_server.py             # Unit tests for MCP tools (mock injection via DI)
└── test_utils.py              # Unit tests for utility functions

✨ 工具

工具 描述 类型
scan_downloads 分析文件夹结构和统计信息 阅读
smart_sort_files 将文件组织到类别文件夹中 写入
deduplicate_by_hash 删除完全重复的文件(SHA-256) 写入
deduplicate_folders 按内容哈希删除重复文件夹 写入
auto_extract_and_cleanup 提取ZIP文件并删除存档 写入
clear_installers 删除超过7天的安装程序文件 写入
find_large_files 查找大于大小阈值(默认500MB)的文件 读取

🚀 快速开始

本地

# Install dependencies
pip install -r requirements.txt

# Run server
python src/server.py

码头工人

docker build -t downloads-warden .
docker run --rm -v ~/Downloads:/root/Downloads downloads-warden

🧪 测试

# Run all tests
pytest tests/ -v

# With coverage
pytest tests/ --cov=src --cov-report=term-missing

49项测试全部通过。


🔧 代码质量

# Lint
ruff check src/

# Type check
mypy src/

🛡️ 速率限制

破坏性(写入)工具由速率限制器保护,以防止意外重复执行。

工具 价格有限
scan_downloads ❌
find_large_files ❌
smart_sort_files ✅ 5秒
deduplicate_by_hash ✅ 5秒
deduplicate_folders ✅ 5秒
auto_extract_and_cleanup ✅ 5秒
clear_installers ✅ 5秒

在窗口内调用限速工具两次会提高 DownloadsWardenError: called too recently.


🧩 依赖注入

服务器使用构造函数注入。 main() 是单一成分根:

def main() -> None:
    service = DownloadsService(get_downloads_path())
    create_server(service=service)
    mcp.run(transport="stdio")

在测试中,直接注入模拟:

mock_service = MagicMock(spec=IDownloadsService)
create_server(service=mock_service)

📦 模型

型号 字段
FileInfo name, size_mb
CategoryStats count, size_mb
ScanResult success, message, total_files, total_size_mb, by_category, by_extension
OperationResult success, message, count, errors
LargeFilesResult 继承 OperationResult + files, total_size_mb

⚠️ 异常

DownloadsWardenError
├── PathNotFoundError
├── FileMoveError
├── HashCalculationError
├── ExtractionError
├── InstallerCleanupError
└── FolderDeletionError

⚙️ Claude桌面设置

添加 claude_desktop_config.json:

{
  "mcpServers": {
    "downloads-warden": {
      "command": "python",
      "args": ["C:/path/to/src/server.py"]
    }
  }
}

🔐 隐私

所有操作运行 本地。没有数据发送到外部服务器。