Skip to main content
Glama

Node.js MCP Server

by WeiWeicode
fileTools.js7.34 kB
import fs from 'fs/promises'; import path from 'path'; class FileTools { getToolDefinitions() { return [ { name: 'read_file', description: '讀取文件內容', inputSchema: { type: 'object', properties: { filepath: { type: 'string', description: '要讀取的文件路徑', }, }, required: ['filepath'], }, }, { name: 'write_file', description: '寫入文件內容', inputSchema: { type: 'object', properties: { filepath: { type: 'string', description: '要寫入的文件路徑', }, content: { type: 'string', description: '要寫入的內容', }, }, required: ['filepath', 'content'], }, }, { name: 'list_directory', description: '列出目錄內容', inputSchema: { type: 'object', properties: { dirpath: { type: 'string', description: '要列出的目錄路徑', }, }, required: ['dirpath'], }, }, { name: 'create_directory', description: '創建目錄', inputSchema: { type: 'object', properties: { dirpath: { type: 'string', description: '要創建的目錄路徑', }, }, required: ['dirpath'], }, }, { name: 'delete_file', description: '刪除文件', inputSchema: { type: 'object', properties: { filepath: { type: 'string', description: '要刪除的文件路徑', }, }, required: ['filepath'], }, }, ]; } hasToolName(name) { return this.getToolDefinitions().some(tool => tool.name === name); } async handleToolCall(name, args) { switch (name) { case 'read_file': return await this.readFile(args.filepath); case 'write_file': return await this.writeFile(args.filepath, args.content); case 'list_directory': return await this.listDirectory(args.dirpath); case 'create_directory': return await this.createDirectory(args.dirpath); case 'delete_file': return await this.deleteFile(args.filepath); default: throw new Error(`Unknown file tool: ${name}`); } } async readFile(filepath) { try { const content = await fs.readFile(filepath, 'utf-8'); return { content: [ { type: 'text', text: `文件內容 (${filepath}):\n\n${content}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `讀取文件失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } async writeFile(filepath, content) { try { // 確保目錄存在 const dir = path.dirname(filepath); await fs.mkdir(dir, { recursive: true }); await fs.writeFile(filepath, content, 'utf-8'); return { content: [ { type: 'text', text: `成功寫入文件: ${filepath}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `寫入文件失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } async listDirectory(dirpath) { try { const items = await fs.readdir(dirpath, { withFileTypes: true }); const itemList = items.map(item => { const type = item.isDirectory() ? '[目錄]' : '[文件]'; return `${type} ${item.name}`; }).join('\n'); return { content: [ { type: 'text', text: `目錄內容 (${dirpath}):\n\n${itemList}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `讀取目錄失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } async createDirectory(dirpath) { try { await fs.mkdir(dirpath, { recursive: true }); return { content: [ { type: 'text', text: `成功創建目錄: ${dirpath}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `創建目錄失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } async deleteFile(filepath) { try { await fs.unlink(filepath); return { content: [ { type: 'text', text: `成功刪除文件: ${filepath}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `刪除文件失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } } export const fileTools = new FileTools();

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/WeiWeicode/20250923MCPtest'

If you have feedback or need assistance with the MCP directory API, please join our Discord server