本项目是一个 MCP 服务的实现,提供文件系统操作、分析与管理功能,并通过标准化工具接口对外提供服务。
本文件系统 MCP 服务器基于 MCP SDK 构建,可通过标准化工具接口提供文件系统操作、分析和管理功能。以下为你介绍如何快速搭建此服务器。
按照以下步骤完成服务器的安装与配置:
git clone
cd filesystem-server
npm install
npm run build
cline_mcp_settings.json
):{
"mcpServers": {
"filesystem": {
"command": "node",
"args": ["path/to/filesystem-server/build/index.js"]
}
}
}
interface ListDirectoryParams {
path: string; // 目录路径
recursive?: boolean; // 是否递归列出(默认:false)
}
interface ListDirectoryResult {
entries: {
name: string;
path: string;
isDirectory: boolean;
size: number;
created: string;
modified: string;
accessed: string;
}[];
}
interface CreateDirectoryParams {
path: string; // 目录路径
}
interface CreateDirectoryResult {
success: boolean;
message?: string;
}
interface ReadFileParams {
path: string; // 文件路径
encoding?: string; // 字符编码(默认:utf-8)
}
interface ReadFileResult {
content: Buffer | string;
mimeType: string;
}
interface WriteFileParams {
path: string; // 文件路径
content: string | Buffer; // 文件内容
encoding?: string; // 字符编码(默认:utf-8)
}
interface WriteFileResult {
success: boolean;
message?: string;
}
interface DeleteFileParams {
path: string; // 文件路径
}
interface DeleteFileResult {
success: boolean;
message?: string;
}
在实际使用中,你可以根据业务需求组合这些工具接口,实现更复杂的文件系统操作。例如,结合 list_directory
和 delete_file
实现批量删除指定目录下的文件。
该服务器基于 MCP SDK 并分为不同的层次,架构图如下:
graph TD
A[MCP 服务器层] --> B[工具注册表]
B --> C[操作层]
C --> D[文件系统操作]
C --> E[分析操作]
C --> F[流操作]
list_directory
:列出目录内容及其元数据。create_directory
:创建新目录。read_file
:读取文件内容。write_file
:写入文件内容。delete_file
:删除文件。MCP 协议定义了以下错误代码:
100
:通用错误101
:身份验证失败102
:权限 denied103
:文件或目录不存在104
:系统资源不足105
:操作超时106
:无效参数错误响应格式如下:
{
"error": {
"code": number,
"message": string
}
}
.
├── operations/ # 核心操作实现
├── tools/ # MCP 工具定义和处理程序
├── __tests__/ # 测试套件
├── index.ts # 入口点
├── server.ts # MCP 服务器设置
├── types.ts # 类型定义
└── utils.ts # 工具函数
npm test
npm run test:coverage
以观察模式运行:
npm run watch
npm run lint
npm run type-check
依赖项 | 功能 |
---|---|
@modelcontextprotocol/sdk | MCP 服务器实现 |
file-type | 文件类型检测 |
mime-types | MIME 类型查找 |
crypto-js | 文件哈希 |
archiver | ZIP 创建 |
extract-zip | ZIP 提取 |
iconv-lite | 文本编码 |
chardet | 编码检测 |
依赖项 | 功能 |
---|---|
typescript | 类型系统 |
jest | 测试 |
eslint | 代码检查 |
prettier | 格式化 |
ts-node | TypeScript 执行 |
nodemon | 开发服务器 |
本服务器基于 MCP SDK 构建,采用分层架构,各层之间职责清晰,通过工具注册表实现工具的统一管理和调度。在文件操作方面,使用了多个第三方库来实现文件类型检测、MIME 类型查找、文件哈希等功能,确保了系统的功能完整性和性能。同时,通过定义详细的错误代码和错误响应格式,方便开发者进行问题定位和处理。
如果你想为项目做出贡献,请按照以下步骤进行:
本项目采用 MIT 许可证。