execute_command
Run commands on a connected VPS via SSH to automate server setup, manage services, configure domains, or integrate CI/CD pipelines.
Instructions
Execute a command on the connected VPS
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | Command to execute |
Implementation Reference
- src/services/mcp-server.ts:290-308 (handler)Handler function for the 'execute_command' MCP tool. Validates input using Zod, checks SSH connection, executes the command via SSHService, and returns formatted stdout/stderr/exit code.private async handleExecuteCommand( args: unknown ): Promise<{ content: Array<{ type: 'text'; text: string }> }> { if (!this.sshService) { throw new Error('SSH connection not established. Please connect first.'); } const { command } = z.object({ command: z.string() }).parse(args); const result = await this.sshService.executeCommand(command); return { content: [ { type: 'text', text: `Command: ${command}\nExit Code: ${result.exitCode}\nOutput:\n${result.stdout}\n${result.stderr ? `Error:\n${result.stderr}` : ''}`, }, ], }; }
- src/services/mcp-server.ts:134-144 (schema)JSON schema definition for the 'execute_command' tool input, requiring a 'command' string, provided in ListTools response.{ name: 'execute_command', description: 'Execute a command on the connected VPS', inputSchema: { type: 'object', properties: { command: { type: 'string', description: 'Command to execute' }, }, required: ['command'], }, },
- src/services/mcp-server.ts:163-164 (registration)Registration/dispatch in the CallToolRequestSchema handler switch statement, routing 'execute_command' calls to the handler.case 'execute_command': return await this.handleExecuteCommand(args);
- src/services/ssh-service.ts:57-98 (helper)Core helper method in SSHService that executes the SSH command using node-ssh library and returns CommandResult with stdout, stderr, exitCode.async executeCommand(command: string): Promise<CommandResult> { if (!this.isConnected) { throw new Error('SSH connection not established'); } try { logger.debug('Executing command', { command }); const result = await this.ssh.execCommand(command); const commandResult: CommandResult = { success: result.code === 0, stdout: result.stdout, stderr: result.stderr, exitCode: result.code || 0, }; if (commandResult.success) { logger.debug('Command executed successfully', { command, exitCode: commandResult.exitCode, }); } else { logger.warn('Command execution failed', { command, exitCode: commandResult.exitCode, stderr: commandResult.stderr, }); } return commandResult; } catch (error) { logger.error('Error executing command', { command, error: error instanceof Error ? error.message : 'Unknown error', }); return { success: false, stdout: '', stderr: error instanceof Error ? error.message : 'Unknown error', exitCode: -1, }; }