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
//! Fluent builder for [`McpSpec`].
use std::collections::BTreeMap;
use crate::error::AgentConfigError;
use super::transport::McpTransport;
use super::{McpSpec, SecretPolicy};
/// Builder for [`McpSpec`].
#[derive(Debug, Clone)]
pub struct McpSpecBuilder {
pub(super) name: String,
pub(super) owner_tag: Option<String>,
pub(super) transport: Option<McpTransport>,
pub(super) friendly_name: Option<String>,
pub(super) secret_policy: SecretPolicy,
pub(super) adopt_unowned: bool,
pub(super) builder_error: Option<String>,
}
impl McpSpecBuilder {
/// Set the consumer's owner tag (recorded in the sidecar ownership ledger).
pub fn owner(mut self, tag: impl Into<String>) -> Self {
self.owner_tag = Some(tag.into());
self
}
/// Adopt a config entry that exists on disk but has no recorded owner.
///
/// Use this to recover from a crash between an earlier install's config
/// write and ledger record. With `false` (default) such entries are
/// refused with [`AgentConfigError::NotOwnedByCaller`] (`actual: None`)
/// to avoid silently taking over a hand-installed entry.
pub fn adopt_unowned(mut self, adopt: bool) -> Self {
self.adopt_unowned = adopt;
self
}
/// Configure a stdio launcher.
pub fn stdio<I, S>(mut self, command: impl Into<String>, args: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.transport = Some(McpTransport::Stdio {
command: command.into(),
args: args.into_iter().map(Into::into).collect(),
env: BTreeMap::new(),
});
self
}
/// Configure an HTTP transport.
pub fn http(mut self, url: impl Into<String>) -> Self {
self.transport = Some(McpTransport::Http {
url: url.into(),
headers: BTreeMap::new(),
});
self
}
/// Configure an SSE transport.
pub fn sse(mut self, url: impl Into<String>) -> Self {
self.transport = Some(McpTransport::Sse {
url: url.into(),
headers: BTreeMap::new(),
});
self
}
/// Set or replace one environment variable on a stdio transport.
///
/// Calling this before configuring stdio, or after configuring a non-stdio
/// transport, records a builder error returned by
/// [`try_build`](Self::try_build).
pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
match &mut self.transport {
Some(McpTransport::Stdio { env, .. }) => {
env.insert(key.into(), value.into());
}
Some(McpTransport::Http { .. }) | Some(McpTransport::Sse { .. }) => {
self.record_builder_error("env() can only be used with stdio MCP transports");
}
None => {
self.record_builder_error("env() called before stdio transport was configured");
}
}
self
}
/// Set an env variable to a placeholder that references the host
/// environment, e.g. `GITHUB_TOKEN=${GITHUB_TOKEN}`.
///
/// Placeholders are not treated as inline secrets by the local-scope
/// secret policy because the actual secret value is not written.
pub fn env_from_host(mut self, key: impl Into<String>) -> Self {
let key = key.into();
let placeholder = format!("${{{key}}}");
self = self.env(key, placeholder);
self
}
/// Set an env variable to a caller-provided placeholder.
///
/// This is useful when a harness supports its own placeholder syntax. The
/// value is still validated as a normal MCP env value.
pub fn env_placeholder(
mut self,
key: impl Into<String>,
placeholder: impl Into<String>,
) -> Self {
self = self.env(key, placeholder);
self
}
/// Set or replace one header on an HTTP/SSE transport.
///
/// Calling this before configuring HTTP/SSE, or after configuring stdio,
/// records a builder error returned by [`try_build`](Self::try_build).
pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
match &mut self.transport {
Some(McpTransport::Http { headers, .. }) | Some(McpTransport::Sse { headers, .. }) => {
headers.insert(key.into(), value.into());
}
Some(McpTransport::Stdio { .. }) => {
self.record_builder_error("header() can only be used with HTTP/SSE MCP transports");
}
None => {
self.record_builder_error(
"header() called before HTTP/SSE transport was configured",
);
}
}
self
}
/// Set a human-friendly display name.
pub fn friendly_name(mut self, name: impl Into<String>) -> Self {
self.friendly_name = Some(name.into());
self
}
/// Explicitly allow likely secret env values in project-local MCP configs.
///
/// The default policy refuses this because local project config files are
/// easy to commit, sync, or share accidentally.
pub fn allow_local_inline_secrets(mut self) -> Self {
self.secret_policy = SecretPolicy::AllowInlineSecretsInLocalScope;
self
}
/// Finalize the spec, panicking on missing or invalid fields.
///
/// Convenience wrapper around [`try_build()`](Self::try_build) for tests
/// and examples. Production code should prefer [`try_build()`](Self::try_build)
/// to propagate errors instead of panicking.
///
/// # Panics
///
/// Panics if required fields are missing or validation fails.
pub fn build(self) -> McpSpec {
self.try_build().expect("McpSpec missing required field")
}
/// Finalize the spec, returning [`Result`] on missing or invalid fields.
///
/// This is the recommended way to build a spec in production code.
/// See [crate-level documentation](crate#production-usage) for a full example.
///
/// # Errors
///
/// - [`AgentConfigError::Other`] when an earlier builder method recorded
/// a deferred error (e.g. invalid env var name).
/// - [`AgentConfigError::MissingSpecField`] with `field = "owner"` or
/// `field = "transport"` when those calls were skipped.
/// - [`AgentConfigError::InvalidTag`] when `name`, `owner_tag`, or any
/// transport field (command path, env var, header value) fails
/// identifier or transport validation.
pub fn try_build(self) -> Result<McpSpec, AgentConfigError> {
if let Some(error) = self.builder_error {
return Err(AgentConfigError::Other(anyhow::anyhow!(error)));
}
let owner_tag = self.owner_tag.ok_or(AgentConfigError::MissingSpecField {
id: "<mcp builder>",
field: "owner",
})?;
let transport = self.transport.ok_or(AgentConfigError::MissingSpecField {
id: "<mcp builder>",
field: "transport",
})?;
let spec = McpSpec {
name: self.name,
owner_tag,
transport,
friendly_name: self.friendly_name,
secret_policy: self.secret_policy,
adopt_unowned: self.adopt_unowned,
};
spec.validate()?;
Ok(spec)
}
fn record_builder_error(&mut self, message: &'static str) {
if self.builder_error.is_none() {
self.builder_error = Some(message.to_string());
}
}
}