文件系统类 MCP 工具
High Contrast
Dark Mode
Light Mode
Sepia
Forest
3 min read621 words

文件系统类 MCP 工具

文件系统类 MCP 是最基础、使用最频繁的一类工具。它让 AI 模型能够读取、写入和管理你的本地文件——这是 Claude Desktop 在日常工作中发挥价值的核心能力。

官方 Filesystem MCP Server

@modelcontextprotocol/server-filesystem 是 Anthropic 官方维护的文件系统 Server,也是最稳定的起点。

暴露的工具列表

工具名 参数 说明
read_file path 读取文件完整内容
read_multiple_files paths (数组) 批量读取,一次调用返回多文件
write_file path, content 写入文件(覆盖)
create_directory path 创建目录(含父级)
list_directory path 列出目录内容(文件+子目录)
move_file source, destination 移动或重命名文件
search_files path, pattern 递归搜索文件名匹配
get_file_info path 获取文件元数据(大小、修改时间等)

安装方式

# 通过 npm 全局安装
npm install -g @modelcontextprotocol/server-filesystem
# 验证安装
npx @modelcontextprotocol/server-filesystem --version

配置示例(claude_desktop_config.json)

{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/yourname/Documents",
"/Users/yourname/Projects"
]
}
}
}

重要:最后的参数是允许访问的根目录列表。Server 只能读写这些路径下的文件,这是核心安全边界。

安全路径设计原则

graph TD A["根目录配置
~/Documents, ~/Projects"] --> B{请求的路径?} B -- "在允许范围内" --> C[✅ 允许访问] B -- "尝试 ../ 路径穿越" --> D[❌ 拒绝,返回错误] B -- "访问 ~/.ssh 等敏感目录" --> D subgraph "绝对不要放入允许目录" E["/ 根目录"] F["~/ 整个家目录"] G["~/.ssh / ~/.aws"] H["系统目录 /etc /var"] end

最佳实践:按项目或场景创建专用工作目录,只授权 AI 访问这些目录:

mkdir -p ~/ai-workspace/{reports,code,data}
# 只在配置中添加 ~/ai-workspace

Git MCP Server

对开发者来说,Git MCP Server 同样重要,它让 AI 能够理解代码变更历史。

安装:

npm install -g @modelcontextprotocol/server-git

配置:

{
"mcpServers": {
"git": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-git",
"--repository",
"/Users/yourname/Projects/myapp"
]
}
}
}

暴露的工具:

工具名 说明
git_log 查看提交历史
git_diff 查看文件差异
git_status 查看工作区状态
git_show 查看指定 commit 详情
git_branch 列出分支

典型工作流场景

场景:AI 辅助代码审查

sequenceDiagram participant U as 开发者 participant C as Claude participant F as Filesystem MCP participant G as Git MCP U->>C: "帮我审查最近3个提交的变更" C->>G: git_log(max_count=3) G-->>C: [commit_hash_1, commit_hash_2, commit_hash_3] C->>G: git_diff(commit="HEAD~3..HEAD") G-->>C: diff 内容 C->>F: read_file(path="src/auth.py") F-->>C: 文件当前内容 C-->>U: 详细的代码审查意见

场景:批量文档处理

用户提示:读取 ~/ai-workspace/reports 下所有 .md 文件,
提取每个文件的标题和摘要,输出一个 CSV 索引

Claude 会自动调用 list_directoryread_multiple_files → 生成 CSV → write_file

常见误区

本节执行清单


下一节:浏览器控制类 MCP 工具