Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 79 additions & 3 deletions packages/plugin-mcp/src/collections/createApiKeysCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const createAPIKeysCollection = (
collections: PluginMCPServerConfig['collections'],
customTools: Array<{ description: string; name: string }> = [],
experimentalTools: NonNullable<PluginMCPServerConfig['experimental']>['tools'] = {},
pluginOptions: PluginMCPServerConfig,
): CollectionConfig => {
const customToolsFields = customTools.map((tool) => {
const camelCasedName = toCamelCase(tool.name)
Expand All @@ -130,6 +131,40 @@ export const createAPIKeysCollection = (
}
})

const customResourceFields =
pluginOptions.mcp?.resources?.map((resource) => {
const camelCasedName = toCamelCase(resource.name)
return {
name: camelCasedName,
type: 'checkbox' as const,
admin: {
description: resource.description,
},
defaultValue: true,
label: camelCasedName,
}
}) || []

const customPromptFields =
pluginOptions.mcp?.prompts?.map((prompt) => {
const camelCasedName = toCamelCase(prompt.name)
return {
name: camelCasedName,
type: 'checkbox' as const,
admin: {
description: prompt.description,
},
defaultValue: true,
label: camelCasedName,
}
}) || []

const userCollection = pluginOptions.userCollection
? typeof pluginOptions.userCollection === 'string'
? pluginOptions.userCollection
: pluginOptions.userCollection.slug
: 'users'

return {
slug: 'payload-mcp-api-keys',
admin: {
Expand All @@ -147,7 +182,7 @@ export const createAPIKeysCollection = (
admin: {
description: 'The user that the API key is associated with.',
},
relationTo: 'users',
relationTo: userCollection,
required: true,
},
{
Expand Down Expand Up @@ -176,12 +211,53 @@ export const createAPIKeysCollection = (
},
fields: [
{
name: 'custom',
name: 'payload-mcp-tool',
type: 'group' as const,
fields: customToolsFields,
label: ' ',
},
],
label: 'Tools',
},
]
: []),

...(pluginOptions.mcp?.resources && pluginOptions.mcp?.resources.length > 0
? [
{
type: 'collapsible' as const,
admin: {
position: 'sidebar' as const,
},
fields: [
{
name: 'payload-mcp-resource',
type: 'group' as const,
fields: customResourceFields,
label: ' ',
},
],
label: 'Resources',
},
]
: []),

...(pluginOptions.mcp?.prompts && pluginOptions.mcp?.prompts.length > 0
? [
{
type: 'collapsible' as const,
admin: {
position: 'sidebar' as const,
},
fields: [
{
name: 'payload-mcp-prompt',
type: 'group' as const,
fields: customPromptFields,
label: ' ',
},
],
label: 'Custom Tools',
label: 'Prompts',
},
]
: []),
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-mcp/src/endpoints/mcp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from 'crypto'
import { APIError, type PayloadHandler, type Where } from 'payload'

import type { PluginMCPServerConfig, ToolSettings } from '../types.js'
import type { MCPAccessSettings, PluginMCPServerConfig } from '../types.js'

import { createRequestFromPayloadRequest } from '../mcp/createRequest.js'
import { getMCPHandler } from '../mcp/getMcpHandler.js'
Expand Down Expand Up @@ -46,13 +46,13 @@ export const initializeMCPHandler = (pluginOptions: PluginMCPServerConfig) => {
throw new APIError('API Key is invalid', 401)
}

const toolSettings = docs[0] as ToolSettings
const mcpAccessSettings = docs[0] as MCPAccessSettings

if (useVerboseLogs) {
payload.logger.info('[payload-mcp] API Key is valid')
}

const handler = getMCPHandler(pluginOptions, toolSettings, req)
const handler = getMCPHandler(pluginOptions, mcpAccessSettings, req)
const request = createRequestFromPayloadRequest(req)
return await handler(request)
}
Expand Down
7 changes: 6 additions & 1 deletion packages/plugin-mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export const mcpPlugin =
* - If a custom tool has gone haywire, admins can disallow that tool.
*
*/
const apiKeyCollection = createAPIKeysCollection(collections, customTools, experimentalTools)
const apiKeyCollection = createAPIKeysCollection(
collections,
customTools,
experimentalTools,
pluginOptions,
)
if (pluginOptions.overrideApiKeyCollection) {
config.collections.push(pluginOptions.overrideApiKeyCollection(apiKeyCollection))
} else {
Expand Down
Loading
Loading