@payloadcms/plugin-mcp 4.0.0-beta.6 → 4.0.0-beta.8

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 (69) hide show
  1. package/dist/endpoint/access.d.ts +5 -0
  2. package/dist/endpoint/access.d.ts.map +1 -1
  3. package/dist/endpoint/access.js +42 -5
  4. package/dist/endpoint/access.js.map +1 -1
  5. package/dist/endpoint/index.d.ts.map +1 -1
  6. package/dist/endpoint/index.js +25 -0
  7. package/dist/endpoint/index.js.map +1 -1
  8. package/dist/endpoint/oauth.d.ts +37 -0
  9. package/dist/endpoint/oauth.d.ts.map +1 -0
  10. package/dist/endpoint/oauth.js +105 -0
  11. package/dist/endpoint/oauth.js.map +1 -0
  12. package/dist/endpoint/oauthLoginToken.d.ts +23 -0
  13. package/dist/endpoint/oauthLoginToken.d.ts.map +1 -0
  14. package/dist/endpoint/oauthLoginToken.js +80 -0
  15. package/dist/endpoint/oauthLoginToken.js.map +1 -0
  16. package/dist/endpoint/oauthServer.d.ts +28 -0
  17. package/dist/endpoint/oauthServer.d.ts.map +1 -0
  18. package/dist/endpoint/oauthServer.js +423 -0
  19. package/dist/endpoint/oauthServer.js.map +1 -0
  20. package/dist/exports/client.d.ts +1 -1
  21. package/dist/exports/client.d.ts.map +1 -1
  22. package/dist/exports/client.js +1 -1
  23. package/dist/exports/client.js.map +1 -1
  24. package/dist/exports/rsc.d.ts +2 -0
  25. package/dist/exports/rsc.d.ts.map +1 -0
  26. package/dist/exports/rsc.js +3 -0
  27. package/dist/exports/rsc.js.map +1 -0
  28. package/dist/index.d.ts +10 -2
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +34 -10
  31. package/dist/index.js.map +1 -1
  32. package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -1
  33. package/dist/mcp/builtin/collections/deleteTool.js +3 -1
  34. package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
  35. package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
  36. package/dist/mcp/sanitizeMCPConfig.js +3 -0
  37. package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
  38. package/dist/types.d.ts +95 -0
  39. package/dist/types.d.ts.map +1 -1
  40. package/dist/types.js.map +1 -1
  41. package/dist/utils/seal.d.ts +17 -0
  42. package/dist/utils/seal.d.ts.map +1 -0
  43. package/dist/utils/seal.js +24 -0
  44. package/dist/utils/seal.js.map +1 -0
  45. package/dist/views/Authorize/AllowButtons/index.d.ts +18 -0
  46. package/dist/views/Authorize/AllowButtons/index.d.ts.map +1 -0
  47. package/dist/views/Authorize/AllowButtons/index.js +88 -0
  48. package/dist/views/Authorize/AllowButtons/index.js.map +1 -0
  49. package/dist/views/Authorize/index.css +19 -0
  50. package/dist/views/Authorize/index.d.ts +9 -0
  51. package/dist/views/Authorize/index.d.ts.map +1 -0
  52. package/dist/views/Authorize/index.js +86 -0
  53. package/dist/views/Authorize/index.js.map +1 -0
  54. package/package.json +10 -3
  55. package/src/endpoint/access.ts +45 -3
  56. package/src/endpoint/index.ts +17 -1
  57. package/src/endpoint/oauth.ts +108 -0
  58. package/src/endpoint/oauthLoginToken.ts +94 -0
  59. package/src/endpoint/oauthServer.ts +452 -0
  60. package/src/exports/client.ts +1 -1
  61. package/src/exports/rsc.ts +1 -0
  62. package/src/index.ts +52 -8
  63. package/src/mcp/builtin/collections/deleteTool.ts +3 -1
  64. package/src/mcp/sanitizeMCPConfig.ts +1 -0
  65. package/src/types.ts +94 -0
  66. package/src/utils/seal.ts +46 -0
  67. package/src/views/Authorize/AllowButtons/index.tsx +97 -0
  68. package/src/views/Authorize/index.css +19 -0
  69. package/src/views/Authorize/index.tsx +93 -0
@@ -0,0 +1,46 @@
1
+ import type { PayloadRequest } from 'payload'
2
+
3
+ /**
4
+ * Encrypts data for the client to hold, so it doesn't need to be stored. `type` keeps one kind
5
+ * from being used as another, e.g. a client ID as an authorization code.
6
+ */
7
+ export const seal = ({
8
+ type,
9
+ data,
10
+ expiresInSeconds,
11
+ req,
12
+ }: {
13
+ data: object
14
+ expiresInSeconds?: number
15
+ req: PayloadRequest
16
+ type: string
17
+ }): string =>
18
+ req.payload.encrypt(
19
+ JSON.stringify({
20
+ ...data,
21
+ type,
22
+ exp: expiresInSeconds && Date.now() + expiresInSeconds * 1000,
23
+ }),
24
+ )
25
+
26
+ export const unseal = <T>({
27
+ type,
28
+ req,
29
+ value,
30
+ }: {
31
+ req: PayloadRequest
32
+ type: string
33
+ value: null | string
34
+ }): T | undefined => {
35
+ // Only the v1 format is authenticated. Older formats decrypt anything without an error.
36
+ if (!value?.startsWith('v1:')) {
37
+ return undefined
38
+ }
39
+
40
+ try {
41
+ const data = JSON.parse(req.payload.decrypt(value, { secret: req.payload.config.secret }))
42
+ return data.type === type && (!data.exp || data.exp > Date.now()) ? data : undefined
43
+ } catch {
44
+ return undefined
45
+ }
46
+ }
@@ -0,0 +1,97 @@
1
+ 'use client'
2
+
3
+ import { Button } from '@payloadcms/ui'
4
+ import React, { useEffect, useState, useSyncExternalStore } from 'react'
5
+
6
+ const baseClass = 'mcp-authorize'
7
+
8
+ /** How long the page must have been in front of the user before the buttons work, in ms. */
9
+ const enableDelay = 1000
10
+
11
+ // Whether the page is inside a frame never changes, so there's nothing to subscribe to
12
+ const subscribe = () => () => {}
13
+
14
+ /**
15
+ * The Allow and Deny buttons, protected against clicks the user didn't mean for this page:
16
+ *
17
+ * - Inside a frame they aren't shown, so other sites can't frame the page and trick users into
18
+ * clicking them. The server can't tell, so they're missing until the browser has checked.
19
+ * - They're disabled until the page has been visible and focused for a moment. Otherwise another
20
+ * site could open the page under the user's cursor while they double-click something else, so
21
+ * the second click lands on Allow. Leaving the page disables them again. Waiting works for every
22
+ * way of clicking, unlike requiring a mouse movement or key press first.
23
+ *
24
+ * @see https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-17#section-7.11 for why the
25
+ * OAuth server must prevent clickjacking
26
+ * @see https://cheatsheetseries.owasp.org/cheatsheets/Clickjacking_Defense_Cheat_Sheet.html#defending-against-doubleclickjacking
27
+ * for the double-click attack and its mitigation
28
+ */
29
+ export function AllowButtons() {
30
+ const isTopLevel = useSyncExternalStore(
31
+ subscribe,
32
+ () => window.top === window.self,
33
+ () => false,
34
+ )
35
+ const [isEnabled, setIsEnabled] = useState(false)
36
+
37
+ useEffect(() => {
38
+ let timeout: ReturnType<typeof setTimeout> | undefined
39
+
40
+ const enableAfterDelay = () => {
41
+ clearTimeout(timeout)
42
+ if (document.visibilityState === 'visible' && document.hasFocus()) {
43
+ timeout = setTimeout(() => setIsEnabled(true), enableDelay)
44
+ }
45
+ }
46
+ const disable = () => {
47
+ clearTimeout(timeout)
48
+ setIsEnabled(false)
49
+ }
50
+ const onVisibilityChange = () => {
51
+ if (document.visibilityState === 'visible') {
52
+ enableAfterDelay()
53
+ } else {
54
+ disable()
55
+ }
56
+ }
57
+
58
+ enableAfterDelay()
59
+ window.addEventListener('focus', enableAfterDelay)
60
+ window.addEventListener('blur', disable)
61
+ document.addEventListener('visibilitychange', onVisibilityChange)
62
+
63
+ return () => {
64
+ clearTimeout(timeout)
65
+ window.removeEventListener('focus', enableAfterDelay)
66
+ window.removeEventListener('blur', disable)
67
+ document.removeEventListener('visibilitychange', onVisibilityChange)
68
+ }
69
+ }, [])
70
+
71
+ if (!isTopLevel) {
72
+ return null
73
+ }
74
+
75
+ return (
76
+ <div className={`${baseClass}__actions`}>
77
+ <Button
78
+ buttonStyle="primary"
79
+ disabled={!isEnabled}
80
+ extraButtonProps={{ name: 'decision', value: 'allow' }}
81
+ margin={false}
82
+ type="submit"
83
+ >
84
+ Allow
85
+ </Button>
86
+ <Button
87
+ buttonStyle="secondary"
88
+ disabled={!isEnabled}
89
+ extraButtonProps={{ name: 'decision', value: 'deny' }}
90
+ margin={false}
91
+ type="submit"
92
+ >
93
+ Deny
94
+ </Button>
95
+ </div>
96
+ )
97
+ }
@@ -0,0 +1,19 @@
1
+ @layer payload-default {
2
+ .mcp-authorize {
3
+ overflow-wrap: anywhere;
4
+ }
5
+
6
+ .mcp-authorize__brand {
7
+ display: flex;
8
+ justify-content: center;
9
+ width: 100%;
10
+ margin-block-end: var(--spacer-5);
11
+ }
12
+
13
+ .mcp-authorize__actions {
14
+ display: flex;
15
+ flex-wrap: wrap;
16
+ justify-content: center;
17
+ gap: var(--spacer-2);
18
+ }
19
+ }
@@ -0,0 +1,93 @@
1
+ import type { AdminViewServerProps } from 'payload'
2
+
3
+ import { FormHeader, Logo, MinimalTemplate } from '@payloadcms/ui/rsc'
4
+ import { formatAdminURL } from 'payload/shared'
5
+ import React from 'react'
6
+
7
+ import type { AuthorizeViewError, Client, Consent } from '../../endpoint/oauthServer.js'
8
+
9
+ // eslint-disable-next-line payload/no-imports-from-exports-dir -- Server component must reference exports dir for proper client boundary
10
+ import { AllowButtons } from '../../exports/client.js'
11
+ import { unseal } from '../../utils/seal.js'
12
+ import './index.css'
13
+
14
+ const baseClass = 'mcp-authorize'
15
+
16
+ const errorTitles: Record<AuthorizeViewError, string> = {
17
+ expired: 'This sign-in request expired',
18
+ 'invalid-link': 'This sign-in link is invalid',
19
+ }
20
+
21
+ /**
22
+ * The admin page where users allow an MCP client to use their account, for the built-in OAuth
23
+ * server. Its authorize endpoint sends users here with the consent to show, or with an error.
24
+ */
25
+ export function MCPAuthorizeView({
26
+ initPageResult,
27
+ params,
28
+ searchParams,
29
+ server,
30
+ user,
31
+ }: AdminViewServerProps) {
32
+ const { locale, permissions, req } = initPageResult
33
+ const { i18n, payload } = req
34
+ const sealedConsent = getSearchParam(searchParams?.consent)
35
+ const consent = unseal<Consent>({ type: 'consent', req, value: sealedConsent })
36
+ const client = consent && unseal<Client>({ type: 'client', req, value: consent.clientID })
37
+ // The consent is only valid for the user it was made for
38
+ const isConsentValid =
39
+ client &&
40
+ consent &&
41
+ req.user?.id === consent.userID &&
42
+ req.user.collection === consent.collection
43
+ const error: AuthorizeViewError =
44
+ getSearchParam(searchParams?.error) === 'invalid-link' ? 'invalid-link' : 'expired'
45
+ const account = String(user?.email ?? consent?.userID)
46
+ const returnHost = consent && (new URL(consent.redirectURI).host || consent.redirectURI)
47
+
48
+ return (
49
+ <MinimalTemplate className={baseClass}>
50
+ <div className={`${baseClass}__brand`}>
51
+ <Logo
52
+ i18n={i18n}
53
+ locale={locale}
54
+ params={params}
55
+ payload={payload}
56
+ permissions={permissions}
57
+ searchParams={searchParams}
58
+ server={server}
59
+ user={user}
60
+ />
61
+ </div>
62
+ {/* Not translated yet, so screen readers in other admin languages read it as English */}
63
+ <div lang="en">
64
+ {isConsentValid ? (
65
+ <>
66
+ <FormHeader
67
+ description={`It will be able to use this app's MCP tools as ${account}, and you will return to ${returnHost}.`}
68
+ heading={`Allow ${client.name} to use your account?`}
69
+ />
70
+ <form
71
+ action={formatAdminURL({
72
+ apiRoute: payload.config.routes.api,
73
+ path: '/mcp/oauth/authorize',
74
+ })}
75
+ method="post"
76
+ >
77
+ <input name="consent" type="hidden" value={sealedConsent!} />
78
+ <AllowButtons />
79
+ </form>
80
+ </>
81
+ ) : (
82
+ <FormHeader
83
+ description="Start the sign-in again from your MCP client."
84
+ heading={errorTitles[error]}
85
+ />
86
+ )}
87
+ </div>
88
+ </MinimalTemplate>
89
+ )
90
+ }
91
+
92
+ const getSearchParam = (value: string | string[] | undefined): null | string =>
93
+ typeof value === 'string' ? value : null