stdio2http 0.1.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
//! Configuration surface: CLI flags plus environment variables, no config file.

use std::net::IpAddr;
use std::path::PathBuf;
use std::time::Duration;

use clap::{Parser, ValueEnum};

/// Access control applied to `/mcp`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum AuthMode {
    /// No authentication.
    None,
    /// Require a matching API key; callers are anonymous to the upstream.
    Bearer,
    /// Require a matching API key and forward the caller's subject upstream.
    IdentityForward,
}

impl AuthMode {
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Bearer => "bearer",
            Self::IdentityForward => "identity-forward",
        }
    }
}

/// Log verbosity, validated against the levels `tracing` understands.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum LogLevel {
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}

impl LogLevel {
    #[must_use]
    pub fn as_filter(self) -> &'static str {
        match self {
            Self::Error => "error",
            Self::Warn => "warn",
            Self::Info => "info",
            Self::Debug => "debug",
            Self::Trace => "trace",
        }
    }
}

/// Split `KEY=VALUE` into its parts, keeping any `=` in the value.
fn parse_key_value(raw: &str) -> Result<(String, String), String> {
    match raw.split_once('=') {
        Some((key, value)) if !key.is_empty() => Ok((key.to_string(), value.to_string())),
        _ => Err(format!("expected KEY=VALUE, got {raw:?}")),
    }
}

#[derive(Debug, Clone, Parser)]
#[command(
    name = "stdio2http",
    version,
    about = "MCP stdio -> Streamable HTTP proxy",
    disable_help_subcommand = true
)]
pub struct Config {
    /// Program of the stdio MCP server to spawn.
    #[arg(long = "command", env = "STDIO2HTTP_COMMAND", value_name = "PROGRAM")]
    pub command_program: String,

    /// Argument for the stdio MCP server; repeat once per argument. Values may
    /// start with `-`, so upstream flags like `--stdio` pass through intact.
    #[arg(
        long = "arg",
        env = "STDIO2HTTP_ARGS",
        value_name = "ARG",
        allow_hyphen_values = true
    )]
    pub command_args: Vec<String>,

    /// Environment variable for the child, as KEY=VALUE; repeatable.
    #[arg(
        long = "child-env",
        env = "STDIO2HTTP_CHILD_ENV",
        value_name = "KEY=VALUE",
        value_parser = parse_key_value
    )]
    pub env: Vec<(String, String)>,

    /// Working directory for the child process.
    #[arg(long = "cwd", env = "STDIO2HTTP_CWD", value_name = "DIR")]
    pub cwd: Option<PathBuf>,

    /// Address to bind.
    #[arg(long = "host", env = "STDIO2HTTP_HOST", default_value = "127.0.0.1")]
    pub host: IpAddr,

    /// Port to bind.
    #[arg(long = "port", env = "STDIO2HTTP_PORT", default_value_t = 8080)]
    pub port: u16,

    /// Path the Streamable HTTP transport is mounted at.
    #[arg(long = "mcp-path", env = "STDIO2HTTP_MCP_PATH", default_value = "/mcp")]
    pub mcp_path: String,

    /// Host accepted in the Host header; repeatable. Empty keeps rmcp's loopback default.
    #[arg(
        long = "allowed-host",
        env = "STDIO2HTTP_ALLOWED_HOSTS",
        value_delimiter = ','
    )]
    pub allowed_hosts: Vec<String>,

    /// Origin accepted in the Origin header; repeatable.
    #[arg(
        long = "allowed-origin",
        env = "STDIO2HTTP_ALLOWED_ORIGINS",
        value_delimiter = ','
    )]
    pub allowed_origins: Vec<String>,

    /// Authentication mode for /mcp.
    #[arg(
        long = "auth-mode",
        env = "STDIO2HTTP_AUTH_MODE",
        value_enum,
        default_value_t = AuthMode::None
    )]
    pub auth_mode: AuthMode,

    /// Accepted API key, optionally KEY=SUBJECT to name the caller; repeatable.
    #[arg(
        long = "api-key",
        env = "STDIO2HTTP_API_KEYS",
        value_name = "KEY[=SUBJECT]",
        value_delimiter = ','
    )]
    pub api_keys: Vec<String>,

    /// Log verbosity.
    #[arg(
        long = "log-level",
        env = "STDIO2HTTP_LOG_LEVEL",
        value_enum,
        default_value_t = LogLevel::Info
    )]
    pub log_level: LogLevel,

    /// Milliseconds allowed for the upstream spawn and initialize handshake.
    #[arg(
        long = "init-timeout-ms",
        env = "STDIO2HTTP_INIT_TIMEOUT_MS",
        default_value_t = 10_000
    )]
    pub init_timeout_ms: u64,

    /// Accept every Host header. Only for deployments that already trust the network edge.
    #[arg(
        long = "disable-allowed-hosts",
        env = "STDIO2HTTP_DISABLE_ALLOWED_HOSTS",
        action = clap::ArgAction::SetTrue
    )]
    pub disable_allowed_hosts: bool,
}

impl Config {
    /// Program followed by its arguments, in the shape the child spawner wants.
    #[must_use]
    pub fn argv(&self) -> Vec<String> {
        std::iter::once(self.command_program.clone())
            .chain(self.command_args.iter().cloned())
            .collect()
    }

    #[must_use]
    pub fn init_timeout(&self) -> Duration {
        Duration::from_millis(self.init_timeout_ms)
    }

    #[must_use]
    pub fn socket_addr(&self) -> std::net::SocketAddr {
        std::net::SocketAddr::new(self.host, self.port)
    }

    /// Reject combinations that would silently run unauthenticated or misconfigured.
    ///
    /// # Errors
    ///
    /// Returns a message naming the offending flag when the command, the mount
    /// path, or the authentication mode is unusable.
    pub fn validate(&self) -> Result<(), String> {
        if self.command_program.is_empty() {
            return Err("--command must name a program".to_string());
        }
        if !self.mcp_path.starts_with('/') {
            return Err(format!(
                "--mcp-path must start with '/', got {:?}",
                self.mcp_path
            ));
        }
        match self.auth_mode {
            AuthMode::None => {}
            AuthMode::Bearer | AuthMode::IdentityForward => {
                if self.api_keys.is_empty() {
                    return Err(format!(
                        "--auth-mode {} requires at least one --api-key",
                        self.auth_mode.as_str()
                    ));
                }
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::CommandFactory;

    fn parse(flags: &[&str]) -> Result<Config, clap::Error> {
        let mut full = vec!["stdio2http"];
        full.extend_from_slice(flags);
        Config::try_parse_from(full)
    }

    #[test]
    fn clap_definition_is_valid() {
        Config::command().debug_assert();
    }

    #[test]
    fn defaults_are_the_documented_ones() {
        let cfg = parse(&["--command", "server"]).expect("parse");
        assert_eq!(cfg.host.to_string(), "127.0.0.1");
        assert_eq!(cfg.port, 8080);
        assert_eq!(cfg.mcp_path, "/mcp");
        assert_eq!(cfg.auth_mode, AuthMode::None);
        assert_eq!(cfg.log_level, LogLevel::Info);
        assert_eq!(cfg.init_timeout(), Duration::from_millis(10_000));
        assert!(cfg.cwd.is_none());
        let no_strings: Vec<String> = Vec::new();
        assert_eq!(cfg.api_keys, no_strings, "no api keys by default");
        assert_eq!(
            cfg.allowed_hosts, no_strings,
            "rmcp keeps its loopback default"
        );
        assert_eq!(cfg.allowed_origins, no_strings, "origin validation is off");
        assert!(!cfg.disable_allowed_hosts);
        cfg.validate().expect("defaults are valid");
    }

    #[test]
    fn argv_joins_program_and_repeated_args() {
        let cfg = parse(&[
            "--command",
            "node",
            "--arg",
            "dist/server.js",
            "--arg",
            "--stdio",
        ])
        .expect("parse");
        assert_eq!(cfg.argv(), ["node", "dist/server.js", "--stdio"]);
    }

    #[test]
    fn child_env_splits_on_the_first_equals() {
        let cfg = parse(&[
            "--command",
            "server",
            "--child-env",
            "TOKEN=abc",
            "--child-env",
            "MODE=fast=1",
        ])
        .expect("parse");
        assert_eq!(
            cfg.env,
            [
                ("TOKEN".to_string(), "abc".to_string()),
                ("MODE".to_string(), "fast=1".to_string())
            ]
        );
    }

    #[test]
    fn child_env_without_equals_is_a_clap_error() {
        let err = parse(&["--command", "s", "--child-env", "TOKEN"]).expect_err("must reject");
        assert!(err.to_string().contains("KEY=VALUE"), "{err}");
    }

    #[test]
    fn api_keys_may_name_a_subject() {
        let cfg =
            parse(&["--command", "s", "--api-key", "k1=alice", "--api-key", "k2"]).expect("parse");
        assert_eq!(cfg.api_keys, ["k1=alice", "k2"]);
    }

    #[test]
    fn invalid_auth_mode_is_a_clap_error() {
        let err = parse(&["--command", "s", "--auth-mode", "oauth"]).expect_err("must reject");
        assert!(err.to_string().contains("oauth"), "{err}");
    }

    #[test]
    fn invalid_log_level_is_a_clap_error() {
        let err = parse(&["--command", "s", "--log-level", "chatty"]).expect_err("must reject");
        assert!(err.to_string().contains("chatty"), "{err}");
    }

    #[test]
    fn non_numeric_port_is_a_clap_error() {
        let err = parse(&["--command", "s", "--port", "http"]).expect_err("must reject");
        assert!(err.to_string().contains("http"), "{err}");
    }

    #[test]
    fn out_of_range_port_is_a_clap_error() {
        let err = parse(&["--command", "s", "--port", "70000"]).expect_err("must reject");
        assert!(err.to_string().contains("70000"), "{err}");
    }

    #[test]
    fn missing_command_is_a_clap_error() {
        let err = parse(&[]).expect_err("--command is required");
        assert!(err.to_string().contains("--command"), "{err}");
    }

    #[test]
    fn auth_modes_without_keys_are_rejected() {
        for mode in ["bearer", "identity-forward"] {
            let cfg = parse(&["--command", "s", "--auth-mode", mode]).expect("parse");
            let err = cfg.validate().expect_err("no keys configured");
            assert!(err.contains("--api-key"), "{err}");
        }
    }

    #[test]
    fn auth_modes_with_keys_are_accepted() {
        for mode in ["bearer", "identity-forward"] {
            let cfg =
                parse(&["--command", "s", "--auth-mode", mode, "--api-key", "k"]).expect("parse");
            cfg.validate().expect("valid");
        }
    }

    #[test]
    fn mcp_path_must_be_absolute() {
        let cfg = parse(&["--command", "s", "--mcp-path", "mcp"]).expect("parse");
        let err = cfg.validate().expect_err("relative path");
        assert!(err.contains("start with"), "{err}");
    }

    #[test]
    fn empty_program_is_rejected() {
        let cfg = parse(&["--command", ""]).expect("parse");
        let err = cfg.validate().expect_err("empty program");
        assert!(err.contains("--command"), "{err}");
    }

    #[test]
    fn socket_addr_combines_host_and_port() {
        let cfg = parse(&["--command", "s", "--host", "0.0.0.0", "--port", "9000"]).expect("parse");
        assert_eq!(cfg.socket_addr().to_string(), "0.0.0.0:9000");
    }

    #[test]
    fn log_level_maps_to_a_tracing_filter() {
        assert_eq!(LogLevel::Info.as_filter(), "info");
        assert_eq!(LogLevel::Trace.as_filter(), "trace");
    }

    #[test]
    fn auth_mode_renders_the_documented_spelling() {
        assert_eq!(AuthMode::None.as_str(), "none");
        assert_eq!(AuthMode::Bearer.as_str(), "bearer");
        assert_eq!(AuthMode::IdentityForward.as_str(), "identity-forward");
    }

    #[test]
    fn help_lists_every_documented_field() {
        let mut cmd = Config::command();
        let help = cmd.render_long_help().to_string();
        for field in [
            "--command",
            "--arg",
            "--child-env",
            "--cwd",
            "--host",
            "--port",
            "--mcp-path",
            "--allowed-host",
            "--allowed-origin",
            "--auth-mode",
            "--api-key",
            "--log-level",
            "--init-timeout-ms",
            "--disable-allowed-hosts",
        ] {
            assert!(help.contains(field), "help is missing {field}");
        }
    }
}