vscode-mcp-comprehensive
Version:
Comprehensive MCP server exposing all VSCode features to AI agents with 101 tools including advanced debugging and console access
198 lines • 9.11 kB
JavaScript
import * as vscode from 'vscode';
import { VSCodeMCPServer } from './mcpServer.js';
import { WorkspaceTools } from './tools/workspaceTools.js';
import { EditorTools } from './tools/editorTools.js';
import { LanguageTools } from './tools/languageTools.js';
import { UITools } from './tools/uiTools.js';
import { TerminalTools } from './tools/terminalTools.js';
import { DebugTools } from './tools/debugTools.js';
import { CommandTools, TaskTools, ExtensionTools } from './tools/otherTools.js';
let mcpServer;
let toolInstances = [];
export async function activate(context) {
console.log('VSCode MCP Server extension is now active!');
try {
// Initialize MCP Server
mcpServer = new VSCodeMCPServer();
// Initialize all tool categories
const workspaceTools = new WorkspaceTools(mcpServer);
const editorTools = new EditorTools(mcpServer);
const languageTools = new LanguageTools(mcpServer);
const uiTools = new UITools(mcpServer);
const terminalTools = new TerminalTools(mcpServer);
const debugTools = new DebugTools(mcpServer);
const commandTools = new CommandTools(mcpServer);
const taskTools = new TaskTools(mcpServer);
const extensionTools = new ExtensionTools(mcpServer);
// Store tool instances for cleanup
toolInstances = [
workspaceTools,
editorTools,
languageTools,
uiTools,
terminalTools,
debugTools,
commandTools,
taskTools,
extensionTools,
];
// Register commands
const startCommand = vscode.commands.registerCommand('vscode-mcp-server.start', async () => {
try {
if (mcpServer?.isServerRunning()) {
vscode.window.showInformationMessage('MCP Server is already running');
return;
}
await mcpServer?.start();
vscode.window.showInformationMessage('MCP Server started successfully');
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
vscode.window.showErrorMessage(`Failed to start MCP Server: ${errorMessage}`);
}
});
const stopCommand = vscode.commands.registerCommand('vscode-mcp-server.stop', async () => {
try {
if (!mcpServer?.isServerRunning()) {
vscode.window.showInformationMessage('MCP Server is not running');
return;
}
await mcpServer?.stop();
vscode.window.showInformationMessage('MCP Server stopped');
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
vscode.window.showErrorMessage(`Failed to stop MCP Server: ${errorMessage}`);
}
});
const restartCommand = vscode.commands.registerCommand('vscode-mcp-server.restart', async () => {
try {
if (mcpServer?.isServerRunning()) {
await mcpServer.stop();
}
await mcpServer?.start();
vscode.window.showInformationMessage('MCP Server restarted successfully');
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
vscode.window.showErrorMessage(`Failed to restart MCP Server: ${errorMessage}`);
}
});
const statusCommand = vscode.commands.registerCommand('vscode-mcp-server.status', () => {
const isRunning = mcpServer?.isServerRunning() || false;
const toolCount = mcpServer?.getRegisteredTools().length || 0;
const statusMessage = `MCP Server Status:
- Running: ${isRunning ? 'Yes' : 'No'}
- Registered Tools: ${toolCount}
- Available Categories: Workspace, Editor, Language, UI, Terminal, Debug, Command, Task, Extension`;
vscode.window.showInformationMessage(statusMessage, { modal: true });
});
const listToolsCommand = vscode.commands.registerCommand('vscode-mcp-server.listTools', () => {
const tools = mcpServer?.getRegisteredTools() || [];
if (tools.length === 0) {
vscode.window.showInformationMessage('No tools registered');
return;
}
// Group tools by category
const categories = {};
tools.forEach(tool => {
const category = tool.split('_')[0];
if (!categories[category]) {
categories[category] = [];
}
categories[category].push(tool);
});
let message = 'Registered MCP Tools:\n\n';
Object.entries(categories).forEach(([category, categoryTools]) => {
message += `${category.toUpperCase()}:\n`;
categoryTools.forEach(tool => {
message += ` - ${tool}\n`;
});
message += '\n';
});
// Show in a new document
vscode.workspace.openTextDocument({
content: message,
language: 'plaintext'
}).then(doc => {
vscode.window.showTextDocument(doc);
});
});
// Register all commands
context.subscriptions.push(startCommand, stopCommand, restartCommand, statusCommand, listToolsCommand);
// Auto-start if configured
const config = getConfiguration();
if (config.autoStart) {
try {
await mcpServer.start();
console.log('MCP Server auto-started');
}
catch (error) {
console.error('Failed to auto-start MCP Server:', error);
vscode.window.showWarningMessage('Failed to auto-start MCP Server. Use the command palette to start manually.');
}
}
// Show activation message
const toolCount = mcpServer.getRegisteredTools().length;
vscode.window.showInformationMessage(`VSCode MCP Server activated with ${toolCount} tools! ${config.autoStart ? 'Server started automatically.' : 'Use Command Palette to start server.'}`);
// Add status bar item
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarItem.text = `$(server) MCP: ${mcpServer.isServerRunning() ? 'Running' : 'Stopped'}`;
statusBarItem.tooltip = 'VSCode MCP Server Status';
statusBarItem.command = 'vscode-mcp-server.status';
statusBarItem.show();
context.subscriptions.push(statusBarItem);
// Update status bar when server state changes
const updateStatusBar = () => {
statusBarItem.text = `$(server) MCP: ${mcpServer?.isServerRunning() ? 'Running' : 'Stopped'}`;
};
// Monitor configuration changes
const configWatcher = vscode.workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration('vscode-mcp-server')) {
const newConfig = getConfiguration();
console.log('MCP Server configuration changed:', newConfig);
// Restart server if it's running and auto-start is enabled
if (mcpServer?.isServerRunning() && newConfig.autoStart) {
vscode.commands.executeCommand('vscode-mcp-server.restart');
}
}
});
context.subscriptions.push(configWatcher);
console.log(`VSCode MCP Server extension activated successfully with ${toolCount} tools`);
}
catch (error) {
console.error('Failed to activate VSCode MCP Server extension:', error);
vscode.window.showErrorMessage(`Failed to activate VSCode MCP Server: ${error instanceof Error ? error.message : String(error)}`);
}
}
export async function deactivate() {
console.log('VSCode MCP Server extension is being deactivated...');
try {
// Stop MCP Server
if (mcpServer?.isServerRunning()) {
await mcpServer.stop();
}
// Dispose tool instances
toolInstances.forEach(tool => {
if (tool && typeof tool.dispose === 'function') {
tool.dispose();
}
});
console.log('VSCode MCP Server extension deactivated successfully');
}
catch (error) {
console.error('Error during deactivation:', error);
}
}
function getConfiguration() {
const config = vscode.workspace.getConfiguration('vscode-mcp-server');
return {
port: config.get('port', 3000),
autoStart: config.get('autoStart', true),
enableDebugTools: config.get('enableDebugTools', true),
enableDeveloperTools: config.get('enableDeveloperTools', true),
};
}
// Export for testing
export { mcpServer, toolInstances };
//# sourceMappingURL=extension.js.map