diff --git a/packages/gitbook/src/components/PageBody/PageHeader.tsx b/packages/gitbook/src/components/PageBody/PageHeader.tsx index 7f4f85e57..d2bfa5729 100644 --- a/packages/gitbook/src/components/PageBody/PageHeader.tsx +++ b/packages/gitbook/src/components/PageBody/PageHeader.tsx @@ -166,9 +166,21 @@ function getPageActionsURLs({ }).toString()}` ) : undefined, - mcp: - context.site.visibility !== SiteVisibility.VisitorAuth - ? context.linker.toAbsoluteURL(context.linker.toPathInSite('~gitbook/mcp')) - : undefined, + mcp: getPageActionsMCPURL(context), }; } + +/** + * Return the MCP URL to be used in the page actions dropdown. + */ +function getPageActionsMCPURL(context: GitBookSiteContext) { + const useAuthenticatedEndpoint = Boolean( + context.site.visibility !== SiteVisibility.VisitorAuth && + context.site.adaptiveContent?.enabled && + context.site.urls.login && + context.isLoggedInVisitor + ); + const endpoint = useAuthenticatedEndpoint ? '~gitbook/mcp/auth' : '~gitbook/mcp'; + + return context.linker.toAbsoluteURL(context.linker.toPathInSite(endpoint)); +} diff --git a/packages/gitbook/src/lib/context.ts b/packages/gitbook/src/lib/context.ts index b17b94633..2269c1743 100644 --- a/packages/gitbook/src/lib/context.ts +++ b/packages/gitbook/src/lib/context.ts @@ -70,6 +70,11 @@ export type SiteURLData = Pick< * Computed in middleware from GITBOOK_BLOCK_SEARCH_INDEXATION env var and x-gitbook-search-indexation header. */ noIndexSearch?: boolean; + + /** + * Whether the request included a visitor token. + */ + isLoggedInVisitor?: boolean; }; /** @@ -164,6 +169,9 @@ export type GitBookSiteContext = GitBookSpaceContext & { /** Whether search indexation is blocked for this deployment. */ noIndexSearch: boolean; + + /** Whether the request included a visitor token. */ + isLoggedInVisitor: boolean; }; /** @@ -244,6 +252,7 @@ export async function fetchSiteContextByURLLookup( contextId: data.contextId, isFallback: data.isFallback ?? false, noIndexSearch: data.noIndexSearch ?? false, + isLoggedInVisitor: data.isLoggedInVisitor ?? false, }); } @@ -264,6 +273,7 @@ export async function fetchSiteContextByIds( contextId?: string; isFallback: boolean; noIndexSearch: boolean; + isLoggedInVisitor: boolean; } ): Promise { const { dataFetcher } = baseContext; @@ -388,6 +398,7 @@ export async function fetchSiteContextByIds( contextId: ids.contextId, isFallback: ids.isFallback, noIndexSearch: ids.noIndexSearch, + isLoggedInVisitor: ids.isLoggedInVisitor, }; } diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index b3fa5f5bb..18ccb64fa 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -378,6 +378,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) { !requestURL.searchParams.has('x-gitbook-search-indexation') ? true : undefined, + isLoggedInVisitor: visitorToken ? true : undefined, }; const requestHeaders = new Headers(request.headers);