Skip to main content
Glama

Windows Automation MCP Server

process.js4.71 kB
/** * 进程管理工具 */ import { exec, spawn } from 'child_process'; import { promisify } from 'util'; const execAsync = promisify(exec); export class ProcessTools { getToolDefinitions() { return [ { name: 'launch_application', description: '启动应用程序', inputSchema: { type: 'object', properties: { path: { type: 'string', description: '应用程序路径或命令' }, args: { type: 'array', items: { type: 'string' }, description: '命令行参数(可选)' }, wait: { type: 'boolean', description: '是否等待程序结束(可选)' }, }, required: ['path'], }, }, { name: 'kill_process', description: '结束进程', inputSchema: { type: 'object', properties: { name: { type: 'string', description: '进程名称(如 notepad.exe)' }, force: { type: 'boolean', description: '是否强制结束(可选)' }, }, required: ['name'], }, }, { name: 'list_processes', description: '列出正在运行的进程', inputSchema: { type: 'object', properties: { filter: { type: 'string', description: '过滤进程名(可选)' }, }, }, }, { name: 'get_process_info', description: '获取进程详细信息', inputSchema: { type: 'object', properties: { name: { type: 'string', description: '进程名称' }, }, required: ['name'], }, }, ]; } canHandle(toolName) { const tools = ['launch_application', 'kill_process', 'list_processes', 'get_process_info']; return tools.includes(toolName); } async executeTool(name, args) { switch (name) { case 'launch_application': return await this.launchApplication(args.path, args.args, args.wait); case 'kill_process': return await this.killProcess(args.name, args.force); case 'list_processes': return await this.listProcesses(args.filter); case 'get_process_info': return await this.getProcessInfo(args.name); default: throw new Error(`未知工具: ${name}`); } } async launchApplication(appPath, args = [], wait = false) { try { const command = args.length > 0 ? `"${appPath}" ${args.join(' ')}` : `"${appPath}"`; if (wait) { const { stdout, stderr } = await execAsync(command); return { success: true, path: appPath, output: stdout, error: stderr }; } else { spawn(command, { shell: true, detached: true, stdio: 'ignore' }).unref(); return { success: true, path: appPath, message: '应用程序已启动' }; } } catch (error) { return { success: false, error: error.message }; } } async killProcess(processName, force = false) { try { const flag = force ? '/F' : ''; await execAsync(`taskkill ${flag} /IM "${processName}"`); return { success: true, process: processName, message: '进程已结束' }; } catch (error) { return { success: false, error: error.message }; } } async listProcesses(filter = '') { try { const { stdout } = await execAsync('tasklist /FO CSV /NH'); const lines = stdout.trim().split('\n'); const processes = lines.map(line => { const parts = line.split(',').map(p => p.replace(/"/g, '')); return { name: parts[0], pid: parts[1], memory: parts[4], }; }); const filtered = filter ? processes.filter(p => p.name.toLowerCase().includes(filter.toLowerCase())) : processes; return { success: true, processes: filtered, count: filtered.length }; } catch (error) { return { success: false, error: error.message }; } } async getProcessInfo(processName) { try { const { stdout } = await execAsync(`tasklist /FI "IMAGENAME eq ${processName}" /FO CSV /V`); const lines = stdout.trim().split('\n'); if (lines.length < 2) { return { success: false, message: '进程未找到' }; } const info = lines[1].split(',').map(p => p.replace(/"/g, '')); return { success: true, process: { name: info[0], pid: info[1], session: info[2], memory: info[4], status: info[5], username: info[6], windowTitle: info[8], }, }; } catch (error) { return { success: false, error: error.message }; } } }

Implementation Reference

Latest Blog Posts

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/eva-wanxin-git/windows-automation-mcp'

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