fix(generate): strip env: prefix for Claude Code MCP env refs#71
Conversation
Claude Code's .mcp.json only expands ${VAR} / ${VAR:-default}, not the
${env:VAR} form. buildClaudeCodeMcpEntry emitted mcp.env verbatim, so
direct (non-proxy) servers received the literal string ${env:VAR} as
their token/URL and failed to authenticate — the proxy masked this by
expanding ${env:VAR} itself. Apply stripEnvPrefix (already used for the
Kiro editor target) in both the CLI and core builders, covering the npx
and docker paths.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughClaude Code MCP generation now strips the CLI-style ChangesMCP environment reference normalization
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/cli/src/commands/generate.ts (1)
501-503: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider importing
stripEnvPrefixandbuildClaudeCodeMcpEntryfrom core instead of duplicating.Both
stripEnvPrefix(line 505) andbuildClaudeCodeMcpEntry(line 479) are duplicated between the CLI andpackages/core/src/prompt-builders.ts, where they are already exported. This PR applies the same fix to both copies, which is correct but widens the duplication surface — future divergences will require synchronized edits in two files.If the CLI cannot depend on core for this path, consider extracting these into a shared utility module.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/cli/src/commands/generate.ts` around lines 501 - 503, Replace the duplicated stripEnvPrefix and buildClaudeCodeMcpEntry implementations in the CLI with imports from packages/core/src/prompt-builders.ts, reusing the exported helpers. Update all call sites in generate.ts and remove the local definitions; only introduce a shared utility if the CLI cannot depend on core.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/cli/src/commands/generate.ts`:
- Around line 501-503: Replace the duplicated stripEnvPrefix and
buildClaudeCodeMcpEntry implementations in the CLI with imports from
packages/core/src/prompt-builders.ts, reusing the exported helpers. Update all
call sites in generate.ts and remove the local definitions; only introduce a
shared utility if the CLI cannot depend on core.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 39c52b06-ee86-4e9e-885a-ebd82cb2a322
📒 Files selected for processing (2)
packages/cli/src/commands/generate.tspackages/core/src/prompt-builders.ts
Problem
Claude Code's
.mcp.jsonexpands only${VAR}and${VAR:-default}— not${env:VAR}(that's Cursor/Kiro-CLI syntax).buildClaudeCodeMcpEntryemittedmcp.envverbatim, so direct (non-proxy) MCP servers received the literal string${env:VAR}as their token/URL and failed to authenticate (e.g. HubSpot 401, Dokploy "Invalid URL"). The proxy hid this because it expands${env:VAR}internally — so anything moved out of the proxy silently broke.Fix
Apply
stripEnvPrefix(already used for the Kiro editor target) inbuildClaudeCodeMcpEntry, covering both thenpxanddockerpaths, in both the CLI (generate.ts) and core (prompt-builders.ts) copies. Result: Claude gets${VAR}, which it expands from its process env.Verified
packages/clitsc clean; fullpnpm build(tsup + DTS) green;pnpm test22/22.arvore-hub: rewriting${env:VAR}→${VAR}+ providing the vars made 22 direct servers connect (HubSpot, Dokploy, MySQL, Postgres, etc.) that were failing.Note
Consumers still need the env vars present in Claude Code's process (Claude does not auto-load
.env) — via a launched-with-env shell or asettings.local.jsonenvblock. This PR fixes the syntax so those values actually resolve.🤖 Generated with Claude Code
Summary by CodeRabbit
${env:VAR_NAME}format are now converted correctly for both Docker-based and npx-based MCP configurations.