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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//! Programmatic proxy builder for library users.
//!
//! Constructs a [`ProxyConfig`] via a fluent API, avoiding the need for
//! TOML files. The resulting config is passed to [`Proxy::from_config()`]
//! as usual.
//!
//! # Example
//!
//! ```rust,no_run
//! use mcp_proxy::builder::ProxyBuilder;
//!
//! # async fn example() -> anyhow::Result<()> {
//! let proxy = ProxyBuilder::new("my-proxy")
//! .version("1.0.0")
//! .listen("0.0.0.0", 9090)
//! .stdio_backend("files", "npx", &["-y", "@modelcontextprotocol/server-filesystem"])
//! .http_backend("api", "http://api:8080")
//! .build()
//! .await?;
//!
//! // Embed in an existing axum app
//! let (router, _session_handle) = proxy.into_router();
//! # Ok(())
//! # }
//! ```
use std::collections::HashMap;
use std::time::Duration;
use anyhow::Result;
use crate::Proxy;
use crate::config::*;
/// Fluent builder for constructing an MCP proxy without TOML config files.
///
/// Call [`build()`](Self::build) to connect backends and produce a
/// ready-to-serve [`Proxy`].
pub struct ProxyBuilder {
config: ProxyConfig,
}
impl ProxyBuilder {
/// Create a new proxy builder with the given name.
///
/// Defaults: version "0.1.0", separator "/", listen 127.0.0.1:8080.
pub fn new(name: impl Into<String>) -> Self {
Self {
config: ProxyConfig {
proxy: ProxySettings {
name: name.into(),
version: "0.1.0".to_string(),
separator: "/".to_string(),
listen: ListenConfig {
host: "127.0.0.1".to_string(),
port: 8080,
},
instructions: None,
shutdown_timeout_seconds: 30,
hot_reload: false,
import_backends: None,
rate_limit: None,
tool_discovery: false,
tool_exposure: crate::config::ToolExposure::default(),
},
backends: Vec::new(),
auth: None,
performance: PerformanceConfig::default(),
security: SecurityConfig::default(),
cache: CacheBackendConfig::default(),
observability: ObservabilityConfig::default(),
composite_tools: Vec::new(),
source_path: None,
},
}
}
/// Set the proxy version (default: "0.1.0").
pub fn version(mut self, version: impl Into<String>) -> Self {
self.config.proxy.version = version.into();
self
}
/// Set the namespace separator (default: "/").
pub fn separator(mut self, separator: impl Into<String>) -> Self {
self.config.proxy.separator = separator.into();
self
}
/// Set the listen address and port (default: 127.0.0.1:8080).
pub fn listen(mut self, host: impl Into<String>, port: u16) -> Self {
self.config.proxy.listen = ListenConfig {
host: host.into(),
port,
};
self
}
/// Set instructions text sent to MCP clients.
pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
self.config.proxy.instructions = Some(instructions.into());
self
}
/// Set the graceful shutdown timeout (default: 30s).
pub fn shutdown_timeout(mut self, timeout: Duration) -> Self {
self.config.proxy.shutdown_timeout_seconds = timeout.as_secs();
self
}
/// Enable hot reload for watching config file changes.
pub fn hot_reload(mut self, enabled: bool) -> Self {
self.config.proxy.hot_reload = enabled;
self
}
/// Set a global rate limit across all backends.
pub fn global_rate_limit(mut self, requests: usize, period: Duration) -> Self {
self.config.proxy.rate_limit = Some(GlobalRateLimitConfig {
requests,
period_seconds: period.as_secs(),
});
self
}
/// Add a stdio backend (subprocess).
pub fn stdio_backend(
mut self,
name: impl Into<String>,
command: impl Into<String>,
args: &[&str],
) -> Self {
self.config.backends.push(BackendConfig {
name: name.into(),
transport: TransportType::Stdio,
command: Some(command.into()),
args: args.iter().map(|s| s.to_string()).collect(),
url: None,
..default_backend()
});
self
}
/// Add a stdio backend with environment variables.
pub fn stdio_backend_with_env(
mut self,
name: impl Into<String>,
command: impl Into<String>,
args: &[&str],
env: HashMap<String, String>,
) -> Self {
self.config.backends.push(BackendConfig {
name: name.into(),
transport: TransportType::Stdio,
command: Some(command.into()),
args: args.iter().map(|s| s.to_string()).collect(),
url: None,
env,
..default_backend()
});
self
}
/// Add an HTTP backend.
pub fn http_backend(mut self, name: impl Into<String>, url: impl Into<String>) -> Self {
self.config.backends.push(BackendConfig {
name: name.into(),
transport: TransportType::Http,
command: None,
url: Some(url.into()),
..default_backend()
});
self
}
/// Add an HTTP backend with a bearer token.
pub fn http_backend_with_token(
mut self,
name: impl Into<String>,
url: impl Into<String>,
token: impl Into<String>,
) -> Self {
self.config.backends.push(BackendConfig {
name: name.into(),
transport: TransportType::Http,
command: None,
url: Some(url.into()),
bearer_token: Some(token.into()),
..default_backend()
});
self
}
/// Configure the last added backend with a per-backend modifier.
///
/// # Panics
///
/// Panics if no backends have been added.
pub fn configure_backend(mut self, f: impl FnOnce(&mut BackendConfig)) -> Self {
let backend = self
.config
.backends
.last_mut()
.expect("configure_backend called with no backends");
f(backend);
self
}
/// Enable bearer token authentication.
/// Enable bearer token authentication.
///
/// All tokens in this list have unrestricted access to all tools.
/// For per-token tool scoping, use [`scoped_bearer_auth`](Self::scoped_bearer_auth).
pub fn bearer_auth(mut self, tokens: Vec<String>) -> Self {
self.config.auth = Some(AuthConfig::Bearer {
tokens,
scoped_tokens: vec![],
});
self
}
/// Enable bearer token authentication with per-token tool scoping.
///
/// Each [`BearerTokenConfig`] can specify
/// `allow_tools` or `deny_tools` to restrict which tools that token can access.
pub fn scoped_bearer_auth(mut self, scoped_tokens: Vec<BearerTokenConfig>) -> Self {
self.config.auth = Some(AuthConfig::Bearer {
tokens: vec![],
scoped_tokens,
});
self
}
/// Enable request coalescing.
pub fn coalesce_requests(mut self, enabled: bool) -> Self {
self.config.performance.coalesce_requests = enabled;
self
}
/// Set the maximum argument size for validation.
pub fn max_argument_size(mut self, max_bytes: usize) -> Self {
self.config.security.max_argument_size = Some(max_bytes);
self
}
/// Enable audit logging.
pub fn audit_logging(mut self, enabled: bool) -> Self {
self.config.observability.audit = enabled;
self
}
/// Enable access logging.
pub fn access_logging(mut self, enabled: bool) -> Self {
self.config.observability.access_log.enabled = enabled;
self
}
/// Set the log level (default: "info").
pub fn log_level(mut self, level: impl Into<String>) -> Self {
self.config.observability.log_level = level.into();
self
}
/// Enable structured JSON logging.
pub fn json_logs(mut self, enabled: bool) -> Self {
self.config.observability.json_logs = enabled;
self
}
/// Enable Prometheus metrics.
pub fn metrics(mut self, enabled: bool) -> Self {
self.config.observability.metrics.enabled = enabled;
self
}
/// Set the timeout for the last added backend.
///
/// # Panics
///
/// Panics if no backends have been added.
///
/// # Example
///
/// ```rust
/// use mcp_proxy::builder::ProxyBuilder;
///
/// let config = ProxyBuilder::new("my-proxy")
/// .http_backend("api", "http://api:8080")
/// .timeout(30)
/// .into_config();
///
/// assert_eq!(config.backends[0].timeout.as_ref().unwrap().seconds, 30);
/// ```
pub fn timeout(mut self, seconds: u64) -> Self {
let backend = self
.config
.backends
.last_mut()
.expect("timeout called with no backends");
backend.timeout = Some(TimeoutConfig { seconds });
self
}
/// Set the rate limit for the last added backend.
///
/// # Panics
///
/// Panics if no backends have been added.
///
/// # Example
///
/// ```rust
/// use mcp_proxy::builder::ProxyBuilder;
///
/// let config = ProxyBuilder::new("my-proxy")
/// .http_backend("api", "http://api:8080")
/// .rate_limit(100, 1)
/// .into_config();
///
/// let rl = config.backends[0].rate_limit.as_ref().unwrap();
/// assert_eq!(rl.requests, 100);
/// assert_eq!(rl.period_seconds, 1);
/// ```
pub fn rate_limit(mut self, requests: usize, period_seconds: u64) -> Self {
let backend = self
.config
.backends
.last_mut()
.expect("rate_limit called with no backends");
backend.rate_limit = Some(RateLimitConfig {
requests,
period_seconds,
});
self
}
/// Set the circuit breaker for the last added backend.
///
/// Uses sensible defaults for other fields: minimum 5 calls,
/// 30-second wait duration, and 3 half-open calls.
///
/// # Panics
///
/// Panics if no backends have been added.
///
/// # Example
///
/// ```rust
/// use mcp_proxy::builder::ProxyBuilder;
///
/// let config = ProxyBuilder::new("my-proxy")
/// .http_backend("api", "http://api:8080")
/// .circuit_breaker(0.5)
/// .into_config();
///
/// let cb = config.backends[0].circuit_breaker.as_ref().unwrap();
/// assert!((cb.failure_rate_threshold - 0.5).abs() < f64::EPSILON);
/// ```
pub fn circuit_breaker(mut self, failure_rate: f64) -> Self {
let backend = self
.config
.backends
.last_mut()
.expect("circuit_breaker called with no backends");
backend.circuit_breaker = Some(CircuitBreakerConfig {
failure_rate_threshold: failure_rate,
minimum_calls: 5,
wait_duration_seconds: 30,
permitted_calls_in_half_open: 3,
});
self
}
/// Set the tool allowlist for the last added backend.
///
/// Only the listed tools will be exposed through the proxy.
///
/// # Panics
///
/// Panics if no backends have been added.
///
/// # Example
///
/// ```rust
/// use mcp_proxy::builder::ProxyBuilder;
///
/// let config = ProxyBuilder::new("my-proxy")
/// .http_backend("api", "http://api:8080")
/// .expose_tools(&["read_file", "list_dir"])
/// .into_config();
///
/// assert_eq!(config.backends[0].expose_tools, vec!["read_file", "list_dir"]);
/// ```
pub fn expose_tools(mut self, tools: &[&str]) -> Self {
let backend = self
.config
.backends
.last_mut()
.expect("expose_tools called with no backends");
backend.expose_tools = tools.iter().map(|s| s.to_string()).collect();
self
}
/// Set the tool denylist for the last added backend.
///
/// The listed tools will be hidden from clients.
///
/// # Panics
///
/// Panics if no backends have been added.
///
/// # Example
///
/// ```rust
/// use mcp_proxy::builder::ProxyBuilder;
///
/// let config = ProxyBuilder::new("my-proxy")
/// .http_backend("api", "http://api:8080")
/// .hide_tools(&["dangerous_op"])
/// .into_config();
///
/// assert_eq!(config.backends[0].hide_tools, vec!["dangerous_op"]);
/// ```
pub fn hide_tools(mut self, tools: &[&str]) -> Self {
let backend = self
.config
.backends
.last_mut()
.expect("hide_tools called with no backends");
backend.hide_tools = tools.iter().map(|s| s.to_string()).collect();
self
}
/// Set the retry policy for the last added backend.
///
/// Uses sensible defaults: 100ms initial backoff, 5000ms max backoff,
/// no budget limit.
///
/// # Panics
///
/// Panics if no backends have been added.
///
/// # Example
///
/// ```rust
/// use mcp_proxy::builder::ProxyBuilder;
///
/// let config = ProxyBuilder::new("my-proxy")
/// .http_backend("api", "http://api:8080")
/// .retry(3)
/// .into_config();
///
/// let retry = config.backends[0].retry.as_ref().unwrap();
/// assert_eq!(retry.max_retries, 3);
/// ```
pub fn retry(mut self, max_retries: u32) -> Self {
let backend = self
.config
.backends
.last_mut()
.expect("retry called with no backends");
backend.retry = Some(RetryConfig {
max_retries,
initial_backoff_ms: 100,
max_backoff_ms: 5000,
budget_percent: None,
min_retries_per_sec: 10,
});
self
}
/// Extract the built [`ProxyConfig`] without connecting to backends.
///
/// Useful for inspection, serialization, or passing to
/// [`Proxy::from_config()`] manually.
pub fn into_config(self) -> ProxyConfig {
self.config
}
/// Build the proxy: validate config, connect to all backends, and
/// construct the middleware stack.
pub async fn build(self) -> Result<Proxy> {
Proxy::from_config(self.config).await
}
}
/// Default backend config with all optional fields set to `None`/empty.
fn default_backend() -> BackendConfig {
BackendConfig {
name: String::new(),
transport: TransportType::Stdio,
command: None,
args: Vec::new(),
url: None,
env: HashMap::new(),
bearer_token: None,
forward_auth: false,
timeout: None,
circuit_breaker: None,
rate_limit: None,
concurrency: None,
retry: None,
outlier_detection: None,
hedging: None,
cache: None,
default_args: serde_json::Map::new(),
inject_args: Vec::new(),
param_overrides: Vec::new(),
expose_tools: Vec::new(),
hide_tools: Vec::new(),
expose_resources: Vec::new(),
hide_resources: Vec::new(),
expose_prompts: Vec::new(),
hide_prompts: Vec::new(),
hide_destructive: false,
read_only_only: false,
failover_for: None,
priority: 0,
canary_of: None,
weight: 100,
aliases: Vec::new(),
mirror_of: None,
mirror_percent: 100,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_builder_minimal() {
let config = ProxyBuilder::new("test-proxy").into_config();
assert_eq!(config.proxy.name, "test-proxy");
assert_eq!(config.proxy.version, "0.1.0");
assert_eq!(config.proxy.separator, "/");
assert_eq!(config.proxy.listen.host, "127.0.0.1");
assert_eq!(config.proxy.listen.port, 8080);
assert!(config.backends.is_empty());
}
#[test]
fn test_builder_with_backends() {
let config = ProxyBuilder::new("test")
.stdio_backend("files", "npx", &["-y", "@mcp/server-files"])
.http_backend("api", "http://localhost:8080")
.into_config();
assert_eq!(config.backends.len(), 2);
assert_eq!(config.backends[0].name, "files");
assert!(matches!(config.backends[0].transport, TransportType::Stdio));
assert_eq!(config.backends[0].command.as_deref(), Some("npx"));
assert_eq!(config.backends[1].name, "api");
assert!(matches!(config.backends[1].transport, TransportType::Http));
assert_eq!(
config.backends[1].url.as_deref(),
Some("http://localhost:8080")
);
}
#[test]
fn test_builder_configure_backend() {
let config = ProxyBuilder::new("test")
.http_backend("api", "http://localhost:8080")
.configure_backend(|b| {
b.timeout = Some(TimeoutConfig { seconds: 30 });
b.rate_limit = Some(RateLimitConfig {
requests: 100,
period_seconds: 1,
});
b.hide_tools = vec!["dangerous_op".to_string()];
})
.into_config();
assert!(config.backends[0].timeout.is_some());
assert!(config.backends[0].rate_limit.is_some());
assert_eq!(config.backends[0].hide_tools, vec!["dangerous_op"]);
}
#[test]
fn test_builder_auth_and_observability() {
let config = ProxyBuilder::new("test")
.bearer_auth(vec!["token1".into(), "token2".into()])
.audit_logging(true)
.access_logging(true)
.metrics(true)
.json_logs(true)
.log_level("debug")
.into_config();
assert!(config.auth.is_some());
assert!(config.observability.audit);
assert!(config.observability.access_log.enabled);
assert!(config.observability.metrics.enabled);
assert!(config.observability.json_logs);
assert_eq!(config.observability.log_level, "debug");
}
#[test]
fn test_builder_global_rate_limit() {
let config = ProxyBuilder::new("test")
.global_rate_limit(500, Duration::from_secs(1))
.into_config();
let rl = config.proxy.rate_limit.unwrap();
assert_eq!(rl.requests, 500);
assert_eq!(rl.period_seconds, 1);
}
#[test]
fn test_builder_all_settings() {
let config = ProxyBuilder::new("enterprise")
.version("2.0.0")
.separator("::")
.listen("0.0.0.0", 9090)
.instructions("Enterprise MCP gateway")
.shutdown_timeout(Duration::from_secs(60))
.coalesce_requests(true)
.max_argument_size(1_048_576)
.into_config();
assert_eq!(config.proxy.name, "enterprise");
assert_eq!(config.proxy.version, "2.0.0");
assert_eq!(config.proxy.separator, "::");
assert_eq!(config.proxy.listen.host, "0.0.0.0");
assert_eq!(config.proxy.listen.port, 9090);
assert_eq!(
config.proxy.instructions.as_deref(),
Some("Enterprise MCP gateway")
);
assert_eq!(config.proxy.shutdown_timeout_seconds, 60);
assert!(config.performance.coalesce_requests);
assert_eq!(config.security.max_argument_size, Some(1_048_576));
}
#[test]
fn test_builder_http_backend_with_token() {
let config = ProxyBuilder::new("test")
.http_backend_with_token("api", "http://api:8080", "secret")
.into_config();
assert_eq!(config.backends[0].bearer_token.as_deref(), Some("secret"));
}
#[test]
fn test_builder_ergonomic_backend_methods() {
let config = ProxyBuilder::new("test")
.http_backend("api", "http://api:8080")
.timeout(30)
.rate_limit(100, 1)
.circuit_breaker(0.7)
.expose_tools(&["read_file", "list_dir"])
.retry(5)
.stdio_backend("files", "npx", &["-y", "@mcp/server-files"])
.hide_tools(&["dangerous_op"])
.timeout(60)
.into_config();
// First backend: api
let api = &config.backends[0];
assert_eq!(api.timeout.as_ref().unwrap().seconds, 30);
let rl = api.rate_limit.as_ref().unwrap();
assert_eq!(rl.requests, 100);
assert_eq!(rl.period_seconds, 1);
let cb = api.circuit_breaker.as_ref().unwrap();
assert!((cb.failure_rate_threshold - 0.7).abs() < f64::EPSILON);
assert_eq!(cb.minimum_calls, 5);
assert_eq!(cb.wait_duration_seconds, 30);
assert_eq!(cb.permitted_calls_in_half_open, 3);
assert_eq!(api.expose_tools, vec!["read_file", "list_dir"]);
let retry = api.retry.as_ref().unwrap();
assert_eq!(retry.max_retries, 5);
assert_eq!(retry.initial_backoff_ms, 100);
assert_eq!(retry.max_backoff_ms, 5000);
assert!(retry.budget_percent.is_none());
// Second backend: files
let files = &config.backends[1];
assert_eq!(files.hide_tools, vec!["dangerous_op"]);
assert_eq!(files.timeout.as_ref().unwrap().seconds, 60);
assert!(files.circuit_breaker.is_none());
assert!(files.rate_limit.is_none());
}
#[test]
fn test_builder_stdio_backend_with_env() {
let mut env = HashMap::new();
env.insert("GITHUB_TOKEN".to_string(), "ghp_xxx".to_string());
let config = ProxyBuilder::new("test")
.stdio_backend_with_env("github", "npx", &["-y", "@mcp/github"], env)
.into_config();
assert_eq!(
config.backends[0].env.get("GITHUB_TOKEN").unwrap(),
"ghp_xxx"
);
}
}