GET holds a short SSE stream, auth failures return JSON-RPC 401, and OAuth discovery advertises bearer-only. Co-authored-by: Cursor <cursoragent@cursor.com>
104 lines
2.8 KiB
TypeScript
104 lines
2.8 KiB
TypeScript
import {
|
|
MCP_SERVER_INFO,
|
|
MCP_TOOLS,
|
|
applyMcpCors,
|
|
callMcpTool,
|
|
jsonRpcError,
|
|
jsonRpcResult,
|
|
mcpAssertAccess,
|
|
mcpBearerOk,
|
|
mcpUnauthorized,
|
|
resolveMcpProtocolVersion
|
|
} from '~/server/utils/aigenMcp'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
applyMcpCors(event)
|
|
const method = String(getMethod(event) || 'GET').toUpperCase()
|
|
|
|
if (method === 'OPTIONS') {
|
|
setResponseStatus(event, 204)
|
|
return null
|
|
}
|
|
|
|
if (method === 'GET') {
|
|
if (!mcpBearerOk(event)) {
|
|
return mcpUnauthorized(event)
|
|
}
|
|
setResponseStatus(event, 200)
|
|
setResponseHeader(event, 'Content-Type', 'text/event-stream')
|
|
setResponseHeader(event, 'Cache-Control', 'no-cache, no-transform')
|
|
setResponseHeader(event, 'Connection', 'keep-alive')
|
|
// Short SSE stream Claude opens after initialize; comment then end.
|
|
return ': connected\n\n'
|
|
}
|
|
|
|
if (method !== 'POST') {
|
|
setResponseHeader(event, 'Allow', 'GET, POST, OPTIONS')
|
|
throw createError({ statusCode: 405, statusMessage: 'Method Not Allowed' })
|
|
}
|
|
|
|
setResponseHeader(event, 'Content-Type', 'application/json')
|
|
|
|
if (!mcpBearerOk(event)) {
|
|
return mcpUnauthorized(event)
|
|
}
|
|
mcpAssertAccess(event)
|
|
|
|
let body: any
|
|
try {
|
|
const raw = await readRawBody(event, 'utf8')
|
|
if (raw == null || String(raw).trim() === '') {
|
|
return jsonRpcError(null, -32700, 'Parse error')
|
|
}
|
|
body = JSON.parse(String(raw))
|
|
} catch {
|
|
return jsonRpcError(null, -32700, 'Parse error')
|
|
}
|
|
|
|
if (body == null || typeof body !== 'object' || Array.isArray(body)) {
|
|
return jsonRpcError(null, -32700, 'Parse error')
|
|
}
|
|
|
|
const rpcMethod = String(body.method || '')
|
|
const id = Object.prototype.hasOwnProperty.call(body, 'id') ? body.id : undefined
|
|
const isNotification = id === undefined
|
|
const params = body.params && typeof body.params === 'object' ? body.params as Record<string, unknown> : {}
|
|
|
|
if (rpcMethod === 'notifications/initialized') {
|
|
setResponseStatus(event, 202)
|
|
return null
|
|
}
|
|
|
|
if (rpcMethod === 'initialize') {
|
|
return jsonRpcResult(id, {
|
|
protocolVersion: resolveMcpProtocolVersion(params.protocolVersion),
|
|
capabilities: { tools: {} },
|
|
serverInfo: MCP_SERVER_INFO
|
|
})
|
|
}
|
|
|
|
if (rpcMethod === 'ping') {
|
|
return jsonRpcResult(id, {})
|
|
}
|
|
|
|
if (rpcMethod === 'tools/list') {
|
|
return jsonRpcResult(id, { tools: MCP_TOOLS })
|
|
}
|
|
|
|
if (rpcMethod === 'tools/call') {
|
|
const name = String(params.name || '')
|
|
const args = params.arguments && typeof params.arguments === 'object' && !Array.isArray(params.arguments)
|
|
? params.arguments as Record<string, unknown>
|
|
: {}
|
|
const result = await callMcpTool(event, name, args)
|
|
return jsonRpcResult(id, result)
|
|
}
|
|
|
|
if (isNotification) {
|
|
setResponseStatus(event, 202)
|
|
return null
|
|
}
|
|
|
|
return jsonRpcError(id, -32601, `Method not found: ${rpcMethod || '(missing)'}`)
|
|
})
|