文件系统 MCP 服务器
模型上下文协议 (MCP) 服务器实现通过标准化工具接口提供文件系统操作、分析和操控功能。
建筑学
该服务器基于 MCP SDK 构建,并分为不同的层:
成分
- 服务器层:处理MCP协议通信和工具调度
- 工具注册表:管理工具注册和执行
- 操作层:实现核心功能
- 文件系统接口:提供安全的文件系统访问
安装
- 克隆存储库:
git clone <repository-url>
cd filesystem-server
- 安装依赖项:
- 构建服务器:
- 配置 MCP 设置(cline_mcp_settings.json):
{
"mcpServers": {
"filesystem": {
"command": "node",
"args": ["path/to/filesystem-server/build/index.js"]
}
}
}
工具参考
目录操作
列表目录
列出带有元数据的目录内容。
interface ListDirectoryParams {
path: string; // Directory path
recursive?: boolean; // List recursively (default: false)
}
interface ListDirectoryResult {
entries: {
name: string;
path: string;
isDirectory: boolean;
size: number;
created: string;
modified: string;
accessed: string;
mode: string;
}[];
}
创建目录
创建新目录。
interface CreateDirectoryParams {
path: string; // Directory path
recursive?: boolean; // Create parent directories (default: true)
}
文件操作
读取文件
读取具有编码支持的文件内容。
interface ReadFileParams {
path: string; // File path
encoding?: string; // File encoding (default: 'utf8')
}
写入文件
将内容写入文件。
interface WriteFileParams {
path: string; // File path
content: string; // Content to write
encoding?: string; // File encoding (default: 'utf8')
}
追加文件
将内容附加到文件。
interface AppendFileParams {
path: string; // File path
content: string; // Content to append
encoding?: string; // File encoding (default: 'utf8')
}
分析操作
分析文本
分析文本文件属性。
interface AnalyzeTextParams {
path: string; // File path
}
interface AnalyzeTextResult {
lineCount: number;
wordCount: number;
charCount: number;
encoding: string;
mimeType: string;
}
计算哈希
使用指定的算法计算文件哈希值。
interface CalculateHashParams {
path: string; // File path
algorithm?: 'md5' | 'sha1' | 'sha256' | 'sha512'; // Hash algorithm
}
interface CalculateHashResult {
hash: string;
algorithm: string;
}
查找重复项
识别目录中的重复文件。
interface FindDuplicatesParams {
path: string; // Directory path
}
interface FindDuplicatesResult {
duplicates: {
hash: string;
size: number;
files: string[];
}[];
}
压缩操作
创建_zip
创建一个 ZIP 档案。
interface CreateZipParams {
files: string[]; // Files to include
output: string; // Output ZIP path
}
提取压缩文件
提取 ZIP 档案。
interface ExtractZipParams {
path: string; // ZIP file path
output: string; // Output directory
}
错误处理
服务器使用标准 MCP 错误代码:
enum ErrorCode {
ParseError = -32700,
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
InternalError = -32603
}
错误响应包括:
错误示例:
{
"code": -32602,
"message": "File not found: /path/to/file.txt"
}
发展
项目结构
src/
├── operations/ # Core operations implementation
├── tools/ # MCP tool definitions and handlers
├── __tests__/ # Test suites
├── index.ts # Entry point
├── server.ts # MCP server setup
├── types.ts # Type definitions
└── utils.ts # Utility functions
运行测试
运行测试套件:
覆盖运行:
开发模式
以监视模式运行:
代码质量
检查代码库:
类型检查:
依赖项
核心依赖项:
- @modelcontextprotocol/sdk:MCP 服务器实现
- file-type:文件类型检测
- mime-types:MIME 类型查找
- crypto-js:文件哈希
- 归档器:ZIP 创建
- extract-zip:ZIP 提取
- iconv-lite:文本编码
- chardet:编码检测
开发依赖项:
- typescript:类型系统
- 开玩笑:测试
- eslint:Lint
- 更漂亮:格式化
- ts-node:TypeScript 执行
- nodemon:开发服务器
贡献
- 分叉存储库
- 创建你的功能分支
- 为新功能编写测试
- 确保所有测试通过
- 提交拉取请求
执照
麻省理工学院