Skip to content

mcp: migrate ${env:NAME} servers as ${NAME} references - #340381

Merged
Vritant Bhardwaj (vritant24) merged 3 commits into
mainfrom
agents/mcp-customization-env-var-migration
Oct 7, 2026
Merged

Vritant Bhardwaj (vritant24) merged 3 commits into
mainfrom
agents/mcp-customization-env-var-migration

Conversation

@vritant24

@vritant24 Vritant Bhardwaj (vritant24) commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Refs #340161 (task B1). Builds on #340369 (merged) and matches #340379.

Problem

The MCP customization migrator refused every server that used ${env:NAME}, so those servers stayed in .vscode/mcp.json and the user mcp.json.

Changes

Rewrite. ${env:NAME} becomes ${NAME}, never the variable's current value, in the fields that both the Copilot runtime and the local harness expand:

  • stdio command, args and env values;
  • the remote URL and header values.

Other portable variables (${workspaceFolder}, ${/}, …) are still resolved to literals exactly as before.

Implementation (mcpServerCustomizationMigration.ts):

  • analyzeSourceConfiguration replaces resolveSourceConfiguration. Planning, the check before writing, and rollback all use this one function, so they always agree.
  • Portable variables are resolved one at a time and ${env:} is skipped. Resolving ${env:FOO} to ${FOO} inside a ConfigurationResolverExpression would make it re-parse ${FOO} as a new, unknown variable.
  • Candidates get an optional migratedConfiguration: the content written to the destination. It's separate from projectedConfiguration, which still holds the current env values and is used to detect stale candidates.

Exclusions. These servers stay behind, each with a localized reason:

Case Reason
${env:} in an env or header name; neither the runtime nor the harness expands names (C4, C9) environmentVariableInName
Name isn't [A-Za-z_][A-Za-z0-9_]*, e.g. ${env:ProgramFiles(x86)}, ${env:my-var}, ${env:HOST:-x} invalidEnvironmentVariableName
Rewritten value is over 1000 UTF-16 code units; the runtime skips expansion above that environmentVariableValueTooLong
URL fails URL.canParse after the rewrite: a port, the scheme, or the whole URL (C7; same rule as #340379) unsupportedEnvironmentVariableInUrl
${env:} mixed with a non-portable variable (${input:}, ${config:}, ${userHome}, …), or ${env:} in cwd unrepresentableConfiguration (unchanged)
A referenced variable's value itself contains a ${...} variable: VS Code resolves it, but the destination keeps the value as is unrepresentableConfiguration

User scope (mcp.json → ~/.copilot/mcp-config.json):

  • User servers are forwarded with their variables unresolved. Those whose only compatibility problem is UnresolvedConfiguration can now be planned.
  • They're compared against the source with ${env:} left as written; any other variable still excludes the server.
  • CopilotMpcDiscoveryAdapter now sets environmentVariableExpansion, using the raw URL, so a migrated ${NAME} keeps working in VS Code's own chat.

Notes for reviewers

  • Behaviour change for unset variables (decision a): VS Code resolves an unset ${env:FOO} to "", but the runtime and the local harness leave an unset ${FOO} as literal text. We chose ${NAME} over ${NAME:-} for readability, for consistency with the issue's Decision, and because a host with a :- default is rejected (C7). D5/E3 will add the unset-variable warning.
  • Copilot user config expansion (decision b): existing literal ${FOO} strings in ~/.copilot/mcp-config.json are now expanded in VS Code, matching the Copilot runtime. Trust nonces are still computed from the unexpanded launch.
  • URLs are still blocked by E11 in practice: the forwarded projection builds url from launch.uri.toString(), which percent-encodes ${env:…}, so it never matches the source. The URL rewrite and C7 exclusion are implemented and tested, but real URL servers with ${env:} stay excluded (as unrepresentable) until E11 is fixed. Header values are unaffected.
  • No changes to parseWorkspaceRootMcpConfiguration or the chat.mcp.discovery.enabled default (mcp: discover Copilot CLI user MCP config by default without forwarding it #340181).

How to test

I ran each of these on this branch:

  • npm run build-fast -- --client-only: passed
  • ./scripts/test.sh --reporter dot --run src/vs/workbench/contrib/chat/test/browser/aiCustomization/mcpServerCustomizationMigration.test.ts --run src/vs/workbench/contrib/chat/test/browser/aiCustomization/customizationMigrationServiceImpl.test.ts --run src/vs/workbench/contrib/mcp/test/common/nativeMcpDiscoveryAdapters.test.ts --run src/vs/workbench/contrib/mcp/test/common/workspaceDotMcpDiscovery.test.ts --run src/vs/workbench/contrib/mcp/test/common/mcpEnvironmentVariableExpansion.test.ts --run src/vs/workbench/contrib/mcp/test/common/mcpRegistry.test.ts --run src/vs/platform/mcp/test/common/envVarResolution.test.ts --run src/vs/workbench/api/test/browser/mainThreadMcp.test.ts: passed, 226 passing
  • The same three migration/discovery files with --grep 'env:|Copilot user configuration': passed, 7 passing. These are all the new tests:
    • rewriting in every supported position, mixed with ${workspaceFolder}, a shared variable, and a value at exactly 1000 characters;
    • every exclusion case, checked at both planning and execution;
    • user-scope servers;
    • a source that changed between planning and writing;
    • rollback followed by a successful retry;
    • provider exclusion details;
    • Copilot adapter expansion.
  • npm run typecheck-client: passed
  • npm run eslint -- <8 changed files>: passed; the pre-commit hygiene check also passed.

The tests ran before merging #340375 (agent-host structured tool results) from main, which doesn't touch these files. Not tested manually end to end against a live Copilot agent-host session.

Manual check: in a workspace whose .vscode/mcp.json has a stdio server with "env": { "TOKEN": "${env:TOKEN}" }, run the customization migration from an agent-host Copilot session. The server should move to .mcp.json as "TOKEN": "${TOKEN}" with no secret value in the file, and should still start in VS Code chat.

Implements B1 of #340161. The MCP customization migrator refused every
server that used `${env:NAME}`, so those servers stayed in
`.vscode/mcp.json` and the user `mcp.json`.

The migrator now rewrites `${env:NAME}` to `${NAME}`, never to its current
value, in the fields that both the Copilot runtime and the local harness
expand: stdio `command`, `args` and `env` values, and the remote URL and
header values. Other portable variables (`${workspaceFolder}`, ...) are
still resolved to literals, one by one, since resolving `${env:}` inside a
ConfigurationResolverExpression would parse the rewritten `${NAME}` as a
new variable. Planning, the checks before writing and rollback share one
deterministic analysis. Candidates carry the rewritten configuration in
`migratedConfiguration`, separate from the projection used for staleness
checks.

Servers stay behind, with a specific exclusion reason, when `${env:}` is
in an env or header name, names something other than
`[A-Za-z_][A-Za-z0-9_]*`, makes a value longer than the runtime's
1000-character expansion limit, or sits in a URL position the runtime
rejects (C7). Servers that still use other non-portable variables stay
excluded as before.

User servers are forwarded with their variables unresolved, so those whose
only problem is unresolved variables are now planned too, and the Copilot
user configuration (`~/.copilot/mcp-config.json`) expands `${VAR}`
references in VS Code like a workspace `.mcp.json` does.

URLs with `${env:}` remain excluded in practice until E11: the forwarded
projection percent-encodes the reference and so never matches the source.

Refs #340161

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 7, 2026 22:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

URL compatibility and nested environment substitutions can diverge from Copilot runtime behavior.

3 open findings
What changed in this PR

Migrates MCP ${env:NAME} references into portable ${NAME} syntax while preserving unresolved values and validating unsupported cases.

Changes:

  • Adds environment-reference analysis, rewriting, and exclusions.
  • Enables expansion for Copilot user configuration.
  • Adds migration and discovery coverage.
File Description
mcpServerCustomizationMigration.ts Implements analysis and rewriting.
customizationMigrationService.ts Extends migration candidates and failures.
agentHostMcpServerMigrationProvider.ts Reports localized exclusion details.
nativeMcpDiscoveryAdapters.ts Enables Copilot configuration expansion.
mcpTypes.ts Updates expansion documentation.
mcpServerCustomizationMigration.test.ts Tests rewriting and exclusions.
customizationMigrationServiceImpl.test.ts Tests profile migration integration.
nativeMcpDiscoveryAdapters.test.ts Tests Copilot-only expansion.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

- Keep servers whose referenced environment variable values contain a
  variable. VS Code resolves those nested variables, but the destination
  substitutes `${NAME}` once and keeps the value verbatim, so the
  migrated server would behave differently.
- Describe the expansion limit without a unit: it is measured in UTF-16
  code units, not characters.

Refs #340161

Co-authored-by: Copilot <[email protected]>
@vritant24
Vritant Bhardwaj (vritant24) merged commit f21d032 into main Oct 7, 2026
55 of 56 checks passed
@vritant24
Vritant Bhardwaj (vritant24) deleted the agents/mcp-customization-env-var-migration branch October 7, 2026 23:05
@vs-code-engineering vs-code-engineering Bot added this to the 1.142.0 milestone Oct 7, 2026
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.

3 participants