Repository: payloadcms/payload
File: packages/plugin-mcp/src/index.ts (TypeScript)

Code before:
/**
 * The MCP Plugin for Payload. This plugin allows you to add MCP capabilities to your Payload project.
 *
 * @param pluginOptions - The options for the MCP plugin.
 * @experimental This plugin is experimental and may change in the future.
 */
export const mcpPlugin =
  (pluginOptions: PluginMCPServerConfig) =>
  (config: Config): Config => {
    if (!config.collections) {
      config.collections = []
    }

    // Collections
    const collections = pluginOptions.collections || {}
    // Extract custom tools for the global config
    const customTools =
      pluginOptions.mcp?.tools?.map((tool) => ({
        name: tool.name,
        description: tool.description,
      })) || []

    // User Collection
    pluginOptions.userCollection =
      typeof pluginOptions.userCollection === 'string'
        ? pluginOptions.userCollection
        : (pluginOptions.userCollection?.slug ?? config?.admin?.user ?? defaults.userCollection)

    const experimentalTools = pluginOptions?.experimental?.tools || {}

    /**
     * API Keys
     * --------
     * High resolution control over MCP capabilities is crucial when using Payload with LLMs.
     *
     * This API Keys collection has ways for admins to create API keys and allow or disallow the MCP capabilities.
     * This is useful when Admins want to allow or disallow the use of the MCP capabilities in real time.
     * For example:
     *  - If a collection has all of its capabilities enabled, admins can allow or disallow the create, update, delete, and find capabilities on that collection.
     *  - If a collection only has the find capability enabled, admins can only allow or disallow the find capability on that collection.
     *  - If a custom tool has gone haywire, admins can disallow that tool.
     *
     */
    const apiKeyCollection = createAPIKeysCollection(
      collections,
      customTools,
      experimentalTools,
      pluginOptions,
    )
    if (pluginOptions.overrideApiKeyCollection) {
      config.collections.push(pluginOptions.overrideApiKeyCollection(apiKeyCollection))

Review comment (suggestion):
This `userCollection` type is odd to me `userCollection?: CollectionConfig | string` why did we not just make it `userCollection?: CollectionSlug`

Code after:
/**
 * The MCP Plugin for Payload. This plugin allows you to add MCP capabilities to your Payload project.
 *
 * @param pluginOptions - The options for the MCP plugin.
 * @experimental This plugin is experimental and may change in the future.
 */
export const mcpPlugin =
  (pluginOptions: PluginMCPServerConfig) =>
  (config: Config): Config => {
    if (!config.collections) {
      config.collections = []
    }

    // Collections
    const collections = pluginOptions.collections || {}
    // Globals
    const globals = pluginOptions.globals || {}
    // Extract custom tools for the global config
    const customTools =
      pluginOptions.mcp?.tools?.map((tool) => ({
        name: tool.name,
        description: tool.description,
      })) || []

    // User Collection
    pluginOptions.userCollection =
      pluginOptions.userCollection ?? config?.admin?.user ?? defaults.userCollection

    const experimentalTools = pluginOptions?.experimental?.tools || {}

    /**
     * API Keys
     * --------
     * High resolution control over MCP capabilities is crucial when using Payload with LLMs.
     *
     * This API Keys collection has ways for admins to create API keys and allow or disallow the MCP capabilities.
     * This is useful when Admins want to allow or disallow the use of the MCP capabilities in real time.
     * For example:
     *  - If a collection has all of its capabilities enabled, admins can allow or disallow the create, update, delete, and find capabilities on that collection.
     *  - If a collection only has the find capability enabled, admins can only allow or disallow the find capability on that collection.
     *  - If a global has all of its capabilities enabled, admins can allow or disallow the find and update capabilities on that global.
     *  - If a custom tool has gone haywire, admins can disallow that tool.
     *
     */
    const apiKeyCollection = createAPIKeysCollection(
      collections,
      globals,
      customTools,
      experimentalTools,
      pluginOptions,
    )