Skip to content

mcp: preserve structured tool results in agent host apps - #340375

Merged
Connor Peet (connor4312) merged 1 commit into
mainfrom
connor4312/340165
Oct 7, 2026
Merged

Connor Peet (connor4312) merged 1 commit into
mainfrom
connor4312/340165

Conversation

@connor4312

Copy link
Copy Markdown
Member

mcp: preserve structured tool results in agent host apps

Pass SDK structuredContent directly through to MCP Apps.

  • Preserve structured results in live tool completions and restored history.
  • Forward structured-only results and results from tools with no input to MCP Apps.
  • Add tests for null-valued properties, empty objects, and live and restored results.

Fixes #340165

(Commit message generated by Copilot)

Co-authored-by: Copilot [email protected]

Validation:

  • npm run build-fast -- --client-only — passed.
  • ./scripts/test.sh --reporter dot --run src/vs/platform/agentHost/test/node/copilotAgentSession.test.ts --run src/vs/platform/agentHost/test/node/mapSessionEvents.test.ts --run src/vs/workbench/contrib/chat/test/browser/agentSessions/stateToProgressAdapter.test.ts — 1,301 tests passed.
  • npm run typecheck-client — passed.
  • npm run eslint -- src/vs/platform/agentHost/node/copilot/copilotAgentSession.ts src/vs/platform/agentHost/node/copilot/mapSessionEvents.ts src/vs/platform/agentHost/test/node/copilotAgentSession.test.ts src/vs/platform/agentHost/test/node/mapSessionEvents.test.ts src/vs/workbench/contrib/chat/browser/agentSessions/agentHost/stateToProgressAdapter.ts src/vs/workbench/contrib/chat/test/browser/agentSessions/stateToProgressAdapter.test.ts — passed.
  • Pre-commit hygiene — passed.

Live provider and MCP App webview flows were not exercised.

Pass SDK structuredContent directly through to MCP Apps.

- Preserve structured results in live tool completions and restored history.
- Forward structured-only results and results from tools with no input to MCP Apps.
- Add tests for null-valued properties, empty objects, and live and restored results.

Fixes #340165

(Commit message generated by Copilot)

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 7, 2026 21:42
@connor4312
Connor Peet (connor4312) enabled auto-merge (squash) October 7, 2026 21:42
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Screenshot Changes

Base: 20f57d8a Current: fc5426c9

Changed (1)

sessions/chatCompositeBar/OverflowingTabs/Light
Before After
before after

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.

🟢 Approval recommended

The focused implementation covers both live and restored paths with comprehensive boundary tests.

0 open findings

What changed in this PR

Preserves MCP structured tool results across Agent Host live execution, history restoration, and MCP App notifications.

Changes:

  • Propagates SDK structuredContent into tool-call state.
  • Supports structured-only results and tools without input.
  • Adds live/restored coverage for empty objects, null properties, and failures.

No actionable defects found.

File Description
copilotAgentSession.ts Preserves live structured results.
mapSessionEvents.ts Restores structured results from history.
stateToProgressAdapter.ts Forwards structured results to MCP Apps.
copilotAgentSession.test.ts Tests live SDK result mapping.
mapSessionEvents.test.ts Tests restored result mapping.
stateToProgressAdapter.test.ts Tests live/restored MCP output combinations.

🧠 Review effort: Balanced


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

@connor4312
Connor Peet (connor4312) merged commit 3129341 into main Oct 7, 2026
36 checks passed
@connor4312
Connor Peet (connor4312) deleted the connor4312/340165 branch October 7, 2026 22:06
@vs-code-engineering vs-code-engineering Bot added this to the 1.142.0 milestone Oct 7, 2026
Vritant Bhardwaj (vritant24) added a commit that referenced this pull request Oct 7, 2026
<!-- Thank you for submitting a Pull Request. Please:
* Read our Pull Request guidelines:

https://github.com/microsoft/vscode/wiki/How-to-Contribute#pull-requests
* Associate an issue with the Pull Request.
* Ensure that the code is up-to-date with the `main` branch.
* Include a description of the proposed changes and how to test them.
-->

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 (#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.
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.

MCP Apps: Agent Host drops structuredContent from tool-result notifications

3 participants