文件系统操作工具

一个完整的MCP(模型上下文协议)实现,用于文件系统操作,提供现代Web界面、命令行接口和REST API。

  • by Aakash231217
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 aakash231217/mcp_server_assignment

使用说明

MCP文件系统操作

一个完整的MCP(模型上下文协议)实现,用于具有现代web界面的文件系统操作。

特性

  • MCP服务器:支持创建、编辑、删除、读取文件和目录
  • MCP客户端:交互式命令行界面
  • Web前端:具有拖放文件夹上传功能的现代React/Next.js界面
  • REST API:前端和MCP服务器之间的桥梁
  • 自然语言处理:将用户提示解析为文件系统操作

建筑

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   Frontend      │────▶│   REST API      │────▶│   MCP Server    │
│   (Next.js)     │     │   (Express)     │     │   (Node.js)     │
└─────────────────┘     └─────────────────┘     └─────────────────┘
        │                        │                        │
        │                        │                        ▼
        │                        │               ┌─────────────────┐
        │                        │               │   File System   │
        │                        │               │   Operations    │
        └────────────────────────┼───────────────┴─────────────────┘
                                 │
                                 ▼
                        ┌─────────────────┐
                        │   MCP Client    │
                        │   (CLI)         │
                        └─────────────────┘

快速开始

1.安装依赖项

# Install main dependencies
npm install

# Install frontend dependencies
cd frontend
npm install
cd ..

2.启动MCP服务器

npm start

3.运行Web界面

# Terminal 1: Start the API server
node api/server.js

# Terminal 2: Start the frontend
cd frontend
npm run dev

4.尝试CLI客户端

npm run client

使用示例

web界面

  1. 上传的文件夹:拖放文件夹以上载所有文件
  2. 使用自然语言:键入以下命令:
    • “使用‘hello World’创建文件‘hello.txt’”
    • “使用‘更新内容’编辑文件‘package.json’”
    • “删除‘旧文件.txt’”
    • “阅读'README.md'”

CLI客户端

📁 > create_file hello.txt "Hello World"
✅ File created successfully: hello.txt

📁 > read_file hello.txt
✅ Hello World

📁 > edit_file hello.txt "Hello MCP World!" replace
✅ File edited successfully: hello.txt

📁 > list_files .
✅ [
  {
    "name": "hello.txt",
    "type": "file",
    "size": 17,
    "modified": "2024-01-15T10:30:00.000Z"
  }
]

📁 > delete_file hello.txt
✅ Deleted successfully: hello.txt

REST API

# Execute MCP operations
curl -X POST http://localhost:3002/api/execute \
  -H "Content-Type: application/json" \
  -d '{"operation": "create_file", "args": {"path": "test.txt", "content": "Hello"}}'

# Use natural language prompts
curl -X POST http://localhost:3002/api/prompt \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Create file '\''hello.txt'\'' with '\''Hello World'\''"}'

# List files
curl http://localhost:3002/api/files?path=.

# Read file
curl http://localhost:3002/api/file/hello.txt

MCP工具可用

工具 说明 参数
create_file 创建新文件 path, content
edit_file 编辑现有文件 path, content, mode (替换/附加)
delete_file 删除文件或目录 path
read_file 读取文件内容 path
list_files 列出目录内容 path (可选)
create_directory 创建目录 path

安全功能

  • 路径验证:防止目录遍历攻击
  • 工作目录限制:操作仅限于指定文件夹
  • 文件类型验证:可配置的文件类型限制
  • 大小限制:可配置的文件大小限制

发展

项目结构

mcp-filesystem/
├── server/
│   └── index.js          # MCP Server implementation
├── client/
│   └── index.js          # CLI client
├── api/
│   └── server.js         # REST API server
├── frontend/
│   ├── app/
│   │   ├── page.tsx      # Main React component
│   │   ├── layout.tsx    # Layout component
│   │   └── globals.css   # Global styles
│   ├── package.json      # Frontend dependencies
│   └── next.config.js    # Next.js configuration
├── package.json          # Main package.json
└── README.md            # This file

添加新工具

  1. 在中添加工具定义 server/index.js:
{
  name: "my_new_tool",
  description: "Description of the tool",
  inputSchema: {
    type: "object",
    properties: {
      param1: { type: "string", description: "Parameter description" }
    },
    required: ["param1"]
  }
}
  1. 机具处理程序:
case "my_new_tool":
  return await this.myNewTool(args.param1);
  1. 添加方法实现:
async myNewTool(param1) {
  // Implementation here
  return {
    content: [{ type: "text", text: "Result message" }]
  };
}

部署

码头工人

FROM node:18-alpine

WORKDIR /app
COPY package*.json ./
RUN npm install

COPY . .
EXPOSE 3000 3001 3002

CMD ["npm", "start"]

环境变量

# API Server
PORT=3002
MCP_SERVER_PATH=../server/index.js

# Frontend
NEXT_PUBLIC_API_URL=http://localhost:3002

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 添加测试
  5. 提交拉取请求

许可证

MIT许可证-有关详细信息,请参阅许可证文件

故障排除

常见问题

  1. MCP服务器连接失败

    • 确保Node.js版本>=18
    • 检查server/index.js是否可执行
    • 验证是否安装了依赖项
  2. 文件上传不起作用

    • 检查文件夹权限
    • 确保API服务器正在端口3002上运行
    • 验证CORS设置
  3. 前端构建错误

    • 跑 npm install 在前端目录中
    • 检查TypeScript版本兼容性
    • 验证是否已安装所有依赖项

调试模式

启用调试日志记录:

DEBUG=mcp:* npm start

支持

对于问题和疑问:

  • 在GitHub上创建问题
  • 检查故障排除部分
  • 查看API文档

内置于❤️ 使用MCP(模型上下文协议)