fix(mcp): answer the discovery documents under the server address too

A client handed https://host/mcp as the server address looks for the
well-known documents below that path instead of asking the origin, and every
one of those probes fell through to the web app: 200 with an HTML body, which
reads to the client as "this server has no OAuth" (Open WebUI reports it as an
unexpected mimetype). The three documents are served under /mcp/.well-known
now, anything else there answers a JSON 404, and the RFC 8414 path-inserted
form /.well-known/oauth-authorization-server/mcp serves the metadata instead
of 404. The wiki says which redirect URI belongs to the client and that a
plain-HTTP APP_URL leaves the discovery document pointing at localhost.
This commit is contained in:
Maurice
2026-09-22 11:52:24 +02:00
parent a44df6cf11
commit da32eb8a3c
5 changed files with 102 additions and 5 deletions
@@ -73,6 +73,15 @@ export class DiscoveryController {
throw new NotFoundException(`Cannot ${req.method} ${req.originalUrl}`);
}
// RFC 8414 path insertion: a client that starts from the resource URL
// (https://host/mcp) rather than the issuer looks for the AS metadata under
// the resource's path. The SDK router only serves the flat form, and the
// catchall below would answer 404 to a document we do have.
@Get('oauth-authorization-server/mcp')
authorizationServerForMcp(@Res() res: Response): void {
res.json(this.meta.getOAuthMetadata());
}
// Return 404 JSON for any /.well-known/* path the SDK metadata router doesn't
// handle. Without this, the SPA catch-all serves HTML — clients probing
// /.well-known/openid-configuration or the RFC 8414 path-suffixed AS metadata
@@ -83,3 +92,37 @@ export class DiscoveryController {
res.status(404).json({ error: 'not_found' });
}
}
/**
* The same documents under the resource URL. A client handed https://host/mcp
* as the server address and looking for discovery appends the well-known path
* to it instead of asking the origin, and until now every one of those probes
* fell through to the SPA: 200 with an HTML body, which reads to the client as
* "this server has no OAuth" (Open WebUI reports it as an unexpected mimetype).
* Answering here costs one route per document and a JSON 404 for the rest.
*/
@Public('OAuth/OIDC discovery metadata read by MCP clients before any session exists')
@Controller('mcp/.well-known')
export class McpResourceDiscoveryController {
constructor(private readonly documents: DiscoveryController) {}
@Get('openid-configuration')
openidConfiguration(@Res() res: Response): void {
this.documents.openidConfiguration(res);
}
@Get('oauth-authorization-server')
authorizationServer(@Res() res: Response): void {
this.documents.authorizationServerForMcp(res);
}
@Get('oauth-protected-resource')
protectedResource(@Res() res: Response): void {
this.documents.protectedResource(res);
}
@All('*path')
fallback(@Res() res: Response): void {
res.status(404).json({ error: 'not_found' });
}
}