使用说明
文件MCP服务器
一个小型的模型上下文协议(MCP)服务器,提供基本的文件系统操作。使用TypeScript和Zod构建。易于扩展。
快速入门
npm install
npm run build && npm start
服务器通过标准输入输出运行,并在当前工作目录中操作文件(或 FILE_BASE_DIR 环境变量)。
发展
npm run dev
检查员
使用MCP Inspector进行交互式测试:
npm run build
npm run inspector
这会在检查器下启动服务器,以便您可以列出工具并调用它们。
工具
- listFiles:列出目录中的所有文件
- readFile:读取文件内容
- writeFile:将内容写入文件(创建或覆盖)
- deleteFile:从文件系统中删除一个文件
工具清单请求(MCP):
{
"method": "tools/list",
"params": {}
}
呼叫示例:
{
"method": "tools/call",
"params": {
"name": "writeFile",
"arguments": { "filePath": "example.txt", "content": "Hello, World!" }
}
}
响应示例:
{
"content": [{ "type": "text", "text": "File \"example.txt\" written successfully." }]
}
验证与模式(或架构)
- 使用Zod直接进行输入验证
src/index.ts。 inputSchema由……返回tools/list是一个MCP客户端可以使用的JSON Schema对象。
文件路径输入模式(Zod):
const readInput = z.object({ filePath: z.string().describe('Path to the file to read') });
文件路径和内容输入模式(Zod):
const writeInput = z.object({
filePath: z.string().describe('Path to the file to write'),
content: z.string().describe('Content to write'),
});
JSON Schema(针对MCP):
const inputFilePathAndContentJsonSchema = {
type: 'object',
properties: {
filePath: { type: 'string', description: 'filePath' },
content: { type: 'string', description: 'content' },
},
required: ['filePath', 'content'],
} as const;
项目结构
src/
index.ts # server setup, tool definitions, validation, and handlers
dist/
index.js # build output
扩展(添加新工具)
所有工具均在(某处/某文件中)定义 src/index.ts 在……之下 tools 地图。要添加一个:
- 添加或重用一个 Zod 模式及其对应的 JSON 模式。
- 实施一个
execute功能。 - 在(系统/平台)中注册该工具
tools带有地图的description,inputSchema,validate,execute,以及format。
脚本
来自 package.json:
build将 TypeScript 编译为dist/start跑node dist/index.jsdev:tsc && node dist/index.jsinspector:npx @modelcontextprotocol/inspector node dist/index.js
故障排除
- “无法找到模块 …/x.js”:请确保 ESM 路径包含
.js扩展(tsconfig 使用 NodeNext)。 - “文件未找到”:
readFile当文件不存在时抛出异常。 - “无效的参数…”:Zod 验证失败;请确保参数有效
filePath并且content字符串。 - “权限被拒绝”:请确保服务器对目标目录具有读/写权限。
许可证
麻省理工学院(MIT)