@payloadcms/plugin-mcp 4.0.0-internal.77191e5 → 4.0.0-internal.7d5c665
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/bin.js +0 -7
- 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 +46 -4
- package/dist/endpoint/index.js.map +1 -1
- package/dist/endpoint/oauth.d.ts +35 -0
- package/dist/endpoint/oauth.d.ts.map +1 -0
- package/dist/endpoint/oauth.js +75 -0
- package/dist/endpoint/oauth.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -1
- package/dist/mcp/buildMcpServer.d.ts +1 -1
- package/dist/mcp/buildMcpServer.js +1 -1
- package/dist/mcp/buildMcpServer.js.map +1 -1
- package/dist/mcp/builtin/collections/authTools.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/authTools.js +4 -3
- package/dist/mcp/builtin/collections/authTools.js.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +19 -3
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -1
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js +19 -3
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.js +1 -0
- package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
- package/dist/stdio.js +4 -3
- package/dist/stdio.js.map +1 -1
- package/dist/types.d.ts +45 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +5 -5
- package/src/endpoint/access.ts +12 -3
- package/src/endpoint/index.ts +41 -3
- package/src/endpoint/oauth.ts +87 -0
- package/src/index.ts +19 -2
- package/src/mcp/buildMcpServer.ts +1 -1
- package/src/mcp/builtin/collections/authTools.ts +5 -2
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +12 -1
- package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +11 -1
- package/src/mcp/sanitizeMCPConfig.ts +1 -0
- package/src/stdio.ts +2 -2
- package/src/types.ts +46 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/plugin-mcp",
|
|
3
|
-
"version": "4.0.0-internal.
|
|
3
|
+
"version": "4.0.0-internal.7d5c665",
|
|
4
4
|
"description": "MCP (Model Context Protocol) capabilities with Payload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"plugin",
|
|
@@ -55,15 +55,15 @@
|
|
|
55
55
|
"bin.js"
|
|
56
56
|
],
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@modelcontextprotocol/server": "2.
|
|
59
|
-
"tsx": "4.
|
|
58
|
+
"@modelcontextprotocol/server": "2.3.1",
|
|
59
|
+
"tsx": "4.23.15"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"@payloadcms/eslint-config": "3.28.0",
|
|
63
|
-
"payload": "4.0.0-internal.
|
|
63
|
+
"payload": "4.0.0-internal.7d5c665"
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
|
-
"payload": "4.0.0-internal.
|
|
66
|
+
"payload": "4.0.0-internal.7d5c665"
|
|
67
67
|
},
|
|
68
68
|
"//deps_notes": {
|
|
69
69
|
"tsx": "Required by the `bin.js` stdio mcp. Keep the version pinned to whatever `payload` ships to avoid duplicate installs."
|
package/src/endpoint/access.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PayloadRequest, SanitizedPermissions } from 'payload'
|
|
2
2
|
|
|
3
|
-
import { getAccessResults, UnauthorizedError } from 'payload'
|
|
3
|
+
import { executeAuthStrategies, getAccessResults, UnauthorizedError } from 'payload'
|
|
4
4
|
|
|
5
5
|
import type { AuthorizedMCP, MCPItem } from '../types.js'
|
|
6
6
|
|
|
@@ -34,9 +34,18 @@ export const getAuthorizedMCP: (args: GetAuthorizedMCPArgs) => Promise<Authorize
|
|
|
34
34
|
const hasAuthorization = headers.has('Authorization')
|
|
35
35
|
|
|
36
36
|
headers.set('DisableAutologin', 'true')
|
|
37
|
-
|
|
37
|
+
// Not `payload.auth()`, which also computes permissions that filterMCPItems computes anyway
|
|
38
|
+
req.user = (await executeAuthStrategies({ headers, payload: req.payload, req })).user
|
|
39
|
+
|
|
40
|
+
// Only bearer tokens that no auth strategy accepted reach the hook, e.g. an OAuth access token.
|
|
41
|
+
// API keys use the `<collection> API-Key <key>` scheme instead of `Bearer`, so they never do.
|
|
42
|
+
const bearerToken = headers.get('Authorization')?.match(/^Bearer (.+)$/)?.[1]
|
|
43
|
+
if (!req.user && bearerToken && pluginConfig.oauth?.authenticate) {
|
|
44
|
+
req.user = await pluginConfig.oauth.authenticate({ req, token: bearerToken })
|
|
45
|
+
}
|
|
38
46
|
|
|
39
|
-
|
|
47
|
+
// With OAuth, the endpoint answers with a challenge that starts the client's sign-in
|
|
48
|
+
if (hasAuthorization && !req.user && !pluginConfig.oauth) {
|
|
40
49
|
throw new UnauthorizedError(req.t)
|
|
41
50
|
}
|
|
42
51
|
}
|
package/src/endpoint/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { PayloadHandler, PayloadRequest } from 'payload'
|
|
|
3
3
|
import {
|
|
4
4
|
createMcpHandler,
|
|
5
5
|
isLegacyRequest,
|
|
6
|
+
readRequestBody,
|
|
6
7
|
WebStandardStreamableHTTPServerTransport,
|
|
7
8
|
} from '@modelcontextprotocol/server'
|
|
8
9
|
import { APIError } from 'payload'
|
|
@@ -10,6 +11,7 @@ import { APIError } from 'payload'
|
|
|
10
11
|
import { buildMcpServer } from '../mcp/buildMcpServer.js'
|
|
11
12
|
import { getPluginConfig } from '../utils/getPluginConfig.js'
|
|
12
13
|
import { getAuthorizedMCP } from './access.js'
|
|
14
|
+
import { getMCPResource, oauthChallenge } from './oauth.js'
|
|
13
15
|
|
|
14
16
|
export const mcpEndpoint: PayloadHandler = async (req) => {
|
|
15
17
|
if (!req.url) {
|
|
@@ -17,6 +19,8 @@ export const mcpEndpoint: PayloadHandler = async (req) => {
|
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
req.payloadAPI = 'MCP' as const
|
|
22
|
+
// Lets auth strategies check that an OAuth access token was issued for this MCP server
|
|
23
|
+
req.context.mcp = { resource: getMCPResource({ config: req.payload.config, url: req.url }) }
|
|
20
24
|
|
|
21
25
|
const pluginConfig = getPluginConfig({ config: req.payload.config })
|
|
22
26
|
const overrideAccessParam = new URL(req.url).searchParams.get('overrideAccess')
|
|
@@ -33,14 +37,22 @@ export const mcpEndpoint: PayloadHandler = async (req) => {
|
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
const authorizedMCP = await getAuthorizedMCP({ overrideAccess, req })
|
|
40
|
+
|
|
41
|
+
if (pluginConfig.oauth && !req.user) {
|
|
42
|
+
return oauthChallenge({ req })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const maxRequestBodySize = pluginConfig.mcp?.maxRequestBodySize
|
|
36
46
|
// Payload augments the original web-standard Request in place.
|
|
37
47
|
const mcpRequest = req as PayloadRequest & Request
|
|
48
|
+
const parsedBody = await parseRequestBody({ maxRequestBodySize, request: mcpRequest })
|
|
38
49
|
|
|
39
50
|
// Keep the old JSON-only, stateless behavior because the SDK's 2025 fallback uses SSE.
|
|
40
|
-
if (await isLegacyRequest(mcpRequest)) {
|
|
51
|
+
if (await isLegacyRequest(mcpRequest, parsedBody, { maxRequestBodySize })) {
|
|
41
52
|
const server = buildMcpServer({ authorizedMCP, pluginConfig, req })
|
|
42
53
|
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
43
54
|
enableJsonResponse: true,
|
|
55
|
+
maxRequestBodySize,
|
|
44
56
|
sessionIdGenerator: undefined, // stateless mode
|
|
45
57
|
})
|
|
46
58
|
transport.onerror = (err) => {
|
|
@@ -49,7 +61,7 @@ export const mcpEndpoint: PayloadHandler = async (req) => {
|
|
|
49
61
|
|
|
50
62
|
try {
|
|
51
63
|
await server.connect(transport)
|
|
52
|
-
return await transport.handleRequest(mcpRequest)
|
|
64
|
+
return await transport.handleRequest(mcpRequest, { parsedBody })
|
|
53
65
|
} finally {
|
|
54
66
|
await server.close().catch((err) => {
|
|
55
67
|
req.payload.logger.error({ err, msg: 'Error closing MCP server' })
|
|
@@ -59,6 +71,7 @@ export const mcpEndpoint: PayloadHandler = async (req) => {
|
|
|
59
71
|
|
|
60
72
|
const handler = createMcpHandler(() => buildMcpServer({ authorizedMCP, pluginConfig, req }), {
|
|
61
73
|
legacy: 'reject',
|
|
74
|
+
maxRequestBodySize,
|
|
62
75
|
// SDK subscriptions always use SSE, so disable them to keep every response JSON-only.
|
|
63
76
|
maxSubscriptions: 0,
|
|
64
77
|
onerror: (err) => {
|
|
@@ -68,10 +81,35 @@ export const mcpEndpoint: PayloadHandler = async (req) => {
|
|
|
68
81
|
})
|
|
69
82
|
|
|
70
83
|
try {
|
|
71
|
-
return await handler.fetch(mcpRequest)
|
|
84
|
+
return await handler.fetch(mcpRequest, { parsedBody })
|
|
72
85
|
} finally {
|
|
73
86
|
await handler.close().catch((err) => {
|
|
74
87
|
req.payload.logger.error({ err, msg: 'Error closing modern MCP handler' })
|
|
75
88
|
})
|
|
76
89
|
}
|
|
77
90
|
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Parses a POST body once so the era check and the SDK handler can share it. The SDK
|
|
94
|
+
* skips its size limit for a pre-parsed body, so this reads with the same limit. It reads
|
|
95
|
+
* a clone and returns `undefined` for any body the SDK should reject (too large, unreadable,
|
|
96
|
+
* or not JSON), leaving the original request for the SDK to read and answer.
|
|
97
|
+
*/
|
|
98
|
+
const parseRequestBody = async ({
|
|
99
|
+
maxRequestBodySize,
|
|
100
|
+
request,
|
|
101
|
+
}: {
|
|
102
|
+
maxRequestBodySize?: number
|
|
103
|
+
request: Request
|
|
104
|
+
}): Promise<unknown> => {
|
|
105
|
+
if (request.method.toUpperCase() !== 'POST') {
|
|
106
|
+
return undefined
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
const body = await readRequestBody(request.clone(), maxRequestBodySize)
|
|
111
|
+
return body.tooLarge ? undefined : JSON.parse(body.text)
|
|
112
|
+
} catch {
|
|
113
|
+
return undefined
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { OAuthProtectedResourceMetadata } from '@modelcontextprotocol/server'
|
|
2
|
+
import type { PayloadHandler, PayloadRequest, SanitizedConfig } from 'payload'
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
bearerAuthChallengeResponse,
|
|
6
|
+
OAuthError,
|
|
7
|
+
OAuthErrorCode,
|
|
8
|
+
} from '@modelcontextprotocol/server'
|
|
9
|
+
|
|
10
|
+
import { getPluginConfig } from '../utils/getPluginConfig.js'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Serves the OAuth protected resource metadata (RFC 9728) at `/api/mcp/oauth-protected-resource`.
|
|
14
|
+
* The OAuth challenge links here, so MCP clients find the authorization server without setup.
|
|
15
|
+
*/
|
|
16
|
+
export const oauthProtectedResourceEndpoint: PayloadHandler = (req) =>
|
|
17
|
+
getMetadataResponse({ config: req.payload.config, url: req.url! })
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Serves the same metadata for clients that ignore the challenge's link and only look at
|
|
21
|
+
* `/.well-known/oauth-protected-resource`. Mount it at the site root, e.g. in Next.js at
|
|
22
|
+
* `app/.well-known/oauth-protected-resource/[[...path]]/route.ts`:
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* import config from '@payload-config'
|
|
26
|
+
* import { oauthProtectedResourceHandler } from '@payloadcms/plugin-mcp'
|
|
27
|
+
*
|
|
28
|
+
* export const GET = oauthProtectedResourceHandler(config)
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export const oauthProtectedResourceHandler =
|
|
32
|
+
(config: Promise<SanitizedConfig> | SanitizedConfig) =>
|
|
33
|
+
async (request: Request): Promise<Response> =>
|
|
34
|
+
getMetadataResponse({ config: await config, url: request.url })
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Answers an unauthenticated MCP request with a challenge linking to the protected resource
|
|
38
|
+
* metadata, which starts the client's OAuth flow.
|
|
39
|
+
*/
|
|
40
|
+
export const oauthChallenge = ({ req }: { req: PayloadRequest }): Response =>
|
|
41
|
+
bearerAuthChallengeResponse(
|
|
42
|
+
new OAuthError(OAuthErrorCode.InvalidToken, req.t('error:unauthorized')),
|
|
43
|
+
{
|
|
44
|
+
requiredScopes: getPluginConfig({ config: req.payload.config }).oauth?.scopes,
|
|
45
|
+
resourceMetadataUrl: `${getMCPURL({ config: req.payload.config, url: req.url! })}/oauth-protected-resource`,
|
|
46
|
+
},
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The MCP endpoint's address, as advertised in the metadata's `resource`. Clients send it when
|
|
51
|
+
* they sign in, and authorization servers that support it put it in the access token's audience.
|
|
52
|
+
*/
|
|
53
|
+
export const getMCPResource = ({ config, url }: { config: SanitizedConfig; url: string }): string =>
|
|
54
|
+
getPluginConfig({ config }).oauth?.resource ?? getMCPURL({ config, url })
|
|
55
|
+
|
|
56
|
+
const getMCPURL = ({ config, url }: { config: SanitizedConfig; url: string }): string =>
|
|
57
|
+
new URL(`${config.routes.api}/mcp`, config.serverURL || url).href
|
|
58
|
+
|
|
59
|
+
const getMetadataResponse = async ({
|
|
60
|
+
config,
|
|
61
|
+
url,
|
|
62
|
+
}: {
|
|
63
|
+
config: SanitizedConfig
|
|
64
|
+
url: string
|
|
65
|
+
}): Promise<Response> => {
|
|
66
|
+
const pluginConfig = getPluginConfig({ config })
|
|
67
|
+
|
|
68
|
+
if (!pluginConfig.oauth) {
|
|
69
|
+
return new Response(null, { status: 404 })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const { authorizationServers, scopes } = pluginConfig.oauth
|
|
73
|
+
|
|
74
|
+
return Response.json(
|
|
75
|
+
{
|
|
76
|
+
authorization_servers:
|
|
77
|
+
typeof authorizationServers === 'function'
|
|
78
|
+
? await authorizationServers()
|
|
79
|
+
: authorizationServers,
|
|
80
|
+
bearer_methods_supported: ['header'],
|
|
81
|
+
resource: getMCPResource({ config, url }),
|
|
82
|
+
scopes_supported: scopes,
|
|
83
|
+
} satisfies OAuthProtectedResourceMetadata,
|
|
84
|
+
// Public document, readable by browser-based MCP clients on any origin
|
|
85
|
+
{ headers: { 'Access-Control-Allow-Origin': '*' } },
|
|
86
|
+
)
|
|
87
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { definePlugin } from 'payload'
|
|
|
3
3
|
import type { AuthorizedMCP, MCPPluginConfig, SanitizedMCPPluginConfig } from './types.js'
|
|
4
4
|
|
|
5
5
|
import { mcpEndpoint } from './endpoint/index.js'
|
|
6
|
+
import { oauthProtectedResourceEndpoint } from './endpoint/oauth.js'
|
|
6
7
|
import { sanitizeMCPConfig } from './mcp/sanitizeMCPConfig.js'
|
|
7
8
|
|
|
8
9
|
declare module 'payload' {
|
|
@@ -13,11 +14,19 @@ declare module 'payload' {
|
|
|
13
14
|
/** After the plugin's `plugin` callback runs, `options` holds the sanitized config. */
|
|
14
15
|
'@payloadcms/plugin-mcp': MCPPluginConfig
|
|
15
16
|
}
|
|
17
|
+
export interface RequestContext {
|
|
18
|
+
/** Set on MCP requests before they're authenticated. */
|
|
19
|
+
mcp?: {
|
|
20
|
+
/** The MCP endpoint's address, which OAuth access tokens for MCP are issued for. */
|
|
21
|
+
resource: string
|
|
22
|
+
}
|
|
23
|
+
}
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
export type { AuthorizedMCP, MCPPluginConfig, SanitizedMCPPluginConfig }
|
|
19
27
|
|
|
20
28
|
export { defineCollectionTool, defineGlobalTool, definePrompt, defineTool } from './defineTool.js'
|
|
29
|
+
export { oauthProtectedResourceHandler } from './endpoint/oauth.js'
|
|
21
30
|
|
|
22
31
|
export const mcpPlugin = definePlugin<MCPPluginConfig>({
|
|
23
32
|
slug: '@payloadcms/plugin-mcp',
|
|
@@ -40,15 +49,23 @@ export const mcpPlugin = definePlugin<MCPPluginConfig>({
|
|
|
40
49
|
...config,
|
|
41
50
|
endpoints: [
|
|
42
51
|
...(config.endpoints ?? []),
|
|
43
|
-
// Payload prefixes /api, so the full path is /api/mcp.
|
|
44
|
-
|
|
52
|
+
// Payload prefixes /api, so the full path is /api/mcp. getAuthorizedMCP authenticates the
|
|
53
|
+
// request with dev auto-login turned off, so Payload doesn't authenticate it first.
|
|
54
|
+
{ disableAuth: true, handler: mcpEndpoint, method: 'post', path: '/mcp' },
|
|
45
55
|
// Streamable HTTP's optional server=>client GET stream. We don't offer
|
|
46
56
|
// one, so answer 405 per spec-— clients then skip it cleanly
|
|
47
57
|
{
|
|
58
|
+
disableAuth: true,
|
|
48
59
|
handler: () => new Response(null, { headers: { Allow: 'POST' }, status: 405 }),
|
|
49
60
|
method: 'get',
|
|
50
61
|
path: '/mcp',
|
|
51
62
|
},
|
|
63
|
+
{
|
|
64
|
+
disableAuth: true,
|
|
65
|
+
handler: oauthProtectedResourceEndpoint,
|
|
66
|
+
method: 'get',
|
|
67
|
+
path: '/mcp/oauth-protected-resource',
|
|
68
|
+
},
|
|
52
69
|
],
|
|
53
70
|
}
|
|
54
71
|
},
|
|
@@ -25,7 +25,7 @@ import { toStandardSchema } from '../utils/toStandardSchema.js'
|
|
|
25
25
|
*
|
|
26
26
|
* `req` is the request context handlers see. For HTTP it's the live
|
|
27
27
|
* `PayloadRequest` derived from the incoming HTTP request; for stdio it's a
|
|
28
|
-
* synthesized one built via `
|
|
28
|
+
* synthesized one built via `createPayloadRequest`.
|
|
29
29
|
*/
|
|
30
30
|
export const buildMcpServer = ({
|
|
31
31
|
authorizedMCP,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createPayloadRequest, strictObject, z } from 'payload'
|
|
2
2
|
|
|
3
3
|
import type { MCPToolResponse } from '../../../types.js'
|
|
4
4
|
|
|
@@ -53,7 +53,10 @@ export const authCollectionTool = defineCollectionTool({
|
|
|
53
53
|
if (input.headers) {
|
|
54
54
|
authHeaders = new Headers(input.headers)
|
|
55
55
|
}
|
|
56
|
-
const authReq = await
|
|
56
|
+
const authReq = await createPayloadRequest({
|
|
57
|
+
payload: req.payload,
|
|
58
|
+
req: { headers: authHeaders },
|
|
59
|
+
})
|
|
57
60
|
const result = await req.payload.auth({ headers: authHeaders, req: authReq })
|
|
58
61
|
|
|
59
62
|
if (result.user) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getAccessResults, getCollectionInputSchema, getCollectionSchemaInputSchema } from 'payload'
|
|
2
|
+
import { getLLMInstructions } from 'payload/internal'
|
|
2
3
|
|
|
3
4
|
import { defaultAccess } from '../../../defaultAccess.js'
|
|
4
5
|
import { defineCollectionTool } from '../../../defineTool.js'
|
|
@@ -16,7 +17,8 @@ export const getCollectionSchemaTool = defineCollectionTool({
|
|
|
16
17
|
readOnlyHint: true,
|
|
17
18
|
title: 'Get Collection Schema',
|
|
18
19
|
},
|
|
19
|
-
description:
|
|
20
|
+
description:
|
|
21
|
+
'Get the input schema and LLM instructions before creating or updating documents in a collection.',
|
|
20
22
|
input: getCollectionSchemaInputSchema,
|
|
21
23
|
}).handler(async ({ slug, authorizedMCP, req }) => {
|
|
22
24
|
const permissions = authorizedMCP.overrideAccess
|
|
@@ -64,17 +66,26 @@ export const getCollectionSchemaTool = defineCollectionTool({
|
|
|
64
66
|
}
|
|
65
67
|
: { enabled: false }
|
|
66
68
|
|
|
69
|
+
const instructions = await getLLMInstructions({
|
|
70
|
+
slug,
|
|
71
|
+
type: 'collection',
|
|
72
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
73
|
+
req,
|
|
74
|
+
})
|
|
75
|
+
|
|
67
76
|
return {
|
|
68
77
|
content: [
|
|
69
78
|
{
|
|
70
79
|
type: 'text',
|
|
71
80
|
text: `Schema for collection "${slug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\`\nUpload configuration:\n\`\`\`json\n${JSON.stringify(upload)}\n\`\`\``,
|
|
72
81
|
},
|
|
82
|
+
...(instructions ? [{ type: 'text' as const, text: instructions }] : []),
|
|
73
83
|
],
|
|
74
84
|
structuredContent: {
|
|
75
85
|
slug,
|
|
76
86
|
schema: inputSchema,
|
|
77
87
|
upload,
|
|
88
|
+
...(instructions ? { instructions } : {}),
|
|
78
89
|
},
|
|
79
90
|
}
|
|
80
91
|
})
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getAccessResults, getGlobalInputSchema, getGlobalSchemaInputSchema } from 'payload'
|
|
2
|
+
import { getLLMInstructions } from 'payload/internal'
|
|
2
3
|
|
|
3
4
|
import { defaultAccess } from '../../../defaultAccess.js'
|
|
4
5
|
import { defineGlobalTool } from '../../../defineTool.js'
|
|
@@ -16,7 +17,7 @@ export const getGlobalSchemaTool = defineGlobalTool({
|
|
|
16
17
|
readOnlyHint: true,
|
|
17
18
|
title: 'Get Global Schema',
|
|
18
19
|
},
|
|
19
|
-
description: 'Get the input schema
|
|
20
|
+
description: 'Get the input schema and LLM instructions before updating a global.',
|
|
20
21
|
input: getGlobalSchemaInputSchema,
|
|
21
22
|
}).handler(async ({ slug, authorizedMCP, req }) => {
|
|
22
23
|
const permissions = authorizedMCP.overrideAccess
|
|
@@ -48,16 +49,25 @@ export const getGlobalSchemaTool = defineGlobalTool({
|
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
|
|
52
|
+
const instructions = await getLLMInstructions({
|
|
53
|
+
slug,
|
|
54
|
+
type: 'global',
|
|
55
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
56
|
+
req,
|
|
57
|
+
})
|
|
58
|
+
|
|
51
59
|
return {
|
|
52
60
|
content: [
|
|
53
61
|
{
|
|
54
62
|
type: 'text',
|
|
55
63
|
text: `Schema for global "${slug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\``,
|
|
56
64
|
},
|
|
65
|
+
...(instructions ? [{ type: 'text' as const, text: instructions }] : []),
|
|
57
66
|
],
|
|
58
67
|
structuredContent: {
|
|
59
68
|
slug,
|
|
60
69
|
schema: inputSchema,
|
|
70
|
+
...(instructions ? { instructions } : {}),
|
|
61
71
|
},
|
|
62
72
|
}
|
|
63
73
|
})
|
package/src/stdio.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { Config, Plugin, SanitizedConfig } from 'payload'
|
|
|
3
3
|
/* eslint-disable no-console */
|
|
4
4
|
import { serveStdio } from '@modelcontextprotocol/server/stdio'
|
|
5
5
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
6
|
-
import {
|
|
6
|
+
import { createPayloadRequest, getPayload } from 'payload'
|
|
7
7
|
import { findConfig, loadEnv } from 'payload/node'
|
|
8
8
|
|
|
9
9
|
import type { SanitizedMCPPluginConfig } from './types.js'
|
|
@@ -91,7 +91,7 @@ export const runMcpStdio = async (): Promise<void> => {
|
|
|
91
91
|
headers.set('Authorization', process.env.PAYLOAD_MCP_AUTHORIZATION)
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
const req = await
|
|
94
|
+
const req = await createPayloadRequest({ payload, req: { headers } })
|
|
95
95
|
req.payloadAPI = 'MCP' as const
|
|
96
96
|
const authorizedMCP = await getAuthorizedMCP({ overrideAccess, req })
|
|
97
97
|
|
package/src/types.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
CallToolResult,
|
|
3
3
|
ContentBlock,
|
|
4
|
+
Implementation,
|
|
4
5
|
JsonSchemaType,
|
|
5
|
-
|
|
6
|
+
McpServerOptions,
|
|
6
7
|
ResourceTemplate,
|
|
7
8
|
ServerContext,
|
|
8
9
|
StandardSchemaWithJSON,
|
|
@@ -279,9 +280,50 @@ export type MCPPluginConfig = {
|
|
|
279
280
|
afterToolCall?: MCPAfterToolCallHook[]
|
|
280
281
|
}
|
|
281
282
|
mcp?: {
|
|
283
|
+
/**
|
|
284
|
+
* Largest HTTP request body, in bytes, that the MCP endpoint accepts. Larger requests
|
|
285
|
+
* are answered with `413`. Base64 file uploads count toward this limit.
|
|
286
|
+
*
|
|
287
|
+
* @default 4194304 (4 MiB, the MCP SDK default)
|
|
288
|
+
*/
|
|
289
|
+
maxRequestBodySize?: number
|
|
282
290
|
serverOptions?: MCPServerOptions
|
|
283
291
|
verboseLogs?: boolean
|
|
284
292
|
}
|
|
293
|
+
/**
|
|
294
|
+
* Lets MCP clients sign in through an OAuth authorization server. MCP requests without an
|
|
295
|
+
* authenticated user get a `401` that points to the protected resource metadata (RFC 9728),
|
|
296
|
+
* served by `oauthProtectedResourceHandler`. Payload's auth strategies must accept the
|
|
297
|
+
* issued access tokens.
|
|
298
|
+
*/
|
|
299
|
+
oauth?: {
|
|
300
|
+
/**
|
|
301
|
+
* Called on MCP requests with a bearer token that Payload's auth strategies didn't accept.
|
|
302
|
+
* Return the Payload user, including its `collection`, or `null` to answer with the OAuth challenge.
|
|
303
|
+
*/
|
|
304
|
+
authenticate?: (args: {
|
|
305
|
+
req: PayloadRequest
|
|
306
|
+
token: string
|
|
307
|
+
}) => MaybePromise<PayloadRequest['user']>
|
|
308
|
+
/**
|
|
309
|
+
* The authorization servers that issue access tokens for MCP clients. Clients require each one
|
|
310
|
+
* to exactly match the `issuer` in that server's metadata, so pass a function when the exact
|
|
311
|
+
* value is only known at runtime, e.g. from the server's OpenID configuration.
|
|
312
|
+
*/
|
|
313
|
+
authorizationServers: (() => MaybePromise<string[]>) | string[]
|
|
314
|
+
/**
|
|
315
|
+
* The URL clients call to reach the MCP endpoint.
|
|
316
|
+
*
|
|
317
|
+
* @default the absolute URL of `/api/mcp`, based on `serverURL` or the request URL
|
|
318
|
+
*/
|
|
319
|
+
resource?: string
|
|
320
|
+
/**
|
|
321
|
+
* Scopes MCP clients should request, e.g. `['openid', 'email', 'profile']`. Without them,
|
|
322
|
+
* clients request none, which some authorization servers reject or answer with a token that
|
|
323
|
+
* doesn't identify the user.
|
|
324
|
+
*/
|
|
325
|
+
scopes?: string[]
|
|
326
|
+
}
|
|
285
327
|
/**
|
|
286
328
|
* Replace the default MCP authorization resolver.
|
|
287
329
|
*
|
|
@@ -301,11 +343,11 @@ export type MCPPluginConfig = {
|
|
|
301
343
|
|
|
302
344
|
export type SanitizedMCPPluginConfig = {
|
|
303
345
|
items: MCPItem[]
|
|
304
|
-
} & Pick<MCPPluginConfig, 'disabled' | 'hooks' | 'mcp' | 'overrideGetAuthorizedMCP'>
|
|
346
|
+
} & Pick<MCPPluginConfig, 'disabled' | 'hooks' | 'mcp' | 'oauth' | 'overrideGetAuthorizedMCP'>
|
|
305
347
|
|
|
306
348
|
export type MCPServerOptions = {
|
|
307
|
-
options?:
|
|
308
|
-
serverInfo?: Partial<
|
|
349
|
+
options?: McpServerOptions
|
|
350
|
+
serverInfo?: Partial<Implementation>
|
|
309
351
|
}
|
|
310
352
|
|
|
311
353
|
/**
|