@@ -3,11 +3,15 @@ import * as path from "path"
33
44import type { Mock } from "vitest"
55import type { ExtensionContext , Uri } from "vscode"
6+ import type { Client } from "@modelcontextprotocol/sdk/client/index.js"
7+ import type { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
68
79import type { ClineProvider } from "../../../core/webview/ClineProvider"
10+ import type { SecretStorageService } from "../SecretStorageService"
811
912import type { McpHub as McpHubType , McpConnection , ConnectedMcpConnection , DisconnectedMcpConnection } from "../McpHub"
1013import { ServerConfigSchema , McpHub } from "../McpHub"
14+ import { McpOAuthClientProvider } from "../McpOAuthClientProvider"
1115import { OAUTH_FLOW_TIMEOUT_MS } from "../constants"
1216import { t } from "../../../i18n"
1317
@@ -128,6 +132,18 @@ vi.mock("@modelcontextprotocol/sdk/client/index.js", () => ({
128132 Client : vi . fn ( ) ,
129133} ) )
130134
135+ vi . mock ( "@modelcontextprotocol/sdk/client/streamableHttp.js" , ( ) => ( {
136+ StreamableHTTPClientTransport : vi . fn ( ) ,
137+ } ) )
138+
139+ vi . mock ( "../McpOAuthClientProvider" , ( ) => ( {
140+ McpOAuthClientProvider : {
141+ create : vi . fn ( ) ,
142+ isKnownNonOAuth : vi . fn ( ) . mockReturnValue ( false ) ,
143+ clearNonOAuthCache : vi . fn ( ) ,
144+ } ,
145+ } ) )
146+
131147// Mock chokidar
132148vi . mock ( "chokidar" , ( ) => ( {
133149 default : {
@@ -392,6 +408,111 @@ describe("McpHub", () => {
392408 } )
393409 } )
394410
411+ describe ( "static OAuth connection wiring" , ( ) => {
412+ it ( "passes static OAuth options, performs discovery, and skips dynamic registration" , async ( ) => {
413+ const streamableModule = await import ( "@modelcontextprotocol/sdk/client/streamableHttp.js" )
414+ const StreamableHTTPClientTransport = streamableModule . StreamableHTTPClientTransport as ReturnType <
415+ typeof vi . fn
416+ >
417+ const clientModule = await import ( "@modelcontextprotocol/sdk/client/index.js" )
418+ const Client = clientModule . Client as ReturnType < typeof vi . fn >
419+ const mockAuthProvider = {
420+ registerClientIfNeeded : vi . fn ( ) ,
421+ close : vi . fn ( ) . mockResolvedValue ( undefined ) ,
422+ }
423+ vi . mocked ( McpOAuthClientProvider . create ) . mockResolvedValue (
424+ mockAuthProvider as unknown as McpOAuthClientProvider ,
425+ )
426+ const mockTransport = { close : vi . fn ( ) . mockResolvedValue ( undefined ) }
427+ StreamableHTTPClientTransport . mockImplementation ( function ( ) {
428+ return mockTransport as unknown as StreamableHTTPClientTransport
429+ } )
430+ Client . mockImplementation ( function ( ) {
431+ return {
432+ connect : vi . fn ( ) . mockResolvedValue ( undefined ) ,
433+ close : vi . fn ( ) . mockResolvedValue ( undefined ) ,
434+ getInstructions : vi . fn ( ) ,
435+ request : vi . fn ( ) . mockResolvedValue ( { tools : [ ] , resources : [ ] , resourceTemplates : [ ] } ) ,
436+ } as unknown as Client
437+ } )
438+ const secretStorage = {
439+ hasOAuthData : vi . fn ( ) . mockResolvedValue ( false ) ,
440+ } as unknown as SecretStorageService
441+ const oauth = {
442+ clientId : "registered-client" ,
443+ scope : "openid offline_access" ,
444+ redirectUri : "http://localhost:33418/callback" ,
445+ resource : false as const ,
446+ }
447+ vi . mocked ( fs . readFile ) . mockResolvedValue (
448+ JSON . stringify ( {
449+ mcpServers : {
450+ "static-oauth-server" : {
451+ type : "streamable-http" ,
452+ url : "https://example.com/mcp" ,
453+ oauth,
454+ } ,
455+ } ,
456+ } ) ,
457+ )
458+
459+ const hub = new McpHub ( mockProvider as ClineProvider , secretStorage )
460+ await hub . waitUntilReady ( )
461+
462+ expect ( McpOAuthClientProvider . create ) . toHaveBeenCalledWith (
463+ "https://example.com/mcp" ,
464+ secretStorage ,
465+ "static-oauth-server" ,
466+ { skipDiscovery : false , ...oauth } ,
467+ )
468+ expect ( mockAuthProvider . registerClientIfNeeded ) . not . toHaveBeenCalled ( )
469+ expect ( StreamableHTTPClientTransport ) . toHaveBeenCalledWith (
470+ new URL ( "https://example.com/mcp" ) ,
471+ expect . objectContaining ( { authProvider : mockAuthProvider } ) ,
472+ )
473+ } )
474+
475+ it ( "closes a static OAuth provider when the hub is disposed during provider creation" , async ( ) => {
476+ const streamableModule = await import ( "@modelcontextprotocol/sdk/client/streamableHttp.js" )
477+ const StreamableHTTPClientTransport = streamableModule . StreamableHTTPClientTransport as ReturnType <
478+ typeof vi . fn
479+ >
480+ let resolveProvider ! : ( provider : McpOAuthClientProvider ) => void
481+ vi . mocked ( McpOAuthClientProvider . create ) . mockReturnValueOnce (
482+ new Promise < McpOAuthClientProvider > ( ( resolve ) => {
483+ resolveProvider = resolve
484+ } ) ,
485+ )
486+ const mockAuthProvider = {
487+ registerClientIfNeeded : vi . fn ( ) ,
488+ close : vi . fn ( ) . mockResolvedValue ( undefined ) ,
489+ } as unknown as McpOAuthClientProvider
490+ const secretStorage = {
491+ hasOAuthData : vi . fn ( ) . mockResolvedValue ( false ) ,
492+ } as unknown as SecretStorageService
493+ vi . mocked ( fs . readFile ) . mockResolvedValue (
494+ JSON . stringify ( {
495+ mcpServers : {
496+ "static-oauth-server" : {
497+ type : "streamable-http" ,
498+ url : "https://example.com/mcp" ,
499+ oauth : { clientId : "registered-client" } ,
500+ } ,
501+ } ,
502+ } ) ,
503+ )
504+
505+ const hub = new McpHub ( mockProvider as ClineProvider , secretStorage )
506+ await Promise . resolve ( )
507+ await hub . dispose ( )
508+ resolveProvider ( mockAuthProvider )
509+ await hub . waitUntilReady ( )
510+
511+ expect ( mockAuthProvider . close ) . toHaveBeenCalledTimes ( 1 )
512+ expect ( StreamableHTTPClientTransport ) . not . toHaveBeenCalled ( )
513+ } )
514+ } )
515+
395516 describe ( "Discriminated union type handling" , ( ) => {
396517 it ( "should create connected connections with proper type" , async ( ) => {
397518 // Mock StdioClientTransport
@@ -2669,7 +2790,7 @@ describe("McpHub", () => {
26692790 const serverName = "oauth-server"
26702791 const serverUrl = "https://example.com/mcp"
26712792 const source = "global" as const
2672- const config = { url : serverUrl }
2793+ const config = ServerConfigSchema . parse ( { type : "streamable-http" , url : serverUrl } )
26732794
26742795 let mockAuthProvider : any
26752796 let mockTransport : any
@@ -3055,6 +3176,30 @@ describe("McpHub", () => {
30553176 expect ( mockAuthProvider . close ) . toHaveBeenCalled ( )
30563177 } )
30573178
3179+ it ( "should close the provider when the hub is disposed during the initial token read" , async ( ) => {
3180+ let resolveTokenRead ! : ( value : null ) => void
3181+ mockSecretStorage . getOAuthData . mockReturnValueOnce (
3182+ new Promise < null > ( ( resolve ) => {
3183+ resolveTokenRead = resolve
3184+ } ) ,
3185+ )
3186+
3187+ const flowPromise = mcpHub [ "_initiateOAuthFlow" ] (
3188+ serverName ,
3189+ source ,
3190+ config ,
3191+ mockAuthProvider ,
3192+ mockTransport ,
3193+ mockConnection ,
3194+ )
3195+ mcpHub [ "isDisposed" ] = true
3196+ resolveTokenRead ( null )
3197+ await flowPromise
3198+
3199+ expect ( mockAuthProvider . close ) . toHaveBeenCalledTimes ( 1 )
3200+ expect ( vsc . window . withProgress ) . not . toHaveBeenCalled ( )
3201+ } )
3202+
30583203 it ( "should not reconnect when hub is disposed while cross-window tokens arrive" , async ( ) => {
30593204 vi . useFakeTimers ( )
30603205 vsc . window . showInformationMessage . mockImplementation ( ( ) => {
0 commit comments