You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# If a matching process is found listening on a port:
72
+
# - DO NOT start a new server
73
+
# - Use the existing port number
74
+
# - Proceed directly to step 3 to call `request_preview` with that port
75
+
```
76
+
77
+
If an existing server process is detected:
78
+
- Extract the port number from the process output
79
+
- Skip server startup and proceed to step 3 to request preview URL
80
+
- Notify the user that an existing server was detected
81
+
82
+
2.**Detect the project type and start the appropriate server in the background** (only if no existing server found):
58
83
59
84
> **Note**: The example commands below run in the foreground. When executing, the model must run these commands in the background (e.g., using the `&` suffix) to avoid blocking the session.
85
+
>
86
+
> **IMPORTANT**: When starting a server in the background, **CAPTURE AND SAVE THE PID** immediately after launch. This PID must be stored for later process management.
60
87
61
88
```bash
62
89
# Step 1: Node.js project
@@ -127,24 +154,103 @@ else
127
154
fi
128
155
```
129
156
130
-
2. Capture the port number from the server output (or use default 8000).
157
+
3.**Capture the PID and port number** after starting the server:
158
+
159
+
When starting a server in the background, immediately capture its PID:
160
+
161
+
```bash
162
+
# Example for Node.js
163
+
npm run dev &
164
+
SERVER_PID=$!
165
+
echo"Server started with PID: $SERVER_PID"
166
+
167
+
# Example for Python
168
+
python manage.py runserver 8000 &
169
+
SERVER_PID=$!
170
+
echo"Server started with PID: $SERVER_PID"
171
+
172
+
# Example for PHP
173
+
php -S localhost:8000 &
174
+
SERVER_PID=$!
175
+
echo"Server started with PID: $SERVER_PID"
176
+
```
177
+
178
+
**Store the PID** in a temporary file or environment variable for later use:
179
+
```bash
180
+
echo$SERVER_PID> /tmp/deploy_website_server.pid
181
+
```
182
+
183
+
Also capture the port number from the server output (or use default 8000).
131
184
132
-
3. Call MCP Tool `request_preview` with the listening port number to get a preview URL.
185
+
4. Call MCP Tool `request_preview` with the listening port number to get a preview URL.
133
186
134
-
4. Present the preview URL to the user:
187
+
5. Present the preview URL to the user:
135
188
- Output a clickable hyperlink pointing to the preview address returned by the tool
189
+
- Inform the user of the server PID for reference
136
190
137
-
5. Handle IP whitelist issues:
191
+
6. Handle IP whitelist issues:
138
192
- If the user reports that access is denied due to IP not being whitelisted, ask the user for their IP address(es) which reported by the deny page
139
193
- Call MCP Tool `request_preview` again with the `additional_ip_whitelist` field to add the user's IP(s) to the whitelist
140
194
- The `additional_ip_whitelist` field accepts multiple IPv4 addresses separated by commas (e.g., `"192.168.1.100,10.0.0.50"`)
141
195
196
+
## Process Management
197
+
198
+
### Recording PIDs
199
+
200
+
When the model starts a server process, **ALWAYS** record the PID:
201
+
- Store in a temporary file: `/tmp/deploy_website_server.pid`
202
+
- Include PID in user notifications
203
+
- Keep track of which workspace the PID belongs to
204
+
205
+
### Stopping Servers
206
+
207
+
When the user requests to stop a server, **ALWAYS** use PID-based termination:
208
+
209
+
```bash
210
+
# Read the PID from storage
211
+
if [ -f"/tmp/deploy_website_server.pid" ];then
212
+
SERVER_PID=$(cat /tmp/deploy_website_server.pid)
213
+
214
+
# Verify the process exists
215
+
if ps -p $SERVER_PID> /dev/null 2>&1;then
216
+
# Graceful termination first
217
+
kill -TERM $SERVER_PID
218
+
219
+
# Wait briefly for graceful shutdown
220
+
sleep 2
221
+
222
+
# Force kill if still running
223
+
if ps -p $SERVER_PID> /dev/null 2>&1;then
224
+
kill -9 $SERVER_PID
225
+
fi
226
+
227
+
echo"Server (PID: $SERVER_PID) has been stopped"
228
+
rm /tmp/deploy_website_server.pid
229
+
else
230
+
echo"Server process (PID: $SERVER_PID) is not running"
231
+
rm /tmp/deploy_website_server.pid
232
+
fi
233
+
else
234
+
echo"No server PID found. Server may have been started manually."
235
+
fi
236
+
```
237
+
238
+
**CRITICAL**: Never use `pkill` or `killall` with process names (e.g., `pkill node`, `killall python`) as this may terminate:
239
+
- User's own manually-started processes
240
+
- Other unrelated development servers
241
+
- System processes with similar names
242
+
243
+
Always prefer PID-based termination for safety.
244
+
142
245
## Notes
143
246
144
247
- The server **MUST** run in the background or as a subagent task in order **NOT TO** block the session.
248
+
- Always check for existing server processes before starting a new one to avoid port conflicts.
145
249
- Default port for static server is 8000.
146
250
- For Node.js projects, the port depends on the project configuration.
147
251
- The port number can be dynamically changed when conflicted with another service.
252
+
- Record and track PIDs for all model-started server processes.
253
+
- Use PID-based termination to avoid accidentally killing user processes.
148
254
- Make sure dependencies are installed before running:
0 commit comments