/**
* Example: Testing a VSCode Extension with the MCP Server
*
* This example demonstrates how an AI agent might use the
* vscode-automation-mcp server to test a VSCode extension.
*
* Note: This is a conceptual example showing the tool calls
* that would be made through the MCP protocol. In practice,
* these would be called by an AI agent like Claude Code/Desktop.
*
* @author Sukarth Acharya
* @license MIT
*/
/**
* Example test scenario: Testing an extension that adds a new command
*/
async function testExtensionCommand(mcpClient: any) {
console.log('=== Testing Extension Command ===\n');
// Step 1: Initialize the VSCode driver
console.log('1. Initializing VSCode driver...');
const initResult = await mcpClient.callTool('vscode_initialize', {});
console.log(' Result:', initResult);
// Step 2: Take a screenshot of the initial state
console.log('\n2. Taking initial screenshot...');
const screenshot1 = await mcpClient.callTool('vscode_take_screenshot', {
filename: 'test-initial-state'
});
console.log(' Screenshot saved:', screenshot1);
// Step 3: Open a test file
console.log('\n3. Opening test file...');
const openResult = await mcpClient.callTool('vscode_open_file', {
filePath: 'test/sample.ts',
line: 1
});
console.log(' Result:', openResult);
// Step 4: Execute the extension's command
console.log('\n4. Executing extension command...');
const commandResult = await mcpClient.callTool('vscode_execute_command', {
commandId: 'myExtension.myCommand'
});
console.log(' Result:', commandResult);
// Step 5: Verify the result
console.log('\n5. Verifying command result...');
const editorContent = await mcpClient.callTool('vscode_get_editor_content', {});
console.log(' Editor content:', editorContent);
// Step 6: Check for any errors
console.log('\n6. Checking for errors...');
const diagnostics = await mcpClient.callTool('vscode_get_diagnostics', {
severity: 'error'
});
console.log(' Diagnostics:', diagnostics);
// Step 7: Take a final screenshot
console.log('\n7. Taking final screenshot...');
const screenshot2 = await mcpClient.callTool('vscode_take_screenshot', {
filename: 'test-final-state'
});
console.log(' Screenshot saved:', screenshot2);
console.log('\n=== Test Complete ===');
}
/**
* Example test scenario: UI Navigation Test
*/
async function testUINavigation(mcpClient: any) {
console.log('=== Testing UI Navigation ===\n');
// Open the Explorer sidebar
console.log('1. Opening Explorer sidebar...');
await mcpClient.callTool('vscode_click_element', {
selector: 'Explorer',
selectorType: 'accessibility'
});
// Verify Explorer is visible
console.log('2. Verifying Explorer is visible...');
const explorerVisible = await mcpClient.callTool('vscode_verify_element', {
selector: '.explorer-viewlet',
selectorType: 'css',
shouldBeVisible: true
});
console.log(' Explorer visible:', explorerVisible);
// Open the Search sidebar
console.log('\n3. Opening Search sidebar...');
await mcpClient.callTool('vscode_click_element', {
selector: 'Search',
selectorType: 'accessibility'
});
// Type in the search box
console.log('4. Typing search query...');
await mcpClient.callTool('vscode_type_text', {
text: 'function',
selector: 'input.search-input',
selectorType: 'css',
pressEnter: true
});
// Take a screenshot of search results
console.log('5. Capturing search results...');
await mcpClient.callTool('vscode_take_screenshot', {
filename: 'search-results'
});
console.log('\n=== UI Navigation Test Complete ===');
}
/**
* Example test scenario: Editor Operations
*/
async function testEditorOperations(mcpClient: any) {
console.log('=== Testing Editor Operations ===\n');
// Create a new file
console.log('1. Creating new file...');
await mcpClient.callTool('vscode_execute_command', {
commandId: 'workbench.action.files.newUntitledFile'
});
// Type some code
console.log('2. Typing code...');
await mcpClient.callTool('vscode_type_text', {
text: `// Test file
function greet(name: string): string {
return \`Hello, \${name}!\`;
}
console.log(greet('World'));`,
delay: 10
});
// Get the editor content
console.log('3. Verifying editor content...');
const content = await mcpClient.callTool('vscode_get_editor_content', {});
console.log(' Content:', content);
// Format the document
console.log('4. Formatting document...');
await mcpClient.callTool('vscode_execute_command', {
commandId: 'editor.action.formatDocument'
});
// Save the file
console.log('5. Saving file...');
await mcpClient.callTool('vscode_execute_command', {
commandId: 'workbench.action.files.save'
});
// Check for diagnostics
console.log('6. Checking for issues...');
const diagnostics = await mcpClient.callTool('vscode_get_diagnostics', {});
console.log(' Diagnostics:', diagnostics);
console.log('\n=== Editor Operations Test Complete ===');
}
/**
* Example test scenario: Command Palette Interaction
*/
async function testCommandPalette(mcpClient: any) {
console.log('=== Testing Command Palette ===\n');
// List available git commands
console.log('1. Listing git commands...');
const gitCommands = await mcpClient.callTool('vscode_list_commands', {
filter: 'git'
});
console.log(' Git commands found:', gitCommands);
// List available file commands
console.log('\n2. Listing file commands...');
const fileCommands = await mcpClient.callTool('vscode_list_commands', {
filter: 'file'
});
console.log(' File commands found:', fileCommands);
// Execute a specific command
console.log('\n3. Executing "Go to Line" command...');
await mcpClient.callTool('vscode_execute_command', {
commandId: 'workbench.action.gotoLine'
});
// Type a line number
console.log('4. Typing line number...');
await mcpClient.callTool('vscode_type_text', {
text: '10',
pressEnter: true
});
console.log('\n=== Command Palette Test Complete ===');
}
// Export examples for documentation
export const examples = {
testExtensionCommand,
testUINavigation,
testEditorOperations,
testCommandPalette,
};
// Main entry point for running examples
if (require.main === module) {
console.log('VSCode Automation MCP Server - Examples');
console.log('========================================\n');
console.log('These examples show how an AI agent would use the MCP tools.');
console.log('To run these in practice, use an MCP client like Claude Desktop.\n');
console.log('Available example scenarios:');
console.log(' 1. testExtensionCommand - Test an extension command');
console.log(' 2. testUINavigation - Test UI navigation');
console.log(' 3. testEditorOperations - Test editor operations');
console.log(' 4. testCommandPalette - Test command palette');
}