Skip to content

agentHost: route Copilot Connector Sign In through Connector authorization - #342479

Merged
Vritant Bhardwaj (vritant24) merged 2 commits into
mainfrom
agents/fix-vscode-copilot-connector-authentication
Oct 19, 2026
Merged

Vritant Bhardwaj (vritant24) merged 2 commits into
mainfrom
agents/fix-vscode-copilot-connector-authentication

Conversation

@vritant24

Copy link
Copy Markdown
Member

Fixes #341123

Problem

The runtime sends Copilot Connector MCP requests with only the SDK session's GitHub credential (the token set through gitHubAuth.setCredentials / gitHubToken). When the gateway returned 403 insufficient_scope, the runtime fell back to a generic mcp.oauth_required (reason=upscope). VS Code's Sign In, and silent auto-auth, then created a GitHub session with only write:plugin_gateway_connections and forwarded it as an MCP token. The runtime ignores that token for connector servers, so the server restarted with the old credential, got another 403, and looped. Meanwhile the client logged "MCP authentication succeeded".

A second issue made the working path unreachable. CopilotConnectorsService.authorize() returned early if any same-account session had the connector scope. That includes the narrow session created by MCP Sign In, which never qualifies as the Copilot credential. So Authorize did nothing in exactly the state the issue logs show.

Changes

Sign In for connector servers goes through Connector authorization

  • authenticateMcpServer detects connector-owned servers. Detection uses new host-published vscode.mcpServerConnectorName metadata, or a write:plugin_gateway_connections challenge as a fallback.
  • For those servers it calls ICopilotConnectorsService.authorize(), forwards the Copilot credential (which prefers the full-scope session), then force-reconciles that connector through a new narrow refreshAgentHostSessions(connectorName) API.
  • resolveMcpServerAuthentication refuses connector-scope challenges. So neither the silent nor the interactive path creates a narrow session or forwards an MCP token for a connector.
  • authorize() now skips consent only when a session has the connector scope and every scope of the default session.

The agent host does not answer connector upscope requests with tokens

  • CopilotAgentSession.resolveMcpAuthentication skips connector-owned pending requests.
  • Before a forced reconcile, affected servers are shown as Starting. After the reconcile succeeds, pending requests from before it are cancelled; requests raised by the restarted server are kept. If a turn is active, refresh is deferred, so stale requests are released straight away instead.
  • Fix MCP auth spinner after rejected token #340547's rejected-token suppression is unchanged.

Truthful reporting

  • The forwarding log now says the token was forwarded, not that authentication succeeded.
  • authenticateMcpServer reports success only once the server leaves AuthRequired. This is a bounded wait (60s) that is cancellation-safe and cleans up its listeners. Connector servers must reach Ready.
  • If a connector returns the same challenge after restarting, Sign In returns false and shows a localized message. It asks the user to authorize Copilot connectors with the same GitHub account they use for Copilot.
  • Behaviour change for non-connector servers: if a forwarded token leaves the challenge unchanged, Sign In now waits up to 60s and returns false. Previously it returned true right after forwarding.

ConnectorStatus.authorizationRequirement

  • Reconcile now reports connector ownership for each runtime server, and authorizationRequirement.
  • A connector server in needs-auth with no mcp.oauth_required is shown as AuthRequired (insufficient scope). This covers the planned runtime fix that will stop emitting mcp.oauth_required for connector 403s.

Notes for reviewers

  • Same token string after Authorize (Refresh Agent Host sessions after connector authorization #338934): _applyGitHubToken skips setCredentials. That should be fine, because the runtime re-reads the session credential on every request and the forced reconcile always restarts the server. I have not confirmed against the live gateway that GitHub widens scope on the same token string.
  • Forced reconcile: the runtime's replace_managed_servers already cancels its pending OAuth before restarting the server, so cancelling VS Code's stale entry afterwards is safe.
  • Mission Control / scoped credentials: updateGitHubCredentials is only called for static-token sessions, so nothing throws. The challenge comes back and Sign In returns false with the message.
  • Known gaps:
    • Connector refresh only exists for the local agent host, so on a remote host Sign In authorizes but usually times out.
    • Servers identified only by the scope fallback can't get a targeted reconcile.
    • The end-to-end integration test (coexisting sessions, Sign In through tool discovery) was not added. It needs a real runtime and gateway.

How to test

  1. Use a default GitHub session that lacks write:plugin_gateway_connections. Optionally add a narrow session that has only that scope.
  2. Open an Agents session with the WorkIQ / Kusto connectors and click Sign In on a connector row.
  3. Expect a consent prompt for the default scopes plus write:plugin_gateway_connections on the same account. Then the connector restarts, reaches Ready, and lists its tools. The gateway's x-oauth-scopes should include the connector scope.

Validation run locally:

Command Result
./scripts/test.sh --run agentHostAuth, copilotConnectorsService, agentHostCustomizationService, copilotAgentSession, copilotSessionLauncher, agentMetaReaders 1621 passing
./scripts/test.sh --run mcpCustomizationController, sessions agentHostCustomizationService, agentHostChatContribution, workbenchAgentHostCustomizationService, chatMcpAuthenticationContentPart, chatToolAuthenticationSubPart 781 passing
./scripts/test.sh --run src/vs/workbench/contrib/chat/test/browser/agentSessions/agentHostClientTools.test.ts 73 passing
npm run eslint -- <changed files> passed
npm run typecheck-client passed
npm run valid-layers-check passed

Not run: integration tests (copilotConnectors.integrationTest.ts was only updated for the new reconcile result shape) and a manual end-to-end run against the live gateway.

…ation

Connector-owned MCP servers authenticate only with the SDK session's GitHub credential, so a token forwarded in answer to mcp.oauth_required was ignored and Sign In looped on insufficient_scope while logging success. Sign In now authorizes Copilot Connectors for the session account, forwards the Copilot credential, and force-reconciles the Connector; connector requests are never satisfied by an MCP token, stale requests are released after reconciliation, and success is reported only once the server leaves the authentication-required state. Also reads ConnectorStatus.authorizationRequirement.

Fixes #341123

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 19, 2026 04:56

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

Credential selection can switch accounts, and refresh cancellation or delays can leave authentication stuck.

3 open issues
What changed in this PR

Routes Agent Host connector Sign In through Copilot Connector authorization rather than generic MCP token delivery.

Changes:

  • Publishes connector ownership and authorization requirements.
  • Forwards Copilot credentials and refreshes connector servers.
  • Adds authentication verification and regression coverage.
File Description
src/​vs/​workbench/​contrib/​chat/​test/​browser/​aiCustomization/​copilotConnectorsService.test.ts Tests narrow-session authorization.
src/​vs/​workbench/​contrib/​chat/​test/​browser/​agentSessions/​agentHostCustomizationService.test.ts Tests connector Sign In and verification.
src/​vs/​workbench/​contrib/​chat/​test/​browser/​agentSessions/​agentHostAuth.test.ts Tests credential selection and token suppression.
src/​vs/​workbench/​contrib/​chat/​browser/​aiCustomization/​copilotConnectorsService.ts Requires full scopes and exposes refresh.
src/​vs/​workbench/​contrib/​chat/​browser/​agentSessions/​agentHost/​agentHostSessionHandler.ts Skips connector auto-authentication.
src/​vs/​workbench/​contrib/​chat/​browser/​agentSessions/​agentHost/​agentHostCustomizationService.ts Routes authorization and verifies outcomes.
src/​vs/​workbench/​contrib/​chat/​browser/​agentSessions/​agentHost/​agentHostAuth.ts Forwards Copilot credentials instead of connector MCP tokens.
src/​vs/​sessions/​common/​agentHostSessionsProvider.ts Exposes connector ownership.
src/​vs/​platform/​agentHost/​test/​node/​providerIntegration/​copilotConnectors.integrationTest.ts Adapts reconciliation assertions.
src/​vs/​platform/​agentHost/​test/​node/​copilotSessionLauncher.test.ts Updates connector metadata fixtures.
src/​vs/​platform/​agentHost/​test/​node/​copilotAgentSession.test.ts Tests connector authentication lifecycle.
src/​vs/​platform/​agentHost/​node/​shared/​mcpCustomizationController.ts Publishes connector metadata.
src/​vs/​platform/​agentHost/​node/​copilot/​copilotSessionLauncher.ts Passes connector reconciliation metadata.
src/​vs/​platform/​agentHost/​node/​copilot/​copilotConnectors.ts Returns ownership and authorization status.
src/​vs/​platform/​agentHost/​node/​copilot/​copilotAgentSession.ts Handles connector challenges and forced refreshes.
src/​vs/​platform/​agentHost/​common/​meta/​vscode/​mcpCustomizationMeta.ts Defines connector metadata readers and writers.
src/​vs/​platform/​agentHost/​common/​meta/​mcpCustomizationMeta.ts Exports connector metadata helpers.

Review effort: Balanced

Forward the Copilot credential of the account that authorized Connectors without falling back to another account, restore the authentication challenge when a forced Connector refresh is cancelled before reconciliation, and release Sign In on its deadline or disposal while the refresh is still pending.

Co-authored-by: Copilot <[email protected]>
@vritant24
Vritant Bhardwaj (vritant24) marked this pull request as ready for review October 19, 2026 18:42
@vritant24
Vritant Bhardwaj (vritant24) merged commit 4f24082 into main Oct 19, 2026
49 checks passed
@vritant24
Vritant Bhardwaj (vritant24) deleted the agents/fix-vscode-copilot-connector-authentication branch October 19, 2026 20:37
@vs-code-engineering vs-code-engineering Bot added this to the 1.144.0 milestone Oct 19, 2026
Elijah King (eli-w-king) added a commit that referenced this pull request Oct 19, 2026
Authentication forwarding reads the default account since #342104, which
merged right after the Connector authorization tests in #342479, so those
tests fail on main. Same change as #342706.

Co-authored-by: Copilot <[email protected]>
@vritant24 Vritant Bhardwaj (vritant24) added the ~release-cherry-pick Trigger: cherry-pick this PR to the latest release branch label Oct 19, 2026
@vs-code-engineering vs-code-engineering Bot added release-cherry-pick Automated cherry-pick between release and main branches and removed ~release-cherry-pick Trigger: cherry-pick this PR to the latest release branch labels Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-cherry-pick Automated cherry-pick between release and main branches

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copilot Connectors: WorkIQ and Kusto Sign In still fails with insufficient_scope after #340547

3 participants