|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/pomerium/mcp-servers/httputil" |
| 11 | + "github.com/pomerium/mcp-servers/stdioproxy" |
| 12 | +) |
| 13 | + |
| 14 | +func stdioProxyCommand(ctx context.Context, args []string) error { |
| 15 | + fs := flag.NewFlagSet("stdio-proxy", flag.ExitOnError) |
| 16 | + |
| 17 | + addr := fs.String("addr", "", "HTTP bind address (default from ADDR env or :8080)") |
| 18 | + logLevel := fs.String("log-level", "", "Log level: debug, info, warn, error (default from LOG_LEVEL env or info)") |
| 19 | + workDir := fs.String("work-dir", "", "Working directory for subprocess") |
| 20 | + |
| 21 | + fs.Usage = func() { |
| 22 | + fmt.Fprintf(os.Stderr, `Usage: %s stdio-proxy [options] -- <command> [args...] |
| 23 | +
|
| 24 | +Proxy HTTP streaming requests to a stdio MCP server. |
| 25 | +
|
| 26 | +Options: |
| 27 | +`, programName) |
| 28 | + fs.PrintDefaults() |
| 29 | + fmt.Fprintf(os.Stderr, ` |
| 30 | +Environment variables: |
| 31 | + ADDR HTTP bind address (default :8080) |
| 32 | + LOG_LEVEL Log level: debug, info, warn, error (default info) |
| 33 | +
|
| 34 | +Examples: |
| 35 | + # Proxy requests to a Node.js MCP server |
| 36 | + %s stdio-proxy -- node /path/to/server.js |
| 37 | +
|
| 38 | + # Proxy with custom port and debug logging |
| 39 | + %s stdio-proxy -addr :9090 -log-level debug -- python -m my_mcp_server |
| 40 | +
|
| 41 | + # Using environment variables |
| 42 | + ADDR=:9090 LOG_LEVEL=debug %s stdio-proxy -- ./my-server |
| 43 | +`, programName, programName, programName) |
| 44 | + } |
| 45 | + |
| 46 | + if err := fs.Parse(args); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + // Remaining args are the subprocess command |
| 51 | + subArgs := fs.Args() |
| 52 | + if len(subArgs) == 0 { |
| 53 | + fs.Usage() |
| 54 | + return fmt.Errorf("missing subprocess command") |
| 55 | + } |
| 56 | + |
| 57 | + // Resolve bind address |
| 58 | + bindAddr := *addr |
| 59 | + if bindAddr == "" { |
| 60 | + bindAddr = os.Getenv("ADDR") |
| 61 | + } |
| 62 | + if bindAddr == "" { |
| 63 | + bindAddr = ":8080" |
| 64 | + } |
| 65 | + |
| 66 | + // Resolve log level |
| 67 | + level := *logLevel |
| 68 | + if level == "" { |
| 69 | + level = os.Getenv("LOG_LEVEL") |
| 70 | + } |
| 71 | + if level == "" { |
| 72 | + level = "info" |
| 73 | + } |
| 74 | + |
| 75 | + // Setup logger (JSON for daemon, text for terminal) |
| 76 | + logger := stdioproxy.SetupLogger(stdioproxy.ParseLogLevel(level)) |
| 77 | + slog.SetDefault(logger) |
| 78 | + |
| 79 | + logger.Info("starting stdio-proxy", |
| 80 | + "addr", bindAddr, |
| 81 | + "command", subArgs[0], |
| 82 | + "args", subArgs[1:], |
| 83 | + ) |
| 84 | + |
| 85 | + // Create process manager |
| 86 | + pm := stdioproxy.NewProcessManager(stdioproxy.ProcessManagerConfig{ |
| 87 | + Command: subArgs[0], |
| 88 | + Args: subArgs[1:], |
| 89 | + WorkDir: *workDir, |
| 90 | + Logger: logger, |
| 91 | + }) |
| 92 | + |
| 93 | + // Start the subprocess |
| 94 | + if err := pm.Start(ctx); err != nil { |
| 95 | + return fmt.Errorf("start subprocess: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Ensure cleanup on shutdown |
| 99 | + defer func() { |
| 100 | + if err := pm.Stop(); err != nil { |
| 101 | + logger.Warn("error stopping subprocess", "error", err) |
| 102 | + } |
| 103 | + }() |
| 104 | + |
| 105 | + // Create proxy server |
| 106 | + proxyServer := stdioproxy.NewProxyServer(pm, logger) |
| 107 | + |
| 108 | + // Create HTTP handler |
| 109 | + handler, err := stdioproxy.NewHTTPHandler(pm, proxyServer, logger) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("create HTTP handler: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + // Start HTTP server (blocks until context is cancelled) |
| 115 | + return httputil.ListenAndServe(ctx, bindAddr, handler) |
| 116 | +} |
0 commit comments