diff --git a/.changeset/hot-socks-watch.md b/.changeset/hot-socks-watch.md new file mode 100644 index 00000000..f28b1316 --- /dev/null +++ b/.changeset/hot-socks-watch.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +Add remote server programatically diff --git a/src/services/mcp/McpHub.ts b/src/services/mcp/McpHub.ts index f944df23..1cf9aa56 100644 --- a/src/services/mcp/McpHub.ts +++ b/src/services/mcp/McpHub.ts @@ -286,6 +286,7 @@ export class McpHub { // Connect await client.connect(transport) + connection.server.status = "connected" connection.server.error = "" @@ -636,6 +637,50 @@ export class McpHub { } } + public async addRemoteServer(serverName: string, serverUrl: string) { + try { + const settings = await this.readAndValidateMcpSettingsFile() + if (!settings) { + throw new Error("Failed to read MCP settings") + } + + if (settings.mcpServers[serverName]) { + throw new Error(`An MCP server with the name "${serverName}" already exists`) + } + + const urlValidation = z.string().url().safeParse(serverUrl) + if (!urlValidation.success) { + throw new Error(`Invalid server URL: ${serverUrl}. Please provide a valid URL.`) + } + + const serverConfig = { + url: serverUrl, + disabled: false, + autoApprove: [], + } + + // TS expects the server config to be a McpServerConfig, but we know it's valid + // The issue is that the type is not having the transportType field added to it + + // ToDo: Add input types reflecting the non-transformed version + settings.mcpServers[serverName] = serverConfig as unknown as McpServerConfig + const settingsPath = await this.getMcpSettingsFilePath() + await fs.writeFile(settingsPath, JSON.stringify(settings, null, 2)) + + await this.updateServerConnections(settings.mcpServers) + + vscode.window.showInformationMessage(`Added and connected to ${serverName} MCP server`) + } catch (error) { + console.error("Failed to add remote MCP server:", error) + + vscode.window.showErrorMessage( + `Failed to add remote MCP server: ${error instanceof Error ? error.message : String(error)}`, + ) + + throw error + } + } + public async deleteServer(serverName: string) { try { const settingsPath = await this.getMcpSettingsFilePath()