stdio2http 0.2.0

MCP stdio -> Streamable HTTP proxy
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
512
//! End-to-end: a real stdio child behind the real HTTP proxy.
//!
//! The upstream is the deterministic fixture in `tests/fixtures/mock-mcp-server`
//! and the HTTP side is a real rmcp Streamable HTTP client, so these exercise
//! session ids, SSE framing, and protocol negotiation rather than stubbing them.

use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;

use clap::Parser;
use rmcp::ClientHandler;
use rmcp::model::{CallToolRequestParams, CallToolResponse, ContentBlock, JsonObject, Tool};
use rmcp::service::{Peer, RoleClient, RunningService, ServiceExt as _};
use rmcp::transport::StreamableHttpClientTransport;
use serde_json::json;
use stdio2http::config::Config;
use stdio2http::http;
use stdio2http::upstream::Upstream;

const KEY_ALICE: &str = "k-alice-secret";
const KEY_BOB: &str = "k-bob-secret";

/// Locate the fixture binary through cargo rather than assuming a target dir.
fn fixture_binary() -> PathBuf {
    let manifest =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mock-mcp-server/Cargo.toml");

    let output = std::process::Command::new(env!("CARGO"))
        .args([
            "metadata",
            "--manifest-path",
            manifest.to_str().expect("utf-8 manifest path"),
            "--format-version",
            "1",
            "--no-deps",
        ])
        .output()
        .expect("cargo metadata runs");

    assert!(
        output.status.success(),
        "cargo metadata failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let metadata: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("metadata is JSON");
    let target_dir = metadata["target_directory"]
        .as_str()
        .expect("metadata carries a target_directory");
    let profile = if cfg!(debug_assertions) {
        "debug"
    } else {
        "release"
    };

    PathBuf::from(target_dir)
        .join(profile)
        .join("mock-mcp-server")
}

/// Install a stderr subscriber once so `upstream_error`'s diagnostic is visible.
fn init_tracing() {
    use std::sync::Once;
    static ONCE: Once = Once::new();
    ONCE.call_once(|| {
        let filter = tracing_subscriber::EnvFilter::try_from_default_env()
            .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
        let _ = tracing_subscriber::fmt()
            .with_env_filter(filter)
            .with_writer(std::io::stderr)
            .try_init();
    });
}

fn test_config(extra: &[&str]) -> Config {
    let binary = fixture_binary();
    assert!(
        binary.exists(),
        "fixture not built at {}; run: cargo build --manifest-path tests/fixtures/mock-mcp-server/Cargo.toml",
        binary.display()
    );

    let mut flags = vec![
        "--command".to_string(),
        binary.to_string_lossy().into_owned(),
        "--port".to_string(),
        "0".to_string(),
    ];
    flags.extend(extra.iter().map(|flag| (*flag).to_string()));

    let mut full = vec!["stdio2http".to_string()];
    full.extend(flags);
    let cfg = Config::parse_from(full.iter().map(String::as_str));
    cfg.validate().expect("test config is valid");
    cfg
}

/// A running proxy: the shared child plus the HTTP listener in front of it.
///
/// The `Upstream` handle is kept alive for the harness's lifetime on purpose:
/// it owns the child's transport, so dropping it would close the pipe under the
/// proxy and make every session fail.
struct Harness {
    addr: SocketAddr,
    child_pid: Option<u32>,
    _upstream: Arc<Upstream>,
}

async fn start(extra: &[&str]) -> Harness {
    init_tracing();
    let cfg = test_config(extra);

    let upstream = Arc::new(Upstream::spawn(&cfg).await.expect("upstream spawns"));
    let child_pid = upstream.child_pid();
    assert!(child_pid.is_some(), "the proxy must own a child pid");

    let listener = http::bind(&cfg).await.expect("proxy binds");
    let addr = listener.local_addr().expect("listener has an address");
    let router = http::router(&cfg, &upstream);
    tokio::spawn(async move {
        let _ = axum::serve(listener, router).await;
    });

    Harness {
        addr,
        child_pid,
        _upstream: upstream,
    }
}

struct TestClient;

impl ClientHandler for TestClient {}

type Client = RunningService<RoleClient, TestClient>;

/// Connect a real MCP client over Streamable HTTP, optionally with a credential.
async fn connect_at(uri: &str, api_key: Option<&str>) -> std::result::Result<Client, String> {
    use rmcp::transport::streamable_http_client::StreamableHttpClientTransportConfig as TransportConfig;

    let mut config = TransportConfig::with_uri(uri);
    if let Some(key) = api_key {
        // rmcp expects the token without the `Bearer ` prefix; it adds the scheme.
        config = config.auth_header(key.to_string());
    }

    let transport = StreamableHttpClientTransport::with_client(reqwest::Client::new(), config);

    TestClient
        .serve(transport)
        .await
        .map_err(|error| format!("{error}"))
}

async fn connect(harness: &Harness, api_key: Option<&str>) -> std::result::Result<Client, String> {
    connect_at(&format!("http://{}/mcp", harness.addr), api_key).await
}

/// Connect, treating a failed handshake as a test failure.
async fn connect_ok(harness: &Harness, api_key: Option<&str>) -> Client {
    connect(harness, api_key)
        .await
        .unwrap_or_else(|error| panic!("handshake failed: {error}"))
}

fn tool_names(tools: &[Tool]) -> Vec<&str> {
    let mut names: Vec<&str> = tools.iter().map(|tool| tool.name.as_ref()).collect();
    names.sort_unstable();
    names
}

/// The fixture always answers with text; anything else is a test bug.
fn text_of(result: &rmcp::model::CallToolResult) -> String {
    result
        .content
        .iter()
        .map(|block| match block {
            ContentBlock::Text(text) => text.text.clone(),
            other => panic!("expected a text block, got {other:?}"),
        })
        .collect()
}

fn echo_args(message: &str) -> JsonObject {
    let mut args = JsonObject::new();
    args.insert("message".to_string(), json!(message));
    args
}

/// Unwrap a completed tool call, failing loudly on any other shape.
fn completed(response: CallToolResponse) -> rmcp::model::CallToolResult {
    match response {
        CallToolResponse::Complete(result) => result,
        other => panic!("expected a completed call, got {other:?}"),
    }
}

async fn call_echo(client: &Client, message: &str) -> String {
    let response = client
        .call_tool_once(CallToolRequestParams::new("echo").with_arguments(echo_args(message)))
        .await
        .expect("tools/call succeeds");
    text_of(&completed(response))
}

async fn call_whoami(client: &Client) -> String {
    let response = client
        .call_tool_once(CallToolRequestParams::new("whoami"))
        .await
        .expect("tools/call succeeds");
    text_of(&completed(response))
}

#[tokio::test]
async fn tools_list_over_http_matches_the_stub() {
    let harness = start(&[]).await;
    let client = connect_ok(&harness, None).await;

    let listed = client.list_all_tools().await.expect("tools/list succeeds");

    assert_eq!(tool_names(&listed), ["echo", "whoami"]);

    let echo = listed
        .iter()
        .find(|tool| tool.name == "echo")
        .expect("echo is advertised");
    assert_eq!(
        echo.input_schema.get("required"),
        Some(&json!(["message"])),
        "echo keeps its required argument: {:?}",
        echo.input_schema
    );
}

#[tokio::test]
async fn a_tool_call_round_trips() {
    let harness = start(&[]).await;
    let client = connect_ok(&harness, None).await;

    let response = client
        .call_tool_once(CallToolRequestParams::new("echo").with_arguments(echo_args("hello")))
        .await
        .expect("tools/call succeeds");
    let result = completed(response);

    assert!(!result.is_error.unwrap_or(false), "echo must succeed");
    assert_eq!(text_of(&result), "hello");
}

#[tokio::test]
async fn a_tool_error_reaches_the_client_as_a_tool_error() {
    let harness = start(&[]).await;
    let client = connect_ok(&harness, None).await;

    // Omitting `message` makes the fixture answer isError, not a JSON-RPC error.
    let response = client
        .call_tool_once(CallToolRequestParams::new("echo"))
        .await
        .expect("the request itself is well-formed");
    let result = completed(response);

    assert!(
        result.is_error.unwrap_or(false),
        "a missing argument must surface as a tool error: {result:?}"
    );
    assert!(text_of(&result).contains("message"), "{result:?}");
}

#[tokio::test]
async fn two_sessions_share_one_child() {
    let harness = start(&[]).await;

    let first = connect_ok(&harness, None).await;
    let second = connect_ok(&harness, None).await;

    assert_eq!(call_echo(&first, "first").await, "first");
    assert_eq!(call_echo(&second, "second").await, "second");

    // Re-reading from the first session proves both answers came from one child
    // rather than a per-session one: the value is not tied to the caller.
    assert_eq!(call_echo(&first, "again").await, "again");
    assert!(harness.child_pid.is_some());
}

#[tokio::test]
async fn concurrent_sessions_interleave_over_one_child() {
    let harness = start(&[]).await;
    let client = connect_ok(&harness, None).await;
    let peer: Peer<RoleClient> = client.peer().clone();

    let mut calls = Vec::new();
    for index in 0..8 {
        let peer = peer.clone();
        calls.push(tokio::spawn(async move {
            let message = format!("call-{index}");
            let response = peer
                .call_tool_once(
                    CallToolRequestParams::new("echo").with_arguments(echo_args(&message)),
                )
                .await;
            response.map(completed).map(|result| text_of(&result))
        }));
    }

    for (index, call) in calls.into_iter().enumerate() {
        let text = call
            .await
            .expect("task does not panic")
            .expect("call succeeds");
        assert_eq!(
            text,
            format!("call-{index}"),
            "rmcp multiplexes by request id"
        );
    }
}

#[tokio::test]
async fn bearer_mode_rejects_an_unauthenticated_handshake() {
    let harness = start(&["--auth-mode", "bearer", "--api-key", KEY_ALICE]).await;

    let uri = format!("http://{}/mcp", harness.addr);
    assert_401(&uri, None).await;
    assert!(
        connect(&harness, None).await.is_err(),
        "an unauthenticated handshake must fail"
    );
}

#[tokio::test]
async fn bearer_mode_accepts_a_configured_key() {
    let harness = start(&["--auth-mode", "bearer", "--api-key", KEY_ALICE]).await;

    let client = connect_ok(&harness, Some(KEY_ALICE)).await;
    let tools = client
        .list_all_tools()
        .await
        .expect("authenticated list succeeds");
    assert_eq!(tool_names(&tools), ["echo", "whoami"]);
}

#[tokio::test]
async fn bearer_mode_rejects_a_wrong_key() {
    let harness = start(&["--auth-mode", "bearer", "--api-key", KEY_ALICE]).await;

    let uri = format!("http://{}/mcp", harness.addr);
    assert_401(&uri, Some(KEY_BOB)).await;
    assert!(
        connect(&harness, Some(KEY_BOB)).await.is_err(),
        "a wrong key must fail"
    );
}

#[tokio::test]
async fn identity_forward_yields_distinct_callers() {
    let harness = start(&[
        "--auth-mode",
        "identity-forward",
        "--api-key",
        &format!("{KEY_ALICE}=alice"),
        "--api-key",
        &format!("{KEY_BOB}=bob"),
    ])
    .await;

    let alice = connect_ok(&harness, Some(KEY_ALICE)).await;
    let bob = connect_ok(&harness, Some(KEY_BOB)).await;

    assert_eq!(call_whoami(&alice).await, "alice");
    assert_eq!(call_whoami(&bob).await, "bob");

    // Re-reading as alice shows the identity is per-request, not a one-time
    // label attached to the shared child.
    assert_eq!(call_whoami(&alice).await, "alice");
}

#[tokio::test]
async fn bearer_mode_authenticates_without_forwarding_identity() {
    let harness = start(&["--auth-mode", "bearer", "--api-key", KEY_ALICE]).await;
    let client = connect_ok(&harness, Some(KEY_ALICE)).await;

    assert_eq!(
        call_whoami(&client).await,
        "<none>",
        "bearer mode authenticates but forwards no identity"
    );
}

#[tokio::test]
async fn healthz_is_reachable_without_credentials() {
    let harness = start(&["--auth-mode", "bearer", "--api-key", KEY_ALICE]).await;

    let response = reqwest::get(format!("http://{}/healthz", harness.addr))
        .await
        .expect("healthz answers");

    assert_eq!(response.status(), 200);
    assert_eq!(response.text().await.expect("body reads"), "ok");
}

#[tokio::test]
async fn the_mount_path_is_configurable() {
    let harness = start(&["--mcp-path", "/rpc"]).await;
    let uri = format!("http://{}/rpc", harness.addr);

    let client = connect_at(&uri, None)
        .await
        .expect("connecting to the configured path must work");
    let tools = client.list_all_tools().await.expect("tools/list succeeds");
    assert_eq!(tool_names(&tools), ["echo", "whoami"]);

    // The default path is gone once remapped.
    let default_uri = format!("http://{}/mcp", harness.addr);
    assert!(
        connect_at(&default_uri, None).await.is_err(),
        "the default mount must not answer after remapping"
    );
}

#[tokio::test]
async fn an_upstream_failure_does_not_leak_child_stderr() {
    let harness = start(&[]).await;
    let client = connect_ok(&harness, None).await;

    let pid = harness.child_pid.expect("child pid recorded");
    let killed = std::process::Command::new("kill")
        .args(["-9", &pid.to_string()])
        .status()
        .expect("kill runs");
    assert!(killed.success(), "killing the child must succeed");

    let outcome = client
        .call_tool_once(CallToolRequestParams::new("echo").with_arguments(echo_args("gone")))
        .await;

    match outcome {
        Ok(response) => panic!(
            "a dead child must not answer: {}",
            text_of(&completed(response))
        ),
        Err(error) => {
            let rendered = format!("{error} {error:?}");
            for leak in [
                "panicked",
                "backtrace",
                "RUST_BACKTRACE",
                "mock-mcp-server",
                env!("CARGO_MANIFEST_DIR"),
            ] {
                assert!(
                    !rendered.contains(leak),
                    "error leaked {leak:?}: {rendered}"
                );
            }
            assert!(
                rendered.contains("internal_error") || rendered.contains("-32603"),
                "an upstream failure must map to internal_error: {rendered}"
            );
        }
    }
}

#[tokio::test]
async fn a_child_that_exits_before_initializing_is_fatal() {
    let cfg = Config::parse_from([
        "stdio2http",
        "--command",
        "true",
        "--init-timeout-ms",
        "2000",
    ]);
    cfg.validate().expect("valid config");

    let error = Upstream::spawn(&cfg)
        .await
        .expect_err("a child that exits before initializing is fatal");
    let rendered = format!("{error:#}");

    assert!(
        rendered.contains("true"),
        "the error must name the program: {rendered}"
    );
}

/// Assert the proxy answers 401 with a challenge, and echoes no key.
async fn assert_401(uri: &str, api_key: Option<&str>) {
    let mut request = reqwest::Client::new()
        .post(uri)
        .header("content-type", "application/json")
        .body(r#"{"jsonrpc":"2.0","id":0,"method":"initialize"}"#);
    if let Some(key) = api_key {
        request = request.header("authorization", format!("Bearer {key}"));
    }

    let response = request.send().await.expect("proxy answers");
    assert_eq!(response.status(), 401, "expected a 401 for {api_key:?}");

    let challenge = response
        .headers()
        .get("www-authenticate")
        .expect("a challenge header is required")
        .to_str()
        .expect("challenge is ascii");
    assert_eq!(challenge, "Bearer realm=\"mcp\"", "unexpected challenge");

    let body = response.text().await.expect("body reads");
    for leak in [KEY_ALICE, KEY_BOB] {
        assert!(!body.contains(leak), "401 body leaked a key: {body}");
    }
}