Skip to content

Commit 3ab686b

Browse files
committed
fix(cms): allow getPageUrl MCP tool to access draft documents
Set overrideAccess: true so the MCP tool can resolve URLs for draft articles that are not yet publicly accessible. Co-Authored-By: JHB-Claw <info@jhb.software>
1 parent 4d63c5c commit 3ab686b

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

cms/src/payload.config.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ const getPageUrlParameters = {
157157
collection: z
158158
.string()
159159
.describe('The page collection slug (e.g. pages, projects, articles, authors).'),
160+
draft: z
161+
.boolean()
162+
.optional()
163+
.default(true)
164+
.describe(
165+
'Whether to query the draft version of the document. Set to false to query only published documents. Defaults to true.',
166+
),
160167
id: z.union([z.string(), z.number()]).describe('The document ID.'),
161168
preview: z
162169
.boolean()
@@ -219,11 +226,11 @@ const mcpPluginConfig: MCPPluginConfig = {
219226
},
220227
{
221228
description:
222-
'Get the frontend page URL for a page document by its collection and ID. Works for both published and draft (unpublished) pages. Returns a preview URL by default; set preview=false for the canonical public URL.',
229+
'Get the frontend page URL for a page document by its collection and ID. Works for both published and draft (unpublished) pages. Returns a preview URL by default; set preview=false for the canonical public URL. Set draft=false to query only published documents.',
223230
handler: async (args, req) => {
224231
const text = (t: string) => ({ content: [{ text: t, type: 'text' as const }] })
225232
try {
226-
const { collection, id, preview } = z.object(getPageUrlParameters).parse(args)
233+
const { collection, draft, id, preview } = z.object(getPageUrlParameters).parse(args)
227234

228235
if (!pageCollectionsSlugs.includes(collection as PageCollectionSlugs)) {
229236
return text(
@@ -233,17 +240,21 @@ const mcpPluginConfig: MCPPluginConfig = {
233240
)
234241
}
235242

236-
const doc = await req.payload
237-
.findByID({
243+
const statusFilter = draft ? ['draft', 'published'] : ['published']
244+
const result = await req.payload
245+
.find({
238246
collection: collection as PageCollectionSlugs,
239247
depth: 0,
240-
draft: true,
241-
id,
248+
draft,
242249
overrideAccess: false,
243250
req,
244251
select: { path: true },
252+
where: {
253+
and: [{ id: { equals: id } }, { _status: { in: statusFilter } }],
254+
},
245255
})
246256
.catch(() => null)
257+
const doc = result?.docs?.[0] ?? null
247258

248259
if (!doc) {
249260
return text(JSON.stringify({ error: `No ${collection} document with id "${id}"` }))

0 commit comments

Comments
 (0)