Skip to content

feat(mcp): expand ${VAR} env vars in mcpServers header values (#2148)#2233

Merged
esengine merged 1 commit into
esengine:mainfrom
nianyi778:feat/mcp-header-env-expand
May 29, 2026
Merged

feat(mcp): expand ${VAR} env vars in mcpServers header values (#2148)#2233
esengine merged 1 commit into
esengine:mainfrom
nianyi778:feat/mcp-header-env-expand

Conversation

@nianyi778

Copy link
Copy Markdown

问题

关闭 #2148。从 Claude Code 迁移到 Reasonix 时,.mcp.json 里 headers 常见写法:

{
  "mcpServers": {
    "my-server": {
      "url": "https://api.example.com/mcp",
      "transport": "sse",
      "headers": {
        "Authorization": "Bearer ${MY_TOKEN}",
        "X-Api-Key": "${env:ANOTHER_KEY}"
      }
    }
  }
}

Reasonix 把这些 ${VAR} 字面量原样传给服务端,导致认证失败。

修改

normalizeMcpConfig 处理 SSE/streamable-HTTP headers 时,用 expandEnvInRecord() 展开 ${VAR_NAME}${env:VAR_NAME} 两种格式。变量未设置则原样保留(保证连接时能看到具体错误)。

验证

新增测试:展开、env: 前缀、static 值不受影响、未设置变量原样保留。

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@nianyi778

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3fd8ca2f4b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/config.ts Outdated
if (!record) return undefined;
const out: Record<string, string> = {};
for (const [k, v] of Object.entries(record)) {
out[k] = v.replace(/\$\{(?:env:)?([^}]+)\}/g, (match, name: string) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Support Claude default env syntax in headers

Claude Code's .mcp.json expansion syntax includes ${VAR:-default} for headers (see https://code.claude.com/docs/en/mcp#environment-variable-expansion-in-mcpjson), but this regex captures API_KEY:-fallback as the variable name and looks up that literal key in process.env. Any migrated header such as Authorization: "Bearer ${API_KEY:-dev-token}" is therefore left unexpanded even when API_KEY is set, so the remote MCP server receives the placeholder and authentication still fails.

Useful? React with 👍 / 👎.

…ne#2148)

Claude Code .mcp.json files commonly use ${TOKEN_NAME} or ${env:TOKEN_NAME}
in header values (e.g. Authorization: Bearer ${API_KEY}). Reasonix was
passing these through as literal strings, making migration from Claude Code
require manual header editing.

Add expandEnvInRecord() which resolves ${VAR} and ${env:VAR} patterns in
header records at normalization time. Unset variables are left as-is.
@nianyi778 nianyi778 force-pushed the feat/mcp-header-env-expand branch from 3fd8ca2 to 25e2be3 Compare May 29, 2026 06:11
@nianyi778

Copy link
Copy Markdown
Author

Fixed in 25e2be37. Regex now parses ${VAR:-default} and ${env:VAR:-default}: captures the var name up to :- and the default value separately. Var set → expanded value; var unset + default → default; var unset + no default → left as-is.

Two new test cases added.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@esengine esengine left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean migration-compat feature. expandEnvInRecord handles ${VAR} / ${env:VAR} / ${VAR:-default} in mcpServers header values, expanding only from process.env (the user's own env → their own config, so no injection surface), and leaves an unset-without-default reference as-is so it surfaces a meaningful error at bridge-connect time rather than silently becoming empty. That's the right call. Test covers it, CI green. Closes the Claude-Code .mcp.json migration gap (#2148). Merging.

@esengine esengine merged commit e95c93f into esengine:main May 29, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants