@@ -268,6 +268,19 @@ export class MCPOAuthHandler {
268
268
/** Add state parameter with flowId to the authorization URL */
269
269
authorizationUrl . searchParams . set ( 'state' , flowId ) ;
270
270
logger . debug ( `[MCPOAuth] Added state parameter to authorization URL` ) ;
271
+
272
+ if ( resourceMetadata ?. resource ) {
273
+ authorizationUrl . searchParams . set ( 'resource' , resourceMetadata . resource ) ;
274
+ } else {
275
+ logger . warn (
276
+ `[MCPOAuth] Resource metadata missing 'resource' property for ${ serverName } . ` +
277
+ 'This can cause issues with some Authorization Servers who expect a "resource" parameter.' ,
278
+ ) ;
279
+ }
280
+
281
+ logger . debug (
282
+ `[MCPOAuth] Added resource parameter to authorization URL: ${ resourceMetadata . resource } ` ,
283
+ ) ;
271
284
} catch ( error ) {
272
285
logger . error ( `[MCPOAuth] startAuthorization failed:` , error ) ;
273
286
throw error ;
@@ -330,12 +343,27 @@ export class MCPOAuthHandler {
330
343
throw new Error ( 'Invalid flow metadata' ) ;
331
344
}
332
345
346
+ let resource ;
347
+ try {
348
+ if ( metadata . resourceMetadata ?. resource ) {
349
+ resource = new URL ( metadata . resourceMetadata . resource ) ;
350
+ logger . debug ( `[MCPOAuth] Resource URL for flow ${ flowId } : ${ resource . toString ( ) } ` ) ;
351
+ }
352
+ } catch ( error ) {
353
+ logger . warn (
354
+ `[MCPOAuth] Invalid resource URL format for flow ${ flowId } : '${ metadata . resourceMetadata ! . resource } '. ` +
355
+ `Error: ${ error instanceof Error ? error . message : 'Unknown error' } . Proceeding without resource parameter.` ,
356
+ ) ;
357
+ resource = undefined ;
358
+ }
359
+
333
360
const tokens = await exchangeAuthorization ( metadata . serverUrl , {
334
361
metadata : metadata . metadata as unknown as SDKOAuthMetadata ,
335
362
clientInformation : metadata . clientInfo ,
336
363
authorizationCode,
337
364
codeVerifier : metadata . codeVerifier ,
338
365
redirectUri : metadata . clientInfo . redirect_uris ?. [ 0 ] || this . getDefaultRedirectUri ( ) ,
366
+ resource : resource ,
339
367
} ) ;
340
368
341
369
logger . debug ( '[MCPOAuth] Raw tokens from exchange:' , {
0 commit comments