|
| 1 | +import type { Page } from '@playwright/test' |
| 2 | + |
| 3 | +import { expect, test } from '@playwright/test' |
| 4 | +import { randomUUID } from 'crypto' |
| 5 | +import path from 'path' |
| 6 | +import { fileURLToPath } from 'url' |
| 7 | + |
| 8 | +import { ensureCompilationIsDone, initPageConsoleErrorCatch } from '../__helpers/e2e/helpers.js' |
| 9 | +import { initPayloadE2ENoConfig } from '../__helpers/shared/initPayloadE2ENoConfig.js' |
| 10 | +import { devUser } from '../credentials.js' |
| 11 | +import { TEST_TIMEOUT_LONG } from '../playwright.config.js' |
| 12 | + |
| 13 | +const filename = fileURLToPath(import.meta.url) |
| 14 | +const dirname = path.dirname(filename) |
| 15 | + |
| 16 | +test.describe('MCP Plugin', () => { |
| 17 | + let page: Page |
| 18 | + let serverURL: string |
| 19 | + let apiKey: string |
| 20 | + |
| 21 | + test.beforeAll(async ({ browser, request }, testInfo) => { |
| 22 | + testInfo.setTimeout(TEST_TIMEOUT_LONG) |
| 23 | + |
| 24 | + const { serverURL: serverFromInit } = await initPayloadE2ENoConfig({ dirname }) |
| 25 | + serverURL = serverFromInit |
| 26 | + |
| 27 | + const context = await browser.newContext() |
| 28 | + page = await context.newPage() |
| 29 | + initPageConsoleErrorCatch(page) |
| 30 | + |
| 31 | + await ensureCompilationIsDone({ page, serverURL }) |
| 32 | + |
| 33 | + // Login as dev user to get a JWT token for API key creation |
| 34 | + const loginRes = await request.post(`${serverURL}/api/users/login`, { |
| 35 | + data: { email: devUser.email, password: devUser.password }, |
| 36 | + }) |
| 37 | + expect(loginRes.ok()).toBeTruthy() |
| 38 | + const loginData = await loginRes.json() |
| 39 | + const token = loginData.token |
| 40 | + const userId = loginData.user.id |
| 41 | + |
| 42 | + // Create an API key with permissions to call tools/list |
| 43 | + const createKeyRes = await request.post(`${serverURL}/api/payload-mcp-api-keys`, { |
| 44 | + data: { |
| 45 | + enableAPIKey: true, |
| 46 | + label: 'E2E Test Key', |
| 47 | + apiKey: randomUUID(), |
| 48 | + user: userId, |
| 49 | + posts: { create: true, find: true, update: true, delete: true }, |
| 50 | + products: { find: true }, |
| 51 | + }, |
| 52 | + headers: { |
| 53 | + Authorization: `JWT ${token}`, |
| 54 | + }, |
| 55 | + }) |
| 56 | + expect(createKeyRes.ok()).toBeTruthy() |
| 57 | + const keyData = await createKeyRes.json() |
| 58 | + apiKey = keyData.doc.apiKey |
| 59 | + }) |
| 60 | + |
| 61 | + test('should not poison the Next.js runtime after MCP requests', async ({ request }) => { |
| 62 | + // --- 1. Baseline: verify routes are healthy before any MCP calls --- |
| 63 | + |
| 64 | + const healthBefore = await request.get(`${serverURL}/api/health`) |
| 65 | + expect(healthBefore.status()).toBe(200) |
| 66 | + |
| 67 | + const notFoundBefore = await request.get(`${serverURL}/api/nonexistent-mcp-test-route`) |
| 68 | + expect(notFoundBefore.status()).toBe(404) |
| 69 | + |
| 70 | + // --- 2. Send valid MCP requests --- |
| 71 | + // These trigger @modelcontextprotocol/sdk's StreamableHTTPServerTransport which |
| 72 | + // uses @hono/node-server's getRequestListener, replacing global.Request/Response. |
| 73 | + |
| 74 | + const mcpHeaders = { |
| 75 | + Accept: 'application/json, text/event-stream', |
| 76 | + Authorization: `Bearer ${apiKey}`, |
| 77 | + 'Content-Type': 'application/json', |
| 78 | + } |
| 79 | + |
| 80 | + const initRes = await request.post(`${serverURL}/api/mcp`, { |
| 81 | + data: { |
| 82 | + id: 1, |
| 83 | + jsonrpc: '2.0', |
| 84 | + method: 'initialize', |
| 85 | + params: { |
| 86 | + capabilities: {}, |
| 87 | + clientInfo: { name: 'e2e-test', version: '1.0.0' }, |
| 88 | + protocolVersion: '2024-11-05', |
| 89 | + }, |
| 90 | + }, |
| 91 | + headers: mcpHeaders, |
| 92 | + }) |
| 93 | + expect(initRes.ok()).toBeTruthy() |
| 94 | + |
| 95 | + const toolsRes = await request.post(`${serverURL}/api/mcp`, { |
| 96 | + data: { |
| 97 | + id: 2, |
| 98 | + jsonrpc: '2.0', |
| 99 | + method: 'tools/list', |
| 100 | + params: {}, |
| 101 | + }, |
| 102 | + headers: mcpHeaders, |
| 103 | + }) |
| 104 | + expect(toolsRes.ok()).toBeTruthy() |
| 105 | + |
| 106 | + // --- 3. After MCP calls: routes must still respond correctly --- |
| 107 | + // Regression check for: https://github.com/payloadcms/payload/issues/15856 |
| 108 | + // Without the fix, global.Response is permanently replaced by @hono/node-server, |
| 109 | + // causing Next.js route handler validation to fail with: |
| 110 | + // "Expected a Response object but received 'NextResponse'" |
| 111 | + |
| 112 | + const healthAfter = await request.get(`${serverURL}/api/health`) |
| 113 | + expect(healthAfter.status()).toBe(200) |
| 114 | + |
| 115 | + const notFoundAfter = await request.get(`${serverURL}/api/nonexistent-mcp-test-route`) |
| 116 | + expect(notFoundAfter.status()).toBe(404) |
| 117 | + }) |
| 118 | +}) |
0 commit comments