Skip to content

chat: Migrate MCP servers that use settings to environment variables - #341349

Merged
Vritant Bhardwaj (vritant24) merged 10 commits into
mainfrom
agents/migrate-config-references-to-env-vars
Oct 13, 2026
Merged

Vritant Bhardwaj (vritant24) merged 10 commits into
mainfrom
agents/migrate-config-references-to-env-vars

Conversation

@vritant24

Copy link
Copy Markdown
Member

Part of #340161 (case B3). Follows #341224, which did the same for ${input:}.

The MCP customization migrator used to exclude any server that uses ${config:key}. It now migrates the reference to an environment variable set to the setting's current value, the same way inputs are migrated.

What changed

  • Name: derived from the setting key with getVariableName, e.g. mcp.command → MCP_COMMAND, with the existing MCP_ prefix for reserved names. A ${config:} with no key becomes MCP_CONFIG. A setting used several times maps to one variable.

  • Rewrite: same fields as inputs (stdio command, args and env values; remote URL and header values). Stdio servers get ${NAME} and an env entry with the value. Remote servers get ${NAME:-value}, or ${NAME} when the value is empty. Remote values are written without a round-trip check, so a rare value containing } is written as is.

  • Value: read in the folder scope VS Code resolves the server in. Strings and numbers are kept, numbers as text. These become an empty variable instead of excluding the server:

    • unset, unnamed, structured or boolean settings;
    • values that contain a variable;
    • for workspace servers, values that don't come from the workspace or folder settings.

    The last rule exists because <root>/.mcp.json is usually committed, and a value from the user's settings could be private, such as a token. User servers move to the user's own configuration and keep every value.

  • Exclusions: the existing collision checks now cover setting names too, against inputs, ${env:} names, other settings and existing env keys. A server is still excluded when a setting reference is in an env or header name, cwd, or a URL position the destination rejects, or when the value makes it too long. No new exclusion reason was needed.

  • Projection: VS Code resolves ${config:} for .vscode/mcp.json servers before forwarding them, so the projection holds the value. When VS Code can't resolve a setting, it forwards the server as written, and the planner compares against that. User servers are always forwarded unresolved.

  • Revalidation: the candidate records each setting's key, name and value (settingVariables). If a value changes between planning and writing, the migration fails with SourceChanged.

  • Migration view: a new localized warning: "Configs will become environment variables set to their current values. Later changes to the configs won't apply." The collision and name-position exclusion messages now mention settings.

Known limitation (tracked separately)

VS Code's own chat expands ${NAME} in stdio command/args only from its process environment, not from the server's env. A migrated reference there stays literal in VS Code's chat; Copilot fills it from env.

Open questions for review

  • Workspace servers leave extension defaults empty too, because those values aren't in the repo. They aren't private, so writing them may be preferable.
  • In multi-root workspaces, values from a .code-workspace file count as workspace settings, even though the file may be outside the repo.
  • The warning says "set to their current values" even when a workspace server's value is left empty.

How to test

  1. Add a server that uses ${config:...} to .vscode/mcp.json or the user mcp.json, e.g. "args": ["--port=${config:mcp.port}"], and set the setting in workspace or user settings.
  2. Open the customization migration view for an Agent Host Copilot session. The server is offered with the new warning.
  3. Migrate it. The destination contains ${MCP_PORT} with "env": { "MCP_PORT": "<value>" } for stdio, or ${MCP_PORT:-<value>} for remote. The value is empty for a workspace server whose setting is only in user settings.

Validation

  • npm run typecheck-client: passed
  • 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/chat/test/browser/aiCustomization/customizationMigration.test.ts --run src/vs/workbench/contrib/chat/test/browser/aiCustomization/aiCustomizationManagementEditor.test.ts: 207 passing
  • npm run eslint -- <changed files>: passed

`${config:key}` references in `.vscode/mcp.json` and user `mcp.json` servers
are now migrated instead of excluding the server (case B3 in #340161). Each
setting becomes an environment variable named after its key (for example
`mcp.command` -> `MCP_COMMAND`), set to the setting's current value: declared
in `env` for stdio servers and written as `${NAME:-value}` for remote servers.

- Read the value with the configuration resolver in the folder scope VS Code
  uses; only string and number values are migrated
- Apply the existing reserved-name, collision, name, URL, length and cwd rules
- Record the values on the candidate and fail with `sourceChanged` if a
  setting changes before writing
- Add the `unsupportedSettingValue` exclusion reason and a migration warning

Refs #340161

Co-authored-by: Copilot <[email protected]>
A `${config:key}` setting that is unset, not a string or number, or whose
value contains a variable no longer excludes the MCP server. It migrates to an
environment variable with an empty value instead, like an input without a
default. When VS Code can't resolve such a setting, it projects the server as
written, which the planner now accounts for.

`unsupportedSettingValue` now only covers a reference without a setting name
and a value that a remote `${NAME:-value}` reference can't carry.

Refs #340161

Co-authored-by: Copilot <[email protected]>
Matches the `${config:}` references in the MCP configuration.

Refs #340161

Co-authored-by: Copilot <[email protected]>
A setting's value in a remote server's `${NAME:-value}` reference is no
longer checked to read back exactly, so a rare value such as one with a `}`
no longer keeps the server from migrating. `unsupportedSettingValue` now only
covers a `${config:}` reference without a setting name.

Refs #340161

Co-authored-by: Copilot <[email protected]>
A `${config:}` reference without a setting name now migrates to an empty
`MCP_CONFIG` environment variable instead of excluding the server. With that,
every setting reference migrates, so the `unsupportedSettingValue` exclusion
reason is removed.

Refs #340161

Co-authored-by: Copilot <[email protected]>
The client never resolves user servers, so a setting it can't resolve only
means a `.vscode/mcp.json` server is projected as written. Also stop exporting
`getSettingVariableName`, which is only used in its module.

Refs #340161

Co-authored-by: Copilot <[email protected]>
A workspace server moves to `<root>/.mcp.json`, which is usually committed.
A `${config:}` setting's value is now only written there if it comes from the
workspace or folder settings, which are already in the repository. A value
from the user's settings, a policy or memory becomes an empty environment
variable instead. User servers, which move to the user's own configuration,
keep every value.

Refs #340161

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 13, 2026 18:00

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

Unresolved nested variables and local environment-value rewrites can produce excluded or ineffective migrations, while the warning overstates value preservation.

3 open findings
What changed in this PR

Migrates MCP ${config:...} references to portable environment variables while preserving current setting values where possible.

Changes:

  • Adds setting-variable analysis, collision detection, rewriting, and revalidation.
  • Adds migration warnings and updated exclusion messages.
  • Expands unit and integration-style migration coverage.
File Description
mcpServerCustomizationMigration.test.ts Tests setting migration and validation.
customizationMigrationServiceImpl.test.ts Tests end-to-end migration behavior.
customizationMigration.test.ts Tests candidate warnings.
customizationMigrationService.ts Adds setting-variable metadata.
mcpServerEnvironmentVariables.ts Adds setting names and collisions.
mcpServerCustomizationMigration.ts Implements setting migration.
customizationMigrationCategories.ts Adds the setting migration warning.
agentHostMcpServerMigrationProvider.ts Injects configuration and revalidates settings.

🧠 Review effort: Balanced


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

- Write a setting's value directly in a stdio server's `env` values, since
  those are expanded from the host's environment, not from the other `env`
  entries. Only settings that `command` or `args` read are declared in `env`.
- Accept variables that the client keeps as text when it resolves a setting's
  value, such as an `${input:}`, instead of rejecting the workspace server.
  Like the client, treat a setting whose resolution leaves a variable
  unresolved as one it can't resolve.
- Only say that configs are set to their current values when a value is
  copied, and explain when they become empty instead.

Refs #340161

Co-authored-by: Copilot <[email protected]>
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Base: 510fe67f Current: 3a814562

No screenshot changes.

… form

Empty environment variable rows showed `API_KEY` and `your-api-key`, which
suggested an API key even for unrelated variables, such as the empty ones the
MCP customization migration creates. They now show `Key` and `Value`.

Refs #340161

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants