claude-code - 💡(How to fix) Fix [BUG] claude mcp add -e API_KEY=xxx my-server -- npx my-mcp-server fails [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#46121Fetched 2026-04-11 06:28:31
View on GitHub
Comments
2
Participants
2
Timeline
6
Reactions
0
Author
Timeline (top)
labeled ×4commented ×2

Error Message

Error Messages/Logs

Code Example

claude mcp add -e API_KEY=xxx my-server -- npx my-mcp-server

---

Invalid environment variable format: my-server, environment variables should be added as: -e KEY1=value1 -e KEY2=value2

---

claude mcp add my-server -- npx my-mcp-server

---

claude -d --verbose mcp add -e API_KEY=xxx my-server -- npx my-mcp-server 
Invalid environment variable format: my-server, environment variables should be added as: -e KEY1=value1 -e KEY2=value2
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

When trying to add a local MCP via

claude mcp add -e API_KEY=xxx my-server -- npx my-mcp-server

I get the following

Invalid environment variable format: my-server, environment variables should be added as: -e KEY1=value1 -e KEY2=value2

Just

claude mcp add my-server -- npx my-mcp-server

works fine.

The behaviour occurs in 2.1.100 and 2.1.89.

What Should Happen?

When passing environment variables via the CLI, they should be added to ~/.claude.json.

Error Messages/Logs

claude -d --verbose mcp add -e API_KEY=xxx my-server -- npx my-mcp-server 
Invalid environment variable format: my-server, environment variables should be added as: -e KEY1=value1 -e KEY2=value2

Steps to Reproduce

  1. claude mcp add -e API_KEY=xxx my-server -- npx my-mcp-server

Claude Model

None

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

2.1.100

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

Additional Information

No response

extent analysis

TL;DR

  • Reorder the command to place the -e option after the my-server argument to correctly add environment variables.

Guidance

  • Verify that the issue is resolved by running the command with the -e option after the server name: claude mcp add my-server -e API_KEY=xxx -- npx my-mcp-server.
  • Check the ~/.claude.json file to ensure the environment variable is added correctly.
  • Test the command with multiple environment variables to confirm the fix works as expected: claude mcp add my-server -e API_KEY=xxx -e KEY2=value2 -- npx my-mcp-server.

Example

claude mcp add my-server -e API_KEY=xxx -- npx my-mcp-server

Notes

  • This fix assumes the issue is due to the order of the command options.
  • If the issue persists, further investigation into the claude command's parsing logic may be necessary.

Recommendation

  • Apply workaround: reorder the command options to place -e after the server name, as this resolves the immediate issue and allows for correct environment variable addition.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING