Skip to content

fix(config): keep ${VAR} references when a command edits a config file - #1765

Merged
edenreich merged 2 commits into
mainfrom
fix/keep-env-references
Nov 8, 2026
Merged

edenreich merged 2 commits into
mainfrom
fix/keep-env-references

Conversation

@edenreich

@edenreich edenreich commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1764

Summary

Every infer mcp command that saves mcp.yaml, and every infer agents command that saves agents.yaml, wrote the file back with each ${VAR} reference replaced by the variable's value. They edited the copy loaded to run with, which LoadYAML had already passed through os.ExpandEnv. A header written as x-api-key: ${AGENTMAIL_API_KEY} ended up as the plain key on disk in a 0644 file, or empty when the variable was unset, and the MCP supervisor then skipped the server.

Re-reading the file at save time would not help, because UpdateEntry receives a whole entry read from the expanded copy. An edit now starts from a copy loaded as written.

Changes

  • config/utils/yamlfile.go:
    • LoadYAML keeps expanding, for running. LoadYAMLAsWritten reads a file as written, for editing. Both share one loader.
    • A save keeps the file's mode, so a file its owner tightened to 0600 stays that way. A new file still gets 0644.
  • config/mcp.go, config/agents.go:
    • LoadMCP and LoadAgents load to run with, and their entries refuse to save with an error that says why, instead of writing values over references.
    • LoadMCPForEdit and LoadAgentsForEdit load as written and can save.
    • The edit commands use the edit loaders: infer mcp add, update, remove, enable, disable, enable-global and disable-global, and infer agents add, update and remove. Listing, status, start and stop keep the expanded copy.
  • config/plugins.go, config/keybindings.go, config/sandbox.go: a run reads these from the merged config, so their loaders are only used to edit and now load as written. ResolveDir expands a plugins dir itself. A saved sandbox grant keeps ${HOME} in the paths already there.
  • docs/mcp-integration.md, docs/agents-configuration.md: edits keep ${VAR} references and the file's mode.

Tests

  • cmd/mcp/mcp_test.go (new) reproduces the issue for all seven infer mcp edit commands, run as infer mcp <edit> through a fresh root.NewCommand() with MCP_CHECK_KEY set:
    • The root's PersistentPreRunE loads the config into a Viper of its own, as a real run does.
    • HOME and the working directory are temp dirs, and every INFER_* variable is blanked. So neither this machine's config, the repository's .infer, nor the developer's environment reaches a run.
    • Each case checks that the reference survives, the value never lands in the file, and the 0600 mode stays.
    • Against unmodified main, all seven fail with the key written into mcp.yaml.
  • Config tests cover:
    • an agent's environment reference surviving an update, and the expanded copy refusing to save;
    • LoadMCP refusing to save and leaving the file untouched;
    • ${HOME} surviving a saved sandbox grant;
    • the plugins dir expanding;
    • LoadYAMLAsWritten keeping a reference through a save, and the file mode kept or defaulted.
  • Existing tests that create, update or delete entries move to the edit loaders. The expansion tests stay on the run loaders.

Verification

  • task precommit:run passes (mod tidy, mocks, fmt, lint with 0 issues, markdownlint).
  • go test ./... passes.
  • task test:e2e passes in 307s.

Trade-offs

  • A non-string field written as a reference, such as port: ${MCP_PORT}, now fails to parse in the edit commands. Before, an edit wrote the expanded value over it. Running is unchanged.
  • A sandbox grant compares paths as written, so approving /Users/me/repos beside an existing ${HOME}/repos entry adds a second entry rather than matching the first.

Cross-repo impact

infer mcp and infer agents edited the copy of their file loaded to run with,
whose ${VAR} references were already expanded, and saved it back. A header
written as x-api-key: ${AGENTMAIL_API_KEY} ended up as the plain key on disk,
or empty when the variable was unset.

An edit now starts from a copy loaded as written. LoadMCP and LoadAgents keep
expanding for running and refuse to save, while LoadMCPForEdit and
LoadAgentsForEdit back the edit commands. plugins.yaml, keybindings.yaml and
sandbox.yaml are read from the merged config to run, so their loaders, used
only to edit, now load as written too. A sandbox grant keeps ${HOME} in the
paths already there.

A save keeps the file's mode, so a file its owner tightened to 0600 stays that
way.

Closes #1764
@edenreich
edenreich requested a review from a team as a code owner November 8, 2026 19:07
@github-project-automation github-project-automation Bot moved this to Todo in 2026 Nov 8, 2026
The regression test built the mcp subtree on its own, so the root's
PersistentPreRunE never loaded the config. Each case now runs
infer mcp <edit> through a fresh root.NewCommand(), whose State owns a
Viper of its own, with SetArgs, SetOut and ExecuteContext(t.Context()).
HOME and the working directory point at temp dirs and every INFER_*
variable is blanked, so neither this machine's config, the repository's
.infer nor the developer's environment reaches a run.
@edenreich
edenreich merged commit d0732cc into main Nov 8, 2026
1 check passed
@edenreich
edenreich deleted the fix/keep-env-references branch November 8, 2026 19:31
@edenreich edenreich moved this from Todo to Done in 2026 Nov 8, 2026
edenreich added a commit that referenced this pull request Nov 8, 2026
…1767)

## Summary

A Kubernetes workspace names the hosted MCP servers of this machine's
`~/.infer/mcp.yaml` its pods get, and the adapter writes them into each
new Orchestrator:

```yaml
# ~/.infer/mcp.yaml
servers:
  - name: agentmail
    enabled: true
    url: https://mcp.agentmail.to/mcp
    headers:
      x-api-key: ${AGENTMAIL_API_KEY}

# ~/.infer/daemon.yaml
workspaces:
  - name: code-reviewer
    kind: kubernetes
    namespace: agents
    image: ghcr.io/inference-gateway/cli:latest
    secrets:
      - code-reviewer-credentials
    mcp_servers:
      - agentmail
```

- `mcp_servers` is accepted on a Kubernetes workspace, also one placed
on a cluster through `compute`. Any other kind fails validation, like
`secrets`.
- When a session's Orchestrator is created, each named server goes into
`spec.mcp.servers` with its name, URL and headers, and
`spec.mcp.enabled` is set.
- `mcp.yaml` is read as written (`LoadMCPForEdit`), so a header stays a
`${VAR}` reference the pod expands from its own `secrets`. A header that
holds a value rather than a reference is refused with an error naming
the server and the header, never the value, so no secret lands in a
Kubernetes object.
- A name `mcp.yaml` lacks and a container server (`run: true`) fail with
an error naming the server before the Orchestrator is created.
- An older operator types `spec.mcp.servers` entries as strings and
refuses the create as invalid. That now also reads as the existing "may
predate ..., upgrade it" error, naming v0.33.0.
- The servers are read when the Orchestrator is created, like `secrets`,
so running sessions keep theirs.

Closes #1760

## Draft: waiting on the operator

This writes `spec.mcp.servers` entries with a `name`, a `url` and
`headers`, the shape inference-gateway/operator#324 gives the field,
which no operator release has yet. Until it ships, a workspace with
`mcp_servers` fails its teleport with the upgrade error and every other
workspace is unaffected. `newFieldsOperatorVersion` is v0.33.0, the next
operator release, which `binding_service` already needs. It has to
follow the release operator#324 actually lands in.

## Tests

- `TestCreateGivesThePodTheNamedMCPServersAsWritten` runs `create`
against a fake cluster: no servers leaves `spec.mcp` unset, named
servers carry their URLs and header references (a URL built from scheme,
host, port and path included), and a missing name, a container server
and a header holding a value fail without creating anything. The
header's variable is set in the test's environment, and its value never
reaches the Orchestrator.
- `TestCreateNamesTheOperatorVersionMCPServersNeed` checks the upgrade
error when an older operator refuses the entries as invalid. The 422
there is modeled on the apiserver's schema error, not captured from a
live older operator.
- Config tests cover `mcp_servers` on a local workspace and on a
workspace placed through `compute`.
- `task test:e2e`, `task precommit:run` and `go test ./...` pass. The
e2e suite does not run Kubernetes workspaces, and no cluster with an
operator that renders hosted servers exists yet.

## Example and docs

- `examples/session-teleport` gains a `k8s-mcp` workspace. It gets the
demo MCP server from `examples/mcp`, deployed into the cluster with
`task mcp:install`, behind an `x-api-key: ${DEMO_MCP_KEY}` header the
pod fills from the `demo-mcp-credentials` Secret. `task mcp:status` runs
`infer mcp status` in the pod. The walkthrough states it needs operator
v0.33.0 or newer.
- `docs/daemon.md` documents `mcp_servers` beside `secrets`, with the
AgentMail example.

## Known gaps

- A refusal fails after the session's TLS Secret exists, as a missing
`secrets` entry already does. A retry reuses it.
- A `${VAR}` in a server's `port` still cannot be read as written, the
gap noted in #1765.

## Cross-repo impact

- `operator`: inference-gateway/operator#324 renders `spec.mcp.servers`
into the pod's `mcp.yaml`. This PR depends on it.
- `desktop`: inference-gateway/desktop#480 writes `mcp_servers` under a
workspace in `daemon.yaml`, the shape this reads.
- `docs`: inference-gateway/docs#1146 documents `mcp_servers` on
Kubernetes workspaces.
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.250.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] infer mcp edits write expanded ${VAR} values into mcp.yaml

1 participant