Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sdks/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
64 changes: 57 additions & 7 deletions sdks/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>("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()
Expand Down Expand Up @@ -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
Expand All @@ -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<boolean>("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()
}
}
Expand Down