Skip to content

Commit 1d475bb

Browse files
blustAIEugene
andauthored
fix(auth-router): correct Protected Resource Metadata for pathful RS and add explicit resourceServerUrl (RFC 9728) (#858)
Co-authored-by: Eugene <eugene@blust.ai>
1 parent 9841a6c commit 1d475bb

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/server/auth/router.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export type AuthRouterOptions = {
4141
*/
4242
resourceName?: string;
4343

44+
/**
45+
* The URL of the protected resource (RS) whose metadata we advertise.
46+
* If not provided, falls back to `baseUrl` and then to `issuerUrl` (AS=RS).
47+
*/
48+
resourceServerUrl?: URL;
49+
4450
// Individual options per route
4551
authorizationOptions?: Omit<AuthorizationHandlerOptions, "provider">;
4652
clientRegistrationOptions?: Omit<ClientRegistrationHandlerOptions, "clientsStore">;
@@ -130,8 +136,8 @@ export function mcpAuthRouter(options: AuthRouterOptions): RequestHandler {
130136

131137
router.use(mcpAuthMetadataRouter({
132138
oauthMetadata,
133-
// This router is used for AS+RS combo's, so the issuer is also the resource server
134-
resourceServerUrl: new URL(oauthMetadata.issuer),
139+
// Prefer explicit RS; otherwise fall back to AS baseUrl, then to issuer (back-compat)
140+
resourceServerUrl: options.resourceServerUrl ?? options.baseUrl ?? new URL(oauthMetadata.issuer),
135141
serviceDocumentationUrl: options.serviceDocumentationUrl,
136142
scopesSupported: options.scopesSupported,
137143
resourceName: options.resourceName
@@ -185,7 +191,7 @@ export type AuthMetadataOptions = {
185191
resourceName?: string;
186192
}
187193

188-
export function mcpAuthMetadataRouter(options: AuthMetadataOptions) {
194+
export function mcpAuthMetadataRouter(options: AuthMetadataOptions): express.Router {
189195
checkIssuerUrl(new URL(options.oauthMetadata.issuer));
190196

191197
const router = express.Router();
@@ -202,9 +208,11 @@ export function mcpAuthMetadataRouter(options: AuthMetadataOptions) {
202208
resource_documentation: options.serviceDocumentationUrl?.href,
203209
};
204210

205-
router.use("/.well-known/oauth-protected-resource", metadataHandler(protectedResourceMetadata));
211+
// Serve PRM at the path-specific URL per RFC 9728
212+
const rsPath = new URL(options.resourceServerUrl.href).pathname;
213+
router.use(`/.well-known/oauth-protected-resource${rsPath === '/' ? '' : rsPath}`, metadataHandler(protectedResourceMetadata));
206214

207-
// Always add this for backwards compatibility
215+
// Always add this for OAuth Authorization Server metadata per RFC 8414
208216
router.use("/.well-known/oauth-authorization-server", metadataHandler(options.oauthMetadata));
209217

210218
return router;
@@ -219,8 +227,10 @@ export function mcpAuthMetadataRouter(options: AuthMetadataOptions) {
219227
*
220228
* @example
221229
* getOAuthProtectedResourceMetadataUrl(new URL('https://api.example.com/mcp'))
222-
* // Returns: 'https://api.example.com/.well-known/oauth-protected-resource'
230+
* // Returns: 'https://api.example.com/.well-known/oauth-protected-resource/mcp'
223231
*/
224232
export function getOAuthProtectedResourceMetadataUrl(serverUrl: URL): string {
225-
return new URL('/.well-known/oauth-protected-resource', serverUrl).href;
233+
const u = new URL(serverUrl.href);
234+
const rsPath = u.pathname && u.pathname !== '/' ? u.pathname : '';
235+
return new URL(`/.well-known/oauth-protected-resource${rsPath}`, u).href;
226236
}

0 commit comments

Comments
 (0)