MCP文件流添加福利群:解决AI开发者的「MCP实战痛点」一个提供增强文件操作功能的模型上下文协议(MCP)服务器,支持流式传输、补丁和变更跟踪。
要通过 Smithery自动为 Claude Desktop 安装文件操作服务器:
npx -y @smithery/cli install @bsmi021/mcp-file-operations-server --client claude
npm install
有关包括 Windows 和 Linux 本地驱动器挂载的详细 Docker 设置说明,请参阅 DOCKER.md。
快速 Docker 启动:
# Stdio 传输(适用于 MCP 客户端)
docker run -it --rm -v "$(pwd):/workspace" ghcr.io/bsmi021/mcp-file-operations-server
# HTTP 传输(适用于 Web/远程访问)
docker run -it --rm -p 3001:3001 -v "$(pwd):/workspace" -e MCP_TRANSPORT=http ghcr.io/bsmi021/mcp-file-operations-server

服务器支持两种传输模式:
适用于与 Claude Desktop 等 MCP 客户端的直接集成:
npm start
适用于远程连接和 Web 应用程序:
npm run start:http
HTTP 服务器提供:
GET http://localhost:3001/sse - 建立流式连接POST http://localhost:3001/messages - 接收客户端消息GET http://localhost:3001/health - 服务器状态GET http://localhost:3001/sessions - 活动连接信息# Stdio 传输,自动重载
npm run dev
# HTTP 传输,自动重载
npm run dev:http

# Stdio 传输
npm start
# HTTP 传输
npm run start:http
# 自定义 HTTP 端口
npm run start:http -- --port 8080

copy_file:将文件复制到新位置read_file:从文件中读取内容write_file:将内容写入文件move_file:移动/重命名文件delete_file:删除文件append_file:将内容追加到文件make_directory:创建目录remove_directory:移除目录copy_directory:递归复制目录(带进度报告)watch_directory:开始监视目录的变更unwatch_directory:停止监视目录get_changes:获取记录的变更列表clear_changes:清除所有记录的变更file:///recent-changes:最近文件系统变更列表file://{path}:访问文件内容metadata://{path}:访问文件元数据directory://{path}:列出目录内容// 复制文件
await fileOperations.copyFile({
source: 'source.txt',
destination: 'destination.txt',
overwrite: false
});
// 监视目录
await fileOperations.watchDirectory({
path: './watched-dir',
recursive: true
});
// 通过资源访问文件内容
const resource = await mcp.readResource('file:///path/to/file.txt');
console.log(resource.contents[0].text);
// 复制目录并跟踪进度
const result = await fileOperations.copyDirectory({
source: './source-dir',
destination: './dest-dir',
overwrite: false
});
// 结果中的进度令牌可用于跟踪进度
console.log(result.progressToken);

通过 JavaScript 连接:
// 建立 SSE 连接
const eventSource = new EventSource('http://localhost:3001/sse');
let sessionId = null;
eventSource.onopen = function() {
console.log('已连接到 MCP 服务器');
};
eventSource.onmessage = function(event) {
const message = JSON.parse(event.data);
// 从第一条消息中提取会话 ID
if (!sessionId && message.sessionId) {
sessionId = message.sessionId;
}
console.log('收到:', message);
};
// 向服务器发送消息
async function sendMessage(method, params) {
const message = {
jsonrpc: '2.0',
id: Date.now(),
method: method,
params: params
};
const response = await fetch('http://localhost:3001/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Session-ID': sessionId
},
body: JSON.stringify(message)
});
return response.json();
}
// 示例:列出工具
sendMessage('tools/list', {});
// 示例:读取文件
sendMessage('tools/call', {
name: 'read_file',
arguments: { path: '/workspace/example.txt' }
});

使用 curl 进行测试:
# 在后台启动 SSE 连接
curl -N http://localhost:3001/sse &
# 检查服务器健康状态
curl http://localhost:3001/health
# 列出活动会话
curl http://localhost:3001/sessions

交互式 Web 客户端:
完整的交互式示例可在 examples/http-client.html 中找到。在 Web 浏览器中打开此文件,使用用户友好的 GUI 测试 HTTP 接口。
# 构建镜像
docker build -t mcp-file-operations-server .
# 使用 stdio 运行(适用于 MCP 客户端)
docker run -it --rm -v "$(pwd):/workspace" mcp-file-operations-server
# 使用 HTTP 接口运行
docker run -it --rm -p 3001:3001 -v "$(pwd):/workspace" -e MCP_TRANSPORT=http mcp-file-operations-server

Windows:
docker run -it --rm -v "C:\MyProject:/workspace" -p 3001:3001 -e MCP_TRANSPORT=http mcp-file-operations-server

Linux/macOS:
docker run -it --rm -v "/home/user/project:/workspace" -p 3001:3001 -e MCP_TRANSPORT=http mcp-file-operations-server

有关包括 Windows 和 Linux 本地驱动器挂载的详细 Docker 设置说明,请参阅 DOCKER.md。
服务器实现了速率限制以防止滥用:
速率限制错误信息中包含重试间隔时间。
所有文件路径都经过验证,以防止目录遍历攻击:
../)长时间运行的操作(如目录复制)提供进度更新:
interface ProgressUpdate {
token: string | number;
message: string;
percentage: number;
}

可以通过操作结果中的进度令牌跟踪进度。
npm run build
npm run lint
npm run format
npm test

| 变量 | 默认值 | 描述 |
|---|---|---|
MCP_TRANSPORT |
stdio |
传输模式:stdio 或 http |
MCP_HTTP_PORT |
3001 |
HTTP 传输的端口 |
服务器可以通过各种设置进行配置:
服务器通过 FileOperationError 类和 MCP 错误代码提供详细的错误信息:
InvalidRequest:无效的参数或请求格式MethodNotFound:请求了未知的工具或资源InvalidParams:无效的参数(例如,路径验证失败)InternalError:服务器端错误每个错误包括:
git checkout -b feature/amazing-feature)git commit -m '添加了很棒的功能')git push origin feature/amazing-feature)本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。