Construction mode hides pages from Markdown, MCP and search; MCP rejects non-object messages

createContentHelpers exposes isPathVisible (built from getConstructionMode)
and passes it to markdownRoute and mcpPOST; createSearchRoute,
createMcpRoute and createMarkdownRoute accept it. A null/number/string
JSON-RPC message now gets -32600 instead of crashing the route.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-22 17:07:45 +02:00
co-authored by Claude Opus 5.5
parent 9a72ebfaff
commit 7c05a1dba9
11 changed files with 116 additions and 13 deletions
+25 -4
View File
@@ -1,9 +1,11 @@
import type { BasePayload } from 'payload'
import type { PathVisibility } from '../construction/visibility.js'
import type { ContentOption } from '../content/index.js'
import type { I18nConfig } from '../i18n/index.js'
import type { ClientIpOptions } from '../shared/rateLimit.js'
import { pathOf } from '../construction/visibility.js'
import { createMarkdownRenderer, searchContent } from '../content/index.js'
import { getDefaultLocale, isValidLocale } from '../i18n/index.js'
import { readSiteMetaConfig } from '../seo/readSiteMetaConfig.js'
@@ -72,6 +74,11 @@ export type CreateMcpRouteArgs = {
settingsSlug?: string
/** Field holding a richer AI summary (buildSummaryField) to prefer in search results. */
summaryField?: string
/**
* Hide pages the site hides (construction mode): filtered out of list_pages
* and search_content, "not found" in get_page. createContentHelpers passes it.
*/
isPathVisible?: PathVisibility
}
type JsonRpcId = null | number | string
@@ -95,6 +102,7 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
config,
content,
getCachedPayload,
isPathVisible,
maxBatch = 10,
serverName = 'ipal-site',
serverVersion = '1.0.0',
@@ -160,6 +168,13 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
? params.locale
: getDefaultLocale(config)
/** Drops results whose page is hidden right now (construction mode). */
async function visible<T extends { url: string }>(items: T[], locale: string): Promise<T[]> {
if (!isPathVisible) {return items}
const keep = await Promise.all(items.map((item) => isPathVisible(pathOf(item.url), locale)))
return items.filter((_, i) => keep[i])
}
const formatResults = (results: { description?: string; title: string; url: string }[]) =>
results.map((r) => `- ${r.title} (${r.url})${r.description ? ` — ${r.description}` : ''}`).join('\n')
@@ -193,7 +208,7 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
const limit = Math.min(Math.max(Math.trunc(typeof params.limit === 'number' ? params.limit : 50) || 50, 1), 200)
const payload = await getCachedPayload()
// Same index as llms.txt and the sitemap rules: pages AND archive entries (blog…).
const index = await collectSiteIndex({
const index = await visible(await collectSiteIndex({
baseUrl: origin,
config,
content,
@@ -202,7 +217,7 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
payload,
settingsSlug,
summaryField,
})
}), locale)
if (index.length === 0) {return 'No published pages.'}
const shown = index.slice(0, limit)
const lines = shown.map(
@@ -221,7 +236,7 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
const payload = await getCachedPayload()
const meta = await siteMeta(payload, locale)
// Shared with the JSON search route — one ranking everywhere.
const results = await searchContent({
const results = await visible(await searchContent({
baseUrl: origin,
collection,
config,
@@ -231,7 +246,7 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
payload,
query,
summaryField,
})
}), locale)
if (results.length === 0) {return `No pages matched "${query}".`}
return formatResults(results)
}
@@ -246,12 +261,18 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
locale = segments[0]
slug = segments.slice(1)
}
if (isPathVisible) {
const path = `/${[...(config.locales.length > 1 ? [locale] : []), ...slug].join('/')}`
if (!(await isPathVisible(path, locale))) {return `No page found at ${raw}.`}
}
const payload = await getCachedPayload()
const md = await renderMarkdown({ slug, locale, payload })
return md ?? `No page found at ${raw}.`
}
async function handleOne(msg: JsonRpcRequest): Promise<null | object> {
// `null`, a number, a string… is not a JSON-RPC request — answer, don't crash.
if (!msg || typeof msg !== 'object' || Array.isArray(msg)) {return rpcError(null, -32600, 'Invalid Request')}
const { id = null, method, params } = msg
// Notifications (no id) get no response body.