Skip to content

Commit 65a16b5

Browse files
Merge pull request #11 from UnitVectorY-Labs/env
Add support for environment variables passed to token command
2 parents 0469be7 + 68ebe01 commit 65a16b5

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ The following attributes can be specified in the file:
2727
- `version`: The version of the MCP server
2828
- `url`: The URL of the GraphQL endpoint
2929
- `token_command`: The command to use to request the Bearer token for the `Authorization` header (optional)
30+
- `env`: A map of environment variables to pass to the token command (optional)
31+
- `env_passthrough`: If set to `true`, passes all environment variables used when invoking mcp-graphql-forge to the token command; if used in conjunction with `env`, the variables from `env` will take precedence (optional, defaults to `false`)
3032

3133
An example configuration would look like:
3234

example/forge.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@
22
name: "ExampleServer"
33
version: "0.1.0"
44
url: "https://api.github.com/graphql"
5-
token_command: "gh auth token"
5+
token_command: "gh auth token"
6+
7+
# All of the environment variables passed to mcp-graphql-forge will be passed when invoking the token command.
8+
# env_passthrough: true
9+
10+
# Specific environment variables to pass when invoking the token command.
11+
env:
12+
FOO: "BAR"

main.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"path/filepath"
1616
"runtime"
1717
"strconv"
18+
"strings"
1819

1920
"gopkg.in/yaml.v3"
2021

@@ -24,10 +25,12 @@ import (
2425

2526
// ForgeConfig holds global server settings
2627
type ForgeConfig struct {
27-
Name string `yaml:"name"`
28-
Version string `yaml:"version"`
29-
URL string `yaml:"url"`
30-
TokenCommand string `yaml:"token_command"`
28+
Name string `yaml:"name"`
29+
Version string `yaml:"version"`
30+
URL string `yaml:"url"`
31+
TokenCommand string `yaml:"token_command"`
32+
Env map[string]string `yaml:"env,omitempty"`
33+
EnvPassthrough bool `yaml:"env_passthrough,omitempty"`
3134
}
3235

3336
// ToolConfig holds one tool's YAML definition
@@ -142,6 +145,36 @@ func makeHandler(cfg ForgeConfig, tcfg ToolConfig) server.ToolHandlerFunc {
142145
cmd = exec.Command("sh", "-c", cfg.TokenCommand)
143146
}
144147

148+
// Build merged environment: start with os.Environ() if passthrough, else start empty,
149+
// then overlay values from cfg.Env to ensure overrides.
150+
var envList []string
151+
if cfg.EnvPassthrough {
152+
envList = os.Environ()
153+
} else {
154+
envList = []string{}
155+
}
156+
157+
for key, value := range cfg.Env {
158+
// Remove any existing entries for this key
159+
prefix := key + "="
160+
filtered := envList[:0]
161+
for _, e := range envList {
162+
if !strings.HasPrefix(e, prefix) {
163+
filtered = append(filtered, e)
164+
}
165+
}
166+
envList = append(filtered, fmt.Sprintf("%s=%s", key, value))
167+
}
168+
169+
cmd.Env = envList
170+
171+
if isDebug {
172+
log.Printf("Executing token command: %s", cfg.TokenCommand)
173+
if len(cmd.Env) > 0 {
174+
log.Printf("Environment variables: %v", cmd.Env)
175+
}
176+
}
177+
145178
// Only get a token if the command is specified
146179
out, err := cmd.Output()
147180
if err != nil {

0 commit comments

Comments
 (0)