Repository navigation
agentHost: route MCP auth during session initialization - #334593
Merged
Merged
Conversation
Track initializing Copilot sessions so host-supplied MCP tokens reach pending SDK authentication callbacks before live-session registration. Dispose tracked sessions during shutdown to unblock initialization. Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Regression coverage must exercise a real create/resume path to prevent recurrence.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review tier: Balanced
Findings: 1
New issues introduced by this change (1)
| Severity | Finding |
|---|---|
src/vs/platform/agentHost/test/node/copilotAgent.test.ts — This regression test calls _withInitializingSession directly, so it would still pass if any (or… |
What changed in this PR
Routes MCP authentication tokens to Copilot sessions during initialization and improves shutdown cleanup.
Changes:
- Tracks initializing sessions for token routing.
- Disposes or rejects sessions during shutdown.
- Adds authentication and cleanup regression tests.
| File | Description |
|---|---|
src/vs/platform/agentHost/test/node/copilotAgent.test.ts |
Adds regression tests, but does not exercise the production create/resume paths. |
src/vs/platform/agentHost/node/copilot/copilotAgent.ts |
Adds initializing-session lifecycle management and token routing. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Centralize session initialization and registration so new call paths cannot bypass MCP auth routing. Exercise concurrent create, provisional materialization, default and peer resume, scope rejection, and shutdown through real CopilotAgent paths. Co-authored-by: Copilot <[email protected]>
Keep initializing sessions owned and routable until atomic registration, and scope silent MCP authentication coalescing to each session so later challenges receive their own token delivery. Add create, materialize, resume, peer, concurrency, and shutdown regression coverage. Co-authored-by: Copilot <[email protected]>
Use resource-aware session identity for silent auth coalescing and make the pre-registration lifecycle phase explicit. Co-authored-by: Copilot <[email protected]>
Paul (pwang347)
marked this pull request as ready for review
September 8, 2026 17:44
Anthony Kim (anthonykim1)
approved these changes
Sep 8, 2026
Alexandru Dima (alexdima)
approved these changes
Sep 8, 2026
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.

Fixes #329223
Root cause
The Copilot runtime intentionally keeps host-injected MCP bearer tokens session-scoped: the embedding host owns refresh, revocation, persistence, and per-server consent. Agent Host therefore has to deliver the workbench's persisted grant to every SDK session that challenges.
Two races could lose that delivery:
CopilotAgentSessioncan issueonMcpAuthRequestwhileinitializeSession()is still running. The session was added to_chatEntriesBySdkIdonly after initialization completed, whilehandleAuthenticationToken()searched only that live map.Before/after evidence
Before — shipped Insiders, pre-fix
A real new Agent Host chat on the affected build immediately challenged again for an MCP whose VS Code authentication session and access grant already existed:
The same startup trace contained repeated
requires authentication→ renderer token-delivery pairs for other previously authorized MCPs. This shows that persistence was intact in the workbench, but the token still had to be recovered again for each new SDK session—and could be lost in the initialization/coalescing races above.After — this branch
Real signed-in Slack OAuth E2E
I loaded an isolated plugin containing only the
github-slackMCP definition, authorized it once through the real Slack browser OAuth flow, and then started two brand-new Agent Host sessions. Isolating the definition prevents a second Slack client registration from changing the provider identity during the test.Before the one-time authorization, VS Code correctly showed the server as requiring authentication. After authorization, both later sessions completed without an auth card, browser redirect, or other user interaction, and the server remained running:
The two later sessions are visible here as separate completed chats:
Each new SDK session still issued its expected session-scoped challenge. The workbench silently resolved that challenge from the persisted grant, and Slack reached
connected (ready)in 326 ms and 350 ms respectively:The separate workspace-local
vscode-automation-mcperror visible in the server list is unrelated to this test; the isolated test target isgithub-slack.Deterministic lifecycle regression coverage
The regression tests start real
CopilotAgentchat creation/resume paths with the fake SDK blocked insideonMcpAuthRequest, then deliver the persisted token through the real Agent Host authentication service:{ kind: 'token', accessToken: 'mcp-token' }and both become live from one host-side fan-out.Observed fixed-run result:
That final lifecycle matrix passed 30/30 repeated executions. No interactive Authenticate path is used in these successful cases.
Change
DisposableSetfrom SDK initialization until atomic ownership transfer to the live-session map.initializeSession()paths in one initialize/register helper so future paths cannot bypass tracking.No protocol, SDK/runtime token-storage, token refresh, scope, consent, or revocation semantics are changed.
Validation
npm run typecheck-clientnpm run hygiene(full repository)copilotAgent.test.ts: 363 passing, 1 pendingCopilotAgentSessionMCP inventory: 27 passingconnected (ready)in 326 ms and 350 msorigin/mainworktree.The public AHP E2E harness cannot inject
authenticatewhilecreateSessionis still in flight, so the exact race is covered at the strongest deterministic layer: realCopilotAgentcreate/resume paths with the SDK auth callback blocked until Agent Host delivers the token.