diff --git a/server/routes/discovery/mcp.ts b/server/routes/discovery/mcp.ts index c72ee0bdc4..85f84c4966 100644 --- a/server/routes/discovery/mcp.ts +++ b/server/routes/discovery/mcp.ts @@ -6,42 +6,37 @@ import { getTeamFromContext } from "@server/utils/passport"; const router = new Router(); -router.get( - [ - "/.well-known/oauth-authorization-server", - "/.well-known/oauth-authorization-server/mcp", - ], - async (ctx) => { - const origin = OAuthHelper.getIssuer(ctx); - const team = await getTeamFromContext(ctx, { includeOAuthState: false }); - const mcpEnabled = team?.getPreference(TeamPreference.MCP) ?? true; +// The issuer has no path, so its metadata lives only at the root (RFC 8414 §3.1). +router.get("/.well-known/oauth-authorization-server", async (ctx) => { + const origin = OAuthHelper.getIssuer(ctx); + const team = await getTeamFromContext(ctx, { includeOAuthState: false }); + const mcpEnabled = team?.getPreference(TeamPreference.MCP) ?? true; - ctx.body = { - issuer: origin, - authorization_endpoint: `${origin}/oauth/authorize`, - token_endpoint: `${origin}/oauth/token`, - revocation_endpoint: `${origin}/oauth/revoke`, - // Registration needs a workspace, so it is not offered on the root - // domain. Clients there use a client ID metadata document instead. - ...(!env.OAUTH_DISABLE_DCR && - team && - mcpEnabled && { - registration_endpoint: `${origin}/oauth/register`, - }), - client_id_metadata_document_supported: mcpEnabled, - response_types_supported: OAuthHelper.responseTypes, - response_modes_supported: ["query"], - grant_types_supported: OAuthHelper.grantTypes, - token_endpoint_auth_methods_supported: - OAuthHelper.tokenEndpointAuthMethods, - revocation_endpoint_auth_methods_supported: - OAuthHelper.tokenEndpointAuthMethods, - code_challenge_methods_supported: ["S256"], - scopes_supported: OAuthHelper.mcpScopes, - }; - } -); + ctx.body = { + issuer: origin, + authorization_endpoint: `${origin}/oauth/authorize`, + token_endpoint: `${origin}/oauth/token`, + revocation_endpoint: `${origin}/oauth/revoke`, + // Registration needs a workspace, so it is not offered on the root + // domain. Clients there use a client ID metadata document instead. + ...(!env.OAUTH_DISABLE_DCR && + team && + mcpEnabled && { + registration_endpoint: `${origin}/oauth/register`, + }), + client_id_metadata_document_supported: mcpEnabled, + response_types_supported: OAuthHelper.responseTypes, + response_modes_supported: ["query"], + grant_types_supported: OAuthHelper.grantTypes, + token_endpoint_auth_methods_supported: OAuthHelper.tokenEndpointAuthMethods, + revocation_endpoint_auth_methods_supported: + OAuthHelper.tokenEndpointAuthMethods, + code_challenge_methods_supported: ["S256"], + scopes_supported: OAuthHelper.mcpScopes, + }; +}); +// The resource must match the path the document is requested at (RFC 9728 §3.3). router.get( [ "/.well-known/oauth-protected-resource", @@ -57,9 +52,11 @@ router.get( } const origin = OAuthHelper.getIssuer(ctx); + const path = ctx.path.endsWith("/mcp") ? "/mcp" : ""; ctx.body = { - resource: `${origin}/mcp`, + resource: `${origin}${path}`, + resource_name: env.APP_NAME, authorization_servers: [origin], scopes_supported: OAuthHelper.mcpScopes, bearer_methods_supported: ["header"], diff --git a/server/routes/oauth/oauth.test.ts b/server/routes/oauth/oauth.test.ts index 4f68a79b7f..5be4d1f3a1 100644 --- a/server/routes/oauth/oauth.test.ts +++ b/server/routes/oauth/oauth.test.ts @@ -613,14 +613,10 @@ describe("GET /.well-known/oauth-authorization-server", () => { expect(body.client_id_metadata_document_supported).toBe(false); }); - it("should return OAuth metadata at /mcp suffix path", async () => { + it("should not serve OAuth metadata at /mcp suffix path", async () => { const res = await server.get("/.well-known/oauth-authorization-server/mcp"); - expect(res.status).toEqual(200); - const body = await res.json(); - expect(body.issuer).toBeTruthy(); - expect(body.authorization_endpoint).toContain("/oauth/authorize"); - expect(body.token_endpoint).toContain("/oauth/token"); + expect(res.status).toEqual(404); }); it("should include port in URLs when configured with a custom port (self-hosted)", async () => { @@ -648,7 +644,8 @@ describe("GET /.well-known/oauth-protected-resource", () => { expect(res.status).toEqual(200); const body = await res.json(); - expect(body.resource).toContain("/mcp"); + expect(body.resource).toEqual(body.authorization_servers[0]); + expect(body.resource_name).toEqual("Outline"); expect(body.authorization_servers).toHaveLength(1); expect(body.scopes_supported).toEqual(["read", "write"]); expect(body.bearer_methods_supported).toEqual(["header"]); @@ -659,7 +656,8 @@ describe("GET /.well-known/oauth-protected-resource", () => { expect(res.status).toEqual(200); const body = await res.json(); - expect(body.resource).toContain("/mcp"); + expect(body.resource).toEqual(`${body.authorization_servers[0]}/mcp`); + expect(body.resource_name).toEqual("Outline"); expect(body.authorization_servers).toHaveLength(1); });