-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(plugin-mcp): add missing MCP built-in tools #17054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlessioGr
wants to merge
2
commits into
main
Choose a base branch
from
feat/mcp-missing-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/plugin-mcp/src/mcp/builtin/collections/countTool.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { z } from 'zod' | ||
|
|
||
| import { defineCollectionTool } from '../../../defineTool.js' | ||
| import { getLogger } from '../../../utils/getLogger.js' | ||
| import { localAPIDefaults } from '../../../utils/localAPIDefaults.js' | ||
| import { whereSchema } from '../../../utils/whereSchema.js' | ||
|
|
||
| const DEFAULT_DESCRIPTION = | ||
| 'Count documents in any collection by passing the collection slug and optional where clause.' | ||
|
|
||
| export const countDocumentsTool = defineCollectionTool({ | ||
| annotations: { | ||
| destructiveHint: false, | ||
| idempotentHint: true, | ||
| openWorldHint: false, | ||
| readOnlyHint: true, | ||
| title: 'Count Documents', | ||
| }, | ||
| description: DEFAULT_DESCRIPTION, | ||
| input: z.object({ | ||
| locale: z.string().describe('Optional: locale code to count documents in').optional(), | ||
| trash: z | ||
| .boolean() | ||
| .describe('Optional: include soft-deleted documents when trash is enabled on the collection') | ||
| .optional(), | ||
| where: whereSchema | ||
| .describe( | ||
| 'Optional: where clause for filtering. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}', | ||
| ) | ||
| .optional(), | ||
| }), | ||
| }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => { | ||
| const payload = req.payload | ||
| const logger = getLogger({ payload }) | ||
| const { locale, trash, where } = input | ||
|
|
||
| logger.info(`Counting documents in collection: ${collectionSlug}`) | ||
|
|
||
| try { | ||
| const result = await payload.count({ | ||
| collection: collectionSlug, | ||
| req, | ||
| ...localAPIDefaults(authorizedMCP), | ||
| ...(locale ? { locale } : {}), | ||
| ...(trash !== undefined ? { trash } : {}), | ||
| ...(where ? { where } : {}), | ||
| }) | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: `Collection "${collectionSlug}" contains ${result.totalDocs} matching documents.\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``, | ||
| }, | ||
| ], | ||
| doc: result, | ||
| } | ||
| } catch (error) { | ||
| const errorMessage = error instanceof Error ? error.message : 'Unknown error' | ||
| logger.error(`Error counting documents in ${collectionSlug}: ${errorMessage}`) | ||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: `❌ **Error counting documents in collection "${collectionSlug}":** ${errorMessage}`, | ||
| }, | ||
| ], | ||
| isError: true, | ||
| } | ||
| } | ||
| }) |
69 changes: 69 additions & 0 deletions
69
packages/plugin-mcp/src/mcp/builtin/collections/countVersionsTool.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { z } from 'zod' | ||
|
|
||
| import { defineCollectionTool } from '../../../defineTool.js' | ||
| import { getLogger } from '../../../utils/getLogger.js' | ||
| import { localAPIDefaults } from '../../../utils/localAPIDefaults.js' | ||
| import { whereSchema } from '../../../utils/whereSchema.js' | ||
|
|
||
| const DEFAULT_DESCRIPTION = | ||
| 'Count document versions in any version-enabled collection by passing the collection slug and optional where clause.' | ||
|
|
||
| export const countVersionsTool = defineCollectionTool({ | ||
| annotations: { | ||
| destructiveHint: false, | ||
| idempotentHint: true, | ||
| openWorldHint: false, | ||
| readOnlyHint: true, | ||
| title: 'Count Versions', | ||
| }, | ||
| description: DEFAULT_DESCRIPTION, | ||
| input: z.object({ | ||
| locale: z | ||
| .string() | ||
| .describe('Optional: locale code to count versions in') | ||
| .optional(), | ||
| where: whereSchema | ||
| .describe( | ||
| 'Optional: where clause for filtering versions. Version document fields are usually under "version". Example: {"version.title":{"contains":"test"}}', | ||
| ) | ||
| .optional(), | ||
| }), | ||
| }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => { | ||
| const payload = req.payload | ||
| const logger = getLogger({ payload }) | ||
| const { locale, where } = input | ||
|
|
||
| logger.info(`Counting versions in collection: ${collectionSlug}`) | ||
|
|
||
| try { | ||
| const result = await payload.countVersions({ | ||
| collection: collectionSlug, | ||
| req, | ||
| ...localAPIDefaults(authorizedMCP), | ||
| ...(locale ? { locale } : {}), | ||
| ...(where ? { where } : {}), | ||
| }) | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: `Collection "${collectionSlug}" contains ${result.totalDocs} matching versions.\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``, | ||
| }, | ||
| ], | ||
| doc: result, | ||
| } | ||
| } catch (error) { | ||
| const errorMessage = error instanceof Error ? error.message : 'Unknown error' | ||
| logger.error(`Error counting versions in ${collectionSlug}: ${errorMessage}`) | ||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: `❌ **Error counting versions in collection "${collectionSlug}":** ${errorMessage}`, | ||
| }, | ||
| ], | ||
| isError: true, | ||
| } | ||
| } | ||
| }) |
132 changes: 132 additions & 0 deletions
132
packages/plugin-mcp/src/mcp/builtin/collections/duplicateTool.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import type { PopulateType, SelectType } from 'payload' | ||
|
|
||
| import { z } from 'zod' | ||
|
|
||
| import { defineCollectionTool } from '../../../defineTool.js' | ||
| import { getLogger } from '../../../utils/getLogger.js' | ||
| import { | ||
| getCollectionVirtualFieldNames, | ||
| stripVirtualFields, | ||
| } from '../../../utils/getVirtualFieldNames.js' | ||
| import { localAPIDefaults } from '../../../utils/localAPIDefaults.js' | ||
| import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js' | ||
| import { formatCollectionError } from './formatCollectionError.js' | ||
|
|
||
| const DEFAULT_DESCRIPTION = | ||
| 'Duplicate a document in any collection by passing the collection slug and source document ID.' | ||
|
|
||
| export const duplicateDocumentTool = defineCollectionTool({ | ||
| annotations: { | ||
| destructiveHint: false, | ||
| idempotentHint: false, | ||
| openWorldHint: false, | ||
| readOnlyHint: false, | ||
| title: 'Duplicate Document', | ||
| }, | ||
| description: DEFAULT_DESCRIPTION, | ||
| input: z.object({ | ||
| id: z.union([z.string(), z.number()]).describe('The ID of the document to duplicate'), | ||
| data: z | ||
| .record(z.string(), z.unknown()) | ||
| .describe('Optional: fields to override on the duplicated document') | ||
| .optional(), | ||
| depth: z | ||
| .number() | ||
| .int() | ||
| .min(0) | ||
| .max(10) | ||
| .describe('How many levels deep to populate relationships in response') | ||
| .optional() | ||
| .default(0), | ||
| draft: z | ||
| .boolean() | ||
| .describe('Whether to create the duplicate as a draft') | ||
| .optional() | ||
| .default(false), | ||
| fallbackLocale: z | ||
| .string() | ||
| .describe('Optional: fallback locale code to use when requested locale is not available') | ||
| .optional(), | ||
| locale: z | ||
| .string() | ||
| .describe( | ||
| 'Optional: locale code to duplicate in (e.g., "en", "es"). Defaults to the default locale', | ||
| ) | ||
| .optional(), | ||
| populate: z | ||
| .record(z.string(), z.unknown()) | ||
| .describe( | ||
| 'Optional: control which fields to include from populated relationship or upload documents.', | ||
| ) | ||
| .optional(), | ||
| select: z | ||
| .record(z.string(), z.unknown()) | ||
| .describe( | ||
| 'Optional: define exactly which fields you\'d like to return in the response, e.g., {"title": true}', | ||
| ) | ||
| .optional(), | ||
| selectedLocales: z | ||
| .array(z.string()) | ||
| .describe('Optional: localized field locales to include in the duplicated document') | ||
| .optional(), | ||
| showHiddenFields: z | ||
| .boolean() | ||
| .describe('Optional: include hidden fields in the duplicated document response') | ||
| .optional(), | ||
| }), | ||
| }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => { | ||
| const payload = req.payload | ||
| const logger = getLogger({ payload }) | ||
| const { | ||
| id, | ||
| data, | ||
| depth, | ||
| draft, | ||
| fallbackLocale, | ||
| locale, | ||
| populate, | ||
| select, | ||
| selectedLocales, | ||
| showHiddenFields, | ||
| } = input | ||
|
|
||
| logger.info(`Duplicating document in collection: ${collectionSlug} with ID: ${id}`) | ||
|
|
||
| try { | ||
| const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug) | ||
| const inputData = data ? stripVirtualFields(data, virtualFieldNames) : undefined | ||
| const parsedData = inputData ? transformPointDataToPayload(inputData) : undefined | ||
|
|
||
| const result = await payload.duplicate({ | ||
| id, | ||
| collection: collectionSlug, | ||
| depth, | ||
| draft, | ||
| req, | ||
| ...localAPIDefaults(authorizedMCP), | ||
| ...(parsedData ? { data: parsedData } : {}), | ||
| ...(locale ? { locale } : {}), | ||
| ...(fallbackLocale ? { fallbackLocale } : {}), | ||
| ...(populate ? { populate: populate as PopulateType } : {}), | ||
| ...(select ? { select: select as SelectType } : {}), | ||
| ...(selectedLocales ? { selectedLocales } : {}), | ||
| ...(showHiddenFields !== undefined ? { showHiddenFields } : {}), | ||
| }) | ||
|
|
||
| logger.info(`Successfully duplicated document in ${collectionSlug} from ID: ${id}`) | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: `Document duplicated successfully in collection "${collectionSlug}"!\nDuplicated document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``, | ||
| }, | ||
| ], | ||
| doc: result as Record<string, unknown>, | ||
| } | ||
| } catch (error) { | ||
| const errorMessage = error instanceof Error ? error.message : 'Unknown error' | ||
| logger.error(`Error duplicating document in ${collectionSlug}: ${errorMessage}`) | ||
| return formatCollectionError({ action: 'duplicating', collectionSlug, error, req }) | ||
| } | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are we doing for access control here? Shouldn't this have
overrideAccess: false?