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
//! The one MCP-server configuration authority for the trusty-* ecosystem (#7452).
//!
//! Why: four crates had each grown their own "describe an MCP server" shape —
//! `trusty_mpm::core::mcp_config`'s Claude-Code JSON entries,
//! `trusty_common::claude_config::mcp_server_entry`'s minimal `{command,args}`
//! value, and two different TOML structs inside `trusty-agents`
//! (`mcp::config::McpService` and `tools::registry::config::EndpointConfig`).
//! Four shapes, zero sharing, and two independent parsers of the identical
//! `.mcp.json` on-disk format. The 2026-09-11 gap analysis
//! (`docs/research/trusty-agents-mcp-connectors-gap-analysis-2026-09-11.md` §5)
//! names `trusty-mcp` as the crate those should collapse onto, because it owns
//! none of this today and is already the crate every MCP participant links.
//!
//! What: [`McpServerConfig`] and [`McpTransport`] (the shape), [`file`](mod@file) (one
//! shared TOML file plus its canonical path), [`resolve`](mod@resolve) (a global tier
//! layered with per-consumer overrides), and [`claude_code`] (pure conversion
//! to and from the `mcpServers` map Claude Code reads). This module holds no
//! transport code: spawning a stdio server and speaking JSON-RPC to it stays
//! `trusty_common::stdio_mcp_client::StdioMcpClient`, which keeps `trusty-mcp`
//! the lean rlib ADR-0040 left it and avoids a dependency edge in the wrong
//! direction.
//!
//! The whole module is behind the default-off `config` feature, for the same
//! reason `daemon_bridge_json_rpc` is: a consumer that links this crate only
//! for the wire types must pay for nothing it does not use.
//!
//! Test: `crates/trusty-mcp/tests/mcp_config.rs` — `toml_round_trip_preserves_extensions`,
//! `load_rejects_duplicate_names`, `resolve_set_replaces_wholesale`,
//! `claude_code_stdio_entry_matches_trusty_mpm_golden`.
use BTreeMap;
use PathBuf;
use ;
pub use ;
pub use ;
pub use ;
/// Everything that can go wrong reading, writing, or converting MCP config.
///
/// Why: consumers must distinguish a missing file (expected on a fresh
/// install) from a file that exists and is wrong. The second case must fail
/// loudly: silently treating unparseable TOML as "no servers configured"
/// would start an assistant with its connectors quietly absent, which reads
/// as a product bug rather than a config error. Every variant that can name a
/// path carries one, so the operator is told which file to fix.
/// What: a `thiserror` enum, per the library-crate error convention. I/O and
/// parse failures carry the offending path; the Claude-Code variants carry the
/// entry name instead, because those functions are pure and never see a file.
/// Test: `load_missing_file_is_io_error`, `load_rejects_malformed_toml`,
/// `load_rejects_duplicate_names`, `read_rejects_unknown_transport`,
/// `save_rejects_null_inside_an_extension_array`.
/// How a client reaches one MCP server.
///
/// Why: the three shapes every consumer already models — a local subprocess
/// speaking MCP over its stdio, and the two remote forms. Making the transport
/// an enum rather than a `transport: String` plus six `Option` fields (the
/// shape `trusty_agents::mcp::config::McpService` grew) means an entry cannot
/// be half-stdio and half-remote, and a match over it is total.
/// What: `Stdio` carries the command line and its environment; `Http` and
/// `Sse` each carry a URL and its request headers. The variants match
/// `trusty_mpm::core::mcp_config::McpTransport`'s three one for one, so
/// `tm mcp add` can route through [`write_mcp_servers`] without losing a
/// transport. `Http` and `Sse` stay separate rather than sharing one variant
/// with a flag, because Claude Code's `type` discriminant is what decides
/// which protocol the client speaks. Every collection is a `BTreeMap`, so the
/// serialised form is byte-stable across runs — a config file that round-trips
/// through load/save must not reorder on every write.
///
/// `Debug` is hand-written and prints `<redacted>` for every `env` and
/// `headers` VALUE, keeping the key names — see the impl below. `Serialize` is
/// untouched, because the file must round-trip.
/// Test: `toml_round_trip_preserves_extensions`,
/// `claude_code_remote_entry_matches_trusty_mpm_golden`,
/// `claude_code_sse_entry_matches_trusty_mpm_golden`,
/// `debug_redacts_env_and_header_values`.
/// A string map rendered with its keys intact and its values hidden.
///
/// Why: an `env` or `headers` map holds API keys and bearer tokens, and a
/// derived `Debug` puts them verbatim into every log line, panic message and
/// error report that formats a config. The key names carry the diagnostic
/// value — which variables are set — and the values carry none.
/// What: a `Debug` wrapper rendering `{"API_KEY": <redacted>}`.
/// Test: `debug_redacts_env_and_header_values`.
;
/// Stands in for one hidden value.
///
/// Why: a bare `"<redacted>"` string would print with quotes, reading like a
/// value that really is that text.
/// Test: `debug_redacts_env_and_header_values`.
;
/// One configured MCP server, as every trusty-* consumer should describe it.
///
/// Why: the single shape the four existing ones collapse onto. It is
/// deliberately the intersection plus one escape hatch, not the union: name,
/// transport and an enable flag are what all four agree on, and everything
/// consumer-specific goes in [`extensions`](Self::extensions) rather than
/// growing a field here that only one crate ever sets.
/// What: `extensions` is an uninterpreted `BTreeMap<String, serde_json::Value>`
/// — trusty-agents' `scopes`, `discovery_ttl_secs` and `auth` live there, and
/// this crate never reads a key out of it. That keeps the authority over the
/// shape here while leaving each consumer's own semantics with the consumer.
///
/// There is NO credential-reference field, on purpose: replacing inline
/// `env` secrets with credential references is #4568's work, and adding a
/// field here before that design lands would fix the wrong shape into the
/// one type every consumer is about to depend on. Secrets in `env` today are
/// exactly as exposed as they were in `McpService.env`; #7452 moves the
/// shape, not the secret handling.
///
/// `#[non_exhaustive]` keeps future fields additive, so construct with
/// [`McpServerConfig::new`] and adjust the public fields afterwards. `Debug`
/// is derived, and inherits [`McpTransport`]'s redaction of `env` and
/// `headers` values through the `transport` field.
/// Test: `toml_round_trip_preserves_extensions`, `new_defaults_to_enabled`,
/// `debug_redacts_env_and_header_values`.
/// `serde`'s default for [`McpServerConfig::enabled`].
///
/// Why: an entry that omits `enabled` means "use it", so absence must
/// deserialise to `true` rather than `bool::default()`'s `false`.
/// Test: `omitted_enabled_defaults_to_true`.