文件操作MCP服务器
一种模型上下文协议(MCP)服务器,使用FastMCP提供全面的文件系统操作。该服务器允许AI助手通过一组定义良好的工具安全地与文件系统交互。
特性
服务器提供以下文件操作:
文件操作
- read_file:读取文件内容
- write_file:将内容写入文件(根据需要创建目录)
- 附录文件:将内容附加到现有文件
- 删除文件:删除文件
- copy_file:将文件从源复制到目标
- move_file:将文件从源移动到目标
目录操作
- list_directory:用文件大小列出目录的内容
- create_directory:创建目录(包括父目录)
- delete_directory:删除目录及其所有内容
信息与搜索
- 获取_文件_信息:获取有关文件或目录的详细信息
- 查找文件:在目录中查找与模式匹配的文件
安装
🚀 快速设置(推荐)
- 克隆此存储库:
git clone
cd your-first-mcp-server- 运行安装脚本:
Linux/macOS:
./setup.shWindows(PowerShell):
.\setup.ps1安装脚本将:
- 创建虚拟环境
- 安装所有依赖项
- 自动配置克劳德桌面
- 创建运行脚本以便于服务器管理
- 测试安装
- 重新启动克劳德桌面 加载新配置
就是这样!服务器现在应该可以在Claude Desktop中使用。
📋 手动安装
如果您更喜欢手动安装:
选项1:使用pip(建议使用虚拟环境)
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt选项2:使用pip(系统范围)
pip install -r requirements.txt选项3:使用setup.py(开发安装)
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -e .用法
运行服务器
如果您使用了安装脚本:
# Linux/macOS
./run_server.sh # Regular server
./test_tools.sh # Run automated tests
./test_tools.sh --interactive # Interactive testing
# Windows
.\run_server.bat # Regular server
.\test_tools.bat # Run automated tests
.\test_tools.bat --interactive # Interactive testing手动启动:
# If using virtual environment
source venv/bin/activate # On Windows: venv\Scripts\activate
python main.py
# If installed system-wide
python main.py如果使用setup.py安装:
file-operations-server服务器将启动并监听MCP连接。
与Claude Desktop一起使用
如果您使用了安装脚本:
Claude Desktop配置已设置! 只需重新启动Claude Desktop,服务器即可使用。
手动配置:
如果您手动安装,则需要配置Claude Desktop:
- 复制示例配置:使用提供的
claude_desktop_config.json文件作为起点 - 更新路径:编辑要替换的文件
/absolute/path/to/your-first-mcp-server/main.py具有服务器文件的实际绝对路径 - 复制到Claude配置位置:将更新的配置复制到Claude Desktop配置目录
快速手动设置:
# Copy the example config
cp claude_desktop_config.json ~/Library/Application\ Support/Claude/claude_desktop_config.json
# Edit the path in the config file
nano ~/Library/Application\ Support/Claude/claude_desktop_config.json配置示例:
{
"mcpServers": {
"file-operations": {
"command": "python",
"args": ["/Users/yourusername/your-first-mcp-server/main.py"]
}
}
}备注:确保使用绝对路径 main.py 文件。
配置文件位置
这 claude_desktop_config.json 文件通常位于:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - 视窗:
%APPDATA%\Claude\claude_desktop_config.json - Linux:
~/.config/Claude/claude_desktop_config.json
工具参考
read_file(路径:str)->str
读取并返回文件的内容。
参数:
path:要读取的文件的路径
退货: 文件内容为字符串,如果无法读取文件,则显示错误消息。
write_file(路径:str,内容:str)->str
将内容写入文件,根据需要创建目录。
参数:
path:要写入的文件的路径content:要写入文件的内容
退货: 成功消息或错误消息。
append_file(路径:str,内容:str)->str
将内容附加到现有文件。
参数:
path:要附加到的文件的路径content:要附加的内容
退货: 成功消息或错误消息。
list_directory(路径:str=“.”)->str
使用文件大小和类型指示符列出目录的内容。
参数:
path:目录路径(默认为当前目录)
退货: 目录内容的格式化列表📁 对于目录和📄 对于文件。
create_directory(路径:str)->str
创建一个目录,包括任何必要的父目录。
参数:
path:要创建的目录路径
退货: 成功消息或错误消息。
delete_file(路径:str)->str
删除文件。
参数:
path:要删除的文件的路径
退货: 成功消息或错误消息。
delete_directory(路径:str)->str
删除目录及其所有内容。
参数:
path:要删除的目录路径
退货: 成功消息或错误消息。
copy_file(源:str,目标:str)->str
将文件从源复制到目标。
参数:
source:源文件路径destination:目标文件路径
退货: 成功消息或错误消息。
move_file(源:str,目标:str)->str
将文件从源移动到目标。
参数:
source:源文件路径destination:目标文件路径
退货: 成功消息或错误消息。
get_file_info(路径:str)->str
获取有关文件或目录的详细信息。
参数:
path:文件或目录的路径
退货: 格式化信息,包括类型、大小、修改时间和权限。
find_files(目录:str=“.”,模式:str=”\*“)->str
在目录中查找与模式匹配的文件。
参数:
directory:要搜索的目录(默认为当前目录)pattern:要匹配的球形图案(默认为“\*”)
退货: 匹配的文件和目录列表。
错误处理
所有工具都包括针对常见场景的全面错误处理:
- 文件未找到
- 权限不足
- 路径无效
- 目录与文件不匹配
错误以描述性字符串而不是异常的形式返回,这使得AI助手可以安全地使用它们。
安全考虑
此服务器在运行进程的权限范围内提供完全的文件系统访问权限。部署时:
- 以最低限度的必要权限运行
- 考虑将服务器进程沙盒化
- 监控文件系统访问模式
- 根据您的用例需要实施额外的访问控制
发展
测试
要测试服务器功能,请执行以下操作:
# Test basic functionality
python -c "
from main import *
print(list_directory('.'))
print(write_file('test.txt', 'Hello, World!'))
print(read_file('test.txt'))
print(delete_file('test.txt'))
"添加新工具
要添加新的文件操作,请执行以下操作:
- 使用定义新函数
@mcp.tool()装饰器 - 添加适当的错误处理
- 返回描述性成功/错误消息
- 更新此文档
许可证
本项目按原样提供,用于教育和发展目的。
