@@ -179,6 +179,14 @@ export class MainThreadMcp extends Disposable implements MainThreadMcpShape {
179179 this . _servers . get ( id ) ?. pushMessage ( message ) ;
180180 }
181181
182+ async $getTokenForProviderId ( id : number , providerId : string , scopes : string [ ] , options : IMcpAuthenticationOptions = { } ) : Promise < string | undefined > {
183+ const server = this . _serverDefinitions . get ( id ) ;
184+ if ( ! server ) {
185+ return undefined ;
186+ }
187+ return this . _getSessionForProvider ( server , providerId , scopes , undefined , options . errorOnUserInteraction ) ;
188+ }
189+
182190 async $getTokenFromServerMetadata ( id : number , authDetails : IMcpAuthenticationDetails , { errorOnUserInteraction, forceNewRegistration } : IMcpAuthenticationOptions = { } ) : Promise < string | undefined > {
183191 const server = this . _serverDefinitions . get ( id ) ;
184192 if ( ! server ) {
@@ -202,7 +210,18 @@ export class MainThreadMcp extends Disposable implements MainThreadMcpShape {
202210 }
203211 providerId = provider . id ;
204212 }
205- const sessions = await this . _authenticationService . getSessions ( providerId , resolvedScopes , { authorizationServer : authorizationServer } , true ) ;
213+
214+ return this . _getSessionForProvider ( server , providerId , resolvedScopes , authorizationServer , errorOnUserInteraction ) ;
215+ }
216+
217+ private async _getSessionForProvider (
218+ server : McpServerDefinition ,
219+ providerId : string ,
220+ scopes : string [ ] ,
221+ authorizationServer ?: URI ,
222+ errorOnUserInteraction : boolean = false
223+ ) : Promise < string | undefined > {
224+ const sessions = await this . _authenticationService . getSessions ( providerId , scopes , { authorizationServer } , true ) ;
206225 const accountNamePreference = this . authenticationMcpServersService . getAccountPreference ( server . id , providerId ) ;
207226 let matchingAccountPreferenceSession : AuthenticationSession | undefined ;
208227 if ( accountNamePreference ) {
@@ -213,12 +232,12 @@ export class MainThreadMcp extends Disposable implements MainThreadMcpShape {
213232 if ( sessions . length ) {
214233 // If we have an existing session preference, use that. If not, we'll return any valid session at the end of this function.
215234 if ( matchingAccountPreferenceSession && this . authenticationMCPServerAccessService . isAccessAllowed ( providerId , matchingAccountPreferenceSession . account . label , server . id ) ) {
216- this . authenticationMCPServerUsageService . addAccountUsage ( providerId , matchingAccountPreferenceSession . account . label , resolvedScopes , server . id , server . label ) ;
235+ this . authenticationMCPServerUsageService . addAccountUsage ( providerId , matchingAccountPreferenceSession . account . label , scopes , server . id , server . label ) ;
217236 return matchingAccountPreferenceSession . accessToken ;
218237 }
219238 // If we only have one account for a single auth provider, lets just check if it's allowed and return it if it is.
220239 if ( ! provider . supportsMultipleAccounts && this . authenticationMCPServerAccessService . isAccessAllowed ( providerId , sessions [ 0 ] . account . label , server . id ) ) {
221- this . authenticationMCPServerUsageService . addAccountUsage ( providerId , sessions [ 0 ] . account . label , resolvedScopes , server . id , server . label ) ;
240+ this . authenticationMCPServerUsageService . addAccountUsage ( providerId , sessions [ 0 ] . account . label , scopes , server . id , server . label ) ;
222241 return sessions [ 0 ] . accessToken ;
223242 }
224243 }
@@ -237,7 +256,7 @@ export class MainThreadMcp extends Disposable implements MainThreadMcpShape {
237256 throw new UserInteractionRequiredError ( 'authentication' ) ;
238257 }
239258 session = provider . supportsMultipleAccounts
240- ? await this . authenticationMcpServersService . selectSession ( providerId , server . id , server . label , resolvedScopes , sessions )
259+ ? await this . authenticationMcpServersService . selectSession ( providerId , server . id , server . label , scopes , sessions )
241260 : sessions [ 0 ] ;
242261 }
243262 else {
@@ -248,7 +267,7 @@ export class MainThreadMcp extends Disposable implements MainThreadMcpShape {
248267 do {
249268 session = await this . _authenticationService . createSession (
250269 providerId ,
251- resolvedScopes ,
270+ scopes ,
252271 {
253272 activateImmediate : true ,
254273 account : accountToCreate ,
@@ -263,7 +282,7 @@ export class MainThreadMcp extends Disposable implements MainThreadMcpShape {
263282
264283 this . authenticationMCPServerAccessService . updateAllowedMcpServers ( providerId , session . account . label , [ { id : server . id , name : server . label , allowed : true } ] ) ;
265284 this . authenticationMcpServersService . updateAccountPreference ( server . id , providerId , session . account ) ;
266- this . authenticationMCPServerUsageService . addAccountUsage ( providerId , session . account . label , resolvedScopes , server . id , server . label ) ;
285+ this . authenticationMCPServerUsageService . addAccountUsage ( providerId , session . account . label , scopes , server . id , server . label ) ;
267286 return session . accessToken ;
268287 }
269288
0 commit comments