@payloadcms/plugin-mcp 4.0.0-internal.c2b57ce → 4.0.0-internal.c6494b5
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/LICENSE.md +1 -1
- package/dist/defaultAccess.d.ts +3 -0
- package/dist/defaultAccess.d.ts.map +1 -0
- package/dist/defaultAccess.js +3 -0
- package/dist/defaultAccess.js.map +1 -0
- package/dist/defineTool.d.ts +8 -8
- package/dist/defineTool.d.ts.map +1 -1
- package/dist/endpoint/access.d.ts +12 -5
- package/dist/endpoint/access.d.ts.map +1 -1
- package/dist/endpoint/access.js +75 -88
- package/dist/endpoint/access.js.map +1 -1
- package/dist/endpoint/index.d.ts +1 -1
- package/dist/endpoint/index.d.ts.map +1 -1
- package/dist/endpoint/index.js +98 -18
- package/dist/endpoint/index.js.map +1 -1
- package/dist/exports/client.d.ts +1 -1
- package/dist/exports/client.d.ts.map +1 -1
- package/dist/exports/client.js +1 -1
- package/dist/exports/client.js.map +1 -1
- package/dist/exports/internal.d.ts +2 -0
- package/dist/exports/internal.d.ts.map +1 -0
- package/dist/exports/internal.js +3 -0
- package/dist/exports/internal.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -20
- package/dist/index.js.map +1 -1
- package/dist/mcp/buildMcpServer.d.ts +6 -6
- package/dist/mcp/buildMcpServer.d.ts.map +1 -1
- package/dist/mcp/buildMcpServer.js +146 -74
- package/dist/mcp/buildMcpServer.js.map +1 -1
- package/dist/mcp/builtin/collections/authTools.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/authTools.js +123 -61
- package/dist/mcp/builtin/collections/authTools.js.map +1 -1
- package/dist/mcp/builtin/collections/countTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/countTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/countTool.js +63 -0
- package/dist/mcp/builtin/collections/countTool.js.map +1 -0
- package/dist/mcp/builtin/collections/countVersionsTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/countVersionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/countVersionsTool.js +60 -0
- package/dist/mcp/builtin/collections/countVersionsTool.js.map +1 -0
- package/dist/mcp/builtin/collections/createTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/createTool.js +114 -64
- package/dist/mcp/builtin/collections/createTool.js.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.js +55 -61
- package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
- package/dist/mcp/builtin/collections/duplicateTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/duplicateTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/duplicateTool.js +90 -0
- package/dist/mcp/builtin/collections/duplicateTool.js.map +1 -0
- package/dist/mcp/builtin/collections/fileInput.d.ts +28 -0
- package/dist/mcp/builtin/collections/fileInput.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/fileInput.js +88 -0
- package/dist/mcp/builtin/collections/fileInput.js.map +1 -0
- package/dist/mcp/builtin/collections/findDistinctTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/findDistinctTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/findDistinctTool.js +77 -0
- package/dist/mcp/builtin/collections/findDistinctTool.js.map +1 -0
- package/dist/mcp/builtin/collections/findTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findTool.js +61 -74
- package/dist/mcp/builtin/collections/findTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/findVersionByIDTool.js +74 -0
- package/dist/mcp/builtin/collections/findVersionByIDTool.js.map +1 -0
- package/dist/mcp/builtin/collections/findVersionsTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/findVersionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/findVersionsTool.js +84 -0
- package/dist/mcp/builtin/collections/findVersionsTool.js.map +1 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +103 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -0
- package/dist/mcp/builtin/collections/restoreVersionTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/restoreVersionTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/restoreVersionTool.js +69 -0
- package/dist/mcp/builtin/collections/restoreVersionTool.js.map +1 -0
- package/dist/mcp/builtin/collections/updateTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.js +118 -133
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- package/dist/mcp/builtin/collections/uploadInstructionsTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/uploadInstructionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/uploadInstructionsTool.js +55 -0
- package/dist/mcp/builtin/collections/uploadInstructionsTool.js.map +1 -0
- package/dist/mcp/builtin/formatEntityError.d.ts +16 -0
- package/dist/mcp/builtin/formatEntityError.d.ts.map +1 -0
- package/dist/mcp/builtin/formatEntityError.js +55 -0
- package/dist/mcp/builtin/formatEntityError.js.map +1 -0
- package/dist/mcp/builtin/getConfigInfoTool.d.ts +2 -0
- package/dist/mcp/builtin/getConfigInfoTool.d.ts.map +1 -0
- package/dist/mcp/builtin/getConfigInfoTool.js +69 -0
- package/dist/mcp/builtin/getConfigInfoTool.js.map +1 -0
- package/dist/mcp/builtin/globals/countVersionsTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/countVersionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/countVersionsTool.js +60 -0
- package/dist/mcp/builtin/globals/countVersionsTool.js.map +1 -0
- package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/findTool.js +27 -36
- package/dist/mcp/builtin/globals/findTool.js.map +1 -1
- package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/findVersionByIDTool.js +68 -0
- package/dist/mcp/builtin/globals/findVersionByIDTool.js.map +1 -0
- package/dist/mcp/builtin/globals/findVersionsTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/findVersionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/findVersionsTool.js +78 -0
- package/dist/mcp/builtin/globals/findVersionsTool.js.map +1 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js +81 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js.map +1 -0
- package/dist/mcp/builtin/globals/restoreVersionTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/restoreVersionTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/restoreVersionTool.js +70 -0
- package/dist/mcp/builtin/globals/restoreVersionTool.js.map +1 -0
- package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/updateTool.js +44 -58
- package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
- package/dist/mcp/builtinTools.d.ts +150 -19
- package/dist/mcp/builtinTools.d.ts.map +1 -1
- package/dist/mcp/builtinTools.js +125 -20
- package/dist/mcp/builtinTools.js.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts +0 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.js +124 -58
- package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
- package/dist/stdio.d.ts +3 -3
- package/dist/stdio.d.ts.map +1 -1
- package/dist/stdio.js +86 -47
- package/dist/stdio.js.map +1 -1
- package/dist/types.d.ts +123 -114
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -4
- package/dist/types.js.map +1 -1
- package/dist/utils/camelCase.d.ts.map +1 -1
- package/dist/utils/getPluginConfig.d.ts +1 -1
- package/dist/utils/getPluginConfig.js +1 -1
- package/dist/utils/getPluginConfig.js.map +1 -1
- package/dist/utils/resolveProjectRoot.d.ts.map +1 -1
- package/dist/utils/toStandardSchema.d.ts.map +1 -1
- package/package.json +14 -21
- package/src/defaultAccess.ts +3 -0
- package/src/endpoint/access.ts +80 -101
- package/src/endpoint/index.ts +88 -16
- package/src/exports/client.ts +2 -1
- package/src/exports/internal.ts +1 -0
- package/src/index.ts +12 -20
- package/src/mcp/buildMcpServer.ts +180 -110
- package/src/mcp/builtin/collections/authTools.ts +122 -75
- package/src/mcp/builtin/collections/countTool.ts +61 -0
- package/src/mcp/builtin/collections/countVersionsTool.ts +60 -0
- package/src/mcp/builtin/collections/createTool.ts +117 -88
- package/src/mcp/builtin/collections/deleteTool.ts +48 -74
- package/src/mcp/builtin/collections/duplicateTool.ts +84 -0
- package/src/mcp/builtin/collections/fileInput.spec.ts +93 -0
- package/src/mcp/builtin/collections/fileInput.ts +127 -0
- package/src/mcp/builtin/collections/findDistinctTool.ts +67 -0
- package/src/mcp/builtin/collections/findTool.ts +57 -110
- package/src/mcp/builtin/collections/findVersionByIDTool.ts +66 -0
- package/src/mcp/builtin/collections/findVersionsTool.ts +83 -0
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +91 -0
- package/src/mcp/builtin/collections/restoreVersionTool.ts +65 -0
- package/src/mcp/builtin/collections/updateTool.ts +106 -138
- package/src/mcp/builtin/collections/uploadInstructionsTool.ts +64 -0
- package/src/mcp/builtin/formatEntityError.ts +77 -0
- package/src/mcp/builtin/getConfigInfoTool.ts +68 -0
- package/src/mcp/builtin/globals/countVersionsTool.ts +60 -0
- package/src/mcp/builtin/globals/findTool.ts +27 -57
- package/src/mcp/builtin/globals/findVersionByIDTool.ts +64 -0
- package/src/mcp/builtin/globals/findVersionsTool.ts +69 -0
- package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +73 -0
- package/src/mcp/builtin/globals/restoreVersionTool.ts +68 -0
- package/src/mcp/builtin/globals/updateTool.ts +51 -77
- package/src/mcp/builtinTools.ts +118 -41
- package/src/mcp/sanitizeMCPConfig.ts +120 -78
- package/src/stdio.ts +80 -47
- package/src/types.ts +141 -120
- package/src/utils/getPluginConfig.ts +1 -1
- package/dist/collection/getAccessField.d.ts +0 -12
- package/dist/collection/getAccessField.d.ts.map +0 -1
- package/dist/collection/getAccessField.js +0 -57
- package/dist/collection/getAccessField.js.map +0 -1
- package/dist/collection/index.d.ts +0 -6
- package/dist/collection/index.d.ts.map +0 -1
- package/dist/collection/index.js +0 -59
- package/dist/collection/index.js.map +0 -1
- package/dist/components/AccessField/index.client.d.ts +0 -10
- package/dist/components/AccessField/index.client.d.ts.map +0 -1
- package/dist/components/AccessField/index.client.js +0 -305
- package/dist/components/AccessField/index.client.js.map +0 -1
- package/dist/components/AccessField/index.css +0 -93
- package/dist/utils/getVirtualFieldNames.d.ts +0 -14
- package/dist/utils/getVirtualFieldNames.d.ts.map +0 -1
- package/dist/utils/getVirtualFieldNames.js +0 -35
- package/dist/utils/getVirtualFieldNames.js.map +0 -1
- 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/dist/utils/schemaConversion/prepareCollectionSchema.d.ts +0 -7
- package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/prepareCollectionSchema.js +0 -37
- package/dist/utils/schemaConversion/prepareCollectionSchema.js.map +0 -1
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts +0 -7
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js +0 -20
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js.map +0 -1
- package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts +0 -13
- package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/sanitizeJsonSchema.js +0 -56
- package/dist/utils/schemaConversion/sanitizeJsonSchema.js.map +0 -1
- package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts +0 -20
- package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts.map +0 -1
- package/dist/utils/schemaConversion/simplifyRelationshipFields.js +0 -56
- package/dist/utils/schemaConversion/simplifyRelationshipFields.js.map +0 -1
- package/dist/utils/schemaConversion/transformPointFields.d.ts +0 -3
- package/dist/utils/schemaConversion/transformPointFields.d.ts.map +0 -1
- package/dist/utils/schemaConversion/transformPointFields.js +0 -57
- package/dist/utils/schemaConversion/transformPointFields.js.map +0 -1
- package/dist/utils/transformPointDataToPayload.d.ts +0 -7
- package/dist/utils/transformPointDataToPayload.d.ts.map +0 -1
- package/dist/utils/transformPointDataToPayload.js +0 -28
- package/dist/utils/transformPointDataToPayload.js.map +0 -1
- package/src/collection/getAccessField.ts +0 -64
- package/src/collection/index.ts +0 -63
- package/src/components/AccessField/index.client.tsx +0 -344
- package/src/components/AccessField/index.css +0 -93
- package/src/utils/getVirtualFieldNames.ts +0 -53
- package/src/utils/localAPIDefaults.ts +0 -22
- package/src/utils/schemaConversion/prepareCollectionSchema.ts +0 -39
- package/src/utils/schemaConversion/removeVirtualFieldsFromSchema.ts +0 -27
- package/src/utils/schemaConversion/sanitizeJsonSchema.ts +0 -62
- package/src/utils/schemaConversion/simplifyRelationshipFields.ts +0 -70
- package/src/utils/schemaConversion/transformPointFields.ts +0 -56
- package/src/utils/transformPointDataToPayload.ts +0 -40
package/src/endpoint/access.ts
CHANGED
|
@@ -1,132 +1,111 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { PayloadRequest, SanitizedPermissions } from 'payload'
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import { UnauthorizedError } from 'payload'
|
|
3
|
+
import { getAccessResults, UnauthorizedError } from 'payload'
|
|
5
4
|
|
|
6
|
-
import type { AuthorizedMCP,
|
|
5
|
+
import type { AuthorizedMCP, MCPItem } from '../types.js'
|
|
7
6
|
|
|
8
|
-
import { getLogger } from '../utils/getLogger.js'
|
|
9
7
|
import { getPluginConfig } from '../utils/getPluginConfig.js'
|
|
10
8
|
|
|
9
|
+
export type GetAuthorizedMCPArgs = {
|
|
10
|
+
overrideAccess: boolean
|
|
11
|
+
req: PayloadRequest
|
|
12
|
+
}
|
|
13
|
+
|
|
11
14
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
15
|
+
* Authenticates an MCP request and removes items the user cannot access.
|
|
16
|
+
* `overrideAccess` skips Payload and MCP item access checks.
|
|
14
17
|
*/
|
|
15
|
-
export const getAuthorizedMCP: (args:
|
|
18
|
+
export const getAuthorizedMCP: (args: GetAuthorizedMCPArgs) => Promise<AuthorizedMCP> = async ({
|
|
19
|
+
overrideAccess,
|
|
16
20
|
req,
|
|
17
21
|
}) => {
|
|
18
|
-
const logger = getLogger({ payload: req.payload })
|
|
19
22
|
const pluginConfig = getPluginConfig({ config: req.payload.config })
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const buildAuthorized = (apiKeyDoc: MCPAPIKeysDoc): AuthorizedMCP => ({
|
|
25
|
-
items: pluginConfig.items.filter((item) => {
|
|
26
|
-
switch (item.type) {
|
|
27
|
-
case 'collectionTool':
|
|
28
|
-
return apiKeyDoc.access.collections?.[item.collectionSlug]?.[item.key] !== false
|
|
29
|
-
case 'globalTool':
|
|
30
|
-
return apiKeyDoc.access.globals?.[item.globalSlug]?.[item.key] !== false
|
|
31
|
-
case 'prompt':
|
|
32
|
-
return apiKeyDoc.access.prompts?.[item.key] !== false
|
|
33
|
-
case 'resource':
|
|
34
|
-
return apiKeyDoc.access.resources?.[item.key] !== false
|
|
35
|
-
case 'tool':
|
|
36
|
-
return apiKeyDoc.access.tools?.[item.key] !== false
|
|
37
|
-
}
|
|
38
|
-
}),
|
|
39
|
-
overrideAccess:
|
|
40
|
-
typeof apiKeyDoc.overrideAccess === 'boolean' ? apiKeyDoc.overrideAccess : false,
|
|
41
|
-
user: apiKeyDoc.user,
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
if (pluginConfig.overrideAuth) {
|
|
45
|
-
return await pluginConfig.overrideAuth({
|
|
46
|
-
getAPIKeyDoc: (overrideApiKey) => getAPIKeyDoc({ logger, overrideApiKey, pluginConfig, req }),
|
|
47
|
-
getAuthorizedMCP: ({ apiKeyDoc }) => buildAuthorized(apiKeyDoc),
|
|
24
|
+
if (pluginConfig.overrideGetAuthorizedMCP) {
|
|
25
|
+
return await pluginConfig.overrideGetAuthorizedMCP({
|
|
26
|
+
overrideAccess,
|
|
48
27
|
pluginConfig,
|
|
49
28
|
req,
|
|
50
29
|
})
|
|
51
30
|
}
|
|
52
31
|
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
32
|
+
if (req.headers) {
|
|
33
|
+
const headers = new Headers(req.headers)
|
|
34
|
+
const hasAuthorization = headers.has('Authorization')
|
|
35
|
+
|
|
36
|
+
headers.set('DisableAutologin', 'true')
|
|
37
|
+
req.user = (await req.payload.auth({ headers, req })).user
|
|
38
|
+
|
|
39
|
+
if (hasAuthorization && !req.user) {
|
|
40
|
+
throw new UnauthorizedError(req.t)
|
|
41
|
+
}
|
|
61
42
|
}
|
|
62
43
|
|
|
63
|
-
return
|
|
44
|
+
return {
|
|
45
|
+
items: await filterMCPItems({
|
|
46
|
+
items: pluginConfig.items,
|
|
47
|
+
overrideAccess,
|
|
48
|
+
req,
|
|
49
|
+
}),
|
|
50
|
+
overrideAccess,
|
|
51
|
+
}
|
|
64
52
|
}
|
|
65
53
|
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
pluginConfig,
|
|
54
|
+
export const filterMCPItems = async ({
|
|
55
|
+
items,
|
|
56
|
+
overrideAccess,
|
|
70
57
|
req,
|
|
71
58
|
}: {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
pluginConfig: ReturnType<typeof getPluginConfig>
|
|
59
|
+
items: MCPItem[]
|
|
60
|
+
overrideAccess: boolean
|
|
75
61
|
req: PayloadRequest
|
|
76
|
-
}): Promise<
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const apiKey =
|
|
81
|
-
overrideApiKey ?? (hasBearerToken ? authHeader?.replace('Bearer ', '').trim() || null : null)
|
|
82
|
-
|
|
83
|
-
if (!apiKey) {
|
|
84
|
-
throw new UnauthorizedError()
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const sha256APIKeyIndex = crypto
|
|
88
|
-
.createHmac('sha256', req.payload.secret)
|
|
89
|
-
.update(apiKey)
|
|
90
|
-
.digest('hex')
|
|
91
|
-
|
|
92
|
-
const doc = await req.payload.db.findOne<MCPAPIKeysDoc>({
|
|
93
|
-
collection: 'payload-mcp-api-keys',
|
|
94
|
-
req,
|
|
95
|
-
where: {
|
|
96
|
-
apiKeyIndex: { equals: sha256APIKeyIndex },
|
|
97
|
-
},
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
if (!doc || !doc.user) {
|
|
101
|
-
throw new UnauthorizedError()
|
|
62
|
+
}): Promise<MCPItem[]> => {
|
|
63
|
+
// Match Payload core: overrideAccess bypasses access evaluation
|
|
64
|
+
if (overrideAccess) {
|
|
65
|
+
return items
|
|
102
66
|
}
|
|
103
67
|
|
|
104
|
-
|
|
68
|
+
const authorizedItems: MCPItem[] = []
|
|
105
69
|
|
|
106
|
-
const
|
|
107
|
-
const userID =
|
|
108
|
-
typeof userRef === 'object' && userRef !== null && 'id' in userRef
|
|
109
|
-
? userRef.id
|
|
110
|
-
: (userRef as unknown as DefaultDocumentIDType)
|
|
70
|
+
const permissions = await getAccessResults({ req })
|
|
111
71
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
req,
|
|
118
|
-
})) as null | TypedUser
|
|
119
|
-
|
|
120
|
-
if (!user) {
|
|
121
|
-
throw new UnauthorizedError()
|
|
72
|
+
for (const item of items) {
|
|
73
|
+
if (!(await checkItemAccess({ item, permissions, req }))) {
|
|
74
|
+
continue
|
|
75
|
+
}
|
|
76
|
+
authorizedItems.push(item)
|
|
122
77
|
}
|
|
123
78
|
|
|
124
|
-
return
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
79
|
+
return authorizedItems
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Runs MCP item access callbacks
|
|
84
|
+
*/
|
|
85
|
+
const checkItemAccess = async ({
|
|
86
|
+
item,
|
|
87
|
+
permissions,
|
|
88
|
+
req,
|
|
89
|
+
}: {
|
|
90
|
+
item: MCPItem
|
|
91
|
+
permissions?: SanitizedPermissions
|
|
92
|
+
req: PayloadRequest
|
|
93
|
+
}): Promise<boolean> => {
|
|
94
|
+
switch (item.type) {
|
|
95
|
+
case 'collectionTool':
|
|
96
|
+
return (
|
|
97
|
+
!item.tool.access ||
|
|
98
|
+
(await item.tool.access({ slug: item.collectionSlug, permissions, req }))
|
|
99
|
+
)
|
|
100
|
+
case 'globalTool':
|
|
101
|
+
return (
|
|
102
|
+
!item.tool.access || (await item.tool.access({ slug: item.globalSlug, permissions, req }))
|
|
103
|
+
)
|
|
104
|
+
case 'prompt':
|
|
105
|
+
return !item.prompt.access || (await item.prompt.access({ permissions, req }))
|
|
106
|
+
case 'resource':
|
|
107
|
+
return !item.resource.access || (await item.resource.access({ permissions, req }))
|
|
108
|
+
case 'tool':
|
|
109
|
+
return !item.tool.access || (await item.tool.access({ permissions, req }))
|
|
131
110
|
}
|
|
132
111
|
}
|
package/src/endpoint/index.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type { PayloadHandler, PayloadRequest } from 'payload'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createMcpHandler,
|
|
5
|
+
isLegacyRequest,
|
|
6
|
+
readRequestBody,
|
|
7
|
+
WebStandardStreamableHTTPServerTransport,
|
|
8
|
+
} from '@modelcontextprotocol/server'
|
|
9
|
+
import { APIError } from 'payload'
|
|
3
10
|
|
|
4
11
|
import { buildMcpServer } from '../mcp/buildMcpServer.js'
|
|
5
12
|
import { getPluginConfig } from '../utils/getPluginConfig.js'
|
|
@@ -7,29 +14,94 @@ import { getAuthorizedMCP } from './access.js'
|
|
|
7
14
|
|
|
8
15
|
export const mcpEndpoint: PayloadHandler = async (req) => {
|
|
9
16
|
if (!req.url) {
|
|
10
|
-
throw new
|
|
17
|
+
throw new APIError('Missing request URL', 400)
|
|
11
18
|
}
|
|
12
19
|
|
|
13
20
|
req.payloadAPI = 'MCP' as const
|
|
14
21
|
|
|
15
22
|
const pluginConfig = getPluginConfig({ config: req.payload.config })
|
|
16
|
-
const
|
|
23
|
+
const overrideAccessParam = new URL(req.url).searchParams.get('overrideAccess')
|
|
24
|
+
|
|
25
|
+
if (overrideAccessParam !== null && process.env.NODE_ENV !== 'development') {
|
|
26
|
+
throw new APIError('MCP overrideAccess is only available in development.', 400)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let overrideAccess = false
|
|
30
|
+
if (overrideAccessParam === 'true') {
|
|
31
|
+
overrideAccess = true
|
|
32
|
+
} else if (overrideAccessParam !== null && overrideAccessParam !== 'false') {
|
|
33
|
+
throw new APIError('MCP overrideAccess must be "true" or "false".', 400)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const authorizedMCP = await getAuthorizedMCP({ overrideAccess, req })
|
|
37
|
+
const maxRequestBodySize = pluginConfig.mcp?.maxRequestBodySize
|
|
38
|
+
// Payload augments the original web-standard Request in place.
|
|
39
|
+
const mcpRequest = req as PayloadRequest & Request
|
|
40
|
+
const parsedBody = await parseRequestBody({ maxRequestBodySize, request: mcpRequest })
|
|
17
41
|
|
|
18
|
-
|
|
42
|
+
// Keep the old JSON-only, stateless behavior because the SDK's 2025 fallback uses SSE.
|
|
43
|
+
if (await isLegacyRequest(mcpRequest, parsedBody, { maxRequestBodySize })) {
|
|
44
|
+
const server = buildMcpServer({ authorizedMCP, pluginConfig, req })
|
|
45
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
46
|
+
enableJsonResponse: true,
|
|
47
|
+
maxRequestBodySize,
|
|
48
|
+
sessionIdGenerator: undefined, // stateless mode
|
|
49
|
+
})
|
|
50
|
+
transport.onerror = (err) => {
|
|
51
|
+
req.payload.logger.error({ err, msg: 'Error serving legacy MCP request' })
|
|
52
|
+
}
|
|
19
53
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
54
|
+
try {
|
|
55
|
+
await server.connect(transport)
|
|
56
|
+
return await transport.handleRequest(mcpRequest, { parsedBody })
|
|
57
|
+
} finally {
|
|
58
|
+
await server.close().catch((err) => {
|
|
59
|
+
req.payload.logger.error({ err, msg: 'Error closing MCP server' })
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const handler = createMcpHandler(() => buildMcpServer({ authorizedMCP, pluginConfig, req }), {
|
|
65
|
+
legacy: 'reject',
|
|
66
|
+
maxRequestBodySize,
|
|
67
|
+
// SDK subscriptions always use SSE, so disable them to keep every response JSON-only.
|
|
68
|
+
maxSubscriptions: 0,
|
|
69
|
+
onerror: (err) => {
|
|
70
|
+
req.payload.logger.error({ err, msg: 'Error serving modern MCP request' })
|
|
71
|
+
},
|
|
72
|
+
responseMode: 'json',
|
|
23
73
|
})
|
|
24
74
|
|
|
25
|
-
|
|
75
|
+
try {
|
|
76
|
+
return await handler.fetch(mcpRequest, { parsedBody })
|
|
77
|
+
} finally {
|
|
78
|
+
await handler.close().catch((err) => {
|
|
79
|
+
req.payload.logger.error({ err, msg: 'Error closing modern MCP handler' })
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
}
|
|
26
83
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Parses a POST body once so the era check and the SDK handler can share it. The SDK
|
|
86
|
+
* skips its size limit for a pre-parsed body, so this reads with the same limit. It reads
|
|
87
|
+
* a clone and returns `undefined` for any body the SDK should reject (too large, unreadable,
|
|
88
|
+
* or not JSON), leaving the original request for the SDK to read and answer.
|
|
89
|
+
*/
|
|
90
|
+
const parseRequestBody = async ({
|
|
91
|
+
maxRequestBodySize,
|
|
92
|
+
request,
|
|
93
|
+
}: {
|
|
94
|
+
maxRequestBodySize?: number
|
|
95
|
+
request: Request
|
|
96
|
+
}): Promise<unknown> => {
|
|
97
|
+
if (request.method.toUpperCase() !== 'POST') {
|
|
98
|
+
return undefined
|
|
99
|
+
}
|
|
33
100
|
|
|
34
|
-
|
|
101
|
+
try {
|
|
102
|
+
const body = await readRequestBody(request.clone(), maxRequestBodySize)
|
|
103
|
+
return body.tooLarge ? undefined : JSON.parse(body.text)
|
|
104
|
+
} catch {
|
|
105
|
+
return undefined
|
|
106
|
+
}
|
|
35
107
|
}
|
package/src/exports/client.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { filterMCPItems, getAuthorizedMCP } from '../endpoint/access.js'
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { definePlugin } from 'payload'
|
|
2
2
|
|
|
3
3
|
import type { AuthorizedMCP, MCPPluginConfig, SanitizedMCPPluginConfig } from './types.js'
|
|
4
4
|
|
|
5
|
-
import { getAPIKeysCollection } from './collection/index.js'
|
|
6
5
|
import { mcpEndpoint } from './endpoint/index.js'
|
|
7
6
|
import { sanitizeMCPConfig } from './mcp/sanitizeMCPConfig.js'
|
|
8
7
|
|
|
9
8
|
declare module 'payload' {
|
|
10
|
-
export interface
|
|
11
|
-
|
|
9
|
+
export interface PayloadRequestAPI {
|
|
10
|
+
MCP: true
|
|
12
11
|
}
|
|
13
12
|
interface RegisteredPlugins {
|
|
14
13
|
/** After the plugin's `plugin` callback runs, `options` holds the sanitized config. */
|
|
@@ -23,18 +22,8 @@ export { defineCollectionTool, defineGlobalTool, definePrompt, defineTool } from
|
|
|
23
22
|
export const mcpPlugin = definePlugin<MCPPluginConfig>({
|
|
24
23
|
slug: '@payloadcms/plugin-mcp',
|
|
25
24
|
order: 10,
|
|
26
|
-
plugin: ({ config,
|
|
27
|
-
|
|
28
|
-
// collection, Payload's later sanitize would pick it as `admin.user`.
|
|
29
|
-
// Pre-seed the default user collection to prevent that.
|
|
30
|
-
if (!config.admin?.user) {
|
|
31
|
-
const firstCollectionWithAuth = (config.collections ?? []).find(({ auth }) => Boolean(auth))
|
|
32
|
-
if (!firstCollectionWithAuth) {
|
|
33
|
-
;(config.collections ??= []).push(defaultUserCollection)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const pluginConfig = sanitizeMCPConfig({ config, pluginConfig: rawConfig })
|
|
25
|
+
plugin: ({ config, options, plugins }) => {
|
|
26
|
+
const pluginConfig = sanitizeMCPConfig({ config, pluginConfig: options })
|
|
38
27
|
|
|
39
28
|
// Stash the sanitized config on plugin options so `getPluginConfig()` reads it.
|
|
40
29
|
const registered = plugins['@payloadcms/plugin-mcp']
|
|
@@ -43,10 +32,6 @@ export const mcpPlugin = definePlugin<MCPPluginConfig>({
|
|
|
43
32
|
registered.sanitizedOptions = pluginConfig as unknown as typeof registered.options
|
|
44
33
|
}
|
|
45
34
|
|
|
46
|
-
;(config.collections ??= []).push(getAPIKeysCollection({ pluginConfig }))
|
|
47
|
-
|
|
48
|
-
// Keep the API-keys collection registered even when disabled, so DB schema
|
|
49
|
-
// and generated types don't drift between enabled/disabled environments.
|
|
50
35
|
if (pluginConfig.disabled) {
|
|
51
36
|
return config
|
|
52
37
|
}
|
|
@@ -57,6 +42,13 @@ export const mcpPlugin = definePlugin<MCPPluginConfig>({
|
|
|
57
42
|
...(config.endpoints ?? []),
|
|
58
43
|
// Payload prefixes /api, so the full path is /api/mcp.
|
|
59
44
|
{ handler: mcpEndpoint, method: 'post', path: '/mcp' },
|
|
45
|
+
// Streamable HTTP's optional server=>client GET stream. We don't offer
|
|
46
|
+
// one, so answer 405 per spec-— clients then skip it cleanly
|
|
47
|
+
{
|
|
48
|
+
handler: () => new Response(null, { headers: { Allow: 'POST' }, status: 405 }),
|
|
49
|
+
method: 'get',
|
|
50
|
+
path: '/mcp',
|
|
51
|
+
},
|
|
60
52
|
],
|
|
61
53
|
}
|
|
62
54
|
},
|