/**
* VSCode Automation MCP Server - Command Tools
*
* Tools for executing VSCode commands and listing available commands.
*
* @author Sukarth Acharya
* @license MIT
*/
import { z } from 'zod';
import { getVSCodeDriver } from '../vscode-driver.js';
import type { CommandInfo } from '../types.js';
/**
* Input schema for vscode_execute_command tool
*/
export const executeCommandInputSchema = {
commandId: z.string().describe('The VSCode command ID to execute (e.g., "workbench.action.files.save")'),
args: z.array(z.unknown()).optional().describe('Optional arguments to pass to the command'),
};
/**
* Input schema for vscode_list_commands tool
*/
export const listCommandsInputSchema = {
filter: z.string().optional().describe('Optional filter string to search for specific commands'),
};
/**
* Execute a VSCode command
*
* This tool allows you to execute any VSCode command programmatically.
* Commands can be found in the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
*
* @example
* // Save the current file
* await executeCommand({ commandId: 'workbench.action.files.save' });
*
* @example
* // Open settings
* await executeCommand({ commandId: 'workbench.action.openSettings' });
*/
export async function executeCommand(input: {
commandId: string;
args?: unknown[];
}): Promise<{ content: Array<{ type: 'text'; text: string }> }> {
const driver = getVSCodeDriver();
const result = await driver.executeCommand(input.commandId, ...(input.args || []));
if (result.success) {
return {
content: [{
type: 'text',
text: JSON.stringify({
success: true,
message: result.message,
commandId: input.commandId,
}, null, 2),
}],
};
}
return {
content: [{
type: 'text',
text: JSON.stringify({
success: false,
error: result.error,
commandId: input.commandId,
}, null, 2),
}],
};
}
/**
* List available VSCode commands
*
* This tool lists commands available in VSCode. You can optionally
* filter the results by providing a search string.
*
* @example
* // List all commands
* await listCommands({});
*
* @example
* // Search for git-related commands
* await listCommands({ filter: 'git' });
*/
export async function listCommands(input: {
filter?: string;
}): Promise<{ content: Array<{ type: 'text'; text: string }> }> {
const driver = getVSCodeDriver();
const result = await driver.listCommands(input.filter);
if (result.success && result.data) {
const commands: CommandInfo[] = result.data;
return {
content: [{
type: 'text',
text: JSON.stringify({
success: true,
count: commands.length,
filter: input.filter || null,
commands: commands.map(cmd => ({
id: cmd.id,
title: cmd.title,
category: cmd.category || null,
})),
}, null, 2),
}],
};
}
return {
content: [{
type: 'text',
text: JSON.stringify({
success: false,
error: result.error,
}, null, 2),
}],
};
}
/**
* Common VSCode command IDs for reference
*/
export const COMMON_COMMANDS = {
// File operations
SAVE: 'workbench.action.files.save',
SAVE_ALL: 'workbench.action.files.saveAll',
SAVE_AS: 'workbench.action.files.saveAs',
NEW_FILE: 'workbench.action.files.newUntitledFile',
OPEN_FILE: 'workbench.action.files.openFile',
CLOSE_EDITOR: 'workbench.action.closeActiveEditor',
CLOSE_ALL_EDITORS: 'workbench.action.closeAllEditors',
// Navigation
GO_TO_FILE: 'workbench.action.quickOpen',
GO_TO_SYMBOL: 'workbench.action.gotoSymbol',
GO_TO_LINE: 'workbench.action.gotoLine',
GO_TO_DEFINITION: 'editor.action.revealDefinition',
// Search
FIND: 'actions.find',
REPLACE: 'editor.action.startFindReplaceAction',
FIND_IN_FILES: 'workbench.action.findInFiles',
// View
TOGGLE_SIDEBAR: 'workbench.action.toggleSidebarVisibility',
TOGGLE_PANEL: 'workbench.action.togglePanel',
TOGGLE_TERMINAL: 'workbench.action.terminal.toggleTerminal',
TOGGLE_PROBLEMS: 'workbench.actions.view.problems',
TOGGLE_OUTPUT: 'workbench.action.output.toggleOutput',
// Editor
FORMAT_DOCUMENT: 'editor.action.formatDocument',
COMMENT_LINE: 'editor.action.commentLine',
FOLD_ALL: 'editor.foldAll',
UNFOLD_ALL: 'editor.unfoldAll',
// Debug
START_DEBUG: 'workbench.action.debug.start',
STOP_DEBUG: 'workbench.action.debug.stop',
STEP_OVER: 'workbench.action.debug.stepOver',
STEP_INTO: 'workbench.action.debug.stepInto',
// Git
GIT_COMMIT: 'git.commit',
GIT_PUSH: 'git.push',
GIT_PULL: 'git.pull',
GIT_SYNC: 'git.sync',
// Extensions
SHOW_EXTENSIONS: 'workbench.view.extensions',
INSTALL_EXTENSION: 'workbench.extensions.installExtension',
// Settings
OPEN_SETTINGS: 'workbench.action.openSettings',
OPEN_KEYBOARD_SHORTCUTS: 'workbench.action.openGlobalKeybindings',
// Command Palette
COMMAND_PALETTE: 'workbench.action.showCommands',
} as const;