Skip to main content
Glama

Windows Automation MCP Server

server.js4.14 kB
#!/usr/bin/env node /** * Windows 完全自动化 MCP Server * 提供完整的 Windows 系统控制能力 */ import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ListToolsRequestSchema, ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js'; import { FileSystemTools } from './tools/filesystem.js'; import { ProcessTools } from './tools/process.js'; import { MouseKeyboardTools } from './tools/mouse-keyboard.js'; import { WindowTools } from './tools/window.js'; import { ScreenTools } from './tools/screen.js'; import { ClipboardTools } from './tools/clipboard.js'; import { PowerShellTools } from './tools/powershell.js'; import { BrowserTools } from './tools/browser.js'; /** * Windows 自动化 MCP 服务器 */ class WindowsAutomationServer { constructor() { this.server = new Server( { name: 'windows-automation', version: '1.0.0', }, { capabilities: { tools: {}, }, } ); // 初始化所有工具模块 this.tools = { filesystem: new FileSystemTools(), process: new ProcessTools(), mouseKeyboard: new MouseKeyboardTools(), window: new WindowTools(), screen: new ScreenTools(), clipboard: new ClipboardTools(), powershell: new PowerShellTools(), browser: new BrowserTools(), }; this.setupHandlers(); this.setupErrorHandling(); } /** * 获取所有工具定义 */ getAllToolDefinitions() { const allTools = []; for (const [category, toolModule] of Object.entries(this.tools)) { allTools.push(...toolModule.getToolDefinitions()); } return allTools; } /** * 设置请求处理器 */ setupHandlers() { // 工具列表 this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: this.getAllToolDefinitions() })); // 工具调用 this.server.setRequestHandler(CallToolRequestSchema, async (request) => this.handleToolCall(request) ); } /** * 处理工具调用 */ async handleToolCall(request) { const { name, arguments: args } = request.params; try { console.error(`[MCP] 调用工具: ${name}`); console.error(`[MCP] 参数:`, JSON.stringify(args, null, 2)); let result = null; // 路由到对应的工具模块 for (const [category, toolModule] of Object.entries(this.tools)) { if (toolModule.canHandle(name)) { result = await toolModule.executeTool(name, args); break; } } if (result === null) { throw new Error(`未知工具: ${name}`); } return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { console.error(`[MCP] 工具调用失败: ${name}`, error); if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `工具执行失败: ${error.message}` ); } } /** * 设置错误处理 */ setupErrorHandling() { this.server.onerror = (error) => { console.error('[MCP] 服务器错误:', error); }; process.on('SIGINT', async () => { console.error('\n[MCP] 正在关闭服务器...'); await this.server.close(); process.exit(0); }); } /** * 启动服务器 */ async start() { const transport = new StdioServerTransport(); await this.server.connect(transport); console.error('[MCP] ✅ Windows 自动化 MCP Server 已启动'); console.error('[MCP] 🖥️ 支持完整的 Windows 系统控制'); console.error('[MCP] 📁 文件系统 | 🖱️ 键鼠控制 | 🪟 窗口管理 | 📸 截图 | 🌐 浏览器自动化'); } } // 启动服务器 const server = new WindowsAutomationServer(); server.start().catch((error) => { console.error('[MCP] ❌ 启动失败:', error); process.exit(1); });

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