forked from modelcontextprotocol/servers
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathstart.ps1
More file actions
120 lines (98 loc) · 3.53 KB
/
Copy pathstart.ps1
File metadata and controls
120 lines (98 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Script khởi động Codex MCP Server trên Windows (foreground, xem log trực tiếp)
param(
[string]$Workspace = $env:WORKSPACE_PATH,
[int]$Port = 3000,
[switch]$Force,
[switch]$OpenUI
)
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $ScriptDir
function Get-PortOwnerPid([int]$TargetPort) {
$lines = netstat -ano | Select-String ":$TargetPort\s" | Select-String "LISTENING"
foreach ($line in $lines) {
$parts = ($line -replace '\s+', ' ').ToString().Trim().Split(' ')
$processId = [int]$parts[-1]
if ($processId -gt 0) { return $processId }
}
return $null
}
function Get-DotEnvValue([string]$Name) {
if (-not (Test-Path ".env")) { return $null }
$line = Get-Content ".env" | Where-Object {
$_ -match "^\s*$Name\s*=" -and -not $_.TrimStart().StartsWith("#")
} | Select-Object -First 1
if (-not $line) { return $null }
$value = ($line -split "=", 2)[1].Trim()
return $value.Trim("'").Trim('"')
}
if (-not (Test-Path ".env")) {
Copy-Item ".env.example" ".env"
Write-Host "Da tao file .env - hay chinh sua WORKSPACE_PATH" -ForegroundColor Yellow
}
if (-not $Workspace) {
$Workspace = Get-DotEnvValue "WORKSPACE_PATH"
}
if (-not $Workspace) {
$Workspace = $ScriptDir
}
$ChatGptAutoApprove = Get-DotEnvValue "CHATGPT_AUTO_APPROVE"
$env:WORKSPACE_PATH = $Workspace
$env:PORT = $Port
if ($ChatGptAutoApprove) {
$env:CHATGPT_AUTO_APPROVE = $ChatGptAutoApprove
}
Write-Host ""
Write-Host "=== Codex MCP Server ===" -ForegroundColor Cyan
Write-Host "Default cwd: $Workspace"
Write-Host "Full machine access: ON"
if ($ChatGptAutoApprove) { Write-Host "ChatGPT auto-approve: $ChatGptAutoApprove" }
$AdminPort = Get-DotEnvValue "ADMIN_PORT"
if (-not $AdminPort) { $AdminPort = "3001" }
$env:ADMIN_PORT = $AdminPort
Write-Host "Port: $Port"
Write-Host "Admin UI: http://127.0.0.1:$AdminPort/ui"
Write-Host ""
if ($env:OPEN_UI -eq "1" -or $OpenUI) {
Start-Process "http://127.0.0.1:$AdminPort/ui"
}
$existingPid = Get-PortOwnerPid -TargetPort $Port
if ($existingPid) {
$proc = Get-Process -Id $existingPid -ErrorAction SilentlyContinue
$procName = if ($proc) { $proc.ProcessName } else { "unknown" }
Write-Host "[CANH BAO] Port $Port dang duoc dung boi PID $existingPid ($procName)" -ForegroundColor Yellow
if ($Force) {
Write-Host "Dang tat process cu (Force)..." -ForegroundColor Yellow
Stop-Process -Id $existingPid -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1
} else {
$answer = Read-Host "Dung server cu va chay lai? (y/n)"
if ($answer -match '^[yY]') {
Stop-Process -Id $existingPid -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1
} else {
Write-Host "Huy. Server cu van dang chay tai http://localhost:$Port" -ForegroundColor Red
exit 1
}
}
}
if (-not (Test-Path "dist/index.js")) {
Write-Host "Building..." -ForegroundColor Yellow
npm run build
if ($LASTEXITCODE -ne 0) {
Write-Host "Build that bai!" -ForegroundColor Red
Read-Host "Nhan Enter de dong"
exit 1
}
}
Write-Host "Khoi dong server (log hien ben duoi)..." -ForegroundColor Green
Write-Host "Nhan Ctrl+C de dung server" -ForegroundColor DarkGray
Write-Host ""
& node dist/index.js
$exitCode = $LASTEXITCODE
Write-Host ""
if ($exitCode -ne 0) {
Write-Host "Server loi (exit code: $exitCode)" -ForegroundColor Red
} else {
Write-Host "Server da dung." -ForegroundColor Yellow
}
exit $exitCode