Filesystem MCP Server:完整安装与使用指南 2026
让AI代理访问你的本地文件——Filesystem MCP Server分步指南
Filesystem MCP Server:完整安装与使用指南 2026
让AI代理访问你的本地文件——Filesystem MCP Server分步指南
官方 Filesystem MCP 服务器让 AI 助手在你授权的目录内安全读写、编辑、搜索文件。基于 @modelcontextprotocol/server-filesystem,讲解 npx/Docker 安装、允许目录访问控制、全部文件工具与动态 roots。
Filesystem MCP Server 是一个基于 Node.js 的 Model Context Protocol (MCP) 服务器,提供文件系统操作能力。它通过 npm 包 @modelcontextprotocol/server-filesystem 发布。
功能特性
前置条件
安装与配置
方式一:npx(推荐)
Claude Desktop 配置(macOS/Linux):
json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/username/Desktop",
"/path/to/other/allowed/dir"
]
}
}
}
Windows 用户:
json
{
"mcpServers": {
"filesystem": {
"command": "cmd",
"args": [
"/c",
"npx",
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/username/Desktop",
"/path/to/other/allowed/dir"
]
}
}
}
方式二:Docker
所有目录必须挂载到 /projects 下。添加 ro 标志可使目录只读。
json
{
"mcpServers": {
"filesystem": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--mount", "type=bind,src=/Users/username/Desktop,dst=/projects/Desktop",
"--mount", "type=bind,src=/path/to/other/allowed/dir,dst=/projects/other/allowed/dir,ro",
"--mount", "type=bind,src=/path/to/file.txt,dst=/projects/path/to/file.txt",
"mcp/filesystem",
"/projects"
]
}
}
}
方式三:VS Code 集成
快速安装按钮:
 
手动配置(用户级,推荐): 打开命令面板(Ctrl + Shift + P),运行 MCP: Open User Configuration,在 mcp.json 中添加:
json
{
"servers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"${workspaceFolder}"
]
}
}
}
Windows 用户:
json
{
"servers": {
"filesystem": {
"command": "cmd",
"args": [
"/c",
"npx",
"-y",
"@modelcontextprotocol/server-filesystem",
"${workspaceFolder}"
]
}
}
}
Docker 方式:
json
{
"servers": {
"filesystem": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--mount", "type=bind,src=${workspaceFolder},dst=/projects/workspace",
"mcp/filesystem",
"/projects"
]
}
}
}
目录访问控制
服务器使用灵活的目录访问控制系统,支持两种方式:
方法一:命令行参数
启动时指定允许访问的目录:
bash
mcp-server-filesystem /path/to/dir1 /path/to/dir2
方法二:MCP Roots(推荐)
支持 Roots 的 MCP 客户端可以动态更新允许的目录。客户端通知的 Roots 会完全替换服务器端的允许目录。
重要:如果服务器启动时没有命令行参数,且客户端不支持 Roots 协议(或提供空的 Roots),服务器将在初始化时抛出错误。
工作流程
initialize 请求,服务器检查客户端是否支持 Roots 协议(capabilities.roots)roots/list 请求客户端的 Roots,并替换所有允许目录
- 运行时更新:客户端发送 notifications/roots/list_changed,服务器重新获取 Roots 并替换
list_allowed_directories 工具查看当前目录可用工具
read_text_filepath (string), head (number, 可选), tail (number, 可选)read_media_filepath (string)read_multiple_filespaths (string[])write_filepath (string), content (string)edit_filepath (string), edits (array), dryRun (boolean)create_directorypath (string)list_directorypath (string)list_directory_with_sizespath (string), sortBy (string, 可选)move_filesource (string), destination (string)search_filespath (string), pattern (string), excludePatterns (string[])directory_treepath (string), excludePatterns (string[])get_file_infopath (string)list_allowed_directories工具注解(MCP Hints)
服务器为每个工具设置了 MCP ToolAnnotations:
read_text_fileread_media_fileread_multiple_fileslist_directorylist_directory_with_sizesdirectory_treesearch_filesget_file_infolist_allowed_directoriescreate_directorywrite_fileedit_filemove_file所有工具均设置 openWorldHint: false,表示仅访问本地文件系统。
动态 Roots 与注意事项
edit_file 时,建议先用 dryRun: true 预览变更read_text_file 始终按 UTF-8 处理文件,不能同时指定 head 和 tailmove_file 在目标已存在时会失败FAQ
Q1: 启动时提示 "No allowed directories" 错误? A: 服务器需要至少一个允许目录。请确保在命令行参数中指定了目录,或者客户端支持 Roots 协议并提供了有效的 Roots。
Q2: 如何动态添加新的允许目录?
A: 使用支持 Roots 的客户端(如 Claude Desktop),通过 Roots 协议更新。客户端发送 notifications/roots/list_changed 通知后,服务器会自动获取新的 Roots 列表。
Q3: Docker 方式下文件权限问题?
A: 确保 Docker 挂载的目录有正确的读写权限。可以使用 ro 标志将目录设为只读,如 --mount type=bind,src=/path,dst=/projects/path,ro。
Q4: Windows 上 npx 启动失败?
A: 需要使用 cmd /c 方式启动,如配置示例所示。确保路径使用正斜杠或双反斜杠。
Q5: 如何查看当前允许的目录?
A: 使用 list_allowed_directories 工具,无需输入参数即可返回当前允许的目录列表。
*最后更新:2026 年 7 月。以官方 @modelcontextprotocol/server-filesystem 为准。*
相关工具
相关教程
让 AI 访问并管理你的 GitHub 仓库——GitHub MCP 服务器逐步指南
通过 AI 处理支付和管理订阅 - Stripe MCP 服务器分步指南
从概念到实践,全面解析 Anthropic Skill 开放标准与 MCP 协议如何构建可复用的 Agent 能力
学习 Stability AI API:Stable Diffusion 图像生成
Transformers.js 与 ONNX Runtime 浏览器端 AI 推理详细对比
使用 Modal 实现 AI 驱动的基础设施工作流的分步指南