Skip to content

Commit 8e3c7c9

Browse files
fix(mcp): use next.config rewrites for .well-known OAuth paths
Middleware doesn't reliably match dotted paths like /.well-known/*. Move the RFC 9728 rewrite rules to next.config.ts rewrites, which run at the server level and handle dotted paths correctly. Maps /.well-known/oauth-protected-resource/* and /.well-known/oauth-authorization-server/* to the catch-all proxy. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e01606c commit 8e3c7c9

2 files changed

Lines changed: 15 additions & 16 deletions

File tree

next.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ const withNextIntl = createNextIntlPlugin('./i18n.ts');
55

66
const nextConfig: NextConfig = {
77
output: 'standalone',
8+
async rewrites() {
9+
return [
10+
// RFC 9728: OAuth metadata at well-known paths for MCP
11+
// Claude Desktop fetches /.well-known/oauth-protected-resource/api/mcp
12+
// Rewrite to our catch-all proxy which serves tenant-aware metadata
13+
{
14+
source: '/.well-known/oauth-protected-resource/:path*',
15+
destination: '/api/mcp/.well-known/oauth-protected-resource',
16+
},
17+
{
18+
source: '/.well-known/oauth-authorization-server/:path*',
19+
destination: '/api/mcp/.well-known/oauth-authorization-server',
20+
},
21+
];
22+
},
823
};
924

1025
export default withNextIntl(nextConfig);

proxy.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,6 @@ export default async function proxy(request: NextRequest) {
8080
return NextResponse.next()
8181
}
8282

83-
// --- OAuth well-known metadata (RFC 9728 path-aware format) ---
84-
// Claude Desktop looks for:
85-
// /.well-known/oauth-protected-resource/api/mcp
86-
// /.well-known/oauth-authorization-server/api/mcp
87-
// Rewrite these to our catch-all proxy at /api/mcp/.well-known/*
88-
if (pathname.startsWith('/.well-known/oauth-protected-resource/api/mcp') ||
89-
pathname.startsWith('/.well-known/oauth-authorization-server/api/mcp')) {
90-
// Extract which well-known type: oauth-protected-resource or oauth-authorization-server
91-
const wellKnownType = pathname.startsWith('/.well-known/oauth-protected-resource')
92-
? 'oauth-protected-resource'
93-
: 'oauth-authorization-server'
94-
const rewriteUrl = request.nextUrl.clone()
95-
rewriteUrl.pathname = `/api/mcp/.well-known/${wellKnownType}`
96-
return NextResponse.rewrite(rewriteUrl)
97-
}
98-
9983
// --- Tenant Resolution (runs for ALL routes including /api) ---
10084
const host = request.headers.get('host') || ''
10185
const tenantSlug = getTenantSlugFromHost(host)

0 commit comments

Comments
 (0)