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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! [`McpServer`]: an owned connection to one MCP server, spawned as a child
//! process and spoken to over stdio.
//!
//! # Child-process lifecycle
//!
//! An MCP server started by [`McpServer::connect`] is a real child process of
//! this one, and every stdio server this process starts is hardened the same
//! way, in [`harden`], before rmcp ever spawns it. Three measures, each
//! covering a different way the parent can go away:
//!
//! - **A process group of its own** (Unix). The child is spawned as the leader
//! of a fresh process group, so every kill on the controlled paths below is a
//! `killpg` that reaches the server *and anything the server spawned*: the
//! `node` behind an `npx` launcher, a language server's own helper, a
//! build tool's worker pool. Killing the one pid rmcp tracks would leave
//! those running.
//! - **Kill on drop.** The spawned handle carries Tokio's kill-on-drop flag, so
//! a connection torn down without a chance to run its own cleanup (a runtime
//! shut down out from under the task rmcp spawns to reap the child) still
//! sends `SIGKILL` rather than leaking the process.
//! - **A parent-death signal** (Linux only). Between `fork` and `exec` the
//! child asks the kernel to send it `SIGKILL` the moment its parent dies, by
//! any means at all, including `SIGKILL` of the parent, which runs no code
//! here. See [`arm_parent_death_signal`].
//!
//! ## What is covered, and what is not
//!
//! Controlled shutdown is covered everywhere: [`close`](McpServer::close) ends
//! the session, closes the child's stdin, waits briefly for it to exit on its
//! own, then kills the group; dropping the handle without closing does the same
//! kill asynchronously. Neither depends on the server noticing its stdin closed,
//! so a server that never reads stdin again is still reaped.
//!
//! Uncontrolled parent death is where the platforms differ, and the honest
//! statement is short:
//!
//! - On **Linux**, the parent-death signal covers it. A parent killed with
//! `SIGKILL`, or ended by any signal it does not handle, takes the server
//! process with it. What can still outlive the parent there is a
//! *grandchild*: the parent-death signal is armed on the server, not on
//! processes the server started, and once the server is gone nothing is left
//! to signal its group.
//! - On **macOS** (and any other Unix without a parent-death signal) there is
//! no equivalent, and no code of ours runs after `SIGKILL`, so nothing can be
//! done from this side. What bounds the damage is the stdio design rather
//! than anything active: the child's stdout is a pipe whose only reader was
//! the parent, so a reparented server dies of `SIGPIPE` on its next write to
//! stdout, and one that reads stdin sees EOF and exits. **What survives is a
//! server that does neither**: blocked writing somewhere else (the reported
//! case was a write to a FIFO with no reader), or looping without touching
//! its stdio. That process keeps running, reparented to init, still holding
//! whatever the run asked it to do. Recovering from that is an operator
//! action: `kill -TERM -<pid>` against the server's process group, which the
//! fresh group above makes a safe thing to type.
//!
//! One consequence of the fresh process group is worth stating rather than
//! discovering: an MCP server is no longer in this process's terminal
//! foreground group, so a Ctrl-C at the terminal reaches the parent and not the
//! server. On Linux the parent-death signal covers that case too. On macOS it
//! puts Ctrl-C in the same bucket as `SIGKILL`: a well-behaved server still
//! exits on stdin EOF, and a server that ignores its stdio survives until an
//! operator kills its group. `salvor-cli`'s `dev_server` module made the same
//! trade for `ng serve`, deliberately and for the same reason.
use ServiceExt;
use ;
use TokioChildProcess;
use ;
use Value;
use Command;
use ProcessGroup;
use ;
use ;
/// A live connection to one MCP server.
///
/// Constructing an `McpServer` runs the MCP initialize handshake and lists the
/// server's tools, turning each into an [`McpTool`]. The handle then owns that
/// connection's lifecycle: the tools it produced hold cheap clones of the
/// client peer, so this handle must stay alive for as long as those tools are
/// dispatched. Closing or dropping it ends the session.
///
/// # Two transports, one handle
///
/// There are two constructors, one per transport, and the handle is identical
/// afterward because both resolve to the same `rmcp` running-service type:
///
/// - [`connect`](Self::connect) spawns the server as a child process and speaks
/// MCP over its stdio: the child's stdin and stdout
/// carry the JSON-RPC stream, its stderr is inherited so a misbehaving
/// server's diagnostics reach the operator.
/// - [`connect_http`](Self::connect_http) reaches a *remote* server by URL over
/// the streamable-HTTP transport: no child process, just HTTP
/// requests to an already-running server, with optional bearer-token auth.
///
/// Once connected, listing, dispatch, effect mapping, overrides, and shutdown
/// are the same for both; the transport is chosen only at construction.
///
/// # Reconnect on resume is safe
///
/// A stdio child process is not durable (it holds no run state) and a remote
/// HTTP endpoint is out of Salvor's control entirely, so neither is resumed. On
/// resume the runtime does not reattach to an old session; it constructs a
/// fresh `McpServer`, which for stdio spawns a new child and for HTTP opens a
/// new connection, then lists tools again. Reconnecting is therefore just
/// reconstruction: same constructor, same arguments, a new handle.
///
/// That is safe, and the reason is the replay contract, not anything this
/// handle does: a completed tool call is read from the event log and never
/// re-executed on resume, regardless of effect class. See
/// [`Effect`](salvor_core::Effect), whose documentation states that rule, and
/// the replay cursor in `salvor-core` that enforces it. So a reconnected server
/// is only ever asked to run calls that had *not* completed. A crash between a
/// recorded write intent and its completion does not silently re-fire against
/// the new session; it surfaces for human reconciliation. Reconnecting is
/// reconstruction, with no risk of a duplicated side effect.
///
/// # Shutdown
///
/// [`close`](Self::close) ends the session and waits for the child to be torn
/// down. Dropping the handle without calling `close` still ends the session:
/// the underlying connection cancels itself on drop and the child is stopped,
/// though asynchronously and without a chance to observe errors, so `close` is
/// the tidy path.
///
/// Either way the child is *killed*, not merely asked to leave: teardown closes
/// its stdin, waits a few seconds for it to exit on its own, then signals its
/// whole process group. A server that never reads its stdin again, or that
/// started subprocesses of its own, is reaped along with the rest. What that
/// does and does not cover when this process dies *without* running any of
/// this, and how the two supported platforms differ there, is the subject of
/// the [module docs](self).
/// Wraps a caller's [`Command`] in the child-lifecycle measures every stdio MCP
/// server gets, and returns the wrapper rmcp's transport spawns.
///
/// Nothing about the caller's command is changed: program, arguments,
/// environment, and working directory are untouched. What is added is how the
/// resulting process is *held*, which is not a decision a caller should have to
/// remember to make. The three measures and what each is for are laid out in
/// the [module docs](self); this function is where they are applied, in one
/// place, so no spawn site can miss one.
///
/// The order matters in one respect only: the parent-death signal is armed on
/// the raw [`Command`] first, because it runs in the child between `fork` and
/// `exec` and must be in place before the process group wrapper's own spawn
/// hook is layered on top.
/// Arms `PR_SET_PDEATHSIG` on the child so the kernel sends it `SIGKILL` the
/// moment this process dies, by any means, including a `SIGKILL` that runs no
/// code here at all. Linux only; there is no macOS equivalent.
///
/// The work happens in a `pre_exec` hook, which runs in the forked child after
/// `fork` and before `exec`. Two details make that placement correct rather
/// than merely convenient. First, the setting survives `exec`, so arming it
/// before the server binary is even loaded still covers the server binary.
/// Second, it closes the obvious race the other way round: the parent could
/// already have died between `fork` and this hook, in which case the
/// parent-death signal would never fire because the death already happened.
/// The hook therefore re-reads its parent's pid and exits immediately if it is
/// no longer the process that spawned it.
///
/// One honest caveat about the kernel's semantics: the parent-death signal
/// fires when the *thread* that spawned the child exits, not necessarily when
/// the whole parent process does. Every spawn here happens on a Tokio worker
/// thread of a runtime that lives as long as the process, so the two coincide
/// in practice; a caller that spawned an MCP server from a short-lived thread
/// of its own would see the child die with that thread.
/// Asks the kernel for `SIGKILL` on the death of the calling thread's parent,
/// the `PR_SET_PDEATHSIG` half of [`arm_parent_death_signal`] on its own.
///
/// Split out because it is the half that can be observed: it changes a setting
/// the same process can read back, so a test can prove the request lands rather
/// than inferring it from a child's fate. The other half of the hook (the
/// `getppid` race check, which ends the process when it loses) cannot be
/// exercised in-process without ending the test, so it is proved from the
/// outside instead, by `tests/mcp_child_lifecycle.rs`.
/// What connecting to or driving an MCP server can fail on.
///
/// The variants track the three stages of bringing a server up plus its
/// teardown. They wrap the rmcp SDK's own error types, keeping rmcp naming out
/// of this crate's other error surfaces: only this module names MCP.