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
//! Traffic mirroring / shadowing middleware.
//!
//! Sends a copy of traffic to a secondary backend (fire-and-forget, response
//! discarded). Useful for testing new backend versions, benchmarking, or
//! audit recording.
//!
//! # Configuration
//!
//! ```toml
//! [[backends]]
//! name = "api"
//! transport = "http"
//! url = "http://api.internal:8080"
//!
//! [[backends]]
//! name = "api-v2"
//! transport = "http"
//! url = "http://api-v2.internal:8080"
//! mirror_of = "api"        # mirror traffic from "api" backend
//! mirror_percent = 10      # mirror 10% of requests
//! ```
//!
//! # How it works
//!
//! 1. Request arrives targeting `api/search`
//! 2. Primary response is returned from the `api` backend as normal
//! 3. A copy of the request is rewritten to `api-v2/search` and sent
//!    fire-and-forget to the `api-v2` backend
//! 4. The mirror response is discarded; errors are logged but don't
//!    affect the primary response

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 [`MirrorService`].
#[derive(Clone)]
pub struct MirrorLayer {
    mirrors: HashMap<String, (String, u32)>,
    separator: String,
}

impl MirrorLayer {
    /// Create a new mirror layer.
    ///
    /// `mirrors` maps source backend names to `(mirror_name, percent)`.
    pub fn new(mirrors: HashMap<String, (String, u32)>, separator: impl Into<String>) -> Self {
        Self {
            mirrors,
            separator: separator.into(),
        }
    }
}

impl<S> Layer<S> for MirrorLayer {
    type Service = MirrorService<S>;

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

/// Mapping from a source backend namespace to its mirror configuration.
#[derive(Debug, Clone)]
struct MirrorMapping {
    /// Source namespace prefix (e.g. "api/").
    source_prefix: String,
    /// Mirror namespace prefix (e.g. "api-v2/").
    mirror_prefix: String,
    /// Percentage of requests to mirror (1-100).
    percent: u32,
    /// Atomic counter for deterministic percentage-based sampling.
    counter: Arc<AtomicU64>,
}

/// Traffic mirroring middleware.
///
/// Wraps the proxy service and sends copies of matching requests to
/// mirror backends. The primary response is always returned; mirror
/// responses are discarded.
#[derive(Clone)]
pub struct MirrorService<S> {
    inner: S,
    mappings: Arc<Vec<MirrorMapping>>,
}

impl<S> MirrorService<S> {
    /// Create a new mirror service.
    ///
    /// `mirrors` maps source backend names to `(mirror_name, percent)`.
    /// The `separator` is used to construct namespace prefixes.
    pub fn new(inner: S, mirrors: HashMap<String, (String, u32)>, separator: &str) -> Self {
        let mappings = mirrors
            .into_iter()
            .map(|(source, (mirror, percent))| MirrorMapping {
                source_prefix: format!("{source}{separator}"),
                mirror_prefix: format!("{mirror}{separator}"),
                percent: percent.clamp(1, 100),
                counter: Arc::new(AtomicU64::new(0)),
            })
            .collect();

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

/// Check if a request name starts with a namespace prefix and return the
/// matching mirror mapping.
fn find_mirror<'a>(name: &str, mappings: &'a [MirrorMapping]) -> Option<&'a MirrorMapping> {
    mappings.iter().find(|m| name.starts_with(&m.source_prefix))
}

/// Rewrite a namespaced name from source to mirror prefix.
fn rewrite_name(name: &str, source_prefix: &str, mirror_prefix: &str) -> String {
    let suffix = &name[source_prefix.len()..];
    format!("{mirror_prefix}{suffix}")
}

/// Clone a request with its name rewritten to the mirror namespace.
fn clone_for_mirror(
    req: &RouterRequest,
    source_prefix: &str,
    mirror_prefix: &str,
) -> Option<RouterRequest> {
    let new_inner = match &req.inner {
        McpRequest::CallTool(params) if params.name.starts_with(source_prefix) => {
            McpRequest::CallTool(CallToolParams {
                name: rewrite_name(&params.name, source_prefix, mirror_prefix),
                arguments: params.arguments.clone(),
                meta: params.meta.clone(),
                task: params.task.clone(),
            })
        }
        McpRequest::ReadResource(params) if params.uri.starts_with(source_prefix) => {
            McpRequest::ReadResource(ReadResourceParams {
                uri: rewrite_name(&params.uri, source_prefix, mirror_prefix),
                meta: params.meta.clone(),
            })
        }
        McpRequest::GetPrompt(params) if params.name.starts_with(source_prefix) => {
            McpRequest::GetPrompt(GetPromptParams {
                name: rewrite_name(&params.name, source_prefix, mirror_prefix),
                arguments: params.arguments.clone(),
                meta: params.meta.clone(),
            })
        }
        // List requests and other types aren't mirrored
        _ => return None,
    };

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

/// Check if the sampling counter says this request should be mirrored.
fn should_mirror(mapping: &MirrorMapping) -> bool {
    if mapping.percent >= 100 {
        return true;
    }
    let count = mapping.counter.fetch_add(1, Ordering::Relaxed);
    (count % 100) < mapping.percent as u64
}

/// 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 MirrorService<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 mirrored
        let mirror_req = request_name(&req.inner)
            .and_then(|name| find_mirror(name, &self.mappings))
            .filter(|mapping| should_mirror(mapping))
            .and_then(|mapping| {
                clone_for_mirror(&req, &mapping.source_prefix, &mapping.mirror_prefix)
            });

        // Send the primary request
        let primary_fut = self.inner.call(req);

        // If mirroring, clone the service and spawn a fire-and-forget task
        let mut mirror_svc = if mirror_req.is_some() {
            Some(self.inner.clone())
        } else {
            None
        };

        Box::pin(async move {
            // Spawn mirror request as a fire-and-forget task
            if let Some(mirror) = mirror_req
                && let Some(ref mut svc) = mirror_svc
            {
                let mut svc = svc.clone();
                tokio::spawn(async move {
                    match svc.call(mirror).await {
                        Ok(resp) => {
                            if resp.inner.is_err() {
                                tracing::debug!("Mirror request returned error (discarded)");
                            }
                        }
                        Err(e) => match e {},
                    }
                });
            }

            primary_fut.await
        })
    }
}

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

    fn make_mirrors(source: &str, mirror: &str, percent: u32) -> HashMap<String, (String, u32)> {
        let mut m = HashMap::new();
        m.insert(source.to_string(), (mirror.to_string(), percent));
        m
    }

    #[test]
    fn test_rewrite_name() {
        assert_eq!(
            rewrite_name("api/search", "api/", "api-v2/"),
            "api-v2/search"
        );
        assert_eq!(
            rewrite_name("api/nested/tool", "api/", "mirror/"),
            "mirror/nested/tool"
        );
    }

    #[test]
    fn test_find_mirror_match() {
        let mappings = vec![MirrorMapping {
            source_prefix: "api/".to_string(),
            mirror_prefix: "api-v2/".to_string(),
            percent: 100,
            counter: Arc::new(AtomicU64::new(0)),
        }];
        assert!(find_mirror("api/search", &mappings).is_some());
        assert!(find_mirror("other/search", &mappings).is_none());
    }

    #[test]
    fn test_should_mirror_100_percent() {
        let mapping = MirrorMapping {
            source_prefix: "api/".to_string(),
            mirror_prefix: "api-v2/".to_string(),
            percent: 100,
            counter: Arc::new(AtomicU64::new(0)),
        };
        // All requests should be mirrored
        for _ in 0..10 {
            assert!(should_mirror(&mapping));
        }
    }

    #[test]
    fn test_should_mirror_percentage() {
        let mapping = MirrorMapping {
            source_prefix: "api/".to_string(),
            mirror_prefix: "api-v2/".to_string(),
            percent: 10,
            counter: Arc::new(AtomicU64::new(0)),
        };
        // Over 100 requests, exactly 10 should be mirrored
        let mirrored: u32 = (0..100).filter(|_| should_mirror(&mapping)).count() as u32;
        assert_eq!(mirrored, 10);
    }

    #[test]
    fn test_clone_for_mirror_call_tool() {
        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 mirrored = clone_for_mirror(&req, "api/", "api-v2/").unwrap();
        match &mirrored.inner {
            McpRequest::CallTool(params) => {
                assert_eq!(params.name, "api-v2/search");
                assert_eq!(params.arguments, serde_json::json!({"q": "test"}));
            }
            _ => panic!("expected CallTool"),
        }
    }

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

        let mirrored = clone_for_mirror(&req, "api/", "mirror/").unwrap();
        match &mirrored.inner {
            McpRequest::ReadResource(params) => {
                assert_eq!(params.uri, "mirror/docs/readme");
            }
            _ => panic!("expected ReadResource"),
        }
    }

    #[test]
    fn test_clone_for_mirror_list_tools_returns_none() {
        let req = RouterRequest {
            id: RequestId::Number(1),
            inner: McpRequest::ListTools(Default::default()),
            extensions: Extensions::new(),
        };
        assert!(clone_for_mirror(&req, "api/", "mirror/").is_none());
    }

    #[tokio::test]
    async fn test_mirror_service_passes_through() {
        let mock = MockService::with_tools(&["api/search", "api-v2/search"]);
        let mirrors = make_mirrors("api", "api-v2", 100);
        let mut svc = MirrorService::new(mock, mirrors, "/");

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

        // Primary response should be returned
        assert!(resp.inner.is_ok());
    }

    #[tokio::test]
    async fn test_mirror_service_non_mirrored_passes_through() {
        let mock = MockService::with_tools(&["other/tool"]);
        let mirrors = make_mirrors("api", "api-v2", 100);
        let mut svc = MirrorService::new(mock, mirrors, "/");

        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_mirror_service_list_tools_not_mirrored() {
        let mock = MockService::with_tools(&["api/search"]);
        let mirrors = make_mirrors("api", "api-v2", 100);
        let mut svc = MirrorService::new(mock, mirrors, "/");

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