一个强大的模型上下文协议(MCP)服务器,用于优化与大型文件和文件系统的智能交互。它提供安全访问文件和目录的功能,并通过智能上下文管理来提高处理大量数据时的效率。
智能上下文管理:高效地处理大型文件和文件系统
智能文件操作:
首先,安装 uv(如果尚未安装):
# 使用官方安装程序安装 uv
curl -fsSL https://raw.githubusercontent.com/astral-sh/uv/main/install.sh | bash
# 或者使用 pipx
pipx install uv
然后克隆仓库并安装依赖项:
# 克隆仓库
git clone https://github.com/safurrier/mcp-filesystem.git
cd mcp-filesystem
# 使用 uv 安装依赖项
uv pip sync requirements.txt requirements-dev.txt
你需要获取仓库位置和任何想要访问的目录的绝对路径:
# 获取仓库的绝对路径
REPO_PATH=$(pwd)
echo "仓库路径: $REPO_PATH"
# 获取想要访问的目录的绝对路径
realpath ~/Documents
realpath ~/Downloads
# 或在没有 realpath 的系统上:
echo "$(cd ~/Documents && pwd)"
打开你的 Claude Desktop 配置文件:
~/Library/Application\ Support/Claude/claude_desktop_config.json添加以下配置(替换为实际路径):
{
"mcpServers": {
"mcp-filesystem": {
"command": "uv",
"args": [
"--directory",
"/绝对路径到/mcp-filesystem",
"run",
"run_server.py",
"/绝对路径到/dir1",
"/绝对路径到/dir2"
]
}
}
}
重要:所有路径必须是绝对路径(从根目录开始的完整路径)。使用
realpath或pwd确保你有正确的绝对路径。
保存配置后,重启 Claude Desktop 使更改生效。
你可以从 Claude Desktop 监控服务器日志:
# 在 macOS 上
tail -n 20 -f ~/Library/Logs/Claude/mcp-server-mcp-filesystem.log
# 在 Windows 上(PowerShell)
Get-Content -Path "$env:APPDATA\Claude\Logs\mcp-server-mcp-filesystem.log" -Tail 20 -Wait
这对于调试问题或查看 Claude 正在请求的具体内容特别有用。
使用特定目录访问权限运行服务器:
# 使用 uv(推荐)
uv run run_server.py /路径到/dir1 /路径到/dir2
# 或使用标准 Python
python run_server.py /路径到/dir1 /路径到/dir2
# 示例使用实际路径
uv run run_server.py /Users/用户名/Documents /Users/用户名/Downloads
--transport 或 -t:传输协议(stdio 或 sse,默认:stdio)--port 或 -p:SSE 传输端口(默认:8000)--debug 或 -d:启用调试日志--version 或 -v:显示版本信息对于交互式测试和调试:
# 基本用法
npx @modelcontextprotocol/inspector uv run run_server.py /路径到/目录
# 使用 SSE 传输
npx @modelcontextprotocol/inspector uv run run_server.py /路径到/目录 --transport sse --port 8080
# 使用调试输出
npx @modelcontextprotocol/inspector uv run run_server.py /路径到/目录 --debug
此服务器已使用 FastMCP SDK 构建,以更好地符合当前 MCP 最佳实践。它使用高效的组件缓存系统和直接装饰器模式。
编辑你的 Claude Desktop 配置文件以集成 MCP-Filesystem:
配置文件位置:
~/Library/Application\ Support/Claude/claude_desktop_config.json{
"mcpServers": {
"mcp-filesystem": {
"command": "uv",
"args": [
"--directory",
"/路径到/mcp-filesystem/仓库",
"run",
"run_server.py"
]
}
}
}
为了允许访问特定目录,请将它们作为附加参数添加:
{
"mcpServers": {
"mcp-filesystem": {
"command": "uv",
"args": [
"--directory",
"/路径到/mcp-filesystem/仓库",
"run",
"run_server.py",
"/Users/你的用户名/项目",
"/Users/你的用户名/Documents"
]
}
}
}
注意:
--directory标志很重要,因为它告诉 uv 在哪里找到包含 run_server.py 的仓库。将/路径到/mcp-filesystem/仓库替换为你系统上克隆仓库的实际路径。
# 运行所有测试
uv run -m pytest tests/
# 运行特定测试文件
uv run -m pytest tests/test_operations_unit.py
# 运行带有覆盖率
uv run -m pytest tests/ --cov=mcp_filesystem --cov-report=term-missing
# 格式化代码
uv run -m ruff format mcp_filesystem
# 检查代码
uv run -m ruff check --fix mcp_filesystem
# 类型检查
uv run -m mypy mcp_filesystem
# 运行所有检查
uv run -m ruff format mcp_filesystem && \
uv run -m ruff check --fix mcp_filesystem && \
uv run -m mypy mcp_filesystem && \
uv run -m pytest tests --cov=mcp_filesystem
工具:read_file_lines
参数:{
"path": "/路径到/file.txt",
"offset": 99, # 0 基础索引(第 100 行)
"limit": 51, # 读取 51 行
"encoding": "utf-8" # 可选编码
}
工具:grep_files
参数:{
"path": "/路径到/search",
"pattern": "function\\s+\\w+\\(",
"is_regex": true,
"context_before": 2, # 显示每个匹配前的 2 行(类似 grep -B)
"context_after": 5, # 显示每个匹配后的 5 行(类似 grep -A)
"include_patterns": ["*.js", "*.ts"],
"results_offset": 0, # 从第一个匹配开始
"results_limit": 20 # 显示最多 20 个匹配
}
工具:edit_file_at_line
参数:{
"path": "/路径到/file.txt",
"line_edits": [
{
"line_number": 15,
"action": "replace",
"content": "这是第 15 行的新内容\n",
"expected_content": "第 15 行的原始内容\n" # 编辑前验证内容
},
{
"line_number": 20,
"action": "delete"
}
],
"offset": 0, # 从此偏移量开始考虑行
"relative_line_numbers": false, # 是否行号相对于偏移量
"abort_on_verification_failure": true, # 验证失败时停止
"dry_run": true # 预览更改而不应用
}
工具:find_duplicate_files
参数:{
"path": "/路径到/search",
"recursive": true,
"min_size": 1024,
"format": "text"
}
MCP-Filesystem 设计用于与大型文件和复杂文件系统的智能交互:
智能上下文发现
grep_files 精确找到所需内容目标读取
read_file_lines 和偏移量/限制参数仅查看相关部分精确编辑
edit_file_at_line 进行内容验证的目标编辑高级分析
find_duplicate_files 和 compare_filesdirectory_tree 生成目录树以快速导航find_large_files 和 find_empty_directories 识别问题区域这种工作流程特别适用于需要处理大型文件和文件系统的 AI 工具。例如,Claude 和其他高级 AI 助手可以利用这些功能高效地导航代码库、分析日志文件或处理任何大型基于文本的数据集,同时保持令牌效率。
与基本文件系统 MCP 服务器相比,MCP-Filesystem 提供:
令牌效率
智能编辑
高级搜索
额外实用工具
安全性重点
find_duplicate_files 或递归搜索可能需要相当长的时间才能完成。服务器强制执行严格的路径验证以防止访问允许目录之外的内容:
为了获得最佳 grep 功能性能:
rg)