Skip to main content
Glama
alxspiker

Windows Command Line MCP Server

execute_command

Execute Windows commands like 'dir' or 'echo' and retrieve their output through a secure MCP server interface.

Instructions

Execute a Windows command and return its output. Only commands in the allowed list can be executed. This tool should be used for running simple commands like 'dir', 'echo', etc.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYesThe command to execute
workingDirNoWorking directory for the command
timeoutNoTimeout in milliseconds

Implementation Reference

  • The core handler logic for the 'execute_command' tool. It performs security checks to block dangerous commands, constructs the shell command using cmd.exe on Windows, sets execution options, calls the executeCommand helper, and returns stdout or an error.
    async ({ command, workingDir, timeout }) => { try { // Security check: Ensure only allowed commands are executed const commandLower = command.toLowerCase(); // Block potentially dangerous commands const dangerousPatterns = [ 'net user', 'net localgroup', 'netsh', 'format', 'rd /s', 'rmdir /s', 'del /f', 'reg delete', 'shutdown', 'taskkill', 'sc delete', 'bcdedit', 'cacls', 'icacls', 'takeown', 'diskpart', 'cipher /w', 'schtasks /create', 'rm -rf', 'sudo', 'chmod', 'chown', 'passwd', 'mkfs', 'dd' ]; // Check for dangerous patterns if (dangerousPatterns.some(pattern => commandLower.includes(pattern.toLowerCase()))) { return { isError: true, content: [ { type: "text", text: "Command contains potentially dangerous operations and cannot be executed.", }, ], }; } const options: any = { timeout }; if (workingDir) { options.cwd = workingDir; } let cmdToExecute; if (isWindows) { cmdToExecute = `cmd.exe /c ${command}`; } else { // For non-Windows, try to execute the command directly cmdToExecute = command; } const stdout = executeCommand(cmdToExecute, options); return { content: [ { type: "text", text: stdout.toString() || 'Command executed successfully (no output)', }, ], }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error executing command: ${error}`, }, ], }; } }
  • Input schema using Zod for the 'execute_command' tool, defining parameters: command (required string), workingDir (optional string), timeout (optional number with default).
    { command: z.string().describe("The command to execute"), workingDir: z.string().optional().describe("Working directory for the command"), timeout: z.number().default(30000).describe("Timeout in milliseconds"), },
  • index.ts:446-514 (registration)
    MCP server registration of the 'execute_command' tool, including the tool name, description, input schema, and inline handler function.
    server.tool( "execute_command", "Execute a Windows command and return its output. Only commands in the allowed list can be executed. This tool should be used for running simple commands like 'dir', 'echo', etc.", { command: z.string().describe("The command to execute"), workingDir: z.string().optional().describe("Working directory for the command"), timeout: z.number().default(30000).describe("Timeout in milliseconds"), }, async ({ command, workingDir, timeout }) => { try { // Security check: Ensure only allowed commands are executed const commandLower = command.toLowerCase(); // Block potentially dangerous commands const dangerousPatterns = [ 'net user', 'net localgroup', 'netsh', 'format', 'rd /s', 'rmdir /s', 'del /f', 'reg delete', 'shutdown', 'taskkill', 'sc delete', 'bcdedit', 'cacls', 'icacls', 'takeown', 'diskpart', 'cipher /w', 'schtasks /create', 'rm -rf', 'sudo', 'chmod', 'chown', 'passwd', 'mkfs', 'dd' ]; // Check for dangerous patterns if (dangerousPatterns.some(pattern => commandLower.includes(pattern.toLowerCase()))) { return { isError: true, content: [ { type: "text", text: "Command contains potentially dangerous operations and cannot be executed.", }, ], }; } const options: any = { timeout }; if (workingDir) { options.cwd = workingDir; } let cmdToExecute; if (isWindows) { cmdToExecute = `cmd.exe /c ${command}`; } else { // For non-Windows, try to execute the command directly cmdToExecute = command; } const stdout = executeCommand(cmdToExecute, options); return { content: [ { type: "text", text: stdout.toString() || 'Command executed successfully (no output)', }, ], }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error executing command: ${error}`, }, ], }; } } );
  • Supporting helper function executeCommand that performs the actual command execution using Node.js execSync, with platform-specific handling (direct exec on Windows, modified fallback on other platforms).
    function executeCommand(command: string, options: any = {}) { if (isWindows) { return execSync(command, options); } else { // Log warning for non-Windows environments console.error(`Warning: Running in a non-Windows environment (${platform()}). Windows commands may not work.`); // For testing purposes on non-Windows platforms try { // For Linux/MacOS, we'll strip cmd.exe and powershell.exe references let modifiedCmd = command; // Replace cmd.exe /c with empty string modifiedCmd = modifiedCmd.replace(/cmd\.exe\s+\/c\s+/i, ''); // Replace powershell.exe -Command with empty string or a compatible command modifiedCmd = modifiedCmd.replace(/powershell\.exe\s+-Command\s+("|')/i, ''); // Remove trailing quotes if we removed powershell -Command if (modifiedCmd !== command) { modifiedCmd = modifiedCmd.replace(/("|')$/, ''); } console.error(`Attempting to execute modified command: ${modifiedCmd}`); return execSync(modifiedCmd, options); } catch (error) { console.error(`Error executing modified command: ${error}`); return Buffer.from(`This tool requires a Windows environment. Current platform: ${platform()}`); } } }

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/alxspiker/Windows-Command-Line-MCP-Server'

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