fix(mcp): evaluate environment variables at transport connection time#94167
fix(mcp): evaluate environment variables at transport connection time#94167lakshitsoni26 wants to merge 1 commit into
Conversation
Fixes openclaw#93144 Prior to this change, templates in MCP config fields (such as `env` and `headers`) were resolved during Gateway startup. As a result, any dynamic rotation of underlying environment variables required a full Gateway restart. This commit shifts the environment variable substitution logic to the MCP transport connection layer. We now intercept the SDK's fetch invocations (for HTTP/SSE) and child process spawn routines (for stdio), dynamically substituting variables based on the live `process.env` state at the exact moment of request, cleanly solving the credential rotation limits.
|
Codex review: needs real behavior proof before merge. Reviewed June 24, 2026, 10:35 AM ET / 14:35 UTC. Summary PR surface: Source +69. Total +69 across 5 files. Reproducibility: yes. at source level: current main resolves config env at activation and passes fixed headers/env into transports. I did not run a live Gateway-to-MCP reproduction in this read-only review. Review metrics: 1 noteworthy metric.
Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Proof guidance:
Risk before merge
Maintainer options:
Next step before merge
Security Review findings
Review detailsBest possible solution: Land a maintainer-approved dynamic MCP credential contract that preserves missing-env diagnostics, covers the intended transport granularity, and includes focused tests plus redacted real Gateway proof. Do we have a high-confidence way to reproduce the issue? Yes at source level: current main resolves config env at activation and passes fixed headers/env into transports. I did not run a live Gateway-to-MCP reproduction in this read-only review. Is this the best way to solve the issue? Unclear as merge-ready. The SDK fetch hook is a plausible seam, but this patch changes credential timing and missing-env behavior without a maintainer-approved contract, regression coverage, or real Gateway proof. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 374076b5a8c5. Label changesLabel justifications:
Evidence reviewedPR surface: Source +69. Total +69 across 5 files. View PR surface stats
Security concerns:
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
Summary
What problem does this PR solve?
${ENV_VAR}templates in MCP config fields were resolved during Gateway startup.Why does this matter now?
What is the intended outcome?
What is intentionally out of scope?
What does success look like?
process.envduring an active transport session and seeing the new token applied on the next request.What should reviewers focus on?
substitutingFetchwrapper insrc/agents/mcp-transport.ts.src/plugins/loader.ts.Linked context
Which issue does this close?
Closes #93144
Which issues, PRs, or discussions are related?
Related #N/A
Was this requested by a maintainer or owner?
No
Real behavior proof (required for external PRs)
${DYNAMIC_SECRET_TOKEN}in MCP transports without restarting.substitutingFetchwrapper behavior.TOKEN_VERSION_1, and the subsequent fetch usesTOKEN_VERSION_2_ROTATEDafterprocess.envwas updated in-memory.TOKEN_VERSION_1.Tests and validation
Which commands did you run?
Ran a local validation script simulating the
substitutingFetchlayer against an active Node environment.What regression coverage was added or updated?
No permanent regression tests were added. The logic relies on the existing
resolveConfigEnvVarscoverage, combined with the real behavior proof attached above to prove the interceptor works.What failed before this fix, if known?
Transport requests would use stale, startup-time environment variables indefinitely.
If no test was added, why not?
The fix is an architectural repositioning of existing, heavily-tested parsing logic (
resolveConfigEnvVars). A manual proof script was used to verify the integration, which is attached as a screenshot above.Risk checklist
Did user-visible behavior change? (
Yes/No)No (backend substitution behavior only)
Did config, environment, or migration behavior change? (
Yes/No)Yes
Did security, auth, secrets, network, or tool execution behavior change? (
Yes/No)Yes (Tokens are now dynamically refreshed at request-time)
What is the highest-risk area?
The highest risk is that some config validations that expect fully resolved URLs/Headers might be disrupted if they occur too early.
How is that risk mitigated?
By ensuring we still apply
resolveConfigEnvVarsdynamically via the HTTP proxy layer immediately beforefetchfires, maintaining API contract integrity.Current review state
What is the next action?
Maintainer review and merge.
What is still waiting on author, maintainer, CI, or external proof?
Waiting for CI to pass and maintainers to review.
Which bot or reviewer comments were addressed?
N/A