@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.
- package/dist/endpoint/access.d.ts.map +1 -1
- package/dist/endpoint/access.js +15 -3
- package/dist/endpoint/access.js.map +1 -1
- package/dist/endpoint/index.d.ts.map +1 -1
- package/dist/endpoint/index.js +13 -0
- package/dist/endpoint/index.js.map +1 -1
- package/dist/endpoint/oauth.d.ts +39 -0
- package/dist/endpoint/oauth.d.ts.map +1 -0
- package/dist/endpoint/oauth.js +94 -0
- package/dist/endpoint/oauth.js.map +1 -0
- package/dist/endpoint/oauthLoginToken.d.ts +15 -0
- package/dist/endpoint/oauthLoginToken.d.ts.map +1 -0
- package/dist/endpoint/oauthLoginToken.js +45 -0
- package/dist/endpoint/oauthLoginToken.js.map +1 -0
- package/dist/endpoint/oauthServer.d.ts +12 -0
- package/dist/endpoint/oauthServer.d.ts.map +1 -0
- package/dist/endpoint/oauthServer.js +386 -0
- package/dist/endpoint/oauthServer.js.map +1 -0
- package/dist/index.d.ts +10 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp/buildMcpServer.js +1 -1
- package/dist/mcp/buildMcpServer.js.map +1 -1
- package/dist/mcp/builtin/collections/authTools.js.map +1 -1
- package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/createTool.js.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
- package/dist/mcp/builtin/collections/duplicateTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findTool.js +1 -1
- package/dist/mcp/builtin/collections/findTool.js.map +1 -1
- package/dist/mcp/builtin/collections/restoreVersionTool.js.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- package/dist/mcp/builtin/collections/uploadInstructionsTool.js.map +1 -1
- package/dist/mcp/builtin/globals/findTool.js.map +1 -1
- package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.js +3 -0
- package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
- package/dist/types.d.ts +81 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/toStandardSchema.js.map +1 -1
- package/package.json +20 -23
- package/src/endpoint/access.ts +12 -3
- package/src/endpoint/index.ts +8 -0
- package/src/endpoint/oauth.ts +92 -0
- package/src/endpoint/oauthLoginToken.ts +59 -0
- package/src/endpoint/oauthServer.ts +440 -0
- package/src/index.ts +40 -5
- package/src/mcp/buildMcpServer.ts +2 -2
- package/src/mcp/builtin/collections/authTools.ts +2 -2
- package/src/mcp/builtin/collections/createTool.ts +1 -3
- package/src/mcp/builtin/collections/deleteTool.ts +1 -1
- package/src/mcp/builtin/collections/duplicateTool.ts +1 -1
- package/src/mcp/builtin/collections/findTool.ts +1 -1
- package/src/mcp/builtin/collections/restoreVersionTool.ts +1 -1
- package/src/mcp/builtin/collections/updateTool.ts +1 -1
- package/src/mcp/builtin/collections/uploadInstructionsTool.ts +1 -1
- package/src/mcp/builtin/globals/findTool.ts +1 -1
- package/src/mcp/builtin/globals/updateTool.ts +1 -1
- package/src/mcp/sanitizeMCPConfig.ts +1 -0
- package/src/types.ts +82 -0
- package/src/utils/toStandardSchema.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../../src/endpoint/access.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAwB,MAAM,SAAS,CAAA;AAInE,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAIzD,MAAM,MAAM,oBAAoB,GAAG;IACjC,cAAc,EAAE,OAAO,CAAA;IACvB,GAAG,EAAE,cAAc,CAAA;CACpB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,CAAC,IAAI,EAAE,oBAAoB,KAAK,OAAO,CAAC,aAAa,
|
|
1
|
+
{"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../../src/endpoint/access.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAwB,MAAM,SAAS,CAAA;AAInE,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAIzD,MAAM,MAAM,oBAAoB,GAAG;IACjC,cAAc,EAAE,OAAO,CAAA;IACvB,GAAG,EAAE,cAAc,CAAA;CACpB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,CAAC,IAAI,EAAE,oBAAoB,KAAK,OAAO,CAAC,aAAa,CA2CnF,CAAA;AAED,eAAO,MAAM,cAAc,oCAIxB;IACD,KAAK,EAAE,OAAO,EAAE,CAAA;IAChB,cAAc,EAAE,OAAO,CAAA;IACvB,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,OAAO,CAAC,OAAO,EAAE,CAkBpB,CAAA"}
|
package/dist/endpoint/access.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getAccessResults, UnauthorizedError } from 'payload';
|
|
1
|
+
import { executeAuthStrategies, getAccessResults, UnauthorizedError } from 'payload';
|
|
2
2
|
import { getPluginConfig } from '../utils/getPluginConfig.js';
|
|
3
3
|
/**
|
|
4
4
|
* Authenticates an MCP request and removes items the user cannot access.
|
|
@@ -18,11 +18,23 @@ import { getPluginConfig } from '../utils/getPluginConfig.js';
|
|
|
18
18
|
const headers = new Headers(req.headers);
|
|
19
19
|
const hasAuthorization = headers.has('Authorization');
|
|
20
20
|
headers.set('DisableAutologin', 'true');
|
|
21
|
-
|
|
21
|
+
// Not `payload.auth()`, which also computes permissions that filterMCPItems computes anyway
|
|
22
|
+
req.user = (await executeAuthStrategies({
|
|
22
23
|
headers,
|
|
24
|
+
payload: req.payload,
|
|
23
25
|
req
|
|
24
26
|
})).user;
|
|
25
|
-
|
|
27
|
+
// Only bearer tokens that no auth strategy accepted reach the hook, e.g. an OAuth access token.
|
|
28
|
+
// API keys use the `<collection> API-Key <key>` scheme instead of `Bearer`, so they never do.
|
|
29
|
+
const bearerToken = headers.get('Authorization')?.match(/^Bearer (.+)$/)?.[1];
|
|
30
|
+
if (!req.user && bearerToken && pluginConfig.oauth?.authenticate) {
|
|
31
|
+
req.user = await pluginConfig.oauth.authenticate({
|
|
32
|
+
req,
|
|
33
|
+
token: bearerToken
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
// With OAuth, the endpoint answers with a challenge that starts the client's sign-in
|
|
37
|
+
if (hasAuthorization && !req.user && !pluginConfig.oauth) {
|
|
26
38
|
throw new UnauthorizedError(req.t);
|
|
27
39
|
}
|
|
28
40
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/endpoint/access.ts"],"sourcesContent":["import type { PayloadRequest, SanitizedPermissions } from 'payload'\n\nimport { getAccessResults, UnauthorizedError } from 'payload'\n\nimport type { AuthorizedMCP, MCPItem } from '../types.js'\n\nimport { getPluginConfig } from '../utils/getPluginConfig.js'\n\nexport type GetAuthorizedMCPArgs = {\n overrideAccess: boolean\n req: PayloadRequest\n}\n\n/**\n * Authenticates an MCP request and removes items the user cannot access.\n * `overrideAccess` skips Payload and MCP item access checks.\n */\nexport const getAuthorizedMCP: (args: GetAuthorizedMCPArgs) => Promise<AuthorizedMCP> = async ({\n overrideAccess,\n req,\n}) => {\n const pluginConfig = getPluginConfig({ config: req.payload.config })\n\n if (pluginConfig.overrideGetAuthorizedMCP) {\n return await pluginConfig.overrideGetAuthorizedMCP({\n overrideAccess,\n pluginConfig,\n req,\n })\n }\n\n if (req.headers) {\n const headers = new Headers(req.headers)\n const hasAuthorization = headers.has('Authorization')\n\n headers.set('DisableAutologin', 'true')\n req.user = (await
|
|
1
|
+
{"version":3,"sources":["../../src/endpoint/access.ts"],"sourcesContent":["import type { PayloadRequest, SanitizedPermissions } from 'payload'\n\nimport { executeAuthStrategies, getAccessResults, UnauthorizedError } from 'payload'\n\nimport type { AuthorizedMCP, MCPItem } from '../types.js'\n\nimport { getPluginConfig } from '../utils/getPluginConfig.js'\n\nexport type GetAuthorizedMCPArgs = {\n overrideAccess: boolean\n req: PayloadRequest\n}\n\n/**\n * Authenticates an MCP request and removes items the user cannot access.\n * `overrideAccess` skips Payload and MCP item access checks.\n */\nexport const getAuthorizedMCP: (args: GetAuthorizedMCPArgs) => Promise<AuthorizedMCP> = async ({\n overrideAccess,\n req,\n}) => {\n const pluginConfig = getPluginConfig({ config: req.payload.config })\n\n if (pluginConfig.overrideGetAuthorizedMCP) {\n return await pluginConfig.overrideGetAuthorizedMCP({\n overrideAccess,\n pluginConfig,\n req,\n })\n }\n\n if (req.headers) {\n const headers = new Headers(req.headers)\n const hasAuthorization = headers.has('Authorization')\n\n headers.set('DisableAutologin', 'true')\n // Not `payload.auth()`, which also computes permissions that filterMCPItems computes anyway\n req.user = (await executeAuthStrategies({ headers, payload: req.payload, req })).user\n\n // Only bearer tokens that no auth strategy accepted reach the hook, e.g. an OAuth access token.\n // API keys use the `<collection> API-Key <key>` scheme instead of `Bearer`, so they never do.\n const bearerToken = headers.get('Authorization')?.match(/^Bearer (.+)$/)?.[1]\n if (!req.user && bearerToken && pluginConfig.oauth?.authenticate) {\n req.user = await pluginConfig.oauth.authenticate({ req, token: bearerToken })\n }\n\n // With OAuth, the endpoint answers with a challenge that starts the client's sign-in\n if (hasAuthorization && !req.user && !pluginConfig.oauth) {\n throw new UnauthorizedError(req.t)\n }\n }\n\n return {\n items: await filterMCPItems({\n items: pluginConfig.items,\n overrideAccess,\n req,\n }),\n overrideAccess,\n }\n}\n\nexport const filterMCPItems = async ({\n items,\n overrideAccess,\n req,\n}: {\n items: MCPItem[]\n overrideAccess: boolean\n req: PayloadRequest\n}): Promise<MCPItem[]> => {\n // Match Payload core: overrideAccess bypasses access evaluation\n if (overrideAccess) {\n return items\n }\n\n const authorizedItems: MCPItem[] = []\n\n const permissions = await getAccessResults({ req })\n\n for (const item of items) {\n if (!(await checkItemAccess({ item, permissions, req }))) {\n continue\n }\n authorizedItems.push(item)\n }\n\n return authorizedItems\n}\n\n/**\n * Runs MCP item access callbacks\n */\nconst checkItemAccess = async ({\n item,\n permissions,\n req,\n}: {\n item: MCPItem\n permissions?: SanitizedPermissions\n req: PayloadRequest\n}): Promise<boolean> => {\n switch (item.type) {\n case 'collectionTool':\n return (\n !item.tool.access ||\n (await item.tool.access({ slug: item.collectionSlug, permissions, req }))\n )\n case 'globalTool':\n return (\n !item.tool.access || (await item.tool.access({ slug: item.globalSlug, permissions, req }))\n )\n case 'prompt':\n return !item.prompt.access || (await item.prompt.access({ permissions, req }))\n case 'resource':\n return !item.resource.access || (await item.resource.access({ permissions, req }))\n case 'tool':\n return !item.tool.access || (await item.tool.access({ permissions, req }))\n }\n}\n"],"names":["executeAuthStrategies","getAccessResults","UnauthorizedError","getPluginConfig","getAuthorizedMCP","overrideAccess","req","pluginConfig","config","payload","overrideGetAuthorizedMCP","headers","Headers","hasAuthorization","has","set","user","bearerToken","get","match","oauth","authenticate","token","t","items","filterMCPItems","authorizedItems","permissions","item","checkItemAccess","push","type","tool","access","slug","collectionSlug","globalSlug","prompt","resource"],"mappings":"AAEA,SAASA,qBAAqB,EAAEC,gBAAgB,EAAEC,iBAAiB,QAAQ,UAAS;AAIpF,SAASC,eAAe,QAAQ,8BAA6B;AAO7D;;;CAGC,GACD,OAAO,MAAMC,mBAA2E,OAAO,EAC7FC,cAAc,EACdC,GAAG,EACJ;IACC,MAAMC,eAAeJ,gBAAgB;QAAEK,QAAQF,IAAIG,OAAO,CAACD,MAAM;IAAC;IAElE,IAAID,aAAaG,wBAAwB,EAAE;QACzC,OAAO,MAAMH,aAAaG,wBAAwB,CAAC;YACjDL;YACAE;YACAD;QACF;IACF;IAEA,IAAIA,IAAIK,OAAO,EA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JD,WAAW,EACXrB,GAAG,EAKJ;IACC,OAAQsB,KAAKG,IAAI;QACf,KAAK;YACH,OACE,CAACH,KAAKI,IAAI,CAACC,MAAM,IAChB,MAAML,KAAKI,IAAI,CAACC,MAAM,CAAC;gBAAEC,MAAMN,KAAKO,cAAc;gBAAER;gBAAarB;YAAI;QAE1E,KAAK;YACH,OACE,CAACsB,KAAKI,IAAI,CAACC,MAAM,IAAK,MAAML,KAAKI,IAAI,CAACC,MAAM,CAAC;gBAAEC,MAAMN,KAAKQ,UAAU;gBAAET;gBAAarB;YAAI;QAE3F,KAAK;YACH,OAAO,CAACsB,KAAKS,MAAM,CAACJ,MAAM,IAAK,MAAML,KAAKS,MAAM,CAACJ,MAAM,CAAC;gBAAEN;gBAAarB;YAAI;QAC7E,KAAK;YACH,OAAO,CAACsB,KAAKU,QAAQ,CAACL,MAAM,IAAK,MAAML,KAAKU,QAAQ,CAACL,MAAM,CAAC;gBAAEN;gBAAarB;YAAI;QACjF,KAAK;YACH,OAAO,CAACsB,KAAKI,IAAI,CAACC,MAAM,IAAK,MAAML,KAAKI,IAAI,CAACC,MAAM,CAAC;gBAAEN;gBAAarB;YAAI;IAC3E;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/endpoint/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAkB,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/endpoint/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAkB,MAAM,SAAS,CAAA;AAe7D,eAAO,MAAM,WAAW,EAAE,cA0EzB,CAAA"}
|
package/dist/endpoint/index.js
CHANGED
|
@@ -3,11 +3,19 @@ import { APIError } from 'payload';
|
|
|
3
3
|
import { buildMcpServer } from '../mcp/buildMcpServer.js';
|
|
4
4
|
import { getPluginConfig } from '../utils/getPluginConfig.js';
|
|
5
5
|
import { getAuthorizedMCP } from './access.js';
|
|
6
|
+
import { getMCPResource, oauthChallenge } from './oauth.js';
|
|
6
7
|
export const mcpEndpoint = async (req)=>{
|
|
7
8
|
if (!req.url) {
|
|
8
9
|
throw new APIError('Missing request URL', 400);
|
|
9
10
|
}
|
|
10
11
|
req.payloadAPI = 'MCP';
|
|
12
|
+
// Lets auth strategies check that an OAuth access token was issued for this MCP server
|
|
13
|
+
req.context.mcp = {
|
|
14
|
+
resource: getMCPResource({
|
|
15
|
+
config: req.payload.config,
|
|
16
|
+
url: req.url
|
|
17
|
+
})
|
|
18
|
+
};
|
|
11
19
|
const pluginConfig = getPluginConfig({
|
|
12
20
|
config: req.payload.config
|
|
13
21
|
});
|
|
@@ -25,6 +33,11 @@ export const mcpEndpoint = async (req)=>{
|
|
|
25
33
|
overrideAccess,
|
|
26
34
|
req
|
|
27
35
|
});
|
|
36
|
+
if (pluginConfig.oauth && !req.user) {
|
|
37
|
+
return oauthChallenge({
|
|
38
|
+
req
|
|
39
|
+
});
|
|
40
|
+
}
|
|
28
41
|
const maxRequestBodySize = pluginConfig.mcp?.maxRequestBodySize;
|
|
29
42
|
// Payload augments the original web-standard Request in place.
|
|
30
43
|
const mcpRequest = req;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/endpoint/index.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\n\nimport {\n createMcpHandler,\n isLegacyRequest,\n readRequestBody,\n WebStandardStreamableHTTPServerTransport,\n} from '@modelcontextprotocol/server'\nimport { APIError } from 'payload'\n\nimport { buildMcpServer } from '../mcp/buildMcpServer.js'\nimport { getPluginConfig } from '../utils/getPluginConfig.js'\nimport { getAuthorizedMCP } from './access.js'\n\nexport const mcpEndpoint: PayloadHandler = async (req) => {\n if (!req.url) {\n throw new APIError('Missing request URL', 400)\n }\n\n req.payloadAPI = 'MCP' as const\n\n const pluginConfig = getPluginConfig({ config: req.payload.config })\n const overrideAccessParam = new URL(req.url).searchParams.get('overrideAccess')\n\n if (overrideAccessParam !== null && process.env.NODE_ENV !== 'development') {\n throw new APIError('MCP overrideAccess is only available in development.', 400)\n }\n\n let overrideAccess = false\n if (overrideAccessParam === 'true') {\n overrideAccess = true\n } else if (overrideAccessParam !== null && overrideAccessParam !== 'false') {\n throw new APIError('MCP overrideAccess must be \"true\" or \"false\".', 400)\n }\n\n const authorizedMCP = await getAuthorizedMCP({ overrideAccess, req })\n const maxRequestBodySize = pluginConfig.mcp?.maxRequestBodySize\n // Payload augments the original web-standard Request in place.\n const mcpRequest = req as PayloadRequest & Request\n const parsedBody = await parseRequestBody({ maxRequestBodySize, request: mcpRequest })\n\n // Keep the old JSON-only, stateless behavior because the SDK's 2025 fallback uses SSE.\n if (await isLegacyRequest(mcpRequest, parsedBody, { maxRequestBodySize })) {\n const server = buildMcpServer({ authorizedMCP, pluginConfig, req })\n const transport = new WebStandardStreamableHTTPServerTransport({\n enableJsonResponse: true,\n maxRequestBodySize,\n sessionIdGenerator: undefined, // stateless mode\n })\n transport.onerror = (err) => {\n req.payload.logger.error({ err, msg: 'Error serving legacy MCP request' })\n }\n\n try {\n await server.connect(transport)\n return await transport.handleRequest(mcpRequest, { parsedBody })\n } finally {\n await server.close().catch((err) => {\n req.payload.logger.error({ err, msg: 'Error closing MCP server' })\n })\n }\n }\n\n const handler = createMcpHandler(() => buildMcpServer({ authorizedMCP, pluginConfig, req }), {\n legacy: 'reject',\n maxRequestBodySize,\n // SDK subscriptions always use SSE, so disable them to keep every response JSON-only.\n maxSubscriptions: 0,\n onerror: (err) => {\n req.payload.logger.error({ err, msg: 'Error serving modern MCP request' })\n },\n responseMode: 'json',\n })\n\n try {\n return await handler.fetch(mcpRequest, { parsedBody })\n } finally {\n await handler.close().catch((err) => {\n req.payload.logger.error({ err, msg: 'Error closing modern MCP handler' })\n })\n }\n}\n\n/**\n * Parses a POST body once so the era check and the SDK handler can share it. The SDK\n * skips its size limit for a pre-parsed body, so this reads with the same limit. It reads\n * a clone and returns `undefined` for any body the SDK should reject (too large, unreadable,\n * or not JSON), leaving the original request for the SDK to read and answer.\n */\nconst parseRequestBody = async ({\n maxRequestBodySize,\n request,\n}: {\n maxRequestBodySize?: number\n request: Request\n}): Promise<unknown> => {\n if (request.method.toUpperCase() !== 'POST') {\n return undefined\n }\n\n try {\n const body = await readRequestBody(request.clone(), maxRequestBodySize)\n return body.tooLarge ? undefined : JSON.parse(body.text)\n } catch {\n return undefined\n }\n}\n"],"names":["createMcpHandler","isLegacyRequest","readRequestBody","WebStandardStreamableHTTPServerTransport","APIError","buildMcpServer","getPluginConfig","getAuthorizedMCP","mcpEndpoint","req","url","payloadAPI","
|
|
1
|
+
{"version":3,"sources":["../../src/endpoint/index.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\n\nimport {\n createMcpHandler,\n isLegacyRequest,\n readRequestBody,\n WebStandardStreamableHTTPServerTransport,\n} from '@modelcontextprotocol/server'\nimport { APIError } from 'payload'\n\nimport { buildMcpServer } from '../mcp/buildMcpServer.js'\nimport { getPluginConfig } from '../utils/getPluginConfig.js'\nimport { getAuthorizedMCP } from './access.js'\nimport { getMCPResource, oauthChallenge } from './oauth.js'\n\nexport const mcpEndpoint: PayloadHandler = async (req) => {\n if (!req.url) {\n throw new APIError('Missing request URL', 400)\n }\n\n req.payloadAPI = 'MCP' as const\n // Lets auth strategies check that an OAuth access token was issued for this MCP server\n req.context.mcp = { resource: getMCPResource({ config: req.payload.config, url: req.url }) }\n\n const pluginConfig = getPluginConfig({ config: req.payload.config })\n const overrideAccessParam = new URL(req.url).searchParams.get('overrideAccess')\n\n if (overrideAccessParam !== null && process.env.NODE_ENV !== 'development') {\n throw new APIError('MCP overrideAccess is only available in development.', 400)\n }\n\n let overrideAccess = false\n if (overrideAccessParam === 'true') {\n overrideAccess = true\n } else if (overrideAccessParam !== null && overrideAccessParam !== 'false') {\n throw new APIError('MCP overrideAccess must be \"true\" or \"false\".', 400)\n }\n\n const authorizedMCP = await getAuthorizedMCP({ overrideAccess, req })\n\n if (pluginConfig.oauth && !req.user) {\n return oauthChallenge({ req })\n }\n\n const maxRequestBodySize = pluginConfig.mcp?.maxRequestBodySize\n // Payload augments the original web-standard Request in place.\n const mcpRequest = req as PayloadRequest & Request\n const parsedBody = await parseRequestBody({ maxRequestBodySize, request: mcpRequest })\n\n // Keep the old JSON-only, stateless behavior because the SDK's 2025 fallback uses SSE.\n if (await isLegacyRequest(mcpRequest, parsedBody, { maxRequestBodySize })) {\n const server = buildMcpServer({ authorizedMCP, pluginConfig, req })\n const transport = new WebStandardStreamableHTTPServerTransport({\n enableJsonResponse: true,\n maxRequestBodySize,\n sessionIdGenerator: undefined, // stateless mode\n })\n transport.onerror = (err) => {\n req.payload.logger.error({ err, msg: 'Error serving legacy MCP request' })\n }\n\n try {\n await server.connect(transport)\n return await transport.handleRequest(mcpRequest, { parsedBody })\n } finally {\n await server.close().catch((err) => {\n req.payload.logger.error({ err, msg: 'Error closing MCP server' })\n })\n }\n }\n\n const handler = createMcpHandler(() => buildMcpServer({ authorizedMCP, pluginConfig, req }), {\n legacy: 'reject',\n maxRequestBodySize,\n // SDK subscriptions always use SSE, so disable them to keep every response JSON-only.\n maxSubscriptions: 0,\n onerror: (err) => {\n req.payload.logger.error({ err, msg: 'Error serving modern MCP request' })\n },\n responseMode: 'json',\n })\n\n try {\n return await handler.fetch(mcpRequest, { parsedBody })\n } finally {\n await handler.close().catch((err) => {\n req.payload.logger.error({ err, msg: 'Error closing modern MCP handler' })\n })\n }\n}\n\n/**\n * Parses a POST body once so the era check and the SDK handler can share it. The SDK\n * skips its size limit for a pre-parsed body, so this reads with the same limit. It reads\n * a clone and returns `undefined` for any body the SDK should reject (too large, unreadable,\n * or not JSON), leaving the original request for the SDK to read and answer.\n */\nconst parseRequestBody = async ({\n maxRequestBodySize,\n request,\n}: {\n maxRequestBodySize?: number\n request: Request\n}): Promise<unknown> => {\n if (request.method.toUpperCase() !== 'POST') {\n return undefined\n }\n\n try {\n const body = await readRequestBody(request.clone(), maxRequestBodySize)\n return body.tooLarge ? undefined : JSON.parse(body.text)\n } catch {\n return undefined\n }\n}\n"],"names":["createMcpHandler","isLegacyRequest","readRequestBody","WebStandardStreamableHTTPServerTransport","APIError","buildMcpServer","getPluginConfig","getAuthorizedMCP","getMCPResource","oauthChallenge","mcpEndpoint","req","url","payloadAPI","context","mcp","resource","config","payload","pluginConfig","overrideAccessParam","URL","searchParams","get","process","env","NODE_ENV","overrideAccess","authorizedMCP","oauth","user","maxRequestBodySize","mcpRequest","parsedBody","parseRequestBody","request","server","transport","enableJsonResponse","sessionIdGenerator","undefined","onerror","err","logger","error","msg","connect","handleRequest","close","catch","handler","legacy","maxSubscriptions","responseMode","fetch","method","toUpperCase","body","clone","tooLarge","JSON","parse","text"],"mappings":"AAEA,SACEA,gBAAgB,EAChBC,eAAe,EACfC,eAAe,EACfC,wCAAwC,QACnC,+BAA8B;AACrC,SAASC,QAAQ,QAAQ,UAAS;AAElC,SAASC,cAAc,QAAQ,2BAA0B;AACzD,SAASC,eAAe,QAAQ,8BAA6B;AAC7D,SAASC,gBAAgB,QAAQ,cAAa;AAC9C,SAASC,cAAc,EAAEC,cAAc,QAAQ,aAAY;AAE3D,OAAO,MAAMC,cAA8B,OAAOC;IAChD,IAAI,CAACA,IAAIC,GAAG,EAAE;QACZ,MAAM,IAAIR,SAAS,uBAAuB;IAC5C;IAEAO,IAAIE,UAAU,GAAG;IACjB,uFAAuF;IACvFF,IAAIG,OAAO,CAACC,GAAG,GAAG;QAAEC,UAAUR,eAAe;YAAES,QAAQN,IAAIO,OAAO,CAACD,MAAM;YAAEL,KAAKD,IAAIC,GAAG;QAAC;IAAG;IAE3F,MAAMO,eAAeb,gBAAgB;QAAEW,QAAQN,IAAIO,OAAO,CAACD,MAAM;IAAC;IAClE,MAAMG,sBAAsB,IAAIC,IAAIV,IAAIC,GAAG,EAAEU,YAAY,CAACC,GAAG,CAAC;IAE9D,IAAIH,wBA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AeT;YAAcR;QAAI,IAAI;QAC3FwC,QAAQ;QACRpB;QACA,sFAAsF;QACtFqB,kBAAkB;QAClBX,SAAS,CAACC;YACR/B,IAAIO,OAAO,CAACyB,MAAM,CAACC,KAAK,CAAC;gBAAEF;gBAAKG,KAAK;YAAmC;QAC1E;QACAQ,cAAc;IAChB;IAEA,IAAI;QACF,OAAO,MAAMH,QAAQI,KAAK,CAACtB,YAAY;YAAEC;QAAW;IACtD,SAAU;QACR,MAAMiB,QAAQF,KAAK,GAAGC,KAAK,CAAC,CAACP;YAC3B/B,IAAIO,OAAO,CAACyB,MAAM,CAACC,KAAK,CAAC;gBAAEF;gBAAKG,KAAK;YAAmC;QAC1E;IACF;AACF,EAAC;AAED;;;;;CAKC,GACD,MAAMX,mBAAmB,OAAO,EAC9BH,kBAAkB,EAClBI,OAAO,EAIR;IACC,IAAIA,QAAQoB,MAAM,CAACC,WAAW,OAAO,QAAQ;QAC3C,OAAOhB;IACT;IAEA,IAAI;QACF,MAAMiB,OAAO,MAAMvD,gBAAgBiC,QAAQuB,KAAK,IAAI3B;QACpD,OAAO0B,KAAKE,QAAQ,GAAGnB,YAAYoB,KAAKC,KAAK,CAACJ,KAAKK,IAAI;IACzD,EAAE,OAAM;QACN,OAAOtB;IACT;AACF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { PayloadHandler, PayloadRequest, SanitizedConfig } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* Serves the OAuth protected resource metadata (RFC 9728) at `/api/mcp/oauth-protected-resource`.
|
|
4
|
+
* The OAuth challenge links here, so MCP clients find the authorization server without setup.
|
|
5
|
+
*/
|
|
6
|
+
export declare const oauthProtectedResourceEndpoint: PayloadHandler;
|
|
7
|
+
/**
|
|
8
|
+
* Serves the same metadata for clients that ignore the challenge's link and only look at
|
|
9
|
+
* `/.well-known/oauth-protected-resource`. Mount it at the site root, e.g. in Next.js at
|
|
10
|
+
* `app/.well-known/oauth-protected-resource/[[...path]]/route.ts`:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import config from '@payload-config'
|
|
14
|
+
* import { oauthProtectedResourceHandler } from '@payloadcms/plugin-mcp'
|
|
15
|
+
*
|
|
16
|
+
* export const GET = oauthProtectedResourceHandler(config)
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare const oauthProtectedResourceHandler: (config: Promise<SanitizedConfig> | SanitizedConfig) => (request: Request) => Promise<Response>;
|
|
20
|
+
/**
|
|
21
|
+
* Answers an unauthenticated MCP request with a challenge linking to the protected resource
|
|
22
|
+
* metadata, which starts the client's OAuth flow.
|
|
23
|
+
*/
|
|
24
|
+
export declare const oauthChallenge: ({ req }: {
|
|
25
|
+
req: PayloadRequest;
|
|
26
|
+
}) => Response;
|
|
27
|
+
/**
|
|
28
|
+
* The MCP endpoint's address, as advertised in the metadata's `resource`. Clients send it when
|
|
29
|
+
* they sign in, and authorization servers that support it put it in the access token's audience.
|
|
30
|
+
*/
|
|
31
|
+
export declare const getMCPResource: ({ config, url }: {
|
|
32
|
+
config: SanitizedConfig;
|
|
33
|
+
url: string;
|
|
34
|
+
}) => string;
|
|
35
|
+
export declare const getMCPURL: ({ config, url }: {
|
|
36
|
+
config: SanitizedConfig;
|
|
37
|
+
url: string;
|
|
38
|
+
}) => string;
|
|
39
|
+
//# sourceMappingURL=oauth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth.d.ts","sourceRoot":"","sources":["../../src/endpoint/oauth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAW9E;;;GAGG;AACH,eAAO,MAAM,8BAA8B,EAAE,cAAsD,CAAA;AAEnG;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,6BAA6B,WAC/B,OAAO,CAAC,eAAe,CAAC,GAAG,eAAe,eACnC,OAAO,KAAG,OAAO,CAAC,QAAQ,CAGtC,CAAA;AAEN;;;GAGG;AACH,eAAO,MAAM,cAAc,YAAa;IAAE,GAAG,EAAE,cAAc,CAAA;CAAE,KAAG,QAcjE,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,oBAAqB;IAAE,MAAM,EAAE,eAAe,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,KAAG,MAChB,CAAA;AAE3E,eAAO,MAAM,SAAS,oBAAqB;IAAE,MAAM,EAAE,eAAe,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,KAAG,MACnB,CAAA"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { bearerAuthChallengeResponse, OAuthError, OAuthErrorCode } from '@modelcontextprotocol/server';
|
|
2
|
+
import { createPayloadRequest, getPayload } from 'payload';
|
|
3
|
+
import { getPluginConfig } from '../utils/getPluginConfig.js';
|
|
4
|
+
/**
|
|
5
|
+
* Serves the OAuth protected resource metadata (RFC 9728) at `/api/mcp/oauth-protected-resource`.
|
|
6
|
+
* The OAuth challenge links here, so MCP clients find the authorization server without setup.
|
|
7
|
+
*/ export const oauthProtectedResourceEndpoint = (req)=>getMetadataResponse({
|
|
8
|
+
req
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Serves the same metadata for clients that ignore the challenge's link and only look at
|
|
12
|
+
* `/.well-known/oauth-protected-resource`. Mount it at the site root, e.g. in Next.js at
|
|
13
|
+
* `app/.well-known/oauth-protected-resource/[[...path]]/route.ts`:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import config from '@payload-config'
|
|
17
|
+
* import { oauthProtectedResourceHandler } from '@payloadcms/plugin-mcp'
|
|
18
|
+
*
|
|
19
|
+
* export const GET = oauthProtectedResourceHandler(config)
|
|
20
|
+
* ```
|
|
21
|
+
*/ export const oauthProtectedResourceHandler = (config)=>async (request)=>getMetadataResponse({
|
|
22
|
+
req: await createPayloadRequest({
|
|
23
|
+
payload: await getPayload({
|
|
24
|
+
config
|
|
25
|
+
}),
|
|
26
|
+
req: request
|
|
27
|
+
})
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* Answers an unauthenticated MCP request with a challenge linking to the protected resource
|
|
31
|
+
* metadata, which starts the client's OAuth flow.
|
|
32
|
+
*/ export const oauthChallenge = ({ req })=>{
|
|
33
|
+
const { oauth } = getPluginConfig({
|
|
34
|
+
config: req.payload.config
|
|
35
|
+
});
|
|
36
|
+
const response = bearerAuthChallengeResponse(new OAuthError(OAuthErrorCode.InvalidToken, req.t('error:unauthorized')), {
|
|
37
|
+
requiredScopes: oauth?.type === 'external' ? oauth.scopes : undefined,
|
|
38
|
+
resourceMetadataUrl: `${getMCPURL({
|
|
39
|
+
config: req.payload.config,
|
|
40
|
+
url: req.url
|
|
41
|
+
})}/oauth-protected-resource`
|
|
42
|
+
});
|
|
43
|
+
// Lets browser-based MCP clients read the challenge, when Payload's `cors` setting allows them
|
|
44
|
+
response.headers.set('Access-Control-Expose-Headers', 'WWW-Authenticate');
|
|
45
|
+
return response;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* The MCP endpoint's address, as advertised in the metadata's `resource`. Clients send it when
|
|
49
|
+
* they sign in, and authorization servers that support it put it in the access token's audience.
|
|
50
|
+
*/ export const getMCPResource = ({ config, url })=>getPluginConfig({
|
|
51
|
+
config
|
|
52
|
+
}).oauth?.resource ?? getMCPURL({
|
|
53
|
+
config,
|
|
54
|
+
url
|
|
55
|
+
});
|
|
56
|
+
export const getMCPURL = ({ config, url })=>new URL(`${config.routes.api}/mcp`, config.serverURL || url).href;
|
|
57
|
+
const getMetadataResponse = async ({ req })=>{
|
|
58
|
+
const { config } = req.payload;
|
|
59
|
+
const { oauth } = getPluginConfig({
|
|
60
|
+
config
|
|
61
|
+
});
|
|
62
|
+
if (!oauth) {
|
|
63
|
+
return new Response(null, {
|
|
64
|
+
status: 404
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return Response.json({
|
|
68
|
+
authorization_servers: oauth.type === 'builtin' ? [
|
|
69
|
+
`${getMCPURL({
|
|
70
|
+
config,
|
|
71
|
+
url: req.url
|
|
72
|
+
})}/oauth`
|
|
73
|
+
] : typeof oauth.authorizationServers === 'function' ? await oauth.authorizationServers({
|
|
74
|
+
req
|
|
75
|
+
}) : oauth.authorizationServers,
|
|
76
|
+
bearer_methods_supported: [
|
|
77
|
+
'header'
|
|
78
|
+
],
|
|
79
|
+
resource: getMCPResource({
|
|
80
|
+
config,
|
|
81
|
+
url: req.url
|
|
82
|
+
}),
|
|
83
|
+
scopes_supported: oauth.type === 'external' ? oauth.scopes : undefined
|
|
84
|
+
}, // Public document, readable by browser-based MCP clients on any origin. Not cached, since
|
|
85
|
+
// without `serverURL` its URLs come from the request.
|
|
86
|
+
{
|
|
87
|
+
headers: {
|
|
88
|
+
'Access-Control-Allow-Origin': '*',
|
|
89
|
+
'Cache-Control': 'no-store'
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
//# sourceMappingURL=oauth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/endpoint/oauth.ts"],"sourcesContent":["import type { OAuthProtectedResourceMetadata } from '@modelcontextprotocol/server'\nimport type { PayloadHandler, PayloadRequest, SanitizedConfig } from 'payload'\n\nimport {\n bearerAuthChallengeResponse,\n OAuthError,\n OAuthErrorCode,\n} from '@modelcontextprotocol/server'\nimport { createPayloadRequest, getPayload } from 'payload'\n\nimport { getPluginConfig } from '../utils/getPluginConfig.js'\n\n/**\n * Serves the OAuth protected resource metadata (RFC 9728) at `/api/mcp/oauth-protected-resource`.\n * The OAuth challenge links here, so MCP clients find the authorization server without setup.\n */\nexport const oauthProtectedResourceEndpoint: PayloadHandler = (req) => getMetadataResponse({ req })\n\n/**\n * Serves the same metadata for clients that ignore the challenge's link and only look at\n * `/.well-known/oauth-protected-resource`. Mount it at the site root, e.g. in Next.js at\n * `app/.well-known/oauth-protected-resource/[[...path]]/route.ts`:\n *\n * ```ts\n * import config from '@payload-config'\n * import { oauthProtectedResourceHandler } from '@payloadcms/plugin-mcp'\n *\n * export const GET = oauthProtectedResourceHandler(config)\n * ```\n */\nexport const oauthProtectedResourceHandler =\n (config: Promise<SanitizedConfig> | SanitizedConfig) =>\n async (request: Request): Promise<Response> =>\n getMetadataResponse({\n req: await createPayloadRequest({ payload: await getPayload({ config }), req: request }),\n })\n\n/**\n * Answers an unauthenticated MCP request with a challenge linking to the protected resource\n * metadata, which starts the client's OAuth flow.\n */\nexport const oauthChallenge = ({ req }: { req: PayloadRequest }): Response => {\n const { oauth } = getPluginConfig({ config: req.payload.config })\n const response = bearerAuthChallengeResponse(\n new OAuthError(OAuthErrorCode.InvalidToken, req.t('error:unauthorized')),\n {\n requiredScopes: oauth?.type === 'external' ? oauth.scopes : undefined,\n resourceMetadataUrl: `${getMCPURL({ config: req.payload.config, url: req.url! })}/oauth-protected-resource`,\n },\n )\n\n // Lets browser-based MCP clients read the challenge, when Payload's `cors` setting allows them\n response.headers.set('Access-Control-Expose-Headers', 'WWW-Authenticate')\n\n return response\n}\n\n/**\n * The MCP endpoint's address, as advertised in the metadata's `resource`. Clients send it when\n * they sign in, and authorization servers that support it put it in the access token's audience.\n */\nexport const getMCPResource = ({ config, url }: { config: SanitizedConfig; url: string }): string =>\n getPluginConfig({ config }).oauth?.resource ?? getMCPURL({ config, url })\n\nexport const getMCPURL = ({ config, url }: { config: SanitizedConfig; url: string }): string =>\n new URL(`${config.routes.api}/mcp`, config.serverURL || url).href\n\nconst getMetadataResponse = async ({ req }: { req: PayloadRequest }): Promise<Response> => {\n const { config } = req.payload\n const { oauth } = getPluginConfig({ config })\n\n if (!oauth) {\n return new Response(null, { status: 404 })\n }\n\n return Response.json(\n {\n authorization_servers:\n oauth.type === 'builtin'\n ? [`${getMCPURL({ config, url: req.url! })}/oauth`]\n : typeof oauth.authorizationServers === 'function'\n ? await oauth.authorizationServers({ req })\n : oauth.authorizationServers,\n bearer_methods_supported: ['header'],\n resource: getMCPResource({ config, url: req.url! }),\n scopes_supported: oauth.type === 'external' ? oauth.scopes : undefined,\n } satisfies OAuthProtectedResourceMetadata,\n // Public document, readable by browser-based MCP clients on any origin. Not cached, since\n // without `serverURL` its URLs come from the request.\n { headers: { 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'no-store' } },\n )\n}\n"],"names":["bearerAuthChallengeResponse","OAuthError","OAuthErrorCode","createPayloadRequest","getPayload","getPluginConfig","oauthProtectedResourceEndpoint","req","getMetadataResponse","oauthProtectedResourceHandler","config","request","payload","oauthChallenge","oauth","response","InvalidToken","t","requiredScopes","type","scopes","undefined","resourceMetadataUrl","getMCPURL","url","headers","set","getMCPResource","resource","URL","routes","api","serverURL","href","Response","status","json","authorization_servers","authorizationServers","bearer_methods_supported","scopes_supported"],"mappings":"AAGA,SACEA,2BAA2B,EAC3BC,UAAU,EACVC,cAAc,QACT,+BAA8B;AACrC,SAASC,oBAAoB,EAAEC,UAAU,QAAQ,UAAS;AAE1D,SAASC,eAAe,QAAQ,8BAA6B;AAE7D;;;CAGC,GACD,OAAO,MAAMC,iCAAiD,CAACC,MAAQC,oBAAoB;QAAED;IAAI,GAAE;AAEnG;;;;;;;;;;;CAWC,GACD,OAAO,MAAME,gCACX,CAACC,SACD,OAAOC,UACLH,oBAAoB;YAClBD,KAAK,MAAMJ,qBAAqB;gBAAES,SAAS,MAAMR,WAAW;oBAAEM;gBAAO;gBAAIH,KAAKI;YAAQ;QACxF,GAAE;AAEN;;;CAGC,GACD,OAAO,MAAME,iBAAiB,CAAC,EAAEN,GAAG,EAA2B;IAC7D,MAAM,EAAEO,KAAK,EAAE,GAAGT,gBAAgB;QAAEK,QAAQH,IAAIK,OAAO,CAACF,MAAM;IAAC;IAC/D,MAAMK,WAAWf,4BACf,IAAIC,WAAWC,eAAec,YAAY,EAAET,IAAIU,CAAC,CAAC,wBAClD;QACEC,gBAAgBJ,OAAOK,SAAS,aAAaL,MAAMM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I,KAAK,aAAaL,MAAMM,MAAM,GAAGC;IAC/D,GACA,0FAA0F;IAC1F,sDAAsD;IACtD;QAAEI,SAAS;YAAE,+BAA+B;YAAK,iBAAiB;QAAW;IAAE;AAEnF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PayloadRequest } from 'payload';
|
|
2
|
+
import type { MCPOAuthTokens } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The default `issueToken`: a Payload login token, which Payload's JWT strategy accepts. When the
|
|
5
|
+
* collection uses sessions, the client also gets a refresh token for the token's session.
|
|
6
|
+
*/
|
|
7
|
+
export declare const issueLoginToken: ({ req, }: {
|
|
8
|
+
req: PayloadRequest;
|
|
9
|
+
}) => Promise<MCPOAuthTokens>;
|
|
10
|
+
/** The default `refreshToken`. Payload logs out the session when a refresh token is reused. */
|
|
11
|
+
export declare const refreshLoginToken: ({ refreshToken, req, }: {
|
|
12
|
+
refreshToken: string;
|
|
13
|
+
req: PayloadRequest;
|
|
14
|
+
}) => Promise<MCPOAuthTokens | null>;
|
|
15
|
+
//# sourceMappingURL=oauthLoginToken.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauthLoginToken.d.ts","sourceRoot":"","sources":["../../src/endpoint/oauthLoginToken.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAI7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD;;;GAGG;AACH,eAAO,MAAM,eAAe,aAEzB;IACD,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,OAAO,CAAC,cAAc,CAyBzB,CAAA;AAED,+FAA+F;AAC/F,eAAO,MAAM,iBAAiB,2BAG3B;IACD,YAAY,EAAE,MAAM,CAAA;IACpB,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAUhC,CAAA"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { APIError, createAuthToken, refreshAuthToken } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* The default `issueToken`: a Payload login token, which Payload's JWT strategy accepts. When the
|
|
4
|
+
* collection uses sessions, the client also gets a refresh token for the token's session.
|
|
5
|
+
*/ export const issueLoginToken = async ({ req })=>{
|
|
6
|
+
const collectionConfig = req.payload.collections[req.user.collection].config;
|
|
7
|
+
if (collectionConfig.auth.disableLocalStrategy) {
|
|
8
|
+
throw new APIError(`MCP clients can't get Payload login tokens for the "${collectionConfig.slug}" collection because its local strategy is disabled. Pass oauth.issueToken to the MCP plugin.`);
|
|
9
|
+
}
|
|
10
|
+
// The full document, like the one a password login starts its session on
|
|
11
|
+
const user = await req.payload.db.findOne({
|
|
12
|
+
collection: collectionConfig.slug,
|
|
13
|
+
req,
|
|
14
|
+
where: {
|
|
15
|
+
id: {
|
|
16
|
+
equals: req.user.id
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
const { refreshToken, token } = await createAuthToken({
|
|
21
|
+
collectionConfig,
|
|
22
|
+
email: user.email,
|
|
23
|
+
req,
|
|
24
|
+
shouldCreateRefreshToken: true,
|
|
25
|
+
user
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
accessToken: token,
|
|
29
|
+
expiresIn: collectionConfig.auth.tokenExpiration,
|
|
30
|
+
refreshToken
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
/** The default `refreshToken`. Payload logs out the session when a refresh token is reused. */ export const refreshLoginToken = async ({ refreshToken, req })=>{
|
|
34
|
+
const refreshed = await refreshAuthToken({
|
|
35
|
+
refreshToken,
|
|
36
|
+
req
|
|
37
|
+
});
|
|
38
|
+
return refreshed && {
|
|
39
|
+
accessToken: refreshed.token,
|
|
40
|
+
expiresIn: refreshed.collectionConfig.auth.tokenExpiration,
|
|
41
|
+
refreshToken: refreshed.refreshToken
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=oauthLoginToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/endpoint/oauthLoginToken.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { APIError, createAuthToken, refreshAuthToken } from 'payload'\n\nimport type { MCPOAuthTokens } from '../types.js'\n\n/**\n * The default `issueToken`: a Payload login token, which Payload's JWT strategy accepts. When the\n * collection uses sessions, the client also gets a refresh token for the token's session.\n */\nexport const issueLoginToken = async ({\n req,\n}: {\n req: PayloadRequest\n}): Promise<MCPOAuthTokens> => {\n const collectionConfig = req.payload.collections[req.user!.collection]!.config\n\n if (collectionConfig.auth.disableLocalStrategy) {\n throw new APIError(\n `MCP clients can't get Payload login tokens for the \"${collectionConfig.slug}\" collection because its local strategy is disabled. Pass oauth.issueToken to the MCP plugin.`,\n )\n }\n\n // The full document, like the one a password login starts its session on\n const user = (await req.payload.db.findOne<NonNullable<PayloadRequest['user']>>({\n collection: collectionConfig.slug,\n req,\n where: { id: { equals: req.user!.id } },\n }))!\n\n const { refreshToken, token } = await createAuthToken({\n collectionConfig,\n email: user.email!,\n req,\n shouldCreateRefreshToken: true,\n user,\n })\n\n return { accessToken: token, expiresIn: collectionConfig.auth.tokenExpiration, refreshToken }\n}\n\n/** The default `refreshToken`. Payload logs out the session when a refresh token is reused. */\nexport const refreshLoginToken = async ({\n refreshToken,\n req,\n}: {\n refreshToken: string\n req: PayloadRequest\n}): Promise<MCPOAuthTokens | null> => {\n const refreshed = await refreshAuthToken({ refreshToken, req })\n\n return (\n refreshed && {\n accessToken: refreshed.token,\n expiresIn: refreshed.collectionConfig.auth.tokenExpiration,\n refreshToken: refreshed.refreshToken,\n }\n )\n}\n"],"names":["APIError","createAuthToken","refreshAuthToken","issueLoginToken","req","collectionConfig","payload","collections","user","collection","config","auth","disableLocalStrategy","slug","db","findOne","where","id","equals","refreshToken","token","email","shouldCreateRefreshToken","accessToken","expiresIn","tokenExpiration","refreshL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Auth authorization server for MCP clients when `oauth.type` is
|
|
4
|
+
* `'builtin'`. Clients register, the user allows them after logging in to the admin panel, and
|
|
5
|
+
* the client receives the tokens `issueToken` creates.
|
|
6
|
+
*
|
|
7
|
+
* Clients, consent forms, authorization codes, and refresh tokens are encrypted and handed to the
|
|
8
|
+
* client instead of being stored. Only the default tokens write to the database, to the user's
|
|
9
|
+
* login sessions.
|
|
10
|
+
*/
|
|
11
|
+
export declare const oauthServerEndpoints: Endpoint[];
|
|
12
|
+
//# sourceMappingURL=oauthServer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauthServer.d.ts","sourceRoot":"","sources":["../../src/endpoint/oauthServer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,QAAQ,EAAkC,MAAM,SAAS,CAAA;AAmCvE;;;;;;;;GAQG;AACH,eAAO,MAAM,oBAAoB,EAAE,QAAQ,EAqC1C,CAAA"}
|