diff --git a/sdks/vscode/package.json b/sdks/vscode/package.json index dd51e0ef16f..f4bcbb02691 100644 --- a/sdks/vscode/package.json +++ b/sdks/vscode/package.json @@ -23,6 +23,16 @@ "activationEvents": [], "main": "./dist/extension.js", "contributes": { + "configuration": { + "title": "OpenCode", + "properties": { + "opencode.autoDetectMcpServer": { + "type": "boolean", + "default": true, + "description": "Automatically detect and enable vscode-mcp-server if installed" + } + } + }, "commands": [ { "command": "opencode.openTerminal", diff --git a/sdks/vscode/src/extension.ts b/sdks/vscode/src/extension.ts index 105ab0293a8..6c2f856c35c 100644 --- a/sdks/vscode/src/extension.ts +++ b/sdks/vscode/src/extension.ts @@ -5,6 +5,30 @@ import * as vscode from "vscode" const TERMINAL_NAME = "opencode" +/** + * Checks if vscode-mcp-server extension is installed and gets its configured port + * @returns Configuration object with port, or null if not installed/configured + */ +function getVscodeMcpServerConfig(): { installed: boolean; port: number } | null { + const ext = vscode.extensions.getExtension("JuehangQin.vscode-mcp-server") + if (!ext) { + return null + } + + // Read the port from vscode-mcp-server's actual configuration + const config = vscode.workspace.getConfiguration("vscode-mcp-server") + const port = config.get("port") + + if (port === undefined) { + return null + } + + return { + installed: true, + port: port, + } +} + export function activate(context: vscode.ExtensionContext) { let openNewTerminalDisposable = vscode.commands.registerCommand("opencode.openNewTerminal", async () => { await openTerminal() @@ -64,11 +88,6 @@ export function activate(context: vscode.ExtensionContext) { terminal.show() terminal.sendText(`opencode --port ${port}`) - const fileRef = getActiveFile() - if (!fileRef) { - return - } - // Wait for the terminal to be ready let tries = 10 let connected = false @@ -83,9 +102,40 @@ export function activate(context: vscode.ExtensionContext) { tries-- } while (tries > 0) - // If connected, append the prompt to the terminal + // If connected, try to register vscode-mcp-server if available if (connected) { - await appendPrompt(port, `In ${fileRef}`) + const opencodeConfig = vscode.workspace.getConfiguration("opencode") + const autoDetect = opencodeConfig.get("autoDetectMcpServer") ?? true + + if (autoDetect) { + const mcpServerConfig = getVscodeMcpServerConfig() + + if (mcpServerConfig?.installed) { + try { + // Register the MCP server with opencode directly + // opencode will handle the connection to the MCP server + await fetch(`http://localhost:${port}/mcp`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + name: "vscode", + config: { + type: "remote", + url: `http://localhost:${mcpServerConfig.port}/mcp`, + }, + }), + }) + } catch (e) { + // MCP server registration failed silently + } + } + } + + // Append the file reference to the prompt if there's an active file + const fileRef = getActiveFile() + if (fileRef) { + await appendPrompt(port, `In ${fileRef}`) + } terminal.show() } }