From 435ca7b16dfd451647c2034e3777066d54fe8a18 Mon Sep 17 00:00:00 2001 From: Laura Promberger Date: Mon, 2 Feb 2026 13:47:40 +0000 Subject: [PATCH] fix vscode plugin to not fail if another vscode host is already running the mcp server --- vscode-plugins/stella-ide-mcp/extension.js | 55 +++++++++++++++++++--- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/vscode-plugins/stella-ide-mcp/extension.js b/vscode-plugins/stella-ide-mcp/extension.js index 747f603..f398337 100644 --- a/vscode-plugins/stella-ide-mcp/extension.js +++ b/vscode-plugins/stella-ide-mcp/extension.js @@ -249,6 +249,29 @@ async function handleCallTool(params) { } } +// Check if server is already running on port +function checkServerRunning(port) { + return new Promise((resolve) => { + const req = http.get(`http://127.0.0.1:${port}/health`, (res) => { + let data = ''; + res.on('data', chunk => data += chunk); + res.on('end', () => { + try { + const json = JSON.parse(data); + resolve(json.status === 'ok' && json.tools === TOOLS.length); + } catch { + resolve(false); + } + }); + }); + req.on('error', () => resolve(false)); + req.setTimeout(1000, () => { + req.destroy(); + resolve(false); + }); + }); +} + // HTTP Server function startServer(port) { server = http.createServer(async (req, res) => { @@ -357,9 +380,21 @@ function startServer(port) { vscode.window.showInformationMessage(`Stella IDE MCP server running on port ${port}`); }); - server.on('error', (e) => { - log(`Server error: ${e.message}`); - vscode.window.showErrorMessage(`Stella IDE MCP server error: ${e.message}`); + server.on('error', async (e) => { + if (e.code === 'EADDRINUSE') { + log(`Port ${port} in use, checking if it's another instance...`); + const isOurServer = await checkServerRunning(port); + if (isOurServer) { + log(`Server already running in another extension host on port ${port} - skipping`); + server = null; // Clear server reference since we're not running it + } else { + log(`Port ${port} occupied by another process`); + vscode.window.showErrorMessage(`Stella IDE MCP: Port ${port} is in use by another process`); + } + } else { + log(`Server error: ${e.message}`); + vscode.window.showErrorMessage(`Stella IDE MCP server error: ${e.message}`); + } }); } @@ -382,13 +417,21 @@ function activate(context) { startServer(port); // Status command - const statusCmd = vscode.commands.registerCommand('stella-ide-mcp.status', () => { + const statusCmd = vscode.commands.registerCommand('stella-ide-mcp.status', async () => { if (server && server.listening) { vscode.window.showInformationMessage( - `Stella IDE MCP server running on port ${port}. Tools: ${TOOLS.map(t => t.name).join(', ')}` + `Stella IDE MCP server running on port ${port} (this extension host). Tools: ${TOOLS.map(t => t.name).join(', ')}` ); } else { - vscode.window.showWarningMessage('Stella IDE MCP server is not running'); + // Check if server is running in another extension host + const isRunning = await checkServerRunning(port); + if (isRunning) { + vscode.window.showInformationMessage( + `Stella IDE MCP server running on port ${port} (another extension host). Tools: ${TOOLS.map(t => t.name).join(', ')}` + ); + } else { + vscode.window.showWarningMessage('Stella IDE MCP server is not running'); + } } });