MCP 文件系统服务器通过模型上下文协议提供了对AI模型的安全文件系统访问。它强制执行严格的路径验证,并且只允许访问预定义的目录。
<br>必须安装以下软件:
$ git clone https://github.com/gabrielmaialva33/mcp-filesystem.git
$ cd mcp-filesystem
<br>
# 安装依赖
$ pnpm install
# 构建应用
$ pnpm build
# 运行服务器(指定允许访问的目录)
$ pnpm start /path/to/allowed/directory
# 或使用配置文件
$ pnpm start --config=config.json
# 全局安装
$ npm install -g @gabrielmaialva33/mcp-filesystem
# 运行服务器
$ mcp-filesystem /path/to/allowed/directory
# 或使用npx(无需安装)
$ npx @gabrielmaialva33/mcp-filesystem /path/to/
allowed/directory
# 创建样本配置文件
$ npx @gabrielmaialva33/mcp-filesystem --create-config=config.json
# 构建Docker镜像
$ docker build -t gabrielmaialva33/mcp-filesystem .
# 使用Docker运行
$ docker run -i --rm -v /path/to/data:/data:ro gabrielmaialva33/mcp-filesystem /data
# 使用配置文件
$ docker run -i --rm -v /path/to/config.json:/app/config.json -v /path/to/data:/data gabrielmaialva33/mcp-filesystem --config=/app/config.json
# 创建数据目录
$ mkdir -p data
# 启动服务器
$ docker-compose up -d
<br>
Claude Desktop可以配置使用此MCP服务器进行文件系统访问。在你的claude_desktop_config.json中添加以下内容:
{
"mcpServers": {
"filesystem": {
"command": "mcp-filesystem",
"args": [
"/Users/gabrielmaia/Documents",
"/Users/gabrielmaia/Desktop",
"/Users/gabrielmaia/Downloads"
]
}
}
}
确保全局可用可执行文件:
# 使二进制文件可执行
chmod +x /Users/gabrielmaia/.nvm/versions/node/v22.14.0/bin/mcp-filesystem
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@gabrielmaialva33/mcp-filesystem",
"/Users/username/Desktop",
"/path/to/other/allowed/dir"
]
}
}
}
注意:当使用Docker时,默认情况下所有目录都必须挂载到/projects。添加ro标志会使目录变为只读。
{
"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",
"gabrielmaialva33/mcp-filesystem",
"/projects"
]
}
}
}
MCP 文件系统服务器提供以下工具:
curl_request工具将允许您对外部API执行HTTP请求:
// 示例:带有身份验证的GET请求
curl_request({
url: 'https://api.example.com/data',
method: 'GET',
headers: {
Authorization: 'Bearer your_token_here',
},
})
// 示例:带有JSON数据的POST请求
curl_request({
url: 'https://api.example.com/create',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
data: '{"name":"Example","value":123}',
})
请参阅docs/curl-tool-examples.md文件以获取更多详细示例。
您可以使用以下命令创建配置文件:
$ mcp-filesystem --create-config=config.json
示例配置:
{
"allowedDirectories": ["/path/to/allowed/dir1", "/path/to/allowed/dir2"],
"logLevel": "info",
"logFile": "/path/to/logs/mcp-filesystem.log",
"serverName": "secure-filesystem-server",
"serverVersion": "0.3.0",
"cache": {
"enabled": true,
"maxSize": 1000,
"ttlMs": 60000
},
"metrics": {
"enabled": true,
"reportIntervalMs": 60000
},
"security": {
"maxFileSize": 10485760,
"allowSymlinks": true,
"validateRealPath": true
}
}
<br>
| Gabriel Maia |