Repository navigation
mcp: migrate ${env:NAME} servers as ${NAME} references - #340381
Merged
Vritant Bhardwaj (vritant24) merged 3 commits intoOct 7, 2026
Merged
Vritant Bhardwaj (vritant24) merged 3 commits into
Vritant Bhardwaj (vritant24) merged 3 commits into
Conversation
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]>
…ion-env-var-migration
Contributor
There was a problem hiding this comment.
🟡 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]>
21 of 42 tasks
roblourens
approved these changes
Oct 7, 2026
Vritant Bhardwaj (vritant24)
deleted the
agents/mcp-customization-env-var-migration
branch
October 7, 2026 23:05
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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.jsonand the usermcp.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:command,argsandenvvalues;Other portable variables (
${workspaceFolder},${/}, …) are still resolved to literals exactly as before.Implementation (
mcpServerCustomizationMigration.ts):analyzeSourceConfigurationreplacesresolveSourceConfiguration. Planning, the check before writing, and rollback all use this one function, so they always agree.${env:}is skipped. Resolving${env:FOO}to${FOO}inside aConfigurationResolverExpressionwould make it re-parse${FOO}as a new, unknown variable.migratedConfiguration: the content written to the destination. It's separate fromprojectedConfiguration, which still holds the current env values and is used to detect stale candidates.Exclusions. These servers stay behind, each with a localized reason:
${env:}in an env or header name; neither the runtime nor the harness expands names (C4, C9)environmentVariableInName[A-Za-z_][A-Za-z0-9_]*, e.g.${env:ProgramFiles(x86)},${env:my-var},${env:HOST:-x}invalidEnvironmentVariableNameenvironmentVariableValueTooLongURL.canParseafter 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:}incwdunrepresentableConfiguration(unchanged)${...}variable: VS Code resolves it, but the destination keeps the value as isunrepresentableConfigurationUser scope (
mcp.json→~/.copilot/mcp-config.json):UnresolvedConfigurationcan now be planned.${env:}left as written; any other variable still excludes the server.CopilotMpcDiscoveryAdapternow setsenvironmentVariableExpansion, using the raw URL, so a migrated${NAME}keeps working in VS Code's own chat.Notes for reviewers
${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.${FOO}strings in~/.copilot/mcp-config.jsonare now expanded in VS Code, matching the Copilot runtime. Trust nonces are still computed from the unexpanded launch.urlfromlaunch.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.parseWorkspaceRootMcpConfigurationor thechat.mcp.discovery.enableddefault (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--grep 'env:|Copilot user configuration': passed, 7 passing. These are all the new tests:${workspaceFolder}, a shared variable, and a value at exactly 1000 characters;npm run typecheck-client: passednpm 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.jsonhas a stdio server with"env": { "TOKEN": "${env:TOKEN}" }, run the customization migration from an agent-host Copilot session. The server should move to.mcp.jsonas"TOKEN": "${TOKEN}"with no secret value in the file, and should still start in VS Code chat.