Skip to content

Commit 07e1f2e

Browse files
feat: Allow passing environment variables to stdio command
This commit adds a new `--env` (or `-e`) flag to the `stdio` command. This allows the user to pass environment variables to the server process that is started by the command. The implementation appends the provided environment variables to the existing environment of the `mcp-cli` process, ensuring that the child process inherits the parent's environment as well as the new variables. The README.md file has been updated to document this new flag and its usage.
1 parent 3980eb5 commit 07e1f2e

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ The `mcp-cli` tool has three main commands, one for each transport protocol.
4747
Connect to an MCP server that communicates over standard input/output.
4848

4949
```sh
50-
mcp-cli stdio "<command-to-start-server>"
50+
mcp-cli stdio --env "VAR=value" --env "ANOTHER_VAR=another_value" "<command-to-start-server>"
5151
```
5252

53+
The `--env` (or `-e`) flag can be used multiple times to pass environment variables to the server process.
54+
5355
**Example:**
5456

5557
```sh
56-
mcp-cli stdio "python /path/to/mcp/server.py"
58+
mcp-cli stdio -e "API_KEY=12345" "python /path/to/mcp/server.py"
5759
```
5860

5961
### `sse`

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func Execute() {
3737

3838
func init() {
3939
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
40+
stdioCmd.Flags().StringSliceP("env", "e", []string{}, "Environment variables to pass to the command")
4041
}
4142

4243
var stdioCmd = &cobra.Command{
@@ -49,11 +50,15 @@ var stdioCmd = &cobra.Command{
4950
log.Printf("Command: %s", command)
5051
}
5152

53+
env, _ := cmd.Flags().GetStringSlice("env")
54+
5255
ctx := context.Background()
5356
client := mcp.NewClient(&mcp.Implementation{Name: "mcp-cli", Version: "v0.1.0"}, nil)
5457

5558
cmdParts := strings.Fields(command)
56-
transport := &mcp.CommandTransport{Command: exec.Command(cmdParts[0], cmdParts[1:]...)}
59+
execCmd := exec.Command(cmdParts[0], cmdParts[1:]...)
60+
execCmd.Env = append(os.Environ(), env...)
61+
transport := &mcp.CommandTransport{Command: execCmd}
5762
session, err := client.Connect(ctx, transport, nil)
5863
if err != nil {
5964
log.Fatalf("Failed to connect to stdio server: %v", err)

0 commit comments

Comments
 (0)