Files
akmon/scripts/install-supabase-mcp.ps1
2026-01-20 08:04:15 +08:00

104 lines
3.8 KiB
PowerShell

param(
[string]$ConnectionString,
[string]$Tenant = "chat-local",
[switch]$IncludeRestServer,
[string]$ServiceRoleKey,
[string]$RestUrl = "http://192.168.0.150:8000/rest/v1",
[string]$OpenApiSpec
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Set-PersistentEnvVar {
param(
[Parameter(Mandatory)] [string]$Name,
[Parameter(Mandatory)] [string]$Value
)
if (-not $Value) {
return
}
Write-Host "Setting persistent environment variable $Name" -ForegroundColor Cyan
$result = setx $Name $Value
Write-Verbose ($result -join [Environment]::NewLine)
}
$globalStorage = Join-Path $env:APPDATA 'Code\User\globalStorage\GitHub.copilot'
$mcpPath = Join-Path $globalStorage 'mcp.json'
if (-not (Test-Path $globalStorage)) {
Write-Host "Creating Copilot global storage directory at $globalStorage" -ForegroundColor Cyan
New-Item -ItemType Directory -Force -Path $globalStorage | Out-Null
}
$workspaceMcpPath = Join-Path $PSScriptRoot '..\.github\copilot\mcp.json'
if (-not (Test-Path $workspaceMcpPath)) {
throw "Cannot locate workspace MCP template at $workspaceMcpPath"
}
if (-not $ConnectionString) {
$ConnectionString = Read-Host 'Enter SUPABASE_DB_URL (e.g. postgresql://postgres.chat-local:password@192.168.0.150:6543/postgres)'
}
Set-PersistentEnvVar -Name 'SUPABASE_DB_URL' -Value $ConnectionString
Set-PersistentEnvVar -Name 'SUPABASE_TENANT' -Value $Tenant
if ($IncludeRestServer) {
if (-not $ServiceRoleKey) {
$ServiceRoleKey = Read-Host 'Enter SUPABASE_SERVICE_ROLE_KEY'
}
if (-not $OpenApiSpec) {
$OpenApiSpec = Read-Host 'Enter absolute path to supabase-rest-openapi.json'
}
Set-PersistentEnvVar -Name 'SUPABASE_SERVICE_ROLE_KEY' -Value $ServiceRoleKey
Set-PersistentEnvVar -Name 'SUPABASE_REST_URL' -Value $RestUrl
Set-PersistentEnvVar -Name 'SUPABASE_OPENAPI_SPEC' -Value $OpenApiSpec
}
$workspaceConfig = Get-Content -Raw -Path $workspaceMcpPath | ConvertFrom-Json
$workspaceHasMcpServers = $workspaceConfig.PSObject.Properties.Name -contains 'mcpServers'
$workspaceHasLegacyServers = $workspaceConfig.PSObject.Properties.Name -contains 'servers'
if (-not $workspaceHasMcpServers) {
$serversValue = if ($workspaceHasLegacyServers) { $workspaceConfig.servers } else { @{} }
$workspaceConfig | Add-Member -NotePropertyName mcpServers -NotePropertyValue $serversValue
}
$targetConfig = if (Test-Path $mcpPath) {
Get-Content -Raw -Path $mcpPath | ConvertFrom-Json
} else {
[pscustomobject]@{
version = 1
mcpServers = @{}
}
}
if (-not $targetConfig.version) {
$targetConfig | Add-Member -NotePropertyName version -NotePropertyValue 1
}
$targetHasMcpServers = $targetConfig.PSObject.Properties.Name -contains 'mcpServers'
$targetHasLegacyServers = $targetConfig.PSObject.Properties.Name -contains 'servers'
if (-not $targetHasMcpServers -and $targetHasLegacyServers) {
$targetConfig | Add-Member -NotePropertyName mcpServers -NotePropertyValue $targetConfig.servers
$targetConfig.PSObject.Properties.Remove('servers') | Out-Null
}
if (-not ($targetConfig.PSObject.Properties.Name -contains 'mcpServers')) {
$targetConfig | Add-Member -NotePropertyName mcpServers -NotePropertyValue @{}
}
$targetConfig.mcpServers.'supabase-local' = $workspaceConfig.mcpServers.'supabase-local'
if ($IncludeRestServer) {
$targetConfig.mcpServers.'supabase-rest' = $workspaceConfig.mcpServers.'supabase-rest'
} else {
$targetConfig.mcpServers.PSObject.Properties.Remove('supabase-rest') | Out-Null
}
$targetJson = $targetConfig | ConvertTo-Json -Depth 10
Set-Content -Path $mcpPath -Value $targetJson -Encoding UTF8
Write-Host "Updated global Copilot MCP configuration at $mcpPath" -ForegroundColor Green
Write-Host "Restart VS Code (Developer: Reload Window) to load the new Supabase servers." -ForegroundColor Yellow