Skip to content

fix: preserve Claude stdio MCP env#1224

Open
imk1t wants to merge 4 commits intomicrosoft:mainfrom
imk1t:fix/claude-stdio-env-1222
Open

fix: preserve Claude stdio MCP env#1224
imk1t wants to merge 4 commits intomicrosoft:mainfrom
imk1t:fix/claude-stdio-env-1222

Conversation

@imk1t
Copy link
Copy Markdown

@imk1t imk1t commented May 9, 2026

Description

Preserve env values for self-defined stdio MCP servers when generating Claude Code .mcp.json.

Root cause: self-defined stdio MCP entries pass env into the shared adapter formatting path as a plain dictionary. The Copilot runtime-substitution path already handled that shape, but the legacy literal-resolution path used by Claude Code only handled registry-style environment variable lists, so the dictionary resolved to an empty object.

This keeps the fix scoped to that literal-resolution path and adds a Claude Code regression test proving that .mcp.json preserves the declared environment variable.

Fixes #1222

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Validation run:

uv run --extra dev pytest tests/unit/test_claude_mcp.py tests/unit/test_build_mcp_entry.py tests/test_codex_empty_string_and_defaults.py -q
uv run --project <repo> --extra dev apm install --target claude --no-policy
uv run --extra dev pytest tests/unit tests/test_console.py -x
uv run --extra dev ruff check src/ tests/
uv run --extra dev ruff format --check src/ tests/

The CLI smoke test used a generic self-defined stdio MCP entry with DEMO_ENV=demo-value and verified that the generated .mcp.json contains:

"env": {
  "DEMO_ENV": "demo-value"
}

@imk1t imk1t marked this pull request as ready for review May 9, 2026 15:07
@imk1t imk1t requested a review from danielmeppiel as a code owner May 9, 2026 15:07
Copilot AI review requested due to automatic review settings May 9, 2026 15:07
@imk1t
Copy link
Copy Markdown
Author

imk1t commented May 9, 2026

@microsoft-github-policy-service agree

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a Claude Code-specific regression where self-defined stdio MCP servers lost their declared env when generating .mcp.json, by teaching the legacy (literal-resolution) env resolver to handle the self-defined env dict shape.

Changes:

  • Extend _resolve_environment_variables to resolve env provided as a plain {NAME: value} dict when runtime env substitution is disabled (Claude Code path).
  • Add a unit regression test asserting that .mcp.json preserves env for a self-defined stdio server.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/unit/test_claude_mcp.py Adds regression coverage for preserving env in Claude Code .mcp.json for self-defined stdio servers.
src/apm_cli/adapters/client/copilot.py Updates legacy env-variable resolution to support dict-shaped env inputs used by self-defined stdio MCP entries.

Comment on lines +791 to +802
if isinstance(env_vars, dict):
resolved = {}
for name, value in env_vars.items():
if not name:
continue
if isinstance(value, str):
resolved[name] = self._resolve_env_variable(
name, value, env_overrides=env_overrides
)
elif value is not None:
resolved[name] = value
return resolved
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

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.

[BUG] Claude .mcp.json drops env for self-defined stdio MCP servers

2 participants