mcp-proxy 0.3.1

Standalone MCP proxy -- config-driven reverse proxy with auth, rate limiting, and observability
Documentation
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
//! Canary / weighted routing middleware.
//!
//! Routes a percentage of requests to a canary backend instead of the primary.
//! The canary backend is registered as a separate backend with its own namespace,
//! but its tools are hidden from `ListTools` (via capability filtering). When a
//! request targets the primary namespace, this middleware probabilistically
//! rewrites it to target the canary namespace instead.
//!
//! # Configuration
//!
//! ```toml
//! [[backends]]
//! name = "api"
//! transport = "http"
//! url = "http://api-v1.internal:8080"
//! weight = 90
//!
//! [[backends]]
//! name = "api-canary"
//! transport = "http"
//! url = "http://api-v2.internal:8080"
//! weight = 10
//! canary_of = "api"  # share namespace with api
//! ```
//!
//! # How it works
//!
//! 1. Both `api` and `api-canary` are registered as separate backends
//! 2. `api-canary`'s tools are auto-hidden via capability filtering
//! 3. When `CallTool("api/search")` arrives, this middleware rolls a weighted
//!    random selection: 90% chance it passes through to `api`, 10% chance it
//!    rewrites to `CallTool("api-canary/search")`
//! 4. `ListTools` always returns only the primary's tools

use std::collections::HashMap;
use std::convert::Infallible;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::task::{Context, Poll};

use tower::{Layer, Service};
use tower_mcp::router::{Extensions, RouterRequest, RouterResponse};
use tower_mcp_types::protocol::{CallToolParams, GetPromptParams, McpRequest, ReadResourceParams};

/// Tower layer that produces a [`CanaryService`].
#[derive(Clone)]
pub struct CanaryLayer {
    canaries: HashMap<String, (String, u32, u32)>,
    separator: String,
}

impl CanaryLayer {
    /// Create a new canary routing layer.
    ///
    /// `canaries` maps primary backend names to `(canary_name, primary_weight, canary_weight)`.
    pub fn new(
        canaries: HashMap<String, (String, u32, u32)>,
        separator: impl Into<String>,
    ) -> Self {
        Self {
            canaries,
            separator: separator.into(),
        }
    }
}

impl<S> Layer<S> for CanaryLayer {
    type Service = CanaryService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        CanaryService::new(inner, self.canaries.clone(), &self.separator)
    }
}

/// Mapping from a primary backend namespace to its canary configuration.
#[derive(Debug, Clone)]
struct CanaryMapping {
    /// Primary namespace prefix (e.g. "api/").
    primary_prefix: String,
    /// Canary namespace prefix (e.g. "api-canary/").
    canary_prefix: String,
    /// Weight of the primary (e.g. 90).
    primary_weight: u32,
    /// Total weight (primary + canary, e.g. 100).
    total_weight: u32,
    /// Atomic counter for deterministic weight-based routing.
    counter: Arc<AtomicU64>,
}

/// Canary routing middleware.
///
/// Wraps the proxy service and probabilistically rewrites requests from
/// the primary namespace to the canary namespace based on configured weights.
#[derive(Clone)]
pub struct CanaryService<S> {
    inner: S,
    mappings: Arc<Vec<CanaryMapping>>,
}

impl<S> CanaryService<S> {
    /// Create a new canary service.
    ///
    /// `canaries` maps primary backend names to `(canary_name, primary_weight, canary_weight)`.
    /// The `separator` is used to construct namespace prefixes.
    pub fn new(inner: S, canaries: HashMap<String, (String, u32, u32)>, separator: &str) -> Self {
        let mappings = canaries
            .into_iter()
            .map(
                |(primary, (canary, primary_weight, canary_weight))| CanaryMapping {
                    primary_prefix: format!("{primary}{separator}"),
                    canary_prefix: format!("{canary}{separator}"),
                    primary_weight,
                    total_weight: primary_weight + canary_weight,
                    counter: Arc::new(AtomicU64::new(0)),
                },
            )
            .collect();

        Self {
            inner,
            mappings: Arc::new(mappings),
        }
    }
}

/// Check if a request targets a primary namespace and return the mapping.
fn find_canary<'a>(name: &str, mappings: &'a [CanaryMapping]) -> Option<&'a CanaryMapping> {
    mappings
        .iter()
        .find(|m| name.starts_with(&m.primary_prefix))
}

/// Deterministic check: should this request go to the canary?
fn should_route_to_canary(mapping: &CanaryMapping) -> bool {
    let count = mapping.counter.fetch_add(1, Ordering::Relaxed);
    let position = count % mapping.total_weight as u64;
    // Primary gets the first primary_weight slots, canary gets the rest
    position >= mapping.primary_weight as u64
}

/// Rewrite a request to target the canary namespace.
fn rewrite_to_canary(req: RouterRequest, mapping: &CanaryMapping) -> RouterRequest {
    let new_inner = match req.inner {
        McpRequest::CallTool(params) if params.name.starts_with(&mapping.primary_prefix) => {
            let suffix = &params.name[mapping.primary_prefix.len()..];
            McpRequest::CallTool(CallToolParams {
                name: format!("{}{suffix}", mapping.canary_prefix),
                arguments: params.arguments,
                meta: params.meta,
                task: params.task,
            })
        }
        McpRequest::ReadResource(params) if params.uri.starts_with(&mapping.primary_prefix) => {
            let suffix = &params.uri[mapping.primary_prefix.len()..];
            McpRequest::ReadResource(ReadResourceParams {
                uri: format!("{}{suffix}", mapping.canary_prefix),
                meta: params.meta,
            })
        }
        McpRequest::GetPrompt(params) if params.name.starts_with(&mapping.primary_prefix) => {
            let suffix = &params.name[mapping.primary_prefix.len()..];
            McpRequest::GetPrompt(GetPromptParams {
                name: format!("{}{suffix}", mapping.canary_prefix),
                arguments: params.arguments,
                meta: params.meta,
            })
        }
        other => other,
    };

    RouterRequest {
        id: req.id,
        inner: new_inner,
        extensions: Extensions::new(),
    }
}

/// Extract the request name for namespace matching.
fn request_name(req: &McpRequest) -> Option<&str> {
    match req {
        McpRequest::CallTool(params) => Some(&params.name),
        McpRequest::ReadResource(params) => Some(&params.uri),
        McpRequest::GetPrompt(params) => Some(&params.name),
        _ => None,
    }
}

impl<S> Service<RouterRequest> for CanaryService<S>
where
    S: Service<RouterRequest, Response = RouterResponse, Error = Infallible>
        + Clone
        + Send
        + 'static,
    S::Future: Send,
{
    type Response = RouterResponse;
    type Error = Infallible;
    type Future = Pin<Box<dyn Future<Output = Result<RouterResponse, Infallible>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: RouterRequest) -> Self::Future {
        // Check if this request should be routed to a canary
        let should_canary = request_name(&req.inner)
            .and_then(|name| find_canary(name, &self.mappings))
            .filter(|mapping| should_route_to_canary(mapping))
            .cloned();

        let req = if let Some(ref mapping) = should_canary {
            tracing::debug!(
                primary = %mapping.primary_prefix,
                canary = %mapping.canary_prefix,
                "Routing request to canary backend"
            );
            rewrite_to_canary(req, mapping)
        } else {
            req
        };

        let fut = self.inner.call(req);
        Box::pin(fut)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_util::{MockService, call_service};
    use tower_mcp::protocol::RequestId;

    fn make_canaries(
        primary: &str,
        canary: &str,
        primary_weight: u32,
        canary_weight: u32,
    ) -> HashMap<String, (String, u32, u32)> {
        let mut m = HashMap::new();
        m.insert(
            primary.to_string(),
            (canary.to_string(), primary_weight, canary_weight),
        );
        m
    }

    #[test]
    fn test_find_canary_match() {
        let mappings = vec![CanaryMapping {
            primary_prefix: "api/".to_string(),
            canary_prefix: "api-canary/".to_string(),
            primary_weight: 90,
            total_weight: 100,
            counter: Arc::new(AtomicU64::new(0)),
        }];
        assert!(find_canary("api/search", &mappings).is_some());
        assert!(find_canary("other/search", &mappings).is_none());
    }

    #[test]
    fn test_should_route_to_canary_weights() {
        let mapping = CanaryMapping {
            primary_prefix: "api/".to_string(),
            canary_prefix: "api-canary/".to_string(),
            primary_weight: 90,
            total_weight: 100,
            counter: Arc::new(AtomicU64::new(0)),
        };

        // Over 100 requests, exactly 10 should go to canary
        let canary_count: u32 = (0..100)
            .filter(|_| should_route_to_canary(&mapping))
            .count() as u32;
        assert_eq!(canary_count, 10);
    }

    #[test]
    fn test_should_route_to_canary_50_50() {
        let mapping = CanaryMapping {
            primary_prefix: "api/".to_string(),
            canary_prefix: "api-canary/".to_string(),
            primary_weight: 50,
            total_weight: 100,
            counter: Arc::new(AtomicU64::new(0)),
        };

        let canary_count: u32 = (0..100)
            .filter(|_| should_route_to_canary(&mapping))
            .count() as u32;
        assert_eq!(canary_count, 50);
    }

    #[test]
    fn test_rewrite_to_canary_call_tool() {
        let mapping = CanaryMapping {
            primary_prefix: "api/".to_string(),
            canary_prefix: "api-canary/".to_string(),
            primary_weight: 90,
            total_weight: 100,
            counter: Arc::new(AtomicU64::new(0)),
        };

        let req = RouterRequest {
            id: RequestId::Number(1),
            inner: McpRequest::CallTool(CallToolParams {
                name: "api/search".to_string(),
                arguments: serde_json::json!({"q": "test"}),
                meta: None,
                task: None,
            }),
            extensions: Extensions::new(),
        };

        let rewritten = rewrite_to_canary(req, &mapping);
        match &rewritten.inner {
            McpRequest::CallTool(params) => {
                assert_eq!(params.name, "api-canary/search");
                assert_eq!(params.arguments, serde_json::json!({"q": "test"}));
            }
            _ => panic!("expected CallTool"),
        }
    }

    #[test]
    fn test_rewrite_to_canary_read_resource() {
        let mapping = CanaryMapping {
            primary_prefix: "api/".to_string(),
            canary_prefix: "api-canary/".to_string(),
            primary_weight: 90,
            total_weight: 100,
            counter: Arc::new(AtomicU64::new(0)),
        };

        let req = RouterRequest {
            id: RequestId::Number(1),
            inner: McpRequest::ReadResource(ReadResourceParams {
                uri: "api/docs/readme".to_string(),
                meta: None,
            }),
            extensions: Extensions::new(),
        };

        let rewritten = rewrite_to_canary(req, &mapping);
        match &rewritten.inner {
            McpRequest::ReadResource(params) => {
                assert_eq!(params.uri, "api-canary/docs/readme");
            }
            _ => panic!("expected ReadResource"),
        }
    }

    #[test]
    fn test_rewrite_leaves_non_matching_unchanged() {
        let mapping = CanaryMapping {
            primary_prefix: "api/".to_string(),
            canary_prefix: "api-canary/".to_string(),
            primary_weight: 90,
            total_weight: 100,
            counter: Arc::new(AtomicU64::new(0)),
        };

        let req = RouterRequest {
            id: RequestId::Number(1),
            inner: McpRequest::ListTools(Default::default()),
            extensions: Extensions::new(),
        };

        let rewritten = rewrite_to_canary(req, &mapping);
        assert!(matches!(rewritten.inner, McpRequest::ListTools(_)));
    }

    #[tokio::test]
    async fn test_canary_service_routes_to_canary() {
        // Weight 0 primary / 100 canary = always canary
        let mock = MockService::with_tools(&["api/search", "api-canary/search"]);
        let canaries = make_canaries("api", "api-canary", 0, 100);
        let mut svc = CanaryService::new(mock, canaries, "/");

        let resp = call_service(
            &mut svc,
            McpRequest::CallTool(CallToolParams {
                name: "api/search".to_string(),
                arguments: serde_json::json!({}),
                meta: None,
                task: None,
            }),
        )
        .await;

        // Should succeed (rewritten to api-canary/search)
        assert!(resp.inner.is_ok());
    }

    #[tokio::test]
    async fn test_canary_service_passes_through_primary() {
        // Weight 100 primary / 0 would panic, so use 100/1 (99% primary)
        let mock = MockService::with_tools(&["api/search"]);
        let canaries = make_canaries("api", "api-canary", 100, 1);
        let mut svc = CanaryService::new(mock, canaries, "/");

        // First request goes to primary (position 0 < 100)
        let resp = call_service(
            &mut svc,
            McpRequest::CallTool(CallToolParams {
                name: "api/search".to_string(),
                arguments: serde_json::json!({}),
                meta: None,
                task: None,
            }),
        )
        .await;

        assert!(resp.inner.is_ok());
    }

    #[tokio::test]
    async fn test_canary_service_non_matching_passes_through() {
        let mock = MockService::with_tools(&["other/tool"]);
        let canaries = make_canaries("api", "api-canary", 0, 100);
        let mut svc = CanaryService::new(mock, canaries, "/");

        let resp = call_service(
            &mut svc,
            McpRequest::CallTool(CallToolParams {
                name: "other/tool".to_string(),
                arguments: serde_json::json!({}),
                meta: None,
                task: None,
            }),
        )
        .await;

        assert!(resp.inner.is_ok());
    }

    #[tokio::test]
    async fn test_canary_service_list_tools_not_affected() {
        let mock = MockService::with_tools(&["api/search"]);
        let canaries = make_canaries("api", "api-canary", 0, 100);
        let mut svc = CanaryService::new(mock, canaries, "/");

        let resp = call_service(&mut svc, McpRequest::ListTools(Default::default())).await;
        assert!(resp.inner.is_ok());
    }
}