MCP文件系统服务器是一个强大的模型上下文协议(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桌面版配置文件:
~/Library/Application\ Support/Claude/claude_desktop_config.json%APPDATA%/Claude/claude_desktop_config.json添加以下配置(替换为你实际的路径):
{
"mcpServers": {
"mcp-filesystem": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/mcp-filesystem",
"run",
"run_server.py",
"/absolute/path/to/dir1",
"/absolute/path/to/dir2"
]
}
}
}
⚠️ 重要提示
所有路径必须是绝对路径(从根目录开始的完整路径)。请使用
realpath或pwd确保你拥有正确的绝对路径。
保存配置后,重启Claude桌面版以使更改生效。
grep。仅允许在明确允许的目录内进行操作。
grep的高级搜索(可用时使用ripgrep):
grep的-A/-B/-C选项)。ripgrep,实现快速搜索。采用行为驱动方法,拥有75多个测试用例。
可在Windows、macOS和Linux上运行。
首先,若尚未安装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
你可以在Claude桌面版中监控服务器日志:
# 在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
运行服务器并访问特定目录:
# 使用uv(推荐)
uv run run_server.py /path/to/dir1 /path/to/dir2
# 或者使用标准Python
python run_server.py /path/to/dir1 /path/to/dir2
# 实际路径示例
uv run run_server.py /Users/username/Documents /Users/username/Downloads
--transport 或 -t:传输协议(stdio 或 sse,默认:stdio)--port 或 -p:SSE传输端口(默认:8000)--debug 或 -d:启用调试日志--version 或 -v:显示版本信息使用MCP检查器进行交互式测试和调试:
# 基本用法
npx @modelcontextprotocol/inspector uv run run_server.py /path/to/directory
# 使用SSE传输
npx @modelcontextprotocol/inspector uv run run_server.py /path/to/directory --transport sse --port 8080
# 输出调试信息
npx @modelcontextprotocol/inspector uv run run_server.py /path/to/directory --debug
Tool: read_file_lines
Arguments: {
"path": "/path/to/file.txt",
"offset": 99, # 基于0的索引(第100行)
"limit": 51, # 读取51行
"encoding": "utf-8" # 可选编码
}
Tool: grep_files
Arguments: {
"path": "/path/to/search",
"pattern": "function\\s+\\w+\\(",
"is_regex": true,
"context_before": 2, # 显示每个匹配项前的2行(如grep的 -A 选项)
"context_after": 5, # 显示每个匹配项后的5行(如grep的 -B 选项)
"include_patterns": ["*.js", "*.ts"],
"results_offset": 0, # 从第一个匹配项开始
"results_limit": 20 # 最多显示20个匹配项
}
Tool: edit_file_at_line
Arguments: {
"path": "/path/to/file.txt",
"line_edits": [
{
"line_number": 15,
"action": "replace",
"content": "This is the new content for line 15\n",
"expected_content": "Original content of line 15\n" # 编辑前验证内容
},
{
"line_number": 20,
"action": "delete"
}
],
"offset": 0, # 从该偏移量开始考虑行
"relative_line_numbers": false, # 行号是否相对于偏移量
"abort_on_verification_failure": true, # 验证失败时停止
"dry_run": true # 预览更改而不应用
}
Tool: find_duplicate_files
Arguments: {
"path": "/path/to/search",
"recursive": true,
"min_size": 1024,
"format": "text"
}
read_file:读取文件的完整内容。read_multiple_files:同时读取多个文件。write_file:创建新文件或覆盖现有文件。create_directory:创建新目录或确保目录存在。list_directory:获取文件和目录的详细列表。move_file:移动或重命名文件和目录。get_file_info:检索文件或目录的详细元数据。list_allowed_directories:列出服务器允许访问的目录。read_file_lines:使用偏移量/限制参数读取特定行范围。edit_file_at_line:具备内容验证和相对行号的精确编辑:
head_file:读取文本文件的前N行。tail_file:读取文本文件的后N行。grep_files:在文件中搜索模式,具备强大选项:
ripgrep以提高性能(有Python后备方案)。grep的-A/-B/-C选项)。search_files:搜索匹配模式的文件,支持内容搜索。directory_tree:获取文件和目录的递归树视图。calculate_directory_size:计算目录的总大小。find_duplicate_files:通过比较内容查找重复文件。compare_files:比较两个文本文件并显示差异。find_large_files:查找大于指定大小的文件。find_empty_directories:查找空目录。grep_files,通过精确的上下文控制找到所需内容。ripgrep,可处理包含数百万个文件和行的大型文件系统。read_file_lines,通过偏移量/限制仅检查相关部分。edit_file_at_line进行目标编辑,具备内容验证。find_duplicate_files和compare_files等专业工具。directory_tree生成目录树,便于快速导航。find_large_files和find_empty_directories识别问题区域。grep,支持上下文控制(不仅是全文件搜索)。ripgrep,提高大型文件系统性能。find_duplicate_files等操作或递归搜索可能需要较长时间才能完成。服务器实施严格的路径验证,防止访问允许目录之外的内容:
为获得最佳grep功能性能:
rg)。ripgrep,并提供Python后备方案。该服务器使用FastMCP SDK构建,以更好地符合当前MCP最佳实践。它采用高效的组件缓存系统和直接装饰器模式。
MIT许可证