这是一个通过标准化工具接口提供文件系统操作、分析和处理能力的 Model Context Protocol (MCP) 服务器实现。
该服务器基于 MCP SDK 构建,并组织成不同的层次:
graph TD
A[MCP 服务器层] --> B[工具注册表]
B --> C[操作层]
C --> D[文件系统操作]
C --> E[分析操作]
C --> F[流操作]
git clone <repository-url>
cd filesystem-server
npm install
npm run build
{
"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;
mode: string;
}[];
}
创建新目录。
interface CreateDirectoryParams {
path: string; // 目录路径
recursive?: boolean; // 是否创建父目录(默认:true)
}
读取文件内容并支持编码。
interface ReadFileParams {
path: string; // 文件路径
encoding?: string; // 文件编码(默认:'utf8')
}
写入内容到文件。
interface WriteFileParams {
path: string; // 文件路径
content: string; // 要写入的内容
encoding?: string; // 文件编码(默认:'utf8')
}
追加内容到文件。
interface AppendFileParams {
path: string; // 文件路径
content: string; // 要追加的内容
encoding?: string; // 文件编码(默认:'utf8')
}
分析文本文件属性。
interface AnalyzeTextParams {
path: string; // 文件路径
}
interface AnalyzeTextResult {
lineCount: number;
wordCount: number;
charCount: number;
encoding: string;
mimeType: string;
}
使用指定算法计算文件哈希值。
interface CalculateHashParams {
path: string; // 文件路径
algorithm?: 'md5' | 'sha1' | 'sha256' | 'sha512'; // 哈希算法
}
interface CalculateHashResult {
hash: string;
algorithm: string;
}
识别目录中的重复文件。
interface FindDuplicatesParams {
path: string; // 目录路径
}
interface FindDuplicatesResult {
duplicates: {
hash: string;
size: number;
files: string[];
}[];
}
创建 ZIP 归档文件。
interface CreateZipParams {
files: string[]; // 要包含的文件
output: string; // 输出 ZIP 路径
}
提取 ZIP 归档文件。
interface ExtractZipParams {
path: string; // ZIP 文件路径
output: string; // 输出目录
}
服务器使用标准 MCP 错误码:
enum ErrorCode {
ParseError = -32700,
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
InternalError = -32603
}
错误响应包括:
示例错误:
{
"code": -32602,
"message": "未找到文件:/path/to/file.txt"
}
src/
├── 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
核心依赖项:
开发依赖项:
MIT