@payloadcms/plugin-mcp 4.0.0-internal.811e0a4 → 4.0.0-internal.8ba6b5b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/endpoint/access.d.ts +2 -7
- package/dist/endpoint/access.d.ts.map +1 -1
- package/dist/endpoint/access.js +4 -11
- package/dist/endpoint/access.js.map +1 -1
- package/dist/mcp/builtin/collections/countTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/countTool.js +1 -2
- package/dist/mcp/builtin/collections/countTool.js.map +1 -1
- package/dist/mcp/builtin/collections/countVersionsTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/countVersionsTool.js +1 -2
- package/dist/mcp/builtin/collections/countVersionsTool.js.map +1 -1
- package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/createTool.js +1 -2
- package/dist/mcp/builtin/collections/createTool.js.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.js +1 -2
- package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
- package/dist/mcp/builtin/collections/duplicateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/duplicateTool.js +1 -2
- package/dist/mcp/builtin/collections/duplicateTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findDistinctTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findDistinctTool.js +1 -2
- package/dist/mcp/builtin/collections/findDistinctTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findTool.js +2 -3
- package/dist/mcp/builtin/collections/findTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findVersionByIDTool.js +1 -2
- package/dist/mcp/builtin/collections/findVersionByIDTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findVersionsTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findVersionsTool.js +1 -2
- package/dist/mcp/builtin/collections/findVersionsTool.js.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +21 -3
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -1
- package/dist/mcp/builtin/collections/restoreVersionTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/restoreVersionTool.js +1 -2
- package/dist/mcp/builtin/collections/restoreVersionTool.js.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.js +2 -3
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- package/dist/mcp/builtin/getConfigInfoTool.d.ts.map +1 -1
- package/dist/mcp/builtin/getConfigInfoTool.js +2 -5
- package/dist/mcp/builtin/getConfigInfoTool.js.map +1 -1
- package/dist/mcp/builtin/globals/countVersionsTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/countVersionsTool.js +1 -2
- package/dist/mcp/builtin/globals/countVersionsTool.js.map +1 -1
- package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/findTool.js +2 -2
- package/dist/mcp/builtin/globals/findTool.js.map +1 -1
- package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/findVersionByIDTool.js +1 -2
- package/dist/mcp/builtin/globals/findVersionByIDTool.js.map +1 -1
- package/dist/mcp/builtin/globals/findVersionsTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/findVersionsTool.js +1 -2
- package/dist/mcp/builtin/globals/findVersionsTool.js.map +1 -1
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js +21 -3
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js.map +1 -1
- package/dist/mcp/builtin/globals/restoreVersionTool.js +1 -2
- package/dist/mcp/builtin/globals/restoreVersionTool.js.map +1 -1
- package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/updateTool.js +2 -2
- package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
- package/dist/types.d.ts +44 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -4
- package/dist/types.js.map +1 -1
- package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts +28 -0
- package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts.map +1 -0
- package/dist/utils/schemaConversion/filterFieldsByAccess.js +71 -0
- package/dist/utils/schemaConversion/filterFieldsByAccess.js.map +1 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts +5 -3
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +1 -1
- package/dist/utils/schemaConversion/getEntityInputSchema.js +40 -6
- package/dist/utils/schemaConversion/getEntityInputSchema.js.map +1 -1
- package/package.json +3 -3
- package/src/endpoint/access.ts +3 -10
- package/src/mcp/builtin/collections/countTool.ts +1 -2
- package/src/mcp/builtin/collections/countVersionsTool.ts +2 -6
- package/src/mcp/builtin/collections/createTool.ts +1 -2
- package/src/mcp/builtin/collections/deleteTool.ts +1 -2
- package/src/mcp/builtin/collections/duplicateTool.ts +1 -2
- package/src/mcp/builtin/collections/findDistinctTool.ts +3 -3
- package/src/mcp/builtin/collections/findTool.ts +2 -3
- package/src/mcp/builtin/collections/findVersionByIDTool.ts +1 -2
- package/src/mcp/builtin/collections/findVersionsTool.ts +1 -2
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +25 -8
- package/src/mcp/builtin/collections/restoreVersionTool.ts +1 -2
- package/src/mcp/builtin/collections/updateTool.ts +2 -3
- package/src/mcp/builtin/getConfigInfoTool.ts +2 -4
- package/src/mcp/builtin/globals/countVersionsTool.ts +2 -6
- package/src/mcp/builtin/globals/findTool.ts +2 -2
- package/src/mcp/builtin/globals/findVersionByIDTool.ts +1 -2
- package/src/mcp/builtin/globals/findVersionsTool.ts +1 -2
- package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +25 -3
- package/src/mcp/builtin/globals/restoreVersionTool.ts +3 -3
- package/src/mcp/builtin/globals/updateTool.ts +2 -2
- package/src/types.ts +43 -7
- package/src/utils/schemaConversion/filterFieldsByAccess.ts +123 -0
- package/src/utils/schemaConversion/getEntityInputSchema.ts +47 -13
- package/dist/utils/localAPIDefaults.d.ts +0 -20
- package/dist/utils/localAPIDefaults.d.ts.map +0 -1
- package/dist/utils/localAPIDefaults.js +0 -19
- package/dist/utils/localAPIDefaults.js.map +0 -1
- package/src/utils/localAPIDefaults.ts +0 -22
|
@@ -5,7 +5,6 @@ import { z } from 'zod'
|
|
|
5
5
|
import { defaultAccess } from '../../../defaultAccess.js'
|
|
6
6
|
import { defineGlobalTool } from '../../../defineTool.js'
|
|
7
7
|
import { getLogger } from '../../../utils/getLogger.js'
|
|
8
|
-
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
9
8
|
import { whereSchema } from '../../../utils/whereSchema.js'
|
|
10
9
|
|
|
11
10
|
const DEFAULT_DESCRIPTION =
|
|
@@ -110,9 +109,9 @@ export const findGlobalVersionsTool = defineGlobalTool({
|
|
|
110
109
|
slug: globalSlug,
|
|
111
110
|
depth,
|
|
112
111
|
limit,
|
|
112
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
113
113
|
page,
|
|
114
114
|
req,
|
|
115
|
-
...localAPIDefaults(authorizedMCP),
|
|
116
115
|
...(fallbackLocale ? { fallbackLocale } : {}),
|
|
117
116
|
...(locale ? { locale } : {}),
|
|
118
117
|
...(pagination !== undefined ? { pagination } : {}),
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { getAccessResults } from 'payload'
|
|
2
|
+
|
|
1
3
|
import { defaultAccess } from '../../../defaultAccess.js'
|
|
2
4
|
import { defineGlobalTool } from '../../../defineTool.js'
|
|
3
5
|
import { getGlobalInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'
|
|
@@ -6,7 +8,7 @@ export const getGlobalSchemaTool = defineGlobalTool({
|
|
|
6
8
|
access: (args) => {
|
|
7
9
|
const permissions = args.permissions?.globals?.[args.globalSlug]
|
|
8
10
|
|
|
9
|
-
return defaultAccess(args) && Boolean(permissions?.
|
|
11
|
+
return defaultAccess(args) && Boolean(permissions?.update)
|
|
10
12
|
},
|
|
11
13
|
annotations: {
|
|
12
14
|
destructiveHint: false,
|
|
@@ -16,8 +18,28 @@ export const getGlobalSchemaTool = defineGlobalTool({
|
|
|
16
18
|
title: 'Get Global Schema',
|
|
17
19
|
},
|
|
18
20
|
description: 'Get the input schema for updating a global.',
|
|
19
|
-
}).handler(({ globalSlug, req }) => {
|
|
20
|
-
const
|
|
21
|
+
}).handler(async ({ authorizedMCP, globalSlug, req }) => {
|
|
22
|
+
const permissions = authorizedMCP.overrideAccess
|
|
23
|
+
? null
|
|
24
|
+
: (await getAccessResults({ req })).globals?.[globalSlug]
|
|
25
|
+
|
|
26
|
+
if (!authorizedMCP.overrideAccess && !permissions?.update) {
|
|
27
|
+
return {
|
|
28
|
+
content: [
|
|
29
|
+
{
|
|
30
|
+
type: 'text',
|
|
31
|
+
text: `Error: MCP access to "getGlobalSchema" is not enabled for global "${globalSlug}"`,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
isError: true,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const inputSchema = getGlobalInputSchema({
|
|
39
|
+
globalSlug,
|
|
40
|
+
req,
|
|
41
|
+
...(permissions ? { permissions } : {}),
|
|
42
|
+
})
|
|
21
43
|
|
|
22
44
|
if (!inputSchema) {
|
|
23
45
|
return {
|
|
@@ -5,9 +5,9 @@ import { z } from 'zod'
|
|
|
5
5
|
import { defaultAccess } from '../../../defaultAccess.js'
|
|
6
6
|
import { defineGlobalTool } from '../../../defineTool.js'
|
|
7
7
|
import { getLogger } from '../../../utils/getLogger.js'
|
|
8
|
-
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
9
8
|
|
|
10
|
-
const DEFAULT_DESCRIPTION =
|
|
9
|
+
const DEFAULT_DESCRIPTION =
|
|
10
|
+
'Restore a global from a previous version in any version-enabled global.'
|
|
11
11
|
|
|
12
12
|
export const restoreGlobalVersionTool = defineGlobalTool({
|
|
13
13
|
access: (args) =>
|
|
@@ -61,8 +61,8 @@ export const restoreGlobalVersionTool = defineGlobalTool({
|
|
|
61
61
|
id,
|
|
62
62
|
slug: globalSlug,
|
|
63
63
|
depth,
|
|
64
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
64
65
|
req,
|
|
65
|
-
...localAPIDefaults(authorizedMCP),
|
|
66
66
|
...(fallbackLocale ? { fallbackLocale } : {}),
|
|
67
67
|
...(locale ? { locale } : {}),
|
|
68
68
|
...(populate ? { populate: populate as PopulateType } : {}),
|
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
getGlobalVirtualFieldNames,
|
|
10
10
|
stripVirtualFields,
|
|
11
11
|
} from '../../../utils/getVirtualFieldNames.js'
|
|
12
|
-
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
13
12
|
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
|
|
14
13
|
import { validateGlobalData } from '../validateEntityData.js'
|
|
15
14
|
|
|
@@ -81,7 +80,8 @@ export const updateGlobalTool = defineGlobalTool({
|
|
|
81
80
|
data: parsedData,
|
|
82
81
|
depth,
|
|
83
82
|
draft,
|
|
84
|
-
|
|
83
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
84
|
+
req,
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
if (locale) {
|
package/src/types.ts
CHANGED
|
@@ -15,7 +15,6 @@ import type {
|
|
|
15
15
|
MaybePromise,
|
|
16
16
|
PayloadRequest,
|
|
17
17
|
SanitizedPermissions,
|
|
18
|
-
User,
|
|
19
18
|
} from 'payload'
|
|
20
19
|
|
|
21
20
|
import type { GetAuthorizedMCPArgs } from './endpoint/access.js'
|
|
@@ -99,6 +98,11 @@ export type GlobalToolHandlerArgs<TSchema = undefined> = {
|
|
|
99
98
|
} & ToolHandlerArgs<TSchema>
|
|
100
99
|
|
|
101
100
|
export type Tool<TSchema extends ToolInputSchema | undefined = ToolInputSchema | undefined> = {
|
|
101
|
+
/**
|
|
102
|
+
* Runs while authorizing each MCP request, before the tool is advertised or called. Return
|
|
103
|
+
* `false` to make the tool unavailable for that request. This is skipped when `overrideAccess`
|
|
104
|
+
* is enabled.
|
|
105
|
+
*/
|
|
102
106
|
access?: (args: MCPAccessArgs) => MaybePromise<boolean>
|
|
103
107
|
annotations?: ToolAnnotations
|
|
104
108
|
description: string
|
|
@@ -113,6 +117,12 @@ export type Tool<TSchema extends ToolInputSchema | undefined = ToolInputSchema |
|
|
|
113
117
|
export type CollectionTool<
|
|
114
118
|
TSchema extends ToolInputSchema | undefined = ToolInputSchema | undefined,
|
|
115
119
|
> = {
|
|
120
|
+
/**
|
|
121
|
+
* Runs while authorizing each MCP request for this collection. Return `false` to reject calls
|
|
122
|
+
* to this tool for the collection. The shared tool is not advertised when no collections allow
|
|
123
|
+
* it, but can remain advertised when it is available for another collection. This is skipped
|
|
124
|
+
* when `overrideAccess` is enabled.
|
|
125
|
+
*/
|
|
116
126
|
access?: (args: CollectionMCPAccessArgs) => MaybePromise<boolean>
|
|
117
127
|
handler: (args: CollectionToolHandlerArgs<TSchema>) => MaybePromise<MCPToolResponse>
|
|
118
128
|
input?: TSchema
|
|
@@ -120,6 +130,12 @@ export type CollectionTool<
|
|
|
120
130
|
|
|
121
131
|
export type GlobalTool<TSchema extends ToolInputSchema | undefined = ToolInputSchema | undefined> =
|
|
122
132
|
{
|
|
133
|
+
/**
|
|
134
|
+
* Runs while authorizing each MCP request for this global. Return `false` to reject calls to
|
|
135
|
+
* this tool for the global. The shared tool is not advertised when no globals allow it, but
|
|
136
|
+
* can remain advertised when it is available for another global. This is skipped when
|
|
137
|
+
* `overrideAccess` is enabled.
|
|
138
|
+
*/
|
|
123
139
|
access?: (args: GlobalMCPAccessArgs) => MaybePromise<boolean>
|
|
124
140
|
handler: (args: GlobalToolHandlerArgs<TSchema>) => MaybePromise<MCPToolResponse>
|
|
125
141
|
input?: TSchema
|
|
@@ -131,6 +147,10 @@ export type GlobalTool<TSchema extends ToolInputSchema | undefined = ToolInputSc
|
|
|
131
147
|
* required handler) from being silently accepted at a built-in key slot.
|
|
132
148
|
*/
|
|
133
149
|
export type MCPBuiltInCollectionToolOverride = {
|
|
150
|
+
/**
|
|
151
|
+
* Replaces the built-in tool's access check. Return `false` to make the tool unavailable for
|
|
152
|
+
* this collection. This is skipped when `overrideAccess` is enabled.
|
|
153
|
+
*/
|
|
134
154
|
access?: (args: CollectionMCPAccessArgs) => MaybePromise<boolean>
|
|
135
155
|
annotations?: ToolAnnotations
|
|
136
156
|
description?: string
|
|
@@ -139,6 +159,10 @@ export type MCPBuiltInCollectionToolOverride = {
|
|
|
139
159
|
}
|
|
140
160
|
|
|
141
161
|
export type MCPBuiltInGlobalToolOverride = {
|
|
162
|
+
/**
|
|
163
|
+
* Replaces the built-in tool's access check. Return `false` to make the tool unavailable for
|
|
164
|
+
* this global. This is skipped when `overrideAccess` is enabled.
|
|
165
|
+
*/
|
|
142
166
|
access?: (args: GlobalMCPAccessArgs) => MaybePromise<boolean>
|
|
143
167
|
annotations?: ToolAnnotations
|
|
144
168
|
description?: string
|
|
@@ -183,6 +207,11 @@ export type PromptHandlerArgs<TSchema = undefined> = {
|
|
|
183
207
|
}
|
|
184
208
|
|
|
185
209
|
export type Prompt<TSchema extends ToolInputSchema = ToolInputSchema> = {
|
|
210
|
+
/**
|
|
211
|
+
* Runs while authorizing each MCP request, before the prompt is advertised or used. Return
|
|
212
|
+
* `false` to make the prompt unavailable for that request. This is skipped when
|
|
213
|
+
* `overrideAccess` is enabled.
|
|
214
|
+
*/
|
|
186
215
|
access?: (args: MCPAccessArgs) => MaybePromise<boolean>
|
|
187
216
|
argsSchema: TSchema
|
|
188
217
|
description: string
|
|
@@ -201,6 +230,11 @@ export type ResourceHandlerArgs = {
|
|
|
201
230
|
}
|
|
202
231
|
|
|
203
232
|
export type Resource = {
|
|
233
|
+
/**
|
|
234
|
+
* Runs while authorizing each MCP request, before the resource is advertised or read. Return
|
|
235
|
+
* `false` to make the resource unavailable for that request. This is skipped when
|
|
236
|
+
* `overrideAccess` is enabled.
|
|
237
|
+
*/
|
|
204
238
|
access?: (args: MCPAccessArgs) => MaybePromise<boolean>
|
|
205
239
|
description: string
|
|
206
240
|
handler: (args: ResourceHandlerArgs) => MaybePromise<{
|
|
@@ -248,7 +282,12 @@ export type MCPPluginConfig = {
|
|
|
248
282
|
serverOptions?: MCPServerOptions
|
|
249
283
|
verboseLogs?: boolean
|
|
250
284
|
}
|
|
251
|
-
/**
|
|
285
|
+
/**
|
|
286
|
+
* Replace the default MCP authorization resolver.
|
|
287
|
+
*
|
|
288
|
+
* This hook replaces the default authentication flow. It must set `req.user` to the
|
|
289
|
+
* authenticated Payload user, or to `null` for an anonymous caller, before returning.
|
|
290
|
+
*/
|
|
252
291
|
overrideGetAuthorizedMCP?: (
|
|
253
292
|
args: {
|
|
254
293
|
pluginConfig: SanitizedMCPPluginConfig
|
|
@@ -312,13 +351,10 @@ export type MCPItem =
|
|
|
312
351
|
| GlobalMCPItem
|
|
313
352
|
|
|
314
353
|
/**
|
|
315
|
-
* The
|
|
316
|
-
*
|
|
317
|
-
* absent from `items`. Tool handlers receive this via `args.authorizedMCP` so
|
|
318
|
-
* they can spread `localAPIDefaults(authorizedMCP)` into every local API call.
|
|
354
|
+
* The MCP items and access mode authorized for this request. The authenticated user is available
|
|
355
|
+
* as `req.user`.
|
|
319
356
|
*/
|
|
320
357
|
export type AuthorizedMCP = {
|
|
321
358
|
items: MCPItem[]
|
|
322
359
|
overrideAccess: boolean
|
|
323
|
-
user: null | User
|
|
324
360
|
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FlattenedBlock,
|
|
3
|
+
FlattenedField,
|
|
4
|
+
Operation,
|
|
5
|
+
SanitizedDocumentPermissions,
|
|
6
|
+
SanitizedFieldsPermissions,
|
|
7
|
+
} from 'payload'
|
|
8
|
+
|
|
9
|
+
type FieldOperation = Exclude<Operation, 'delete'>
|
|
10
|
+
type AllowedFieldOperations = Record<FieldOperation, boolean>
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Filters fields using the access results in `permissions`. `shouldExcludeField` receives the
|
|
14
|
+
* operations allowed by the entity, field, and its parents and decides whether to remove the
|
|
15
|
+
* field.
|
|
16
|
+
*
|
|
17
|
+
* Arrays, groups, named tabs, and blocks are filtered recursively. Rows and unnamed tabs are
|
|
18
|
+
* flattened by Payload, so their children are filtered at the containing level.
|
|
19
|
+
*
|
|
20
|
+
* @example Include fields writable through any collection operation the user can perform.
|
|
21
|
+
* ```ts
|
|
22
|
+
* filterFieldsByAccess({
|
|
23
|
+
* fields,
|
|
24
|
+
* permissions: collectionPermissions,
|
|
25
|
+
* shouldExcludeField: ({ create, update }) => !create && !update,
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export const filterFieldsByAccess = ({
|
|
30
|
+
blocks,
|
|
31
|
+
fields,
|
|
32
|
+
permissions,
|
|
33
|
+
shouldExcludeField,
|
|
34
|
+
}: {
|
|
35
|
+
blocks?: FlattenedBlock[]
|
|
36
|
+
fields: FlattenedField[]
|
|
37
|
+
permissions: SanitizedDocumentPermissions
|
|
38
|
+
shouldExcludeField: (allowedOperations: AllowedFieldOperations) => boolean
|
|
39
|
+
}): FlattenedField[] => {
|
|
40
|
+
const filterFields = (
|
|
41
|
+
nestedFields: FlattenedField[],
|
|
42
|
+
nestedPermissions: SanitizedFieldsPermissions | undefined,
|
|
43
|
+
parentAllowedOperations: AllowedFieldOperations,
|
|
44
|
+
): FlattenedField[] => {
|
|
45
|
+
const accessibleFields: FlattenedField[] = []
|
|
46
|
+
|
|
47
|
+
for (const field of nestedFields) {
|
|
48
|
+
const fieldPermissions = nestedPermissions === true ? true : nestedPermissions?.[field.name]
|
|
49
|
+
const isOperationAllowed = (operation: FieldOperation): boolean =>
|
|
50
|
+
parentAllowedOperations[operation] &&
|
|
51
|
+
(fieldPermissions === true || fieldPermissions?.[operation] === true)
|
|
52
|
+
const allowedOperations: AllowedFieldOperations = {
|
|
53
|
+
create: isOperationAllowed('create'),
|
|
54
|
+
read: isOperationAllowed('read'),
|
|
55
|
+
update: isOperationAllowed('update'),
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (shouldExcludeField(allowedOperations)) {
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (field.type === 'blocks') {
|
|
63
|
+
const accessibleBlocks: (FlattenedBlock | string)[] = []
|
|
64
|
+
|
|
65
|
+
for (const blockOrReference of field.blocks) {
|
|
66
|
+
const block =
|
|
67
|
+
typeof blockOrReference === 'string'
|
|
68
|
+
? blocks?.find(({ slug }) => slug === blockOrReference)
|
|
69
|
+
: blockOrReference
|
|
70
|
+
|
|
71
|
+
if (!block) {
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const blockPermissions =
|
|
76
|
+
fieldPermissions === true
|
|
77
|
+
? true
|
|
78
|
+
: fieldPermissions?.blocks === true
|
|
79
|
+
? true
|
|
80
|
+
: fieldPermissions?.blocks?.[block.slug]
|
|
81
|
+
|
|
82
|
+
accessibleBlocks.push({
|
|
83
|
+
...block,
|
|
84
|
+
flattenedFields: filterFields(
|
|
85
|
+
block.flattenedFields,
|
|
86
|
+
blockPermissions === true ? true : blockPermissions?.fields,
|
|
87
|
+
allowedOperations,
|
|
88
|
+
),
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (accessibleBlocks.length === 0) {
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
accessibleFields.push({ ...field, blocks: accessibleBlocks })
|
|
97
|
+
continue
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (field.type === 'array' || field.type === 'group' || field.type === 'tab') {
|
|
101
|
+
accessibleFields.push({
|
|
102
|
+
...field,
|
|
103
|
+
flattenedFields: filterFields(
|
|
104
|
+
field.flattenedFields,
|
|
105
|
+
fieldPermissions === true ? true : fieldPermissions?.fields,
|
|
106
|
+
allowedOperations,
|
|
107
|
+
),
|
|
108
|
+
})
|
|
109
|
+
continue
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
accessibleFields.push(field)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return accessibleFields
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return filterFields(fields, permissions.fields, {
|
|
119
|
+
create: 'create' in permissions && permissions.create === true,
|
|
120
|
+
read: permissions.read === true,
|
|
121
|
+
update: permissions.update === true,
|
|
122
|
+
})
|
|
123
|
+
}
|
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type CollectionSlug,
|
|
3
3
|
entityToStandaloneJSONSchema,
|
|
4
|
+
type FlattenedField,
|
|
4
5
|
type GlobalSlug,
|
|
5
6
|
type PayloadRequest,
|
|
6
7
|
type SanitizedCollectionConfig,
|
|
8
|
+
type SanitizedCollectionPermission,
|
|
7
9
|
type SanitizedGlobalConfig,
|
|
10
|
+
type SanitizedGlobalPermission,
|
|
8
11
|
} from 'payload'
|
|
9
12
|
|
|
10
13
|
import type { JsonSchemaType } from '../../types.js'
|
|
11
14
|
|
|
15
|
+
import { filterFieldsByAccess } from './filterFieldsByAccess.js'
|
|
12
16
|
import { sanitizeEntitySchema } from './sanitizeEntitySchema.js'
|
|
13
17
|
|
|
14
18
|
export const getCollectionInputSchema = ({
|
|
15
19
|
collectionSlug,
|
|
20
|
+
permissions,
|
|
16
21
|
req,
|
|
17
22
|
}: {
|
|
18
23
|
collectionSlug: CollectionSlug
|
|
24
|
+
permissions?: SanitizedCollectionPermission
|
|
19
25
|
req: PayloadRequest
|
|
20
26
|
}): JsonSchemaType | null => {
|
|
21
27
|
const collection = req.payload.collections[collectionSlug]?.config
|
|
@@ -24,14 +30,27 @@ export const getCollectionInputSchema = ({
|
|
|
24
30
|
return null
|
|
25
31
|
}
|
|
26
32
|
|
|
27
|
-
|
|
33
|
+
if (!permissions) {
|
|
34
|
+
return buildEntityInputSchema({ entity: collection, req })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const fieldsAllowedByAccess = filterFieldsByAccess({
|
|
38
|
+
blocks: req.payload.config.blocks,
|
|
39
|
+
fields: collection.flattenedFields,
|
|
40
|
+
permissions,
|
|
41
|
+
shouldExcludeField: ({ create, update }) => !create && !update,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
return buildEntityInputSchema({ entity: collection, fields: fieldsAllowedByAccess, req })
|
|
28
45
|
}
|
|
29
46
|
|
|
30
47
|
export const getGlobalInputSchema = ({
|
|
31
48
|
globalSlug,
|
|
49
|
+
permissions,
|
|
32
50
|
req,
|
|
33
51
|
}: {
|
|
34
52
|
globalSlug: GlobalSlug
|
|
53
|
+
permissions?: SanitizedGlobalPermission
|
|
35
54
|
req: PayloadRequest
|
|
36
55
|
}): JsonSchemaType | null => {
|
|
37
56
|
const global = req.payload.config.globals.find((globalConfig) => globalConfig.slug === globalSlug)
|
|
@@ -40,23 +59,38 @@ export const getGlobalInputSchema = ({
|
|
|
40
59
|
return null
|
|
41
60
|
}
|
|
42
61
|
|
|
43
|
-
|
|
62
|
+
if (!permissions) {
|
|
63
|
+
return buildEntityInputSchema({ entity: global, req })
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const fieldsAllowedByAccess = filterFieldsByAccess({
|
|
67
|
+
blocks: req.payload.config.blocks,
|
|
68
|
+
fields: global.flattenedFields,
|
|
69
|
+
permissions,
|
|
70
|
+
shouldExcludeField: ({ create, update }) => !create && !update,
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
return buildEntityInputSchema({ entity: global, fields: fieldsAllowedByAccess, req })
|
|
44
74
|
}
|
|
45
75
|
|
|
46
76
|
const buildEntityInputSchema = ({
|
|
47
77
|
entity,
|
|
78
|
+
fields = entity.flattenedFields,
|
|
48
79
|
req,
|
|
49
80
|
}: {
|
|
50
81
|
entity: SanitizedCollectionConfig | SanitizedGlobalConfig
|
|
82
|
+
fields?: FlattenedField[]
|
|
51
83
|
req: PayloadRequest
|
|
52
|
-
}): JsonSchemaType =>
|
|
53
|
-
// The
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
84
|
+
}): JsonSchemaType => {
|
|
85
|
+
// The core schema generator reads flattenedFields from the entity and has no fields argument.
|
|
86
|
+
const entityForSchema = { ...entity, flattenedFields: fields }
|
|
87
|
+
const schema = entityToStandaloneJSONSchema({
|
|
88
|
+
config: req.payload.config,
|
|
89
|
+
defaultIDType: req.payload.db.defaultIDType,
|
|
90
|
+
entity: entityForSchema,
|
|
91
|
+
i18n: req.i18n,
|
|
92
|
+
variant: 'input',
|
|
93
|
+
}) as unknown as JsonSchemaType
|
|
94
|
+
|
|
95
|
+
return sanitizeEntitySchema(schema)
|
|
96
|
+
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { AuthorizedMCP } from '../types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Returns the `user` and `overrideAccess` arguments every Payload local API call
|
|
4
|
-
* should receive when initiated from an MCP request. Spread the result into the
|
|
5
|
-
* call's arg object so the two pieces of caller state can't drift apart.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* await req.payload.create({
|
|
10
|
-
* collection: 'posts',
|
|
11
|
-
* data,
|
|
12
|
-
* ...localAPIDefaults(authorizedMCP),
|
|
13
|
-
* })
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export declare const localAPIDefaults: (authorizedMCP: AuthorizedMCP) => {
|
|
17
|
-
overrideAccess: boolean;
|
|
18
|
-
user: AuthorizedMCP["user"];
|
|
19
|
-
};
|
|
20
|
-
//# sourceMappingURL=localAPIDefaults.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"localAPIDefaults.d.ts","sourceRoot":"","sources":["../../src/utils/localAPIDefaults.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAEhD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,gBAAgB,GAC3B,eAAe,aAAa,KAC3B;IAAE,cAAc,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;CAGvD,CAAA"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the `user` and `overrideAccess` arguments every Payload local API call
|
|
3
|
-
* should receive when initiated from an MCP request. Spread the result into the
|
|
4
|
-
* call's arg object so the two pieces of caller state can't drift apart.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```ts
|
|
8
|
-
* await req.payload.create({
|
|
9
|
-
* collection: 'posts',
|
|
10
|
-
* data,
|
|
11
|
-
* ...localAPIDefaults(authorizedMCP),
|
|
12
|
-
* })
|
|
13
|
-
* ```
|
|
14
|
-
*/ export const localAPIDefaults = (authorizedMCP)=>({
|
|
15
|
-
overrideAccess: authorizedMCP.overrideAccess,
|
|
16
|
-
user: authorizedMCP.user
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
//# sourceMappingURL=localAPIDefaults.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/localAPIDefaults.ts"],"sourcesContent":["import type { AuthorizedMCP } from '../types.js'\n\n/**\n * Returns the `user` and `overrideAccess` arguments every Payload local API call\n * should receive when initiated from an MCP request. Spread the result into the\n * call's arg object so the two pieces of caller state can't drift apart.\n *\n * @example\n * ```ts\n * await req.payload.create({\n * collection: 'posts',\n * data,\n * ...localAPIDefaults(authorizedMCP),\n * })\n * ```\n */\nexport const localAPIDefaults = (\n authorizedMCP: AuthorizedMCP,\n): { overrideAccess: boolean; user: AuthorizedMCP['user'] } => ({\n overrideAccess: authorizedMCP.overrideAccess,\n user: authorizedMCP.user,\n})\n"],"names":["localAPIDefaults","authorizedMCP","overrideAccess","user"],"mappings":"AAEA;;;;;;;;;;;;;CAaC,GACD,OAAO,MAAMA,mBAAmB,CAC9BC,gBAC8D,CAAA;QAC9DC,gBAAgBD,cAAcC,cAAc;QAC5CC,MAAMF,cAAcE,IAAI;IAC1B,CAAA,EAAE"}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { AuthorizedMCP } from '../types.js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Returns the `user` and `overrideAccess` arguments every Payload local API call
|
|
5
|
-
* should receive when initiated from an MCP request. Spread the result into the
|
|
6
|
-
* call's arg object so the two pieces of caller state can't drift apart.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```ts
|
|
10
|
-
* await req.payload.create({
|
|
11
|
-
* collection: 'posts',
|
|
12
|
-
* data,
|
|
13
|
-
* ...localAPIDefaults(authorizedMCP),
|
|
14
|
-
* })
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
export const localAPIDefaults = (
|
|
18
|
-
authorizedMCP: AuthorizedMCP,
|
|
19
|
-
): { overrideAccess: boolean; user: AuthorizedMCP['user'] } => ({
|
|
20
|
-
overrideAccess: authorizedMCP.overrideAccess,
|
|
21
|
-
user: authorizedMCP.user,
|
|
22
|
-
})
|