mcp-proxy 0.4.0

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
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
//! HTTP transport-level E2E tests.
//!
//! Unlike `e2e.rs` which tests the MCP pipeline in-process, these tests
//! exercise the full HTTP stack: they start a real TCP server on a random
//! port and make HTTP requests with `reqwest`.

use std::collections::HashMap;
use std::convert::Infallible;
use std::net::SocketAddr;

use schemars::JsonSchema;
use serde::Deserialize;
use tokio::net::TcpListener;
use tower::util::BoxCloneService;

use tower_mcp::client::ChannelTransport;
use tower_mcp::proxy::McpProxy;
use tower_mcp::router::RouterRequest;
use tower_mcp::router::RouterResponse;
use tower_mcp::{CallToolResult, McpRouter, ToolBuilder};

use mcp_proxy::admin::{self, BackendMeta};
use mcp_proxy::config::ProxyConfig;

// ---------------------------------------------------------------------------
// Test backend routers
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize, JsonSchema)]
struct AddInput {
    a: i64,
    b: i64,
}

fn math_router() -> McpRouter {
    let add = ToolBuilder::new("add")
        .description("Add two numbers")
        .handler(|input: AddInput| async move {
            Ok(CallToolResult::text(format!("{}", input.a + input.b)))
        })
        .build();

    McpRouter::new()
        .server_info("math-server", "1.0.0")
        .tool(add)
}

fn text_router() -> McpRouter {
    let echo = ToolBuilder::new("echo")
        .description("Echo a message")
        .handler(|input: EchoInput| async move { Ok(CallToolResult::text(input.message)) })
        .build();

    McpRouter::new()
        .server_info("text-server", "1.0.0")
        .tool(echo)
}

#[derive(Debug, Deserialize, JsonSchema)]
struct EchoInput {
    message: String,
}

// ---------------------------------------------------------------------------
// Test helpers
// ---------------------------------------------------------------------------

/// Build an McpProxy with in-process channel backends, wrap it in the HTTP
/// transport and admin router, and bind to a random port. Returns the
/// server address and a handle to abort the server task.
async fn spawn_proxy_server() -> (SocketAddr, tokio::task::JoinHandle<()>) {
    let proxy = McpProxy::builder("test-proxy", "1.0.0")
        .separator("/")
        .backend("math", ChannelTransport::new(math_router()))
        .await
        .backend("text", ChannelTransport::new(text_router()))
        .await
        .build_strict()
        .await
        .expect("proxy should build");

    let proxy_for_admin = proxy.clone();
    let proxy_for_mgmt = proxy.clone();

    let service: BoxCloneService<RouterRequest, RouterResponse, Infallible> =
        BoxCloneService::new(proxy);

    let (router, session_handle) =
        tower_mcp::transport::http::HttpTransport::from_service(service).into_router_with_handle();

    let backend_meta: HashMap<String, BackendMeta> = [
        (
            "math".to_string(),
            BackendMeta {
                transport: "channel".to_string(),
            },
        ),
        (
            "text".to_string(),
            BackendMeta {
                transport: "channel".to_string(),
            },
        ),
    ]
    .into();

    let admin_state = admin::spawn_health_checker(
        proxy_for_admin,
        "test-proxy".into(),
        "1.0.0".into(),
        2,
        backend_meta,
    );

    let test_config = ProxyConfig::parse(
        r#"
        [proxy]
        name = "test-proxy"
        version = "1.0.0"
        [proxy.listen]

        [[backends]]
        name = "math"
        transport = "stdio"
        command = "echo"

        [[backends]]
        name = "text"
        transport = "stdio"
        command = "echo"
        "#,
    )
    .unwrap();

    let router = router.nest(
        "/admin",
        admin::admin_router(
            admin_state,
            None,
            session_handle,
            None,
            proxy_for_mgmt,
            &test_config,
            None,
            std::collections::HashMap::new(),
        ),
    );

    let listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind to random port");
    let addr = listener.local_addr().unwrap();

    let handle = tokio::spawn(async move {
        axum::serve(listener, router).await.ok();
    });

    (addr, handle)
}

/// Build an McpProxy like [`spawn_proxy_server`], but with bearer auth enforced
/// on the MCP endpoint and an admin token protecting `/admin/*`.
///
/// The single `token` is used for *both* the MCP bearer auth and the admin
/// token, mirroring how `apply_auth` (src/proxy.rs) wraps the MCP transport
/// router with `AuthLayer::new(StaticBearerValidator::new(...))` and how
/// `admin::admin_router` builds its own auth layer from
/// `resolve_admin_tokens` (src/admin.rs). The two layers are independent: the
/// MCP layer is applied before the `/admin` nest, so it does not cover admin
/// routes, and the admin router carries its own layer derived from
/// `security.admin_token`.
async fn spawn_proxy_server_with_bearer(token: &str) -> (SocketAddr, tokio::task::JoinHandle<()>) {
    let proxy = McpProxy::builder("test-proxy", "1.0.0")
        .separator("/")
        .backend("math", ChannelTransport::new(math_router()))
        .await
        .backend("text", ChannelTransport::new(text_router()))
        .await
        .build_strict()
        .await
        .expect("proxy should build");

    let proxy_for_admin = proxy.clone();
    let proxy_for_mgmt = proxy.clone();

    let service: BoxCloneService<RouterRequest, RouterResponse, Infallible> =
        BoxCloneService::new(proxy);

    let (router, session_handle) =
        tower_mcp::transport::http::HttpTransport::from_service(service).into_router_with_handle();

    // Enforce bearer auth on the MCP endpoint, mirroring `apply_auth`. Applied
    // before the `/admin` nest so it covers only the MCP routes.
    let validator = tower_mcp::auth::StaticBearerValidator::new([token.to_string()]);
    let router = router.layer(tower_mcp::auth::AuthLayer::new(validator));

    let backend_meta: HashMap<String, BackendMeta> = [
        (
            "math".to_string(),
            BackendMeta {
                transport: "channel".to_string(),
            },
        ),
        (
            "text".to_string(),
            BackendMeta {
                transport: "channel".to_string(),
            },
        ),
    ]
    .into();

    let admin_state = admin::spawn_health_checker(
        proxy_for_admin,
        "test-proxy".into(),
        "1.0.0".into(),
        2,
        backend_meta,
    );

    // Config carries an admin_token so `admin_router` builds its auth layer.
    let test_config = ProxyConfig::parse(&format!(
        r#"
        [proxy]
        name = "test-proxy"
        version = "1.0.0"
        [proxy.listen]

        [security]
        admin_token = "{token}"

        [[backends]]
        name = "math"
        transport = "stdio"
        command = "echo"

        [[backends]]
        name = "text"
        transport = "stdio"
        command = "echo"
        "#
    ))
    .unwrap();

    let router = router.nest(
        "/admin",
        admin::admin_router(
            admin_state,
            None,
            session_handle,
            None,
            proxy_for_mgmt,
            &test_config,
            None,
            std::collections::HashMap::new(),
        ),
    );

    let listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind to random port");
    let addr = listener.local_addr().unwrap();

    let handle = tokio::spawn(async move {
        axum::serve(listener, router).await.ok();
    });

    (addr, handle)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// The `/admin/backends` endpoint returns proxy metadata and backend list.
#[tokio::test]
async fn test_admin_backends_endpoint() {
    let (addr, handle) = spawn_proxy_server().await;
    let url = format!("http://{}/admin/backends", addr);

    let resp = reqwest::get(&url).await.expect("GET /admin/backends");
    assert_eq!(resp.status(), 200);

    let body: serde_json::Value = resp.json().await.unwrap();
    assert_eq!(body["proxy"]["name"], "test-proxy");
    assert_eq!(body["proxy"]["version"], "1.0.0");
    assert_eq!(body["proxy"]["backend_count"], 2);
    assert!(body["backends"].is_array());

    handle.abort();
}

/// The `/admin/health` endpoint returns a health status object.
#[tokio::test]
async fn test_admin_health_endpoint() {
    let (addr, handle) = spawn_proxy_server().await;
    let url = format!("http://{}/admin/health", addr);

    let resp = reqwest::get(&url).await.expect("GET /admin/health");
    assert_eq!(resp.status(), 200);

    let body: serde_json::Value = resp.json().await.unwrap();
    // The health checker may not have run yet, but the endpoint should
    // respond with a valid status field.
    assert!(
        body["status"] == "healthy" || body["status"] == "degraded",
        "unexpected status: {}",
        body["status"]
    );
    assert!(body["unhealthy_backends"].is_array());

    handle.abort();
}

/// The `/admin/sessions` endpoint returns the active session count.
#[tokio::test]
async fn test_admin_sessions_endpoint() {
    let (addr, handle) = spawn_proxy_server().await;
    let url = format!("http://{}/admin/sessions", addr);

    let resp = reqwest::get(&url).await.expect("GET /admin/sessions");
    assert_eq!(resp.status(), 200);

    let body: serde_json::Value = resp.json().await.unwrap();
    // No MCP sessions established, so count should be 0.
    assert_eq!(body["active_sessions"], 0);

    handle.abort();
}

/// The `/admin/cache/stats` endpoint returns an empty array when no caches
/// are configured.
#[tokio::test]
async fn test_admin_cache_stats_no_cache() {
    let (addr, handle) = spawn_proxy_server().await;
    let url = format!("http://{}/admin/cache/stats", addr);

    let resp = reqwest::get(&url).await.expect("GET /admin/cache/stats");
    assert_eq!(resp.status(), 200);

    let body: serde_json::Value = resp.json().await.unwrap();
    assert!(body.as_array().unwrap().is_empty());

    handle.abort();
}

/// The `/admin/config` endpoint returns the proxy configuration.
#[tokio::test]
async fn test_admin_config_endpoint() {
    let (addr, handle) = spawn_proxy_server().await;
    let url = format!("http://{}/admin/config", addr);

    let resp = reqwest::get(&url).await.expect("GET /admin/config");
    assert_eq!(resp.status(), 200);

    let body = resp.text().await.unwrap();
    // The config endpoint returns TOML; it should contain the proxy name.
    assert!(
        body.contains("test-proxy"),
        "config should contain proxy name"
    );

    handle.abort();
}

// ---------------------------------------------------------------------------
// Negative-path auth tests (issue #178)
// ---------------------------------------------------------------------------
//
// The auth deny path -- the entire value of an auth gateway -- needs coverage
// at the wired HTTP level, not just in per-module unit tests. These tests use
// `spawn_proxy_server_with_bearer` to stand up the real stack (bearer auth on
// the MCP endpoint, admin_token on `/admin/*`) and assert that the proxy
// returns the right status when auth is missing, wrong, or malformed. The
// admin pair is the end-to-end guard for the admin-protection fix in #174.
//
// Deliberately NOT covered here (deferred, not forgotten):
//   - JWT-signature cases (expired token, `alg=none`, JWKS rotation): these
//     require minting signed JWTs and a JWKS mock that this repo has no infra
//     for. Signature validation lives in the `tower-mcp` dependency and the
//     auth-gateway audit scoped it out of the proxy.
//   - Introspection `aud` handling (#177), RBAC `default_deny` (#176), and
//     per-token `required_scopes` (#175): each already has unit tests in its
//     own module; re-testing them here would add no coverage.

const TEST_TOKEN: &str = "sk-test-secret-token";

/// MCP endpoint with no `Authorization` header is rejected with 401.
#[tokio::test]
async fn test_mcp_no_auth_header_rejected() {
    let (addr, handle) = spawn_proxy_server_with_bearer(TEST_TOKEN).await;
    let url = format!("http://{}/", addr);

    let resp = reqwest::Client::new()
        .post(&url)
        .header("Content-Type", "application/json")
        .header("Accept", "application/json, text/event-stream")
        .body(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#)
        .send()
        .await
        .expect("POST /");
    assert_eq!(resp.status(), 401, "missing Authorization must be 401");

    handle.abort();
}

/// MCP endpoint with a wrong/garbage bearer token is rejected with 401.
#[tokio::test]
async fn test_mcp_wrong_token_rejected() {
    let (addr, handle) = spawn_proxy_server_with_bearer(TEST_TOKEN).await;
    let url = format!("http://{}/", addr);

    let resp = reqwest::Client::new()
        .post(&url)
        .header("Content-Type", "application/json")
        .header("Accept", "application/json, text/event-stream")
        .header("Authorization", "Bearer not-the-right-token")
        .body(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#)
        .send()
        .await
        .expect("POST /");
    assert_eq!(resp.status(), 401, "wrong bearer token must be 401");

    handle.abort();
}

/// MCP endpoint with a malformed `Authorization` header (an unsupported auth
/// scheme) is rejected with 401.
///
/// Note: `tower-mcp`'s `extract_api_key` intentionally accepts a *raw* key with
/// no scheme prefix (e.g. `Authorization: <token>`), so a prefix-less but
/// otherwise-correct token would pass. The genuinely-malformed case the
/// validator rejects is an unrecognized scheme such as Basic auth: it contains
/// a space and is neither `Bearer ` nor `ApiKey `, so no token is extracted.
#[tokio::test]
async fn test_mcp_malformed_auth_header_rejected() {
    let (addr, handle) = spawn_proxy_server_with_bearer(TEST_TOKEN).await;
    let url = format!("http://{}/", addr);

    let resp = reqwest::Client::new()
        .post(&url)
        .header("Content-Type", "application/json")
        .header("Accept", "application/json, text/event-stream")
        // Unsupported scheme -- the validator cannot extract a token.
        .header("Authorization", "Basic dXNlcjpwYXNzd29yZA==")
        .body(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#)
        .send()
        .await
        .expect("POST /");
    assert_eq!(resp.status(), 401, "malformed Authorization must be 401");

    handle.abort();
}

/// Sanity check: with the correct bearer token, the MCP endpoint does NOT
/// return 401 -- auth passes and the request reaches the handler. (The exact
/// non-401 status depends on the MCP handler; we only assert auth let it
/// through.)
#[tokio::test]
async fn test_mcp_correct_token_passes_auth() {
    let (addr, handle) = spawn_proxy_server_with_bearer(TEST_TOKEN).await;
    let url = format!("http://{}/", addr);

    let resp = reqwest::Client::new()
        .post(&url)
        .header("Content-Type", "application/json")
        .header("Accept", "application/json, text/event-stream")
        .bearer_auth(TEST_TOKEN)
        .body(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#)
        .send()
        .await
        .expect("POST /");
    assert_ne!(
        resp.status(),
        401,
        "correct bearer token must pass auth (got {})",
        resp.status()
    );

    handle.abort();
}

/// `/admin/*` with no token is rejected with 401. This is the end-to-end guard
/// for the admin-protection fix in #174.
#[tokio::test]
async fn test_admin_no_token_rejected() {
    let (addr, handle) = spawn_proxy_server_with_bearer(TEST_TOKEN).await;
    let url = format!("http://{}/admin/backends", addr);

    let resp = reqwest::get(&url).await.expect("GET /admin/backends");
    assert_eq!(resp.status(), 401, "admin without token must be 401");

    handle.abort();
}

/// `/admin/*` with the correct admin token returns 200.
#[tokio::test]
async fn test_admin_correct_token_allowed() {
    let (addr, handle) = spawn_proxy_server_with_bearer(TEST_TOKEN).await;
    let url = format!("http://{}/admin/backends", addr);

    let resp = reqwest::Client::new()
        .get(&url)
        .bearer_auth(TEST_TOKEN)
        .send()
        .await
        .expect("GET /admin/backends");
    assert_eq!(resp.status(), 200, "admin with correct token must be 200");

    handle.abort();
}