文件系统 MCP 服务器是一个基于 MCP 协议的标准工具接口,可实现文件系统操作、分析和管理功能,为用户提供便捷的文件处理方案。
该服务器基于 MCP SDK 构建,其结构划分为不同层次,各层次相互协作实现服务器功能。以下是具体的结构和组件说明:
graph TD
A[ MCP 服务器层 ] --> B[ 工具注册表 ]
B --> C[ 操作层 ]
C --> D[ 文件系统操作 ]
C --> E[ 分析操作 ]
C --> F[ 流操作 ]
git clone
cd filesystem-server
npm install
npm run build
{
"mcpServers": {
"filesystem": {
"command": "node",
"args": ["path/to/filesystem-server/build/index.js"]
}
}
}
此工具用于列出目录内容及其元数据。 接口:
interface ListDirectoryParams {
/** 要列出的目录路径 */
path: string;
}
interface ListDirectoryResult {
/** 目录中的文件和子目录列表 */
entries: Array<{
/** 文件或子目录的名称 */
name: string;
/** 文件类型('file' 或 'directory') */
type: string;
/** 修改时间 */
modifiedTime: Date;
}>;
}
示例:
const result = await filesystem.listFiles('/path/to/dir');
// 结果结构如上所示
用于读取文件内容。 接口:
interface ReadFileParams {
/** 要读取的文件路径 */
path: string;
}
interface ReadFileResult {
/** 文件内容(二进制缓冲区) */
content: Buffer;
}
示例:
const content = await filesystem.readFile('/path/to/file.txt');
// content 包含文件数据
用于写入文件内容。 接口:
interface WriteFileParams {
/** 要写的文件路径 */
path: string;
/** 文件内容 */
content: Buffer | string;
}
interface WriteFileResult {
/** 写入的字节数 */
bytesWritten: number;
}
示例:
const result = await filesystem.writeFile('/path/to/file.txt', '写入的内容');
// 返回写入的字节数
错误码 | 详情 |
---|---|
400 Bad Request | 无效请求参数。 |
401 Unauthorized | 权限不足,无法执行操作。 |
404 Not Found | 指定文件或目录不存在。 |
500 Internal Server Error | 服务器内部错误。 |
npm run watch
检查代码:
npm run lint
类型检查:
npm run type-check
本项目采用 MIT 许可证。