Skip to main content
Glama

Node.js MCP Server

by WeiWeicode
systemTools.js10.2 kB
import os from 'os'; import { exec } from 'child_process'; import { promisify } from 'util'; const execAsync = promisify(exec); class SystemTools { getToolDefinitions() { return [ { name: 'get_system_info', description: '獲取系統基本信息(OS、CPU、內存等)', inputSchema: { type: 'object', properties: {}, }, }, { name: 'get_current_time', description: '獲取當前時間和日期', inputSchema: { type: 'object', properties: { timezone: { type: 'string', description: '時區(可選),例如:Asia/Taipei', }, }, }, }, { name: 'execute_command', description: '執行系統命令(限制安全命令)', inputSchema: { type: 'object', properties: { command: { type: 'string', description: '要執行的命令', }, }, required: ['command'], }, }, { name: 'get_network_info', description: '獲取網絡接口信息', inputSchema: { type: 'object', properties: {}, }, }, { name: 'get_process_info', description: '獲取當前進程信息', inputSchema: { type: 'object', properties: {}, }, }, ]; } hasToolName(name) { return this.getToolDefinitions().some(tool => tool.name === name); } async handleToolCall(name, args) { switch (name) { case 'get_system_info': return await this.getSystemInfo(); case 'get_current_time': return await this.getCurrentTime(args.timezone); case 'execute_command': return await this.executeCommand(args.command); case 'get_network_info': return await this.getNetworkInfo(); case 'get_process_info': return await this.getProcessInfo(); default: throw new Error(`Unknown system tool: ${name}`); } } async getSystemInfo() { try { const systemInfo = { 平台: os.platform(), 架構: os.arch(), 操作系統: os.type(), 版本: os.release(), 主機名: os.hostname(), CPU核心數: os.cpus().length, CPU模型: os.cpus()[0]?.model || 'Unknown', 總內存: `${(os.totalmem() / 1024 / 1024 / 1024).toFixed(2)} GB`, 可用內存: `${(os.freemem() / 1024 / 1024 / 1024).toFixed(2)} GB`, 用戶目錄: os.homedir(), 臨時目錄: os.tmpdir(), 系統正常運行時間: `${(os.uptime() / 3600).toFixed(2)} 小時`, }; return { content: [ { type: 'text', text: `系統信息:\n${JSON.stringify(systemInfo, null, 2)}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `獲取系統信息失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } async getCurrentTime(timezone) { try { const now = new Date(); const timeInfo = { 本地時間: now.toLocaleString('zh-TW'), UTC時間: now.toISOString(), 時間戳: now.getTime(), 年: now.getFullYear(), 月: now.getMonth() + 1, 日: now.getDate(), 星期: ['日', '一', '二', '三', '四', '五', '六'][now.getDay()], 小時: now.getHours(), 分鐘: now.getMinutes(), 秒: now.getSeconds(), }; if (timezone) { try { timeInfo['指定時區時間'] = now.toLocaleString('zh-TW', { timeZone: timezone }); } catch (e) { timeInfo['時區錯誤'] = `無效的時區: ${timezone}`; } } return { content: [ { type: 'text', text: `時間信息:\n${JSON.stringify(timeInfo, null, 2)}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `獲取時間信息失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } async executeCommand(command) { try { // 安全檢查:只允許特定的安全命令 const safeCommands = [ 'ls', 'dir', 'pwd', 'whoami', 'date', 'echo', 'node --version', 'npm --version', 'git --version', 'ping -c 1', 'ping -n 1' ]; const isCommandSafe = safeCommands.some(safe => command.toLowerCase().startsWith(safe.toLowerCase())); if (!isCommandSafe) { throw new Error(`不安全的命令: ${command}. 只允許執行: ${safeCommands.join(', ')}`); } const { stdout, stderr } = await execAsync(command, { timeout: 10000, // 10秒超時 maxBuffer: 1024 * 1024 // 1MB 最大輸出 }); let result = ''; if (stdout) result += `輸出:\n${stdout}\n`; if (stderr) result += `錯誤:\n${stderr}\n`; return { content: [ { type: 'text', text: `命令執行結果 (${command}):\n${result || '命令執行完成,無輸出'}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `命令執行失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } async getNetworkInfo() { try { const networkInterfaces = os.networkInterfaces(); const networkInfo = {}; for (const [name, interfaces] of Object.entries(networkInterfaces)) { if (interfaces) { networkInfo[name] = interfaces.map(iface => ({ 地址: iface.address, 網絡掩碼: iface.netmask, 家族: iface.family, 是否內部: iface.internal, MAC地址: iface.mac, })); } } return { content: [ { type: 'text', text: `網絡接口信息:\n${JSON.stringify(networkInfo, null, 2)}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `獲取網絡信息失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } async getProcessInfo() { try { const processInfo = { 進程ID: process.pid, 父進程ID: process.ppid, Node版本: process.version, 平台: process.platform, 架構: process.arch, 執行路徑: process.execPath, 工作目錄: process.cwd(), 內存使用: { RSS: `${(process.memoryUsage().rss / 1024 / 1024).toFixed(2)} MB`, 堆總計: `${(process.memoryUsage().heapTotal / 1024 / 1024).toFixed(2)} MB`, 堆已用: `${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)} MB`, 外部: `${(process.memoryUsage().external / 1024 / 1024).toFixed(2)} MB`, }, CPU使用時間: { 用戶: `${(process.cpuUsage().user / 1000000).toFixed(2)} 秒`, 系統: `${(process.cpuUsage().system / 1000000).toFixed(2)} 秒`, }, 運行時間: `${(process.uptime()).toFixed(2)} 秒`, }; return { content: [ { type: 'text', text: `進程信息:\n${JSON.stringify(processInfo, null, 2)}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `獲取進程信息失敗: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } } export const systemTools = new SystemTools();

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