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
//! Spawning one configured MCP server, in isolation (#5428).
//!
//! Why: a task run may have several connectors configured and exactly one of
//! them broken — a typo'd command, a server that hangs on `initialize`, a
//! binary that exits immediately. None of that may abort the run or the other
//! servers, so every connection attempt is independently bounded and every
//! failure is a value, never an error that propagates.
//! What: [`connect`] spawns the child through the SHARED
//! `trusty_common::stdio_mcp_client::StdioMcpClient` (never a bespoke
//! transport), runs the handshake and `tools/list` under one
//! [`HANDSHAKE_TIMEOUT`], and returns either the live client plus its tools or
//! a one-line reason. A remote transport is refused here with the same shape,
//! because "slice 1 does not speak HTTP" is a status, not a fault.
//! Test: `super::tests::spawn_tests`, `mcp_loader_e2e::spawn_failure_isolates_the_healthy_server`.
use HashMap;
use Arc;
use Duration;
use Mutex;
use timeout;
use ;
use ;
/// Wall-clock bound on one server's whole startup: handshake plus `tools/list`.
///
/// Why: `StdioMcpClient` bounds each REQUEST at 30s, which is far too long to
/// wait at task startup and, with two requests, twice as long again. A server
/// that cannot introduce itself in ten seconds is not one this run should
/// block on.
/// Test: `super::tests::spawn_tests::handshake_timeout_is_ten_seconds`.
pub const HANDSHAKE_TIMEOUT: Duration = from_secs;
/// `clientInfo.name` advertised during the MCP handshake.
const CLIENT_NAME: &str = "trusty-code";
/// A live server: its client handle and the tools it advertised.
///
/// What: the client is already behind the `Arc<Mutex<_>>` every
/// [`super::tool::McpBridgeTool`] for this server shares, because that shared
/// handle is also what keeps the child process alive.
/// Spawn one server and discover its tools, or say why not.
///
/// Why: the isolation boundary. Every failure mode — an unsupported transport,
/// a command that does not exist, a handshake that times out, a server that
/// will not list its tools — comes back as `Err(reason)` for the caller to
/// record as a per-server status, so one bad entry costs exactly itself.
/// What: stdio only. The child leads its own process group (#11650), so
/// dropping the client kills a wrapper command's grandchildren too. `env` is
/// overlaid on the inherited environment through `spawn_grouped_with_env`; its VALUES are never logged, here or in the returned
/// reason, because they are credentials.
/// Test: `super::tests::spawn_tests::a_remote_transport_is_refused_with_a_reason`,
/// `super::tests::spawn_tests::a_missing_binary_is_refused_with_a_reason`,
/// `super::tests::spawn_tests::a_wrapper_grandchild_dies_with_the_client`,
/// `mcp_loader_e2e::spawn_failure_isolates_the_healthy_server`.
pub async
/// [`connect`] with the startup bound supplied.
///
/// Why: a test that proves the per-server bound is real — that a hung server
/// costs one timeout, not one per sibling — cannot afford to wait
/// [`HANDSHAKE_TIMEOUT`] to do it. Injecting the bound keeps the proof to a
/// second or two without weakening what production uses.
/// Test: `mcp_loader_e2e::a_hung_server_does_not_serialise_its_siblings`.
pub async
/// `initialize` then `tools/list`, as one bounded unit.
///
/// Why: both are startup, and a server that handshakes but will not list its
/// tools contributes nothing — so one timeout covers the pair rather than
/// letting a slow server spend the budget twice.
/// Test: covered through [`connect`].
async