Skip to content

Commit baf3b4a

Browse files
authored
🔐 feat: Add Resource Parameter to OAuth Requests per MCP Spec (#8599)
1 parent e5d08cc commit baf3b4a

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"@langchain/core": "^0.3.62",
7373
"@librechat/agents": "^2.4.67",
7474
"@librechat/data-schemas": "*",
75-
"@modelcontextprotocol/sdk": "^1.13.3",
75+
"@modelcontextprotocol/sdk": "^1.16.0",
7676
"axios": "^1.8.2",
7777
"diff": "^7.0.0",
7878
"eventsource": "^3.0.2",

packages/api/src/mcp/oauth/handler.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ export class MCPOAuthHandler {
268268
/** Add state parameter with flowId to the authorization URL */
269269
authorizationUrl.searchParams.set('state', flowId);
270270
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+
);
271284
} catch (error) {
272285
logger.error(`[MCPOAuth] startAuthorization failed:`, error);
273286
throw error;
@@ -330,12 +343,27 @@ export class MCPOAuthHandler {
330343
throw new Error('Invalid flow metadata');
331344
}
332345

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+
333360
const tokens = await exchangeAuthorization(metadata.serverUrl, {
334361
metadata: metadata.metadata as unknown as SDKOAuthMetadata,
335362
clientInformation: metadata.clientInfo,
336363
authorizationCode,
337364
codeVerifier: metadata.codeVerifier,
338365
redirectUri: metadata.clientInfo.redirect_uris?.[0] || this.getDefaultRedirectUri(),
366+
resource: resource,
339367
});
340368

341369
logger.debug('[MCPOAuth] Raw tokens from exchange:', {

0 commit comments

Comments
 (0)