Skip to main content
Glama

Node.js MCP Server

by WeiWeicode
fileTools.ts6.12 kB
import fs from 'fs/promises'; import path from 'path'; export interface ToolDefinition { name: string; description: string; inputSchema: { type: string; properties: Record<string, any>; required?: string[]; }; } export interface ToolResult { content: Array<{ type: 'text'; text: string; }>; isError?: boolean; } class FileTools { getToolDefinitions(): ToolDefinition[] { 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: string): boolean { return this.getToolDefinitions().some(tool => tool.name === name); } async handleToolCall(name: string, args: Record<string, any>): Promise<ToolResult> { 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}`); } } private async readFile(filepath: string): Promise<ToolResult> { 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, }; } } private async writeFile(filepath: string, content: string): Promise<ToolResult> { 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, }; } } private async listDirectory(dirpath: string): Promise<ToolResult> { 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, }; } } private async createDirectory(dirpath: string): Promise<ToolResult> { 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, }; } } private async deleteFile(filepath: string): Promise<ToolResult> { 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