@payloadcms/plugin-mcp 4.0.0-internal.40de3ec → 4.0.0-internal.45cfa3d
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/endpoint/access.d.ts +17 -5
- package/dist/endpoint/access.d.ts.map +1 -1
- package/dist/endpoint/access.js +82 -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 +68 -17
- 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 +4 -4
- package/dist/mcp/buildMcpServer.d.ts.map +1 -1
- package/dist/mcp/buildMcpServer.js +133 -80
- 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 +44 -6
- 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 +69 -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 +65 -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 +38 -42
- 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 +14 -20
- 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 +97 -0
- package/dist/mcp/builtin/collections/duplicateTool.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 +93 -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 +49 -43
- 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 +84 -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 +99 -0
- package/dist/mcp/builtin/collections/findVersionsTool.js.map +1 -0
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts +9 -0
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/formatCollectionError.js +60 -0
- package/dist/mcp/builtin/collections/formatCollectionError.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 +47 -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 +82 -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 +87 -86
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- 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 +71 -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 +65 -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 +18 -21
- 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 +80 -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 +95 -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 +47 -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 +78 -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 +31 -40
- package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
- package/dist/mcp/builtin/validateEntityData.d.ts +14 -0
- package/dist/mcp/builtin/validateEntityData.d.ts.map +1 -0
- package/dist/mcp/builtin/validateEntityData.js +82 -0
- package/dist/mcp/builtin/validateEntityData.js.map +1 -0
- package/dist/mcp/builtinTools.d.ts +144 -19
- package/dist/mcp/builtinTools.d.ts.map +1 -1
- package/dist/mcp/builtinTools.js +119 -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 +121 -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 +82 -45
- package/dist/stdio.js.map +1 -1
- package/dist/types.d.ts +75 -110
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +4 -4
- package/dist/types.js.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/schemaConversion/getEntityInputSchema.d.ts +11 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +1 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.js +32 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.js.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts +5 -10
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +1 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js +15 -95
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js.map +1 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js +15 -40
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js.map +1 -1
- package/dist/utils/whereSchema.d.ts +9 -0
- package/dist/utils/whereSchema.d.ts.map +1 -0
- package/dist/utils/whereSchema.js +13 -0
- package/dist/utils/whereSchema.js.map +1 -0
- package/package.json +10 -15
- package/src/defaultAccess.ts +3 -0
- package/src/endpoint/access.ts +88 -101
- package/src/endpoint/index.ts +58 -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 +170 -115
- package/src/mcp/builtin/collections/authTools.ts +45 -9
- package/src/mcp/builtin/collections/countTool.ts +74 -0
- package/src/mcp/builtin/collections/countVersionsTool.ts +73 -0
- package/src/mcp/builtin/collections/createTool.ts +55 -59
- package/src/mcp/builtin/collections/deleteTool.ts +19 -16
- package/src/mcp/builtin/collections/duplicateTool.ts +135 -0
- package/src/mcp/builtin/collections/findDistinctTool.ts +119 -0
- package/src/mcp/builtin/collections/findTool.ts +63 -33
- package/src/mcp/builtin/collections/findVersionByIDTool.ts +110 -0
- package/src/mcp/builtin/collections/findVersionsTool.ts +155 -0
- package/src/mcp/builtin/collections/formatCollectionError.ts +84 -0
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +46 -0
- package/src/mcp/builtin/collections/restoreVersionTool.ts +108 -0
- package/src/mcp/builtin/collections/updateTool.ts +111 -110
- package/src/mcp/builtin/getConfigInfoTool.ts +69 -0
- package/src/mcp/builtin/globals/countVersionsTool.ts +72 -0
- package/src/mcp/builtin/globals/findTool.ts +26 -17
- package/src/mcp/builtin/globals/findVersionByIDTool.ts +104 -0
- package/src/mcp/builtin/globals/findVersionsTool.ts +148 -0
- package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +41 -0
- package/src/mcp/builtin/globals/restoreVersionTool.ts +99 -0
- package/src/mcp/builtin/globals/updateTool.ts +50 -55
- package/src/mcp/builtin/validateEntityData.ts +132 -0
- package/src/mcp/builtinTools.ts +111 -41
- package/src/mcp/sanitizeMCPConfig.ts +117 -78
- package/src/stdio.ts +77 -45
- package/src/types.ts +92 -115
- package/src/utils/getPluginConfig.ts +1 -1
- package/src/utils/schemaConversion/getEntityInputSchema.ts +62 -0
- package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +12 -19
- package/src/utils/schemaConversion/sanitizeEntitySchema.ts +17 -114
- package/src/utils/whereSchema.ts +24 -0
- package/dist/@types/assets.d.js +0 -2
- package/dist/@types/assets.d.js.map +0 -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 -60
- 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/schemaConversion/buildToolInput.d.ts +0 -29
- package/dist/utils/schemaConversion/buildToolInput.d.ts.map +0 -1
- package/dist/utils/schemaConversion/buildToolInput.js +0 -51
- package/dist/utils/schemaConversion/buildToolInput.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/src/@types/assets.d.ts +0 -3
- package/src/collection/getAccessField.ts +0 -64
- package/src/collection/index.ts +0 -64
- package/src/components/AccessField/index.client.tsx +0 -344
- package/src/components/AccessField/index.css +0 -93
- package/src/utils/schemaConversion/buildToolInput.ts +0 -68
- package/src/utils/schemaConversion/removeVirtualFieldsFromSchema.ts +0 -27
package/src/endpoint/access.ts
CHANGED
|
@@ -1,132 +1,119 @@
|
|
|
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
|
-
* Resolves the
|
|
13
|
-
*
|
|
15
|
+
* Resolves the MCP caller and returns the MCP surface authorized for that request.
|
|
16
|
+
*
|
|
17
|
+
* Authorization has two layers:
|
|
18
|
+
* 1. Payload collection/global permissions determine whether built-in operation tools are shown.
|
|
19
|
+
* 2. MCP `access` callbacks can further hide any configured tool, prompt, or resource.
|
|
20
|
+
*
|
|
21
|
+
* Like Payload core operations, `overrideAccess` skips both layers.
|
|
14
22
|
*/
|
|
15
|
-
export const getAuthorizedMCP: (args:
|
|
23
|
+
export const getAuthorizedMCP: (args: GetAuthorizedMCPArgs) => Promise<AuthorizedMCP> = async ({
|
|
24
|
+
overrideAccess,
|
|
16
25
|
req,
|
|
17
26
|
}) => {
|
|
18
|
-
const logger = getLogger({ payload: req.payload })
|
|
19
27
|
const pluginConfig = getPluginConfig({ config: req.payload.config })
|
|
20
28
|
|
|
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),
|
|
29
|
+
if (pluginConfig.overrideGetAuthorizedMCP) {
|
|
30
|
+
return await pluginConfig.overrideGetAuthorizedMCP({
|
|
31
|
+
overrideAccess,
|
|
48
32
|
pluginConfig,
|
|
49
33
|
req,
|
|
50
34
|
})
|
|
51
35
|
}
|
|
52
36
|
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
37
|
+
if (req.headers) {
|
|
38
|
+
const headers = new Headers(req.headers)
|
|
39
|
+
const hasAuthorization = headers.has('Authorization')
|
|
40
|
+
|
|
41
|
+
headers.set('DisableAutologin', 'true')
|
|
42
|
+
req.user = (await req.payload.auth({ headers, req })).user
|
|
43
|
+
|
|
44
|
+
if (hasAuthorization && !req.user) {
|
|
45
|
+
throw new UnauthorizedError(req.t)
|
|
46
|
+
}
|
|
61
47
|
}
|
|
62
48
|
|
|
63
|
-
return
|
|
49
|
+
return {
|
|
50
|
+
items: await filterMCPItems({
|
|
51
|
+
items: pluginConfig.items,
|
|
52
|
+
overrideAccess,
|
|
53
|
+
req,
|
|
54
|
+
}),
|
|
55
|
+
overrideAccess,
|
|
56
|
+
user: req.user,
|
|
57
|
+
}
|
|
64
58
|
}
|
|
65
59
|
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
pluginConfig,
|
|
60
|
+
export const filterMCPItems = async ({
|
|
61
|
+
items,
|
|
62
|
+
overrideAccess,
|
|
70
63
|
req,
|
|
71
64
|
}: {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
pluginConfig: ReturnType<typeof getPluginConfig>
|
|
65
|
+
items: MCPItem[]
|
|
66
|
+
overrideAccess: boolean
|
|
75
67
|
req: PayloadRequest
|
|
76
|
-
}): Promise<
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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()
|
|
68
|
+
}): Promise<MCPItem[]> => {
|
|
69
|
+
// Match Payload core: overrideAccess bypasses access evaluation instead of
|
|
70
|
+
// forcing each access function to return true.
|
|
71
|
+
if (overrideAccess) {
|
|
72
|
+
return items
|
|
102
73
|
}
|
|
103
74
|
|
|
104
|
-
|
|
75
|
+
const authorizedItems: MCPItem[] = []
|
|
105
76
|
|
|
106
|
-
const
|
|
107
|
-
const userID =
|
|
108
|
-
typeof userRef === 'object' && userRef !== null && 'id' in userRef
|
|
109
|
-
? userRef.id
|
|
110
|
-
: (userRef as unknown as DefaultDocumentIDType)
|
|
77
|
+
const permissions = await getAccessResults({ req })
|
|
111
78
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
req,
|
|
118
|
-
})) as null | TypedUser
|
|
119
|
-
|
|
120
|
-
if (!user) {
|
|
121
|
-
throw new UnauthorizedError()
|
|
79
|
+
for (const item of items) {
|
|
80
|
+
if (!(await checkItemAccess({ item, permissions, req }))) {
|
|
81
|
+
continue
|
|
82
|
+
}
|
|
83
|
+
authorizedItems.push(item)
|
|
122
84
|
}
|
|
123
85
|
|
|
124
|
-
return
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
86
|
+
return authorizedItems
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Runs MCP item access callbacks
|
|
91
|
+
*/
|
|
92
|
+
const checkItemAccess = async ({
|
|
93
|
+
item,
|
|
94
|
+
permissions,
|
|
95
|
+
req,
|
|
96
|
+
}: {
|
|
97
|
+
item: MCPItem
|
|
98
|
+
permissions?: SanitizedPermissions
|
|
99
|
+
req: PayloadRequest
|
|
100
|
+
}): Promise<boolean> => {
|
|
101
|
+
switch (item.type) {
|
|
102
|
+
case 'collectionTool':
|
|
103
|
+
return (
|
|
104
|
+
!item.tool.access ||
|
|
105
|
+
(await item.tool.access({ collectionSlug: item.collectionSlug, permissions, req }))
|
|
106
|
+
)
|
|
107
|
+
case 'globalTool':
|
|
108
|
+
return (
|
|
109
|
+
!item.tool.access ||
|
|
110
|
+
(await item.tool.access({ globalSlug: item.globalSlug, permissions, req }))
|
|
111
|
+
)
|
|
112
|
+
case 'prompt':
|
|
113
|
+
return !item.prompt.access || (await item.prompt.access({ permissions, req }))
|
|
114
|
+
case 'resource':
|
|
115
|
+
return !item.resource.access || (await item.resource.access({ permissions, req }))
|
|
116
|
+
case 'tool':
|
|
117
|
+
return !item.tool.access || (await item.tool.access({ permissions, req }))
|
|
131
118
|
}
|
|
132
119
|
}
|
package/src/endpoint/index.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type { PayloadHandler, PayloadRequest } from 'payload'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createMcpHandler,
|
|
5
|
+
isLegacyRequest,
|
|
6
|
+
WebStandardStreamableHTTPServerTransport,
|
|
7
|
+
} from '@modelcontextprotocol/server'
|
|
8
|
+
import { APIError } from 'payload'
|
|
3
9
|
|
|
4
10
|
import { buildMcpServer } from '../mcp/buildMcpServer.js'
|
|
5
11
|
import { getPluginConfig } from '../utils/getPluginConfig.js'
|
|
@@ -13,23 +19,59 @@ export const mcpEndpoint: PayloadHandler = async (req) => {
|
|
|
13
19
|
req.payloadAPI = 'MCP' as const
|
|
14
20
|
|
|
15
21
|
const pluginConfig = getPluginConfig({ config: req.payload.config })
|
|
16
|
-
const
|
|
22
|
+
const overrideAccessParam = new URL(req.url).searchParams.get('overrideAccess')
|
|
17
23
|
|
|
18
|
-
|
|
24
|
+
if (overrideAccessParam !== null && process.env.NODE_ENV !== 'development') {
|
|
25
|
+
throw new APIError('MCP overrideAccess is only available in development.', 400)
|
|
26
|
+
}
|
|
19
27
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
})
|
|
28
|
+
let overrideAccess = false
|
|
29
|
+
if (overrideAccessParam === 'true') {
|
|
30
|
+
overrideAccess = true
|
|
31
|
+
} else if (overrideAccessParam !== null && overrideAccessParam !== 'false') {
|
|
32
|
+
throw new APIError('MCP overrideAccess must be "true" or "false".', 400)
|
|
33
|
+
}
|
|
24
34
|
|
|
25
|
-
await
|
|
35
|
+
const authorizedMCP = await getAuthorizedMCP({ overrideAccess, req })
|
|
36
|
+
// Payload augments the original web-standard Request in place.
|
|
37
|
+
const mcpRequest = req as PayloadRequest & Request
|
|
26
38
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
// Keep the old JSON-only, stateless behavior because the SDK's 2025 fallback uses SSE.
|
|
40
|
+
if (await isLegacyRequest(mcpRequest)) {
|
|
41
|
+
const server = buildMcpServer({ authorizedMCP, pluginConfig, req })
|
|
42
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
43
|
+
enableJsonResponse: true,
|
|
44
|
+
sessionIdGenerator: undefined, // stateless mode
|
|
45
|
+
})
|
|
46
|
+
transport.onerror = (err) => {
|
|
47
|
+
req.payload.logger.error({ err, msg: 'Error serving legacy MCP request' })
|
|
48
|
+
}
|
|
33
49
|
|
|
34
|
-
|
|
50
|
+
try {
|
|
51
|
+
await server.connect(transport)
|
|
52
|
+
return await transport.handleRequest(mcpRequest)
|
|
53
|
+
} finally {
|
|
54
|
+
await server.close().catch((err) => {
|
|
55
|
+
req.payload.logger.error({ err, msg: 'Error closing MCP server' })
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const handler = createMcpHandler(() => buildMcpServer({ authorizedMCP, pluginConfig, req }), {
|
|
61
|
+
legacy: 'reject',
|
|
62
|
+
// SDK subscriptions always use SSE, so disable them to keep every response JSON-only.
|
|
63
|
+
maxSubscriptions: 0,
|
|
64
|
+
onerror: (err) => {
|
|
65
|
+
req.payload.logger.error({ err, msg: 'Error serving modern MCP request' })
|
|
66
|
+
},
|
|
67
|
+
responseMode: 'json',
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
return await handler.fetch(mcpRequest)
|
|
72
|
+
} finally {
|
|
73
|
+
await handler.close().catch((err) => {
|
|
74
|
+
req.payload.logger.error({ err, msg: 'Error closing modern MCP handler' })
|
|
75
|
+
})
|
|
76
|
+
}
|
|
35
77
|
}
|
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
|
},
|