param( [string]$VsixPath = "", [string]$BaseUrl = "https://dualsoft.ai", [string]$InstallDir = "", [string]$ConfigRoot = "", [switch]$SkipVerify ) $ErrorActionPreference = "Stop" Add-Type -AssemblyName System.IO.Compression.FileSystem $Utf8NoBom = New-Object System.Text.UTF8Encoding($false) function Write-Utf8NoBom { param([string]$Path, [string]$Text) $parent = Split-Path -Parent $Path if ($parent -and -not (Test-Path $parent)) { New-Item -ItemType Directory -Force -Path $parent | Out-Null } $normalized = ($Text -replace "`r`n?", "`n") [System.IO.File]::WriteAllText($Path, $normalized, $Utf8NoBom) } function ConvertTo-Hashtable { param($InputObject) if ($null -eq $InputObject) { return @{} } if ($InputObject -is [System.Collections.IDictionary]) { $ht = [ordered]@{} foreach ($k in $InputObject.Keys) { $ht[$k] = ConvertTo-Hashtable $InputObject[$k] } return $ht } if ($InputObject -is [System.Collections.IEnumerable] -and -not ($InputObject -is [string])) { $arr = New-Object System.Collections.ArrayList foreach ($item in $InputObject) { [void]$arr.Add((ConvertTo-Hashtable $item)) } return ,$arr.ToArray() } if ($InputObject -is [PSCustomObject]) { $ht = [ordered]@{} foreach ($prop in $InputObject.PSObject.Properties) { $ht[$prop.Name] = ConvertTo-Hashtable $prop.Value } return $ht } return $InputObject } function Merge-JsonMcpConfig { param( [string]$Path, [string]$Command, [string[]]$CmdArgs, [bool]$IncludeTypeStdio = $false ) $root = [ordered]@{} if (Test-Path $Path) { $raw = [System.IO.File]::ReadAllText($Path, $Utf8NoBom) if (-not [string]::IsNullOrWhiteSpace($raw)) { $parsed = ConvertFrom-Json -InputObject $raw $root = ConvertTo-Hashtable $parsed } } if (-not $root.Contains("mcpServers") -or -not ($root["mcpServers"] -is [System.Collections.IDictionary])) { $root["mcpServers"] = [ordered]@{} } $entry = [ordered]@{ command = $Command args = [string[]]@($CmdArgs) } if ($IncludeTypeStdio) { $entry["type"] = "stdio" } $root["mcpServers"]["dsdx"] = $entry $json = ($root | ConvertTo-Json -Depth 16) + "`n" Write-Utf8NoBom -Path $Path -Text $json Write-Host "[DSDX-MCP] Registered JSON MCP server -> $Path" } function Escape-TomlString { param([string]$Value) return ($Value -replace '\\', '\\\\' -replace '"', '\"') } function Merge-CodexTomlConfig { param( [string]$Path, [string]$Command, [string[]]$CmdArgs ) $existing = "" if (Test-Path $Path) { $existing = ([System.IO.File]::ReadAllText($Path, $Utf8NoBom) -replace "`r`n?", "`n") } $lines = New-Object System.Collections.Generic.List[string] if (-not [string]::IsNullOrWhiteSpace($existing)) { $inTargetSection = $false foreach ($line in ($existing -split "`n")) { if ($line -match '^\s*\[([^\]]+)\]\s*(?:#.*)?$') { $sectionName = $matches[1].Trim() if ($sectionName -eq 'mcp_servers.dsdx' -or $sectionName.StartsWith('mcp_servers.dsdx.')) { $inTargetSection = $true continue } else { $inTargetSection = $false } } if (-not $inTargetSection) { [void]$lines.Add($line) } } } while ($lines.Count -gt 0 -and [string]::IsNullOrWhiteSpace($lines[$lines.Count - 1])) { $lines.RemoveAt($lines.Count - 1) } $argsToml = ($CmdArgs | ForEach-Object { '"' + (Escape-TomlString $_) + '"' }) -join ", " $block = @( '[mcp_servers.dsdx]', ('command = "' + (Escape-TomlString $Command) + '"'), ('args = [' + $argsToml + ']') ) if ($lines.Count -gt 0) { [void]$lines.Add("") } foreach ($b in $block) { [void]$lines.Add($b) } $outText = ($lines -join "`n").TrimEnd("`n") + "`n" Write-Utf8NoBom -Path $Path -Text $outText Write-Host "[DSDX-MCP] Registered TOML MCP server -> $Path" } if ([string]::IsNullOrWhiteSpace($InstallDir)) { $localApp = $env:LOCALAPPDATA if ([string]::IsNullOrWhiteSpace($localApp)) { $localApp = Join-Path $HOME ".local\share" } $InstallDir = Join-Path $localApp "DualSoft\DSDX\mcp" } $downloadedTemp = $null try { if ([string]::IsNullOrWhiteSpace($VsixPath)) { $url = "$($BaseUrl.TrimEnd('/'))/download/dsdx-vscode-latest.vsix" $downloadedTemp = Join-Path ([System.IO.Path]::GetTempPath()) ("dsdx-vscode-" + [Guid]::NewGuid().ToString("N") + ".vsix") Write-Host "[DSDX-MCP] Downloading single-artifact bundle: $url" $wc = New-Object System.Net.WebClient try { $wc.Headers.Add("User-Agent", "DSDX-Local-MCP-Setup/1.0") $wc.DownloadFile($url, $downloadedTemp) } finally { $wc.Dispose() } $VsixPath = $downloadedTemp } elseif (-not (Test-Path $VsixPath)) { throw "VSIX package not found: $VsixPath" } $stagingDir = "$InstallDir.staging.$([Guid]::NewGuid().ToString('N').Substring(0, 8))" New-Item -ItemType Directory -Force -Path $stagingDir | Out-Null $zip = [System.IO.Compression.ZipFile]::OpenRead((Resolve-Path $VsixPath).Path) try { $extractedCount = 0 foreach ($entry in $zip.Entries) { if ([string]::IsNullOrEmpty($entry.Name)) { continue } $norm = $entry.FullName -replace '\\', '/' $relTarget = $null if ($norm.StartsWith("extension/runtime/")) { $relTarget = $norm.Substring("extension/runtime/".Length) } elseif ($norm.StartsWith("extension/fixtures/")) { $relTarget = "fixtures/" + $norm.Substring("extension/fixtures/".Length) } if ($null -eq $relTarget) { continue } $safeRel = ($relTarget -split '/' | Where-Object { $_ -and $_ -ne '.' -and $_ -ne '..' }) -join [System.IO.Path]::DirectorySeparatorChar if ([string]::IsNullOrWhiteSpace($safeRel)) { continue } $destPath = Join-Path $stagingDir $safeRel $destParent = Split-Path -Parent $destPath if ($destParent -and -not (Test-Path $destParent)) { New-Item -ItemType Directory -Force -Path $destParent | Out-Null } [System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $destPath, $true) $extractedCount++ } if ($extractedCount -eq 0) { throw "No extension/runtime payload found inside VSIX: $VsixPath" } } finally { $zip.Dispose() } if (Test-Path $InstallDir) { Remove-Item -Recurse -Force $InstallDir } $installParent = Split-Path -Parent $InstallDir if ($installParent -and -not (Test-Path $installParent)) { New-Item -ItemType Directory -Force -Path $installParent | Out-Null } Move-Item -Path $stagingDir -Destination $InstallDir Write-Host "[DSDX-MCP] Installed local MCP runtime -> $InstallDir" $exePath = Join-Path $InstallDir "Dsdx.Authoring.exe" $dllPath = Join-Path $InstallDir "Dsdx.Authoring.dll" if (Test-Path $exePath) { $mcpCommand = (Resolve-Path $exePath).Path $mcpArgs = @("--stdio") } elseif (Test-Path $dllPath) { $mcpCommand = "dotnet" $mcpArgs = @((Resolve-Path $dllPath).Path, "--stdio") } else { throw "Neither Dsdx.Authoring.exe nor Dsdx.Authoring.dll found in $InstallDir" } if (-not $SkipVerify) { $psi = New-Object System.Diagnostics.ProcessStartInfo $psi.FileName = $mcpCommand $psi.Arguments = ($mcpArgs -join " ") $psi.WorkingDirectory = $InstallDir $psi.UseShellExecute = $false $psi.RedirectStandardInput = $true $psi.RedirectStandardOutput = $true $psi.RedirectStandardError = $true $psi.StandardOutputEncoding = $Utf8NoBom $psi.StandardErrorEncoding = $Utf8NoBom $proc = [System.Diagnostics.Process]::Start($psi) try { $initReq = '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"dsdx-setup","version":"1.0"}}}' $listReq = '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' $proc.StandardInput.WriteLine($initReq) $proc.StandardInput.WriteLine($listReq) $proc.StandardInput.Close() $stdout = $proc.StandardOutput.ReadToEnd() $stderr = $proc.StandardError.ReadToEnd() if (-not $proc.WaitForExit(30000)) { try { $proc.Kill() } catch {} throw "Timed out waiting for Dsdx.Authoring --stdio self-check" } if ($proc.ExitCode -ne 0) { throw "Dsdx.Authoring --stdio failed with exit code $($proc.ExitCode): $stderr" } $toolCount = 0 foreach ($line in ($stdout -split "`r?`n")) { if ([string]::IsNullOrWhiteSpace($line)) { continue } $msg = ConvertFrom-Json -InputObject $line if ($msg.id -eq 2 -and $msg.result -and $msg.result.tools) { $toolCount = @($msg.result.tools).Count } } if ($toolCount -lt 60) { throw "Expected full local MCP toolset (>= 60 tools) from --stdio, got $toolCount" } Write-Host "[DSDX-MCP] Verified local MCP stdio server ($toolCount tools ready)" } finally { $proc.Dispose() } } $useCustomRoot = -not [string]::IsNullOrWhiteSpace($ConfigRoot) $homeBase = if ($useCustomRoot) { $ConfigRoot } elseif (-not [string]::IsNullOrWhiteSpace($env:USERPROFILE)) { $env:USERPROFILE } else { $HOME } $roamingBase = if ($useCustomRoot) { Join-Path $ConfigRoot "AppData\Roaming" } elseif (-not [string]::IsNullOrWhiteSpace($env:APPDATA)) { $env:APPDATA } else { Join-Path $homeBase "AppData\Roaming" } Merge-JsonMcpConfig -Path (Join-Path $homeBase ".gemini\antigravity\mcp_config.json") -Command $mcpCommand -CmdArgs $mcpArgs Merge-JsonMcpConfig -Path (Join-Path $homeBase ".gemini\settings.json") -Command $mcpCommand -CmdArgs $mcpArgs Merge-JsonMcpConfig -Path (Join-Path $roamingBase "Claude\claude_desktop_config.json") -Command $mcpCommand -CmdArgs $mcpArgs Merge-JsonMcpConfig -Path (Join-Path $homeBase ".claude.json") -Command $mcpCommand -CmdArgs $mcpArgs -IncludeTypeStdio $true Merge-CodexTomlConfig -Path (Join-Path $homeBase ".codex\config.toml") -Command $mcpCommand -CmdArgs $mcpArgs Merge-JsonMcpConfig -Path (Join-Path $homeBase ".cursor\mcp.json") -Command $mcpCommand -CmdArgs $mcpArgs Merge-JsonMcpConfig -Path (Join-Path $homeBase ".codeium\windsurf\mcp_config.json") -Command $mcpCommand -CmdArgs $mcpArgs Write-Host "[DSDX-MCP] Setup complete. Restart your AI IDE (Antigravity, Claude, Gemini, OpenAI Codex, Cursor, Windsurf) to use full local DSDX tools." } finally { if ($downloadedTemp -and (Test-Path $downloadedTemp)) { Remove-Item -Force -ErrorAction SilentlyContinue $downloadedTemp } }