Skip to content

Commit cc8a4fa

Browse files
rmasterssparfenyuk
andauthored
feat: Allow passing through all environment variables to server (sparfenyuk#27)
This PR adds the ability to pass through all environment variables to the server, using the flag `--pass-environment` (default off). This change allows a compose spec like this to access all environment variables from an env_file (as well as PATH): ```yaml server-brave-search: image: mcp-proxy:latest env_file: .brave.env # ENTRYPOINT from custom SSE-only image, for context entrypoint: ["mcp-proxy", "--sse-host=0.0.0.0", "--sse-port=8080", "--pass-environment", "--"] command: ["npx", "--yes", "@modelcontextprotocol/server-brave-search"] ``` The motivation behind this is I am developing in Docker Compose, with a custom client and multiple servers. I made a debian-based mcp-proxy image, with an entrypoint that set some SSE settings. When it came to adding brave search, I spent a bit of time struggling against var interpolation and an overridden entrypoint (cause: you can't access env vars in entrypoints, I think). I figured passing through all environment variables would be easier (and safe in a container). I started adding tests for this, but found myself refactoring \_\_main\_\_.py quite a bit to make it more testable. I've left that for now in the aim of an easy-to-review PR, but am happy to continue that effort if you'd like? Essentially breaking out the argument parser and the SSE client/server parts into functions and validating their config. Thanks again for this tool - it's made adding support for stdio servers in our client much easier! --------- Co-authored-by: Sergey Parfenyuk <[email protected]>
1 parent b84e774 commit cc8a4fa

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ This mode requires the `--sse-port` argument to be set. The `--sse-host` argumen
104104

105105
Arguments
106106

107-
| Name | Required | Description | Example |
108-
| ---------------- | -------------------------- | ---------------------------------------------------------------- | -------------------- |
109-
| `command_or_url` | Yes | The command to spawn the MCP stdio server | uvx mcp-server-fetch |
110-
| `--sse-port` | No, random available | The SSE server port to listen on | 8080 |
111-
| `--sse-host` | No, `127.0.0.1` by default | The host IP address that the SSE server will listen on | 0.0.0.0 |
112-
| `--env` | No | Additional environment variables to pass to the MCP stdio server | FOO=BAR |
107+
| Name | Required | Description | Example |
108+
| -------------------- | -------------------------- | ---------------------------------------------------------------- | -------------------- |
109+
| `command_or_url` | Yes | The command to spawn the MCP stdio server | uvx mcp-server-fetch |
110+
| `--sse-port` | No, random available | The SSE server port to listen on | 8080 |
111+
| `--sse-host` | No, `127.0.0.1` by default | The host IP address that the SSE server will listen on | 0.0.0.0 |
112+
| `--env` | No | Additional environment variables to pass to the MCP stdio server | FOO=BAR |
113+
| `--pass-environment` | No | Pass through all environment variables when spawning the server | --no-pass-environment |
113114

114115
### 2.2 Example usage
115116

@@ -181,7 +182,7 @@ docker run -t ghcr.io/sparfenyuk/mcp-proxy:v0.3.2-alpine --help
181182
## Command line arguments
182183

183184
```bash
184-
usage: mcp-proxy [-h] [-H KEY VALUE] [-e KEY VALUE] [--sse-port SSE_PORT] [--sse-host SSE_HOST] [command_or_url] [args ...]
185+
usage: mcp-proxy [-h] [-H KEY VALUE] [-e KEY VALUE] [--sse-port SSE_PORT] [--sse-host SSE_HOST] [--pass-environment] [command_or_url] [args ...]
185186

186187
Start the MCP proxy in one of two possible modes: as an SSE or stdio client.
187188

@@ -199,6 +200,8 @@ stdio client options:
199200
args Any extra arguments to the command to spawn the server
200201
-e KEY VALUE, --env KEY VALUE
201202
Environment variables used when spawning the server. Can be used multiple times.
203+
--pass-environment, --no-pass-environment
204+
Pass through all environment variables when spawning the server.
202205

203206
SSE server options:
204207
--sse-port SSE_PORT Port to expose an SSE server on. Default is a random port

src/mcp_proxy/__main__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ def main() -> None:
7777
help="Environment variables used when spawning the server. Can be used multiple times.",
7878
default=[],
7979
)
80+
stdio_client_options.add_argument(
81+
"--pass-environment",
82+
action=argparse.BooleanOptionalAction,
83+
help="Pass through all environment variables when spawning the server.",
84+
default=False,
85+
)
8086

8187
sse_server_group = parser.add_argument_group("SSE server options")
8288
sse_server_group.add_argument(
@@ -112,10 +118,19 @@ def main() -> None:
112118

113119
# Start a client connected to the given command, and expose as an SSE server
114120
logging.debug("Starting stdio client and SSE server")
121+
122+
# The environment variables passed to the server process
123+
env: dict[str, str] = {}
124+
# Pass through current environment variables if configured
125+
if args.pass_environment:
126+
env.update(os.environ)
127+
# Pass in and override any environment variables with those passed on the command line
128+
env.update(dict(args.env))
129+
115130
stdio_params = StdioServerParameters(
116131
command=args.command_or_url,
117132
args=args.args,
118-
env=dict(args.env),
133+
env=env,
119134
)
120135
sse_settings = SseServerSettings(
121136
bind_host=args.sse_host,

0 commit comments

Comments
 (0)