From e83ef1c6c2006cc2312412ee48f72119696d6e4c Mon Sep 17 00:00:00 2001 From: Towsty Date: Mon, 2 Nov 2026 22:46:16 -0600 Subject: [PATCH] Allow MCP handshake methods without Authorization. Claude probes initialize/tools/list before sending the bearer; only tools/call stays 401-protected. Co-authored-by: Cursor --- server/routes/mcp.ts | 10 +++++----- tests/aigen-api.test.mjs | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/server/routes/mcp.ts b/server/routes/mcp.ts index a7ce2be..b289123 100644 --- a/server/routes/mcp.ts +++ b/server/routes/mcp.ts @@ -37,11 +37,6 @@ export default defineEventHandler(async (event) => { setResponseHeader(event, 'Content-Type', 'application/json') - if (!mcpBearerOk(event)) { - return mcpUnauthorized(event) - } - mcpAssertAccess(event) - let body: any try { const raw = await readRawBody(event, 'utf8') @@ -62,6 +57,7 @@ export default defineEventHandler(async (event) => { const isNotification = id === undefined const params = body.params && typeof body.params === 'object' ? body.params as Record : {} + // Handshake methods must succeed with no Authorization (Claude probes before sending the saved header). if (rpcMethod === 'notifications/initialized') { setResponseStatus(event, 202) return null @@ -84,6 +80,10 @@ export default defineEventHandler(async (event) => { } if (rpcMethod === 'tools/call') { + if (!mcpBearerOk(event)) { + return mcpUnauthorized(event, id ?? null) + } + mcpAssertAccess(event) const name = String(params.name || '') const args = params.arguments && typeof params.arguments === 'object' && !Array.isArray(params.arguments) ? params.arguments as Record diff --git a/tests/aigen-api.test.mjs b/tests/aigen-api.test.mjs index ef3c3d3..92d8192 100644 --- a/tests/aigen-api.test.mjs +++ b/tests/aigen-api.test.mjs @@ -84,6 +84,7 @@ test('aigen routes exist, stay on the studio-2 enqueue path, and 404 on xAIGen', assert.match(mcp, /tools\/list/) assert.match(mcp, /tools\/call/) assert.match(mcp, /resolveMcpProtocolVersion/) + assert.match(mcp, /tools\/call[\s\S]*mcpBearerOk|mcpBearerOk[\s\S]*tools\/call/) assert.doesNotMatch(mcp, /405.*GET|Method Not Allowed[\s\S]*GET/) assert.match(mcpUtil, /aigen_engines/) assert.match(mcpUtil, /aigen_generate/)