Compare commits

...

1 Commits

Author SHA1 Message Date
Tomek Gargula db88aeac4d Serve the MCP endpoint independently of the Connect with MCP page action
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 14:10:08 +01:00
9 changed files with 85 additions and 13 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Serve the published-docs MCP endpoint independently of the Connect with MCP page action.
@@ -1,6 +1,7 @@
import { CustomizationPageActionType, type RevisionPageDocument } from '@gitbook/api';
import type { GitBookSiteContext } from '@/lib/context';
import { isSiteMcpEnabled } from '@/lib/mcp/endpoints';
const CONFIGURABLE_PAGE_ACTIONS = [
CustomizationPageActionType.Assistant,
@@ -29,8 +30,14 @@ export function isPageActionEnabled(
* Whether any of the configured page actions is enabled. The RSS feed is not part of them: it is
* only offered on pages with an updates block.
*/
export function hasConfiguredPageActions(customization: GitBookSiteContext['customization']) {
return CONFIGURABLE_PAGE_ACTIONS.some((type) => isPageActionEnabled(customization, type));
export function hasConfiguredPageActions(context: Pick<GitBookSiteContext, 'customization'>) {
const { customization } = context;
return CONFIGURABLE_PAGE_ACTIONS.some((type) => {
if (type === CustomizationPageActionType.Mcp && !isSiteMcpEnabled(context)) {
return false;
}
return isPageActionEnabled(customization, type);
});
}
/**
@@ -8,7 +8,7 @@ import type { PageActionsDropdownURLs } from './PageActionsDropdown';
import type { GitBookSiteContext } from '@/lib/context';
import { getPageDocument } from '@/lib/data';
import { hasTopLevelBlock } from '@/lib/document';
import { hasAdaptiveMcpEndpoint } from '@/lib/mcp/endpoints';
import { hasAdaptiveMcpEndpoint, isMcpPageActionEnabled } from '@/lib/mcp/endpoints';
import { getPageRSSURL } from '@/routes/rss';
/**
@@ -51,7 +51,7 @@ export async function getPageActionsURLs(
}).toString()}`
)
: undefined,
mcp: getPageActionsMCPURL(context),
mcp: isMcpPageActionEnabled(context) ? getPageActionsMCPURL(context) : undefined,
};
}
@@ -139,8 +139,7 @@ export async function PageHeader(props: {
// Show page actions if *any* of the configured actions are enabled, or if the RSS feed is
// available. RSS is contextual (only on update/blog index pages) and is not part of the
// configured `items` list, so it is checked separately.
const hasPageActions =
pageActionsEnabled && (hasConfiguredPageActions(context.customization) || withRSSFeed);
const hasPageActions = pageActionsEnabled && (hasConfiguredPageActions(context) || withRSSFeed);
if (!page.layout.title && !page.layout.description && !hasPageActions) {
return null;
@@ -3,7 +3,7 @@ import Script from 'next/script';
import React from 'react';
import * as ReactDOM from 'react-dom';
import { CustomizationDefaultThemeMode, CustomizationPageActionType } from '@gitbook/api';
import { CustomizationDefaultThemeMode } from '@gitbook/api';
import { AIContextProvider } from '../AI';
import { LLMsTxtDirective } from './LLMsTxtDirective';
@@ -20,6 +20,7 @@ import { buildVersion } from '@/lib/build';
import type { GitBookSiteContext } from '@/lib/context';
import { GITBOOK_API_PUBLIC_URL, GITBOOK_ASSETS_URL, GITBOOK_ICONS_URL } from '@/lib/env';
import { getResizedImageURL } from '@/lib/images';
import { isMcpPageActionEnabled } from '@/lib/mcp/endpoints';
import { removeTrailingSlash } from '@/lib/paths';
import { isSiteIndexable } from '@/lib/seo';
import { getOpenSearchURL } from '@/routes/opensearch';
@@ -127,7 +128,7 @@ export async function SiteLayout(props: {
</AIContextProvider>
<LoadIntegrations />
{customization.pageActions.items.includes(CustomizationPageActionType.Mcp) ? (
{isMcpPageActionEnabled(context) ? (
<WebMCP mcpURL={context.linker.toPathInSite('~gitbook/mcp')} />
) : null}
{scripts.map(({ script }) =>
@@ -24,6 +24,7 @@ function makeContext(
siteId?: string;
visibility?: SiteVisibility;
pageActions?: CustomizationPageActionType[];
mcpEnabled?: boolean;
} = {}
): GitBookSiteContext {
const {
@@ -32,11 +33,16 @@ function makeContext(
siteId = 'site_123',
visibility = SiteVisibility.Public,
pageActions = [CustomizationPageActionType.Mcp],
mcpEnabled,
} = options;
return {
site: { id: siteId, title: 'Acme', visibility },
customization: { pageActions: { items: pageActions } },
customization: {
pageActions: { items: pageActions },
// DO NOT MERGE - mocking type for development
...(mcpEnabled === undefined ? {} : { mcp: { enabled: mcpEnabled } }),
},
linker: createLinker({ host, siteBasePath, spaceBasePath: siteBasePath }),
} as unknown as GitBookSiteContext;
}
@@ -160,6 +166,14 @@ describe('serveSiteAiCatalog', () => {
}
);
it('does not advertise the MCP server when the page action is off', () => {
expect(buildSiteAiCatalog(makeContext({ mcpEnabled: true, pageActions: [] }))).toBeNull();
});
it('does not advertise the MCP server when the endpoint is off', () => {
expect(buildSiteAiCatalog(makeContext({ mcpEnabled: false }))).toBeNull();
});
it('does not serve a catalog when there is nothing to advertise', async () => {
const res = await serveSiteAiCatalog(makeContext({ pageActions: [] }), catalogRequest());
+2 -2
View File
@@ -1,6 +1,6 @@
import type { GitBookSiteContext } from '@/lib/context';
import { serveDiscoveryDocument } from '@/lib/discoveryDocument';
import { isSiteMcpEnabled } from '@/lib/mcp/endpoints';
import { isMcpPageActionEnabled } from '@/lib/mcp/endpoints';
import { MCP_SERVER_CARD_PATH } from '@/lib/mcp/paths';
import { SERVER_CARD_MEDIA_TYPE } from '@/lib/mcp/serverCard';
@@ -45,7 +45,7 @@ export function buildSiteAiCatalog(context: GitBookSiteContext): AiCatalog | nul
const entries: AiCatalogEntry[] = [];
if (isSiteMcpEnabled(context)) {
if (isMcpPageActionEnabled(context)) {
entries.push({
identifier: buildCatalogIdentifier(context, 'mcp', site.id),
type: SERVER_CARD_MEDIA_TYPE,
+21 -2
View File
@@ -3,12 +3,31 @@ import { CustomizationPageActionType, SiteVisibility } from '@gitbook/api';
import type { GitBookSiteContext } from '@/lib/context';
/**
* Whether the site publishes an MCP server at all. Gates the endpoint and everything describing it.
* Whether the site serves its MCP endpoint. Falls back to the `mcp` page action for cached
* customizations that predate the setting.
*/
export function isSiteMcpEnabled(context: GitBookSiteContext): boolean {
export function isSiteMcpEnabled(context: Pick<GitBookSiteContext, 'customization'>): boolean {
// DO NOT MERGE - mocking type for development
const mcp = (context.customization as { mcp?: { enabled: boolean } }).mcp;
if (mcp) {
return mcp.enabled;
}
return context.customization.pageActions.items.includes(CustomizationPageActionType.Mcp);
}
/**
* Whether the site advertises its MCP server to visitors: the endpoint is served and the
* "Connect with MCP server" page action is enabled for this visitor.
*/
export function isMcpPageActionEnabled(
context: Pick<GitBookSiteContext, 'customization'>
): boolean {
return (
isSiteMcpEnabled(context) &&
context.customization.pageActions.items.includes(CustomizationPageActionType.Mcp)
);
}
/**
* Whether `~gitbook/mcp` endpoint requires auth for the site (i.e VA site).
*/
@@ -31,6 +31,7 @@ function makeContext(
adaptiveContent?: boolean;
aiMode?: CustomizationAIMode;
pageActions?: CustomizationPageActionType[];
mcpEnabled?: boolean;
} = {}
): GitBookSiteContext {
const {
@@ -43,6 +44,7 @@ function makeContext(
adaptiveContent = true,
aiMode = CustomizationAIMode.Search,
pageActions = [CustomizationPageActionType.Mcp],
mcpEnabled,
} = options;
return {
@@ -59,6 +61,8 @@ function makeContext(
customization: {
ai: { mode: aiMode },
pageActions: { items: pageActions },
// DO NOT MERGE - mocking type for development
...(mcpEnabled === undefined ? {} : { mcp: { enabled: mcpEnabled } }),
},
linker: createLinker({ host, siteBasePath, spaceBasePath }),
} as unknown as GitBookSiteContext;
@@ -384,6 +388,29 @@ describe('serveSiteMcpServerCard', () => {
expect(card.$schema).toBe(SERVER_CARD_SCHEMA_URL);
});
it.each([
{
scenario: 'the endpoint is on and the page action is off',
options: { mcpEnabled: true, pageActions: [] },
status: 200,
},
{
scenario: 'the endpoint is off and the page action is on',
options: { mcpEnabled: false },
status: 404,
},
{ scenario: 'the setting is missing and the page action is on', options: {}, status: 200 },
{
scenario: 'the setting is missing and the page action is off',
options: { pageActions: [] },
status: 404,
},
])('answers $status when $scenario', async ({ options, status }) => {
const res = await serveSiteMcpServerCard(makeContext(options), cardRequest());
expect(res.status).toBe(status);
});
it('answers a preflight with a 204', async () => {
const res = await serveSiteMcpServerCard(makeContext(), cardRequest({ method: 'OPTIONS' }));