@payloadcms/plugin-mcp 4.0.0-internal.5f0cd13 → 4.0.0-internal.63f8536
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 +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 +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 -73
- 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 +68 -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 +64 -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 +39 -40
- 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 +15 -22
- 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 +96 -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 +92 -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 +51 -46
- 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 +83 -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 +98 -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 +65 -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 +81 -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 +88 -84
- 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 +68 -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 +64 -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 +20 -23
- 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 +79 -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 +94 -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 +65 -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 +77 -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 +33 -38
- 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 +112 -110
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -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/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 +13 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +1 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.js +66 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.js.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts +10 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js +384 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js +133 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js.map +1 -0
- 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 +11 -16
- package/src/defaultAccess.ts +3 -0
- package/src/endpoint/access.ts +81 -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 +169 -109
- package/src/mcp/builtin/collections/authTools.ts +45 -9
- package/src/mcp/builtin/collections/countTool.ts +73 -0
- package/src/mcp/builtin/collections/countVersionsTool.ts +69 -0
- package/src/mcp/builtin/collections/createTool.ts +56 -61
- package/src/mcp/builtin/collections/deleteTool.ts +20 -18
- package/src/mcp/builtin/collections/duplicateTool.ts +134 -0
- package/src/mcp/builtin/collections/findDistinctTool.ts +119 -0
- package/src/mcp/builtin/collections/findTool.ts +65 -36
- package/src/mcp/builtin/collections/findVersionByIDTool.ts +109 -0
- package/src/mcp/builtin/collections/findVersionsTool.ts +154 -0
- package/src/mcp/builtin/collections/formatCollectionError.ts +84 -0
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +63 -0
- package/src/mcp/builtin/collections/restoreVersionTool.ts +107 -0
- package/src/mcp/builtin/collections/updateTool.ts +112 -107
- package/src/mcp/builtin/getConfigInfoTool.ts +67 -0
- package/src/mcp/builtin/globals/countVersionsTool.ts +68 -0
- package/src/mcp/builtin/globals/findTool.ts +28 -19
- package/src/mcp/builtin/globals/findVersionByIDTool.ts +103 -0
- package/src/mcp/builtin/globals/findVersionsTool.ts +147 -0
- package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +63 -0
- package/src/mcp/builtin/globals/restoreVersionTool.ts +99 -0
- package/src/mcp/builtin/globals/updateTool.ts +52 -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 +128 -115
- package/src/utils/getPluginConfig.ts +1 -1
- package/src/utils/schemaConversion/filterFieldsByAccess.ts +123 -0
- package/src/utils/schemaConversion/getEntityInputSchema.ts +96 -0
- package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +96 -0
- package/src/utils/schemaConversion/sanitizeEntitySchema.ts +432 -0
- package/src/utils/whereSchema.ts +24 -0
- 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/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/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/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
|
@@ -1,34 +1,26 @@
|
|
|
1
1
|
import { McpServer, type ServerContext } from '@modelcontextprotocol/server'
|
|
2
|
-
import { APIError,
|
|
2
|
+
import { APIError, type PayloadRequest } from 'payload'
|
|
3
|
+
import { z } from 'zod'
|
|
3
4
|
|
|
4
5
|
import type {
|
|
5
6
|
AuthorizedMCP,
|
|
7
|
+
CollectionMCPItem,
|
|
8
|
+
GlobalMCPItem,
|
|
6
9
|
JsonSchemaType,
|
|
7
10
|
MCPResponseOverride,
|
|
8
11
|
MCPToolResponse,
|
|
9
12
|
SanitizedMCPPluginConfig,
|
|
13
|
+
ToolInputSchema,
|
|
10
14
|
} from '../types.js'
|
|
11
15
|
|
|
12
|
-
import { toCamelCase } from '../utils/camelCase.js'
|
|
13
16
|
import { getLogger } from '../utils/getLogger.js'
|
|
14
|
-
import {
|
|
15
|
-
getCollectionVirtualFieldNames,
|
|
16
|
-
getGlobalVirtualFieldNames,
|
|
17
|
-
} from '../utils/getVirtualFieldNames.js'
|
|
18
|
-
import { removeVirtualFieldsFromSchema } from '../utils/schemaConversion/removeVirtualFieldsFromSchema.js'
|
|
19
17
|
import { toStandardSchema } from '../utils/toStandardSchema.js'
|
|
20
18
|
|
|
21
|
-
/** `findPosts`, `updateSiteSettings` — auto-prefixed wire name for collection/global tools. */
|
|
22
|
-
const wireName = (key: string, slug: string): string => {
|
|
23
|
-
const camel = toCamelCase(slug)
|
|
24
|
-
return `${key}${camel.charAt(0).toUpperCase()}${camel.slice(1)}`
|
|
25
|
-
}
|
|
26
|
-
|
|
27
19
|
/**
|
|
28
|
-
*
|
|
29
|
-
* `McpServer` and returns it. The
|
|
30
|
-
*
|
|
31
|
-
*
|
|
20
|
+
* Serving-entry-agnostic core: registers every authorized MCP item onto a fresh
|
|
21
|
+
* `McpServer` and returns it. The HTTP and stdio entry point callers provide fresh
|
|
22
|
+
* instances from this builder while they own the transport and protocol-era
|
|
23
|
+
* decision.
|
|
32
24
|
*
|
|
33
25
|
* `req` is the request context handlers see. For HTTP it's the live
|
|
34
26
|
* `PayloadRequest` derived from the incoming HTTP request; for stdio it's a
|
|
@@ -55,105 +47,130 @@ export const buildMcpServer = ({
|
|
|
55
47
|
* Wrap a tool handler's response with the tool's `overrideResponse`, then
|
|
56
48
|
* strip the internal `doc` field so it doesn't leak onto the wire.
|
|
57
49
|
*/
|
|
58
|
-
const finalizeToolResponse = (
|
|
59
|
-
|
|
60
|
-
overrideResponse
|
|
61
|
-
|
|
62
|
-
|
|
50
|
+
const finalizeToolResponse = async ({
|
|
51
|
+
input,
|
|
52
|
+
overrideResponse,
|
|
53
|
+
response,
|
|
54
|
+
toolName,
|
|
55
|
+
}: {
|
|
56
|
+
input: unknown
|
|
57
|
+
overrideResponse?: MCPResponseOverride
|
|
58
|
+
response: MCPToolResponse
|
|
59
|
+
toolName: string
|
|
60
|
+
}): Promise<MCPToolResponse> => {
|
|
61
|
+
let overridden = overrideResponse?.(response, response.doc ?? {}, req) ?? response
|
|
62
|
+
for (const hook of pluginConfig.hooks?.afterToolCall ?? []) {
|
|
63
|
+
overridden = await hook({ input, req, response: overridden, toolName })
|
|
64
|
+
}
|
|
63
65
|
const { doc: _doc, ...rest } = overridden
|
|
64
66
|
return rest
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Runs a collection/global tool call:
|
|
71
|
+
* - reads `collectionSlug` / `globalSlug` from the input
|
|
72
|
+
* - runs access control: errors if `authorizedMCP.items` has no entry for this tool + slug
|
|
73
|
+
* - runs the tool handler and finalizes its response
|
|
74
|
+
*/
|
|
75
|
+
const callEntityTool = async ({
|
|
76
|
+
input,
|
|
77
|
+
item,
|
|
78
|
+
serverContext,
|
|
79
|
+
}: {
|
|
80
|
+
input: unknown
|
|
81
|
+
item: CollectionMCPItem | GlobalMCPItem
|
|
82
|
+
serverContext: ServerContext
|
|
83
|
+
}): Promise<MCPToolResponse> => {
|
|
84
|
+
const entity = item.type === 'collectionTool' ? 'collection' : 'global'
|
|
85
|
+
const slugKey = item.type === 'collectionTool' ? 'collectionSlug' : 'globalSlug'
|
|
86
|
+
const toolInput = (input ?? {}) as Record<string, unknown>
|
|
87
|
+
const slug = toolInput[slugKey] as string | undefined
|
|
88
|
+
|
|
89
|
+
if (!slug) {
|
|
90
|
+
return {
|
|
91
|
+
content: [
|
|
92
|
+
{
|
|
93
|
+
type: 'text',
|
|
94
|
+
text: `Error: "${item.mcpName}" requires ${slugKey}. Use getConfigInfo to inspect ${entity} slugs.`,
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
isError: true,
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const match = authorizedMCP.items.find(
|
|
102
|
+
(candidate): candidate is CollectionMCPItem | GlobalMCPItem =>
|
|
103
|
+
candidate.type === item.type &&
|
|
104
|
+
candidate.mcpName === item.mcpName &&
|
|
105
|
+
(candidate.type === 'collectionTool'
|
|
106
|
+
? candidate.collectionSlug === slug
|
|
107
|
+
: candidate.type === 'globalTool' && candidate.globalSlug === slug),
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
if (!match) {
|
|
111
|
+
return {
|
|
112
|
+
content: [
|
|
113
|
+
{
|
|
114
|
+
type: 'text',
|
|
115
|
+
text: `Error: MCP access to "${item.mcpName}" is not enabled for ${entity} "${slug}"`,
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
isError: true,
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const handlerArgs = {
|
|
123
|
+
authorizedMCP,
|
|
124
|
+
input: toolInput,
|
|
125
|
+
req,
|
|
126
|
+
serverContext,
|
|
127
|
+
}
|
|
128
|
+
const response = await (match.type === 'collectionTool'
|
|
129
|
+
? match.tool.handler({ ...handlerArgs, collectionSlug: slug })
|
|
130
|
+
: match.tool.handler({ ...handlerArgs, globalSlug: slug }))
|
|
131
|
+
|
|
132
|
+
return finalizeToolResponse({
|
|
133
|
+
input: toolInput,
|
|
134
|
+
overrideResponse: match.tool.overrideResponse,
|
|
135
|
+
response,
|
|
136
|
+
toolName: match.mcpName,
|
|
137
|
+
})
|
|
138
|
+
}
|
|
73
139
|
|
|
74
140
|
try {
|
|
141
|
+
const registeredEntityTools = new Set<string>()
|
|
142
|
+
|
|
75
143
|
for (const item of authorizedMCP.items) {
|
|
76
144
|
switch (item.type) {
|
|
77
|
-
case 'collectionTool':
|
|
78
|
-
const tool = item.tool
|
|
79
|
-
const name = wireName(item.key, item.collectionSlug)
|
|
80
|
-
let inputSchema = tool.input
|
|
81
|
-
if (typeof inputSchema === 'function') {
|
|
82
|
-
const raw = configSchema.definitions?.[item.collectionSlug]
|
|
83
|
-
if (!raw) {
|
|
84
|
-
throw new APIError(
|
|
85
|
-
`Collection schema not found for slug: ${item.collectionSlug}`,
|
|
86
|
-
500,
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
const collectionSchema = removeVirtualFieldsFromSchema(
|
|
90
|
-
JSON.parse(JSON.stringify(raw)) as JsonSchemaType,
|
|
91
|
-
getCollectionVirtualFieldNames(req.payload.config, item.collectionSlug),
|
|
92
|
-
)
|
|
93
|
-
inputSchema = inputSchema({ collectionSchema })
|
|
94
|
-
}
|
|
95
|
-
server.registerTool(
|
|
96
|
-
name,
|
|
97
|
-
{
|
|
98
|
-
description: tool.description,
|
|
99
|
-
inputSchema: inputSchema ? toStandardSchema(inputSchema) : undefined,
|
|
100
|
-
},
|
|
101
|
-
async (input: unknown, ctx: ServerContext) =>
|
|
102
|
-
finalizeToolResponse(
|
|
103
|
-
await tool.handler({
|
|
104
|
-
authorizedMCP,
|
|
105
|
-
collectionSlug: item.collectionSlug,
|
|
106
|
-
input: (input ?? {}) as Record<string, unknown>,
|
|
107
|
-
req,
|
|
108
|
-
serverContext: ctx,
|
|
109
|
-
}),
|
|
110
|
-
tool.overrideResponse,
|
|
111
|
-
),
|
|
112
|
-
)
|
|
113
|
-
logger.info(`✅ Tool: ${name} Registered.`)
|
|
114
|
-
break
|
|
115
|
-
}
|
|
145
|
+
case 'collectionTool':
|
|
116
146
|
case 'globalTool': {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
let inputSchema = tool.input
|
|
120
|
-
if (typeof inputSchema === 'function') {
|
|
121
|
-
const raw = configSchema.definitions?.[item.globalSlug]
|
|
122
|
-
if (!raw) {
|
|
123
|
-
throw new APIError(`Global schema not found for slug: ${item.globalSlug}`, 500)
|
|
124
|
-
}
|
|
125
|
-
const globalSchema = removeVirtualFieldsFromSchema(
|
|
126
|
-
JSON.parse(JSON.stringify(raw)) as JsonSchemaType,
|
|
127
|
-
getGlobalVirtualFieldNames(req.payload.config, item.globalSlug),
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
inputSchema = inputSchema({ globalSchema })
|
|
147
|
+
if (registeredEntityTools.has(item.mcpName)) {
|
|
148
|
+
break
|
|
131
149
|
}
|
|
150
|
+
registeredEntityTools.add(item.mcpName)
|
|
151
|
+
|
|
152
|
+
const inputSchema = withSlugInput({
|
|
153
|
+
name: item.type === 'collectionTool' ? 'collectionSlug' : 'globalSlug',
|
|
154
|
+
input: item.tool.input,
|
|
155
|
+
})
|
|
156
|
+
|
|
132
157
|
server.registerTool(
|
|
133
|
-
|
|
158
|
+
item.mcpName,
|
|
134
159
|
{
|
|
135
|
-
|
|
136
|
-
|
|
160
|
+
annotations: item.tool.annotations,
|
|
161
|
+
description: item.tool.description,
|
|
162
|
+
inputSchema: toStandardSchema(inputSchema),
|
|
137
163
|
},
|
|
138
164
|
async (input: unknown, ctx: ServerContext) =>
|
|
139
|
-
|
|
140
|
-
await tool.handler({
|
|
141
|
-
authorizedMCP,
|
|
142
|
-
globalSlug: item.globalSlug,
|
|
143
|
-
input: (input ?? {}) as Record<string, unknown>,
|
|
144
|
-
req,
|
|
145
|
-
serverContext: ctx,
|
|
146
|
-
}),
|
|
147
|
-
tool.overrideResponse,
|
|
148
|
-
),
|
|
165
|
+
callEntityTool({ input, item, serverContext: ctx }),
|
|
149
166
|
)
|
|
150
|
-
logger.info(`✅ Tool: ${
|
|
167
|
+
logger.info(`✅ Tool: ${item.mcpName} Registered.`)
|
|
151
168
|
break
|
|
152
169
|
}
|
|
153
170
|
case 'prompt': {
|
|
154
171
|
const prompt = item.prompt
|
|
155
172
|
server.registerPrompt(
|
|
156
|
-
item.
|
|
173
|
+
item.mcpName,
|
|
157
174
|
{
|
|
158
175
|
argsSchema: prompt.argsSchema ? toStandardSchema(prompt.argsSchema) : undefined,
|
|
159
176
|
description: prompt.description,
|
|
@@ -172,7 +189,7 @@ export const buildMcpServer = ({
|
|
|
172
189
|
case 'resource': {
|
|
173
190
|
const resource = item.resource
|
|
174
191
|
server.registerResource(
|
|
175
|
-
item.
|
|
192
|
+
item.mcpName,
|
|
176
193
|
// @ts-expect-error - Overload type ambiguity (string OR ResourceTemplate is valid)
|
|
177
194
|
resource.uri,
|
|
178
195
|
{
|
|
@@ -195,23 +212,29 @@ export const buildMcpServer = ({
|
|
|
195
212
|
case 'tool': {
|
|
196
213
|
const tool = item.tool
|
|
197
214
|
server.registerTool(
|
|
198
|
-
item.
|
|
215
|
+
item.mcpName,
|
|
199
216
|
{
|
|
217
|
+
annotations: tool.annotations,
|
|
200
218
|
description: tool.description,
|
|
201
219
|
inputSchema: tool.input ? toStandardSchema(tool.input) : undefined,
|
|
202
220
|
},
|
|
203
|
-
async (input: unknown, ctx: ServerContext) =>
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
221
|
+
async (input: unknown, ctx: ServerContext) => {
|
|
222
|
+
const toolInput = (input ?? {}) as Record<string, unknown>
|
|
223
|
+
const response = await tool.handler({
|
|
224
|
+
authorizedMCP,
|
|
225
|
+
input: toolInput,
|
|
226
|
+
req,
|
|
227
|
+
serverContext: ctx,
|
|
228
|
+
})
|
|
229
|
+
return finalizeToolResponse({
|
|
230
|
+
input: toolInput,
|
|
231
|
+
overrideResponse: tool.overrideResponse,
|
|
232
|
+
response,
|
|
233
|
+
toolName: item.mcpName,
|
|
234
|
+
})
|
|
235
|
+
},
|
|
213
236
|
)
|
|
214
|
-
logger.info(`✅ Tool: ${item.
|
|
237
|
+
logger.info(`✅ Tool: ${item.mcpName} Registered.`)
|
|
215
238
|
break
|
|
216
239
|
}
|
|
217
240
|
}
|
|
@@ -222,3 +245,40 @@ export const buildMcpServer = ({
|
|
|
222
245
|
|
|
223
246
|
return server
|
|
224
247
|
}
|
|
248
|
+
|
|
249
|
+
const withSlugInput = ({
|
|
250
|
+
name,
|
|
251
|
+
input,
|
|
252
|
+
}: {
|
|
253
|
+
input?: ToolInputSchema
|
|
254
|
+
name: 'collectionSlug' | 'globalSlug'
|
|
255
|
+
}): ToolInputSchema => {
|
|
256
|
+
const description = name === 'collectionSlug' ? 'The collection slug' : 'The global slug'
|
|
257
|
+
const slugSchema = z.string().describe(description)
|
|
258
|
+
|
|
259
|
+
if (!input) {
|
|
260
|
+
return z.object({ [name]: slugSchema })
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (input instanceof z.ZodObject) {
|
|
264
|
+
return input.extend({ [name]: slugSchema })
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const schema = input as {
|
|
268
|
+
properties?: Record<string, JsonSchemaType>
|
|
269
|
+
required?: string[]
|
|
270
|
+
} & JsonSchemaType
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
...schema,
|
|
274
|
+
type: 'object',
|
|
275
|
+
properties: {
|
|
276
|
+
...schema.properties,
|
|
277
|
+
[name]: {
|
|
278
|
+
type: 'string',
|
|
279
|
+
description,
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
required: Array.from(new Set([name, ...(schema.required ?? [])])),
|
|
283
|
+
}
|
|
284
|
+
}
|
|
@@ -32,13 +32,18 @@ const wrapError =
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export const authCollectionTool = defineCollectionTool({
|
|
35
|
+
annotations: {
|
|
36
|
+
destructiveHint: false,
|
|
37
|
+
idempotentHint: true,
|
|
38
|
+
openWorldHint: false,
|
|
39
|
+
readOnlyHint: true,
|
|
40
|
+
title: 'Check Auth Status',
|
|
41
|
+
},
|
|
35
42
|
description: 'Checks authentication status for the current user.',
|
|
36
43
|
input: z.object({
|
|
37
44
|
headers: z
|
|
38
|
-
.string()
|
|
39
|
-
.describe(
|
|
40
|
-
'Optional JSON string containing custom headers to send with the authentication request',
|
|
41
|
-
)
|
|
45
|
+
.record(z.string(), z.string())
|
|
46
|
+
.describe('Optional custom headers to send with the authentication request')
|
|
42
47
|
.optional(),
|
|
43
48
|
}),
|
|
44
49
|
}).handler(async ({ collectionSlug, input, req }) => {
|
|
@@ -46,11 +51,7 @@ export const authCollectionTool = defineCollectionTool({
|
|
|
46
51
|
try {
|
|
47
52
|
let authHeaders = new Headers()
|
|
48
53
|
if (input.headers) {
|
|
49
|
-
|
|
50
|
-
authHeaders = new Headers(JSON.parse(input.headers) as Record<string, string>)
|
|
51
|
-
} catch {
|
|
52
|
-
logger.warn(`Invalid headers JSON for auth: ${input.headers}, using empty headers`)
|
|
53
|
-
}
|
|
54
|
+
authHeaders = new Headers(input.headers)
|
|
54
55
|
}
|
|
55
56
|
const result = await req.payload.auth({ headers: authHeaders })
|
|
56
57
|
return {
|
|
@@ -70,6 +71,13 @@ export const authCollectionTool = defineCollectionTool({
|
|
|
70
71
|
})
|
|
71
72
|
|
|
72
73
|
export const forgotPasswordCollectionTool = defineCollectionTool({
|
|
74
|
+
annotations: {
|
|
75
|
+
destructiveHint: false,
|
|
76
|
+
idempotentHint: false,
|
|
77
|
+
openWorldHint: true,
|
|
78
|
+
readOnlyHint: false,
|
|
79
|
+
title: 'Forgot Password',
|
|
80
|
+
},
|
|
73
81
|
description: 'Sends a password reset email to a user.',
|
|
74
82
|
input: z.object({
|
|
75
83
|
disableEmail: z
|
|
@@ -104,6 +112,13 @@ export const forgotPasswordCollectionTool = defineCollectionTool({
|
|
|
104
112
|
})
|
|
105
113
|
|
|
106
114
|
export const loginCollectionTool = defineCollectionTool({
|
|
115
|
+
annotations: {
|
|
116
|
+
destructiveHint: false,
|
|
117
|
+
idempotentHint: false,
|
|
118
|
+
openWorldHint: false,
|
|
119
|
+
readOnlyHint: false,
|
|
120
|
+
title: 'User Login',
|
|
121
|
+
},
|
|
107
122
|
description: 'Authenticates a user with email and password.',
|
|
108
123
|
input: z.object({
|
|
109
124
|
depth: z
|
|
@@ -148,6 +163,13 @@ export const loginCollectionTool = defineCollectionTool({
|
|
|
148
163
|
})
|
|
149
164
|
|
|
150
165
|
export const resetPasswordCollectionTool = defineCollectionTool({
|
|
166
|
+
annotations: {
|
|
167
|
+
destructiveHint: false,
|
|
168
|
+
idempotentHint: false,
|
|
169
|
+
openWorldHint: false,
|
|
170
|
+
readOnlyHint: false,
|
|
171
|
+
title: 'Reset Password',
|
|
172
|
+
},
|
|
151
173
|
description: 'Resets a user password with a reset token.',
|
|
152
174
|
input: z.object({
|
|
153
175
|
password: z.string().describe('The new password for the user'),
|
|
@@ -178,6 +200,13 @@ export const resetPasswordCollectionTool = defineCollectionTool({
|
|
|
178
200
|
})
|
|
179
201
|
|
|
180
202
|
export const unlockCollectionTool = defineCollectionTool({
|
|
203
|
+
annotations: {
|
|
204
|
+
destructiveHint: false,
|
|
205
|
+
idempotentHint: true,
|
|
206
|
+
openWorldHint: false,
|
|
207
|
+
readOnlyHint: false,
|
|
208
|
+
title: 'Unlock Account',
|
|
209
|
+
},
|
|
181
210
|
description: 'Unlocks a user account that has been locked due to failed login attempts.',
|
|
182
211
|
input: z.object({ email: emailSchema }),
|
|
183
212
|
}).handler(async ({ collectionSlug, input, req }) => {
|
|
@@ -205,6 +234,13 @@ export const unlockCollectionTool = defineCollectionTool({
|
|
|
205
234
|
})
|
|
206
235
|
|
|
207
236
|
export const verifyCollectionTool = defineCollectionTool({
|
|
237
|
+
annotations: {
|
|
238
|
+
destructiveHint: false,
|
|
239
|
+
idempotentHint: false,
|
|
240
|
+
openWorldHint: false,
|
|
241
|
+
readOnlyHint: false,
|
|
242
|
+
title: 'Email Verification',
|
|
243
|
+
},
|
|
208
244
|
description: 'Verifies a user email with a verification token.',
|
|
209
245
|
input: z.object({
|
|
210
246
|
token: z.string().describe('The verification token sent to the user email'),
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
import { defaultAccess } from '../../../defaultAccess.js'
|
|
4
|
+
import { defineCollectionTool } from '../../../defineTool.js'
|
|
5
|
+
import { getLogger } from '../../../utils/getLogger.js'
|
|
6
|
+
import { whereSchema } from '../../../utils/whereSchema.js'
|
|
7
|
+
|
|
8
|
+
const DEFAULT_DESCRIPTION =
|
|
9
|
+
'Count documents in any collection by passing the collection slug and optional where clause.'
|
|
10
|
+
|
|
11
|
+
export const countDocumentsTool = defineCollectionTool({
|
|
12
|
+
access: (args) =>
|
|
13
|
+
defaultAccess(args) && Boolean(args.permissions?.collections?.[args.collectionSlug]?.read),
|
|
14
|
+
annotations: {
|
|
15
|
+
destructiveHint: false,
|
|
16
|
+
idempotentHint: true,
|
|
17
|
+
openWorldHint: false,
|
|
18
|
+
readOnlyHint: true,
|
|
19
|
+
title: 'Count Documents',
|
|
20
|
+
},
|
|
21
|
+
description: DEFAULT_DESCRIPTION,
|
|
22
|
+
input: z.object({
|
|
23
|
+
locale: z.string().describe('Optional: locale code to count documents in').optional(),
|
|
24
|
+
trash: z
|
|
25
|
+
.boolean()
|
|
26
|
+
.describe('Optional: include soft-deleted documents when trash is enabled on the collection')
|
|
27
|
+
.optional(),
|
|
28
|
+
where: whereSchema
|
|
29
|
+
.describe(
|
|
30
|
+
'Optional: where clause for filtering. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}',
|
|
31
|
+
)
|
|
32
|
+
.optional(),
|
|
33
|
+
}),
|
|
34
|
+
}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
|
|
35
|
+
const payload = req.payload
|
|
36
|
+
const logger = getLogger({ payload })
|
|
37
|
+
const { locale, trash, where } = input
|
|
38
|
+
|
|
39
|
+
logger.info(`Counting documents in collection: ${collectionSlug}`)
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const result = await payload.count({
|
|
43
|
+
collection: collectionSlug,
|
|
44
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
45
|
+
req,
|
|
46
|
+
...(locale ? { locale } : {}),
|
|
47
|
+
...(trash !== undefined ? { trash } : {}),
|
|
48
|
+
...(where ? { where } : {}),
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
content: [
|
|
53
|
+
{
|
|
54
|
+
type: 'text',
|
|
55
|
+
text: `Collection "${collectionSlug}" contains ${result.totalDocs} matching documents.\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
doc: result,
|
|
59
|
+
}
|
|
60
|
+
} catch (error) {
|
|
61
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
62
|
+
logger.error(`Error counting documents in ${collectionSlug}: ${errorMessage}`)
|
|
63
|
+
return {
|
|
64
|
+
content: [
|
|
65
|
+
{
|
|
66
|
+
type: 'text',
|
|
67
|
+
text: `❌ **Error counting documents in collection "${collectionSlug}":** ${errorMessage}`,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
isError: true,
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
})
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
import { defaultAccess } from '../../../defaultAccess.js'
|
|
4
|
+
import { defineCollectionTool } from '../../../defineTool.js'
|
|
5
|
+
import { getLogger } from '../../../utils/getLogger.js'
|
|
6
|
+
import { whereSchema } from '../../../utils/whereSchema.js'
|
|
7
|
+
|
|
8
|
+
const DEFAULT_DESCRIPTION =
|
|
9
|
+
'Count document versions in any version-enabled collection by passing the collection slug and optional where clause.'
|
|
10
|
+
|
|
11
|
+
export const countVersionsTool = defineCollectionTool({
|
|
12
|
+
access: (args) =>
|
|
13
|
+
defaultAccess(args) &&
|
|
14
|
+
Boolean(args.permissions?.collections?.[args.collectionSlug]?.readVersions),
|
|
15
|
+
annotations: {
|
|
16
|
+
destructiveHint: false,
|
|
17
|
+
idempotentHint: true,
|
|
18
|
+
openWorldHint: false,
|
|
19
|
+
readOnlyHint: true,
|
|
20
|
+
title: 'Count Versions',
|
|
21
|
+
},
|
|
22
|
+
description: DEFAULT_DESCRIPTION,
|
|
23
|
+
input: z.object({
|
|
24
|
+
locale: z.string().describe('Optional: locale code to count versions in').optional(),
|
|
25
|
+
where: whereSchema
|
|
26
|
+
.describe(
|
|
27
|
+
'Optional: where clause for filtering versions. Version document fields are usually under "version". Example: {"version.title":{"contains":"test"}}',
|
|
28
|
+
)
|
|
29
|
+
.optional(),
|
|
30
|
+
}),
|
|
31
|
+
}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
|
|
32
|
+
const payload = req.payload
|
|
33
|
+
const logger = getLogger({ payload })
|
|
34
|
+
const { locale, where } = input
|
|
35
|
+
|
|
36
|
+
logger.info(`Counting versions in collection: ${collectionSlug}`)
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
const result = await payload.countVersions({
|
|
40
|
+
collection: collectionSlug,
|
|
41
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
42
|
+
req,
|
|
43
|
+
...(locale ? { locale } : {}),
|
|
44
|
+
...(where ? { where } : {}),
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
content: [
|
|
49
|
+
{
|
|
50
|
+
type: 'text',
|
|
51
|
+
text: `Collection "${collectionSlug}" contains ${result.totalDocs} matching versions.\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
doc: result,
|
|
55
|
+
}
|
|
56
|
+
} catch (error) {
|
|
57
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
58
|
+
logger.error(`Error counting versions in ${collectionSlug}: ${errorMessage}`)
|
|
59
|
+
return {
|
|
60
|
+
content: [
|
|
61
|
+
{
|
|
62
|
+
type: 'text',
|
|
63
|
+
text: `❌ **Error counting versions in collection "${collectionSlug}":** ${errorMessage}`,
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
isError: true,
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
})
|