@payloadcms/plugin-mcp 4.0.0-internal.ea6e8f8 → 4.0.0-internal.ee5d8bd

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.
Files changed (64) hide show
  1. package/dist/endpoint/access.d.ts.map +1 -1
  2. package/dist/endpoint/access.js +15 -3
  3. package/dist/endpoint/access.js.map +1 -1
  4. package/dist/endpoint/index.d.ts.map +1 -1
  5. package/dist/endpoint/index.js +13 -0
  6. package/dist/endpoint/index.js.map +1 -1
  7. package/dist/endpoint/oauth.d.ts +39 -0
  8. package/dist/endpoint/oauth.d.ts.map +1 -0
  9. package/dist/endpoint/oauth.js +94 -0
  10. package/dist/endpoint/oauth.js.map +1 -0
  11. package/dist/endpoint/oauthLoginToken.d.ts +15 -0
  12. package/dist/endpoint/oauthLoginToken.d.ts.map +1 -0
  13. package/dist/endpoint/oauthLoginToken.js +45 -0
  14. package/dist/endpoint/oauthLoginToken.js.map +1 -0
  15. package/dist/endpoint/oauthServer.d.ts +12 -0
  16. package/dist/endpoint/oauthServer.d.ts.map +1 -0
  17. package/dist/endpoint/oauthServer.js +386 -0
  18. package/dist/endpoint/oauthServer.js.map +1 -0
  19. package/dist/index.d.ts +10 -2
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +15 -2
  22. package/dist/index.js.map +1 -1
  23. package/dist/mcp/buildMcpServer.js +1 -1
  24. package/dist/mcp/buildMcpServer.js.map +1 -1
  25. package/dist/mcp/builtin/collections/authTools.js.map +1 -1
  26. package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
  27. package/dist/mcp/builtin/collections/createTool.js.map +1 -1
  28. package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
  29. package/dist/mcp/builtin/collections/duplicateTool.js.map +1 -1
  30. package/dist/mcp/builtin/collections/findTool.js +1 -1
  31. package/dist/mcp/builtin/collections/findTool.js.map +1 -1
  32. package/dist/mcp/builtin/collections/restoreVersionTool.js.map +1 -1
  33. package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
  34. package/dist/mcp/builtin/collections/uploadInstructionsTool.js.map +1 -1
  35. package/dist/mcp/builtin/globals/findTool.js.map +1 -1
  36. package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
  37. package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
  38. package/dist/mcp/sanitizeMCPConfig.js +3 -0
  39. package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
  40. package/dist/types.d.ts +81 -0
  41. package/dist/types.d.ts.map +1 -1
  42. package/dist/types.js.map +1 -1
  43. package/dist/utils/toStandardSchema.js.map +1 -1
  44. package/package.json +20 -23
  45. package/src/endpoint/access.ts +12 -3
  46. package/src/endpoint/index.ts +8 -0
  47. package/src/endpoint/oauth.ts +92 -0
  48. package/src/endpoint/oauthLoginToken.ts +59 -0
  49. package/src/endpoint/oauthServer.ts +440 -0
  50. package/src/index.ts +40 -5
  51. package/src/mcp/buildMcpServer.ts +2 -2
  52. package/src/mcp/builtin/collections/authTools.ts +2 -2
  53. package/src/mcp/builtin/collections/createTool.ts +1 -3
  54. package/src/mcp/builtin/collections/deleteTool.ts +1 -1
  55. package/src/mcp/builtin/collections/duplicateTool.ts +1 -1
  56. package/src/mcp/builtin/collections/findTool.ts +1 -1
  57. package/src/mcp/builtin/collections/restoreVersionTool.ts +1 -1
  58. package/src/mcp/builtin/collections/updateTool.ts +1 -1
  59. package/src/mcp/builtin/collections/uploadInstructionsTool.ts +1 -1
  60. package/src/mcp/builtin/globals/findTool.ts +1 -1
  61. package/src/mcp/builtin/globals/updateTool.ts +1 -1
  62. package/src/mcp/sanitizeMCPConfig.ts +1 -0
  63. package/src/types.ts +82 -0
  64. package/src/utils/toStandardSchema.ts +1 -1
package/src/types.ts CHANGED
@@ -290,6 +290,14 @@ export type MCPPluginConfig = {
290
290
  serverOptions?: MCPServerOptions
291
291
  verboseLogs?: boolean
292
292
  }
293
+ /**
294
+ * How MCP clients sign in. MCP requests without an authenticated user get a `401` that points
295
+ * to the protected resource metadata (RFC 9728). Set to `false` to only accept API keys and
296
+ * anonymous requests.
297
+ *
298
+ * @default { type: 'builtin' }
299
+ */
300
+ oauth?: false | MCPOAuthConfig
293
301
  /**
294
302
  * Replace the default MCP authorization resolver.
295
303
  *
@@ -307,8 +315,82 @@ export type MCPPluginConfig = {
307
315
  tools?: MCPTopLevelToolsMap
308
316
  }
309
317
 
318
+ export type MCPOAuthConfig = MCPBuiltinOAuthConfig | MCPExternalOAuthConfig
319
+
320
+ /**
321
+ * This Payload app is the authorization server. Users sign in to the admin panel and allow the
322
+ * MCP client, which then gets an access token for them.
323
+ */
324
+ export type MCPBuiltinOAuthConfig = {
325
+ /**
326
+ * Creates the tokens the MCP client gets when the user clicks Allow. `req` is the user's own
327
+ * browser request, so `req.user` is the user who allowed it. Pass `authenticate` too if
328
+ * Payload's auth strategies don't accept the access token.
329
+ *
330
+ * @default a Payload login token for that user, with a refresh token when the collection uses sessions
331
+ */
332
+ issueToken?: (args: { req: PayloadRequest }) => MaybePromise<MCPOAuthTokens>
333
+ /**
334
+ * Trades a refresh token from `issueToken`, or from an earlier refresh, for new tokens. Return
335
+ * `null` when it's no longer valid, and the client sends the user through sign-in again. `req`
336
+ * is the MCP client's request, without a user. Only used together with `issueToken`.
337
+ *
338
+ * OAuth requires noticing copied refresh tokens: return a new refresh token every time, and
339
+ * revoke the tokens when an already used refresh token comes back.
340
+ */
341
+ refreshToken?: (args: {
342
+ refreshToken: string
343
+ req: PayloadRequest
344
+ }) => MaybePromise<MCPOAuthTokens | null>
345
+ type: 'builtin'
346
+ } & MCPOAuthSharedConfig
347
+
348
+ export type MCPOAuthTokens = {
349
+ accessToken: string
350
+ /** Seconds until the access token expires. */
351
+ expiresIn?: number
352
+ /** Lets the client get new tokens without the user, through `refreshToken`. */
353
+ refreshToken?: string
354
+ }
355
+
356
+ /** Another service, e.g. your identity provider, is the authorization server. */
357
+ export type MCPExternalOAuthConfig = {
358
+ /**
359
+ * The authorization servers that issue access tokens for MCP clients. Clients require each one
360
+ * to exactly match the `issuer` in that server's metadata, so pass a function when the exact
361
+ * value is only known at runtime, e.g. from the server's OpenID configuration or per host.
362
+ */
363
+ authorizationServers: ((args: { req: PayloadRequest }) => MaybePromise<string[]>) | string[]
364
+ /**
365
+ * Scopes MCP clients should request, e.g. `['openid', 'email', 'profile']`. Without them,
366
+ * clients request none, which some authorization servers reject or answer with a token that
367
+ * doesn't identify the user.
368
+ */
369
+ scopes?: string[]
370
+ type: 'external'
371
+ } & MCPOAuthSharedConfig
372
+
373
+ type MCPOAuthSharedConfig = {
374
+ /**
375
+ * Called on MCP requests with a bearer token that Payload's auth strategies didn't accept.
376
+ * Return the Payload user, including its `collection`, or `null` to answer with the OAuth challenge.
377
+ */
378
+ authenticate?: (args: {
379
+ req: PayloadRequest
380
+ token: string
381
+ }) => MaybePromise<PayloadRequest['user']>
382
+ /**
383
+ * The URL clients call to reach the MCP endpoint.
384
+ *
385
+ * @default the absolute URL of `/api/mcp`, based on `serverURL` or the request URL
386
+ */
387
+ resource?: string
388
+ }
389
+
310
390
  export type SanitizedMCPPluginConfig = {
311
391
  items: MCPItem[]
392
+ /** `undefined` when OAuth is turned off. */
393
+ oauth?: MCPOAuthConfig
312
394
  } & Pick<MCPPluginConfig, 'disabled' | 'hooks' | 'mcp' | 'overrideGetAuthorizedMCP'>
313
395
 
314
396
  export type MCPServerOptions = {
@@ -5,5 +5,5 @@ import type { ToolInputSchema } from '../types.js'
5
5
  /** Normalize a tool/prompt input to a Standard Schema. Raw JSON Schema gets wrapped; Standard Schema instances pass through. */
6
6
  export const toStandardSchema = (schema: ToolInputSchema): StandardSchemaWithJSON =>
7
7
  typeof schema === 'object' && schema !== null && '~standard' in schema
8
- ? (schema as StandardSchemaWithJSON)
8
+ ? schema
9
9
  : fromJsonSchema(schema)