From d51605fed81a3ddbf985a98dcca5256b3f1af76d Mon Sep 17 00:00:00 2001 From: spastorelli Date: Fri, 27 Nov 2026 22:38:06 +0100 Subject: [PATCH] Advertise the public URL in the MCP metadata of sites served from a custom subdirectory (#4860) --- .changeset/proxied-site-mcp-metadata.md | 5 ++ bun.lock | 8 +-- package.json | 2 +- .../gitbook/src/lib/oauth-protected.test.ts | 60 +++++++++++++++++++ packages/gitbook/src/lib/oauth-protected.ts | 45 ++++++++++---- packages/gitbook/src/middleware.ts | 1 + 6 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 .changeset/proxied-site-mcp-metadata.md diff --git a/.changeset/proxied-site-mcp-metadata.md b/.changeset/proxied-site-mcp-metadata.md new file mode 100644 index 000000000..00a137519 --- /dev/null +++ b/.changeset/proxied-site-mcp-metadata.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Advertise the public URL in the MCP OAuth metadata of sites served from a custom subdirectory, so MCP clients accept the connection. diff --git a/bun.lock b/bun.lock index 45507d981..f9c69e7cc 100644 --- a/bun.lock +++ b/bun.lock @@ -7,8 +7,8 @@ "devDependencies": { "@changesets/cli": "^2.31.1", "oxfmt": "^0.62.0", - "oxlint": "^1.93.0", - "turbo": "^2.11.11", + "oxlint": "^1.95.0", + "turbo": "^2.11.13", "vercel": "50.37.3", }, }, @@ -357,7 +357,7 @@ }, "catalog": { "@base-ui/react": "^1.7.0", - "@gitbook/api": "0.215.0", + "@gitbook/api": "0.216.0", "@scalar/api-client-react": "^1.3.46", "@tsconfig/node20": "^20.1.6", "@tsconfig/strictest": "^2.0.6", @@ -725,7 +725,7 @@ "@fortawesome/fontawesome-svg-core": ["@fortawesome/fontawesome-svg-core@7.2.0", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "7.2.0" } }, "sha512-6639htZMjEkwskf3J+e6/iar+4cTNM9qhoWuRfj9F3eJD6r7iCzV1SWnQr2Mdv0QT0suuqU8BoJCZUyCtP9R4Q=="], - "@gitbook/api": ["@gitbook/api@0.215.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-y9Xx3Xmls5A6C0PX24VYUdcb1CSFgjwlrnkPagq5ZpAYsDAx4xMu0SNiF/aoaqgJdeHlR+jtQkUx/5y0osAvGA=="], + "@gitbook/api": ["@gitbook/api@0.216.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-AYByJnI+cZKYGBzxW2SH/lQTogOiRo8xpDFRrQJ6570UlJ9pOrewGs75EeMGWguvU5jO7DNt9thBarr91KPhUQ=="], "@gitbook/browser-types": ["@gitbook/browser-types@workspace:packages/browser-types"], diff --git a/package.json b/package.json index e82d8bb70..7e97501cc 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "@tsconfig/strictest": "^2.0.6", "@tsconfig/node20": "^20.1.6", "@base-ui/react": "^1.7.0", - "@gitbook/api": "0.215.0", + "@gitbook/api": "0.216.0", "@scalar/api-client-react": "^1.3.46", "@types/react": "^19.0.0", "@types/react-dom": "^19.0.0", diff --git a/packages/gitbook/src/lib/oauth-protected.test.ts b/packages/gitbook/src/lib/oauth-protected.test.ts index c043902a1..3d45b203b 100644 --- a/packages/gitbook/src/lib/oauth-protected.test.ts +++ b/packages/gitbook/src/lib/oauth-protected.test.ts @@ -80,6 +80,66 @@ describe('OAuth protected resources flow', () => { expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8'); expect(res.headers.get('Cache-Control')).toBe('no-store'); }); + + describe('on a site served through the proxy', () => { + const proxyURL = 'https://proxy.gitbook.site/sites/site_123'; + const publicURL = 'https://www.acme.org/docs'; + + it('should advertise the public URL as the resource', async () => { + const metadataPath = '/.well-known/oauth-protected-resource/~gitbook/mcp/auth'; + const res = handleUnauthedOAuthProtectedResourceRequest({ + siteRequestURL: new URL(`${proxyURL}${metadataPath}`), + siteURLData: { + target: 'external', + redirect: 'https://login.acme.org/oauth2', + site: 'site_123', + canonicalUrl: `${publicURL}${metadataPath}`, + }, + urlMode: 'url-host', + }); + + const json = await res.json(); + expect(json).toEqual({ + resource: `${publicURL}/~gitbook/mcp/auth`, + authorization_servers: ['https://sites.gitbook.com/oauth2/v1/site_123'], + }); + }); + + it('should point the challenge at the metadata on the public URL', () => { + const res = handleUnauthedOAuthProtectedResourceRequest({ + siteRequestURL: new URL(`${proxyURL}/~gitbook/mcp`), + siteURLData: { + target: 'external', + redirect: 'https://login.acme.org/oauth2', + site: 'site_123', + canonicalUrl: `${publicURL}/~gitbook/mcp`, + }, + urlMode: 'url-host', + }); + + expect(res.status).toBe(401); + expect(res.headers.get('WWW-Authenticate')).toBe( + `Bearer realm="mcp", resource_metadata="${publicURL}/.well-known/oauth-protected-resource/~gitbook/mcp"` + ); + }); + + it('should advertise the proxy URL when the public URL is missing', async () => { + const res = handleUnauthedOAuthProtectedResourceRequest({ + siteRequestURL: new URL( + `${proxyURL}/.well-known/oauth-protected-resource/~gitbook/mcp` + ), + siteURLData: { + target: 'external', + redirect: 'https://login.acme.org/oauth2', + site: 'site_123', + }, + urlMode: 'url-host', + }); + + const json = await res.json(); + expect(json).toMatchObject({ resource: `${proxyURL}/~gitbook/mcp` }); + }); + }); }); describe('createOAuthProtectedResourceMetadataResponse', () => { diff --git a/packages/gitbook/src/lib/oauth-protected.ts b/packages/gitbook/src/lib/oauth-protected.ts index 84d043448..bbb390b9b 100644 --- a/packages/gitbook/src/lib/oauth-protected.ts +++ b/packages/gitbook/src/lib/oauth-protected.ts @@ -3,6 +3,7 @@ import { type NextRequest, NextResponse } from 'next/server'; import type { PublishedContentRedirect } from '@gitbook/api'; import { GITBOOK_OAUTH_SERVER_URL, GITBOOK_URL } from './env'; +import { isProxyRequest } from './proxy'; type OAuthProtectedResource = { /** Endpoint of the protected resource */ @@ -38,13 +39,12 @@ export function createOAuthProtectedResourceMetadataResponse(args: { siteRequestURL: URL; siteId: string; urlMode: 'url' | 'url-host'; + /** Public URL of the request, which differs from the request URL on proxied sites. */ + canonicalURL?: string; }) { - const { siteRequestURL, urlMode, siteId } = args; + const { siteId } = args; - const resourceUrl = - urlMode === 'url-host' - ? siteRequestURL - : new URL(`/url/${siteRequestURL.host}${siteRequestURL.pathname}`, GITBOOK_URL); + const resourceUrl = getPublicResourceURL(args); const protectedResourceMetadata = { resource: resourceUrl.toString().replace(OAUTH_PROTECTED_RESOURCE_METADATA_PATH, ''), @@ -62,6 +62,7 @@ export function handleUnauthedOAuthProtectedResourceRequest(args: { urlMode: 'url' | 'url-host'; }) { const { siteRequestURL, urlMode, siteURLData } = args; + const canonicalURL = siteURLData.canonicalUrl; // When the request is for the protected resource metadata return the info relative to the site. if (isOAuthProtectedResourceMetadataRequest(siteRequestURL)) { @@ -69,6 +70,7 @@ export function handleUnauthedOAuthProtectedResourceRequest(args: { siteRequestURL, siteId: siteURLData.site, urlMode, + canonicalURL, }); } @@ -76,6 +78,7 @@ export function handleUnauthedOAuthProtectedResourceRequest(args: { return createOAuthProtectedResourceUnauthResponse({ siteRequestURL, urlMode, + canonicalURL, }); } @@ -85,8 +88,10 @@ export function handleUnauthedOAuthProtectedResourceRequest(args: { export function createOAuthProtectedResourceUnauthResponse(args: { siteRequestURL: URL; urlMode: 'url' | 'url-host'; + /** Public URL of the request, which differs from the request URL on proxied sites. */ + canonicalURL?: string; }) { - const { siteRequestURL, urlMode } = args; + const { siteRequestURL } = args; const matched = getMatchedProtectedResourceEndpoint(siteRequestURL); if (!matched) { @@ -95,10 +100,7 @@ export function createOAuthProtectedResourceUnauthResponse(args: { ); } - const baseUrl = - urlMode === 'url-host' - ? siteRequestURL - : new URL(`/url/${siteRequestURL.host}${siteRequestURL.pathname}`, GITBOOK_URL); + const baseUrl = getPublicResourceURL(args); const resourceMetadataUrl = baseUrl .toString() @@ -179,3 +181,26 @@ function getOAuthProtectedResourceMetadataPath(endpoint: string) { const normalizedEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`; return `${OAUTH_PROTECTED_RESOURCE_METADATA_PATH}${normalizedEndpoint}`; } + +/** + * Get the URL clients use to reach the requested resource. + */ +function getPublicResourceURL(args: { + siteRequestURL: URL; + urlMode: 'url' | 'url-host'; + canonicalURL?: string; +}): URL { + const { siteRequestURL, urlMode, canonicalURL } = args; + + if (urlMode === 'url') { + return new URL(`/url/${siteRequestURL.host}${siteRequestURL.pathname}`, GITBOOK_URL); + } + + // A proxied site's request URL is the proxy's (`/sites/site_xxx/...`), but MCP clients connect + // to its public URL and reject metadata naming another resource. + if (isProxyRequest(siteRequestURL) && canonicalURL) { + return new URL(canonicalURL); + } + + return siteRequestURL; +} diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 41eeae196..7092a806d 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -503,6 +503,7 @@ async function serveSiteRequest(requestURL: URL, request: NextRequest, match: UR siteRequestURL, siteId: siteURLData.site, urlMode: mode, + canonicalURL: siteURLData.canonicalUrl, }); }