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 <cursoragent@cursor.com>
This commit is contained in:
@@ -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<string, unknown> : {}
|
||||
|
||||
// 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<string, unknown>
|
||||
|
||||
@@ -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/)
|
||||
|
||||
Reference in New Issue
Block a user