diff --git a/server/routes/index.ts b/server/routes/index.ts index 0e0c1950bc..88ca0e6786 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -111,27 +111,58 @@ router.get("/locales/:lng.json", async (ctx) => { }); }); -router.get("/.well-known/oauth-authorization-server", async (ctx) => { - const origin = ctx.request.URL.origin; - const team = await getTeamFromContext(ctx, { includeStateCookie: false }); - const mcpEnabled = team?.getPreference(TeamPreference.MCP) ?? true; +router.get( + [ + "/.well-known/oauth-authorization-server", + "/.well-known/oauth-authorization-server/mcp", + ], + async (ctx) => { + const origin = ctx.request.URL.origin; + const team = await getTeamFromContext(ctx, { includeStateCookie: 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`, - ...(!env.OAUTH_DISABLE_DCR && - mcpEnabled && { - registration_endpoint: `${origin}/oauth/register`, - }), - response_types_supported: ["code"], - grant_types_supported: ["authorization_code", "refresh_token"], - token_endpoint_auth_methods_supported: ["client_secret_post", "none"], - code_challenge_methods_supported: ["S256"], - scopes_supported: ["read", "write"], - }; -}); + ctx.body = { + issuer: origin, + authorization_endpoint: `${origin}/oauth/authorize`, + token_endpoint: `${origin}/oauth/token`, + revocation_endpoint: `${origin}/oauth/revoke`, + ...(!env.OAUTH_DISABLE_DCR && + mcpEnabled && { + registration_endpoint: `${origin}/oauth/register`, + }), + response_types_supported: ["code"], + grant_types_supported: ["authorization_code", "refresh_token"], + token_endpoint_auth_methods_supported: ["client_secret_post", "none"], + code_challenge_methods_supported: ["S256"], + scopes_supported: ["read", "write"], + }; + } +); + +router.get( + [ + "/.well-known/oauth-protected-resource", + "/.well-known/oauth-protected-resource/mcp", + ], + async (ctx) => { + const team = await getTeamFromContext(ctx, { includeStateCookie: false }); + const mcpEnabled = team?.getPreference(TeamPreference.MCP) ?? true; + + if (!mcpEnabled) { + ctx.status = 404; + return; + } + + const origin = ctx.request.URL.origin; + + ctx.body = { + resource: `${origin}/mcp`, + authorization_servers: [origin], + scopes_supported: ["read", "write"], + bearer_methods_supported: ["header"], + }; + } +); router.get("/robots.txt", (ctx) => { ctx.body = robotsResponse(); diff --git a/server/routes/oauth/oauth.test.ts b/server/routes/oauth/oauth.test.ts index c413a72584..13b6e938d8 100644 --- a/server/routes/oauth/oauth.test.ts +++ b/server/routes/oauth/oauth.test.ts @@ -1,4 +1,5 @@ import { faker } from "@faker-js/faker"; +import { TeamPreference } from "@shared/types"; import { OAuthClient } from "@server/models"; import { buildTeam } from "@server/test/factories"; import { getTestServer } from "@server/test/support"; @@ -433,4 +434,49 @@ describe("GET /.well-known/oauth-authorization-server", () => { ]); expect(body.code_challenge_methods_supported).toEqual(["S256"]); }); + + it("should return 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"); + }); +}); + +describe("GET /.well-known/oauth-protected-resource", () => { + it("should return protected resource metadata", async () => { + const res = await server.get("/.well-known/oauth-protected-resource"); + + expect(res.status).toEqual(200); + const body = await res.json(); + expect(body.resource).toContain("/mcp"); + expect(body.authorization_servers).toHaveLength(1); + expect(body.scopes_supported).toEqual(["read", "write"]); + expect(body.bearer_methods_supported).toEqual(["header"]); + }); + + it("should return protected resource metadata at /mcp suffix path", async () => { + const res = await server.get("/.well-known/oauth-protected-resource/mcp"); + + expect(res.status).toEqual(200); + const body = await res.json(); + expect(body.resource).toContain("/mcp"); + expect(body.authorization_servers).toHaveLength(1); + }); + + it("should return 404 when MCP is disabled", async () => { + const team = await buildTeam({ + subdomain: faker.internet.domainWord(), + preferences: { [TeamPreference.MCP]: false }, + }); + + const res = await server.get("/.well-known/oauth-protected-resource", { + headers: { host: `${team.subdomain}.outline.dev` }, + }); + + expect(res.status).toEqual(404); + }); });