Add hosted MCP endpoint at POST /mcp for remote agents.
Bearer-auth JSON-RPC exposes engines, generate, and job tools without duplicating the aigen API path. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { assertAigenApiAccess } from '~/server/utils/aigenApi'
|
||||
import {
|
||||
MCP_PROTOCOL_VERSION,
|
||||
MCP_SERVER_INFO,
|
||||
MCP_TOOLS,
|
||||
applyMcpCors,
|
||||
callMcpTool,
|
||||
jsonRpcError,
|
||||
jsonRpcResult
|
||||
} 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') {
|
||||
setResponseHeader(event, 'Allow', 'POST')
|
||||
throw createError({ statusCode: 405, statusMessage: 'Method Not Allowed' })
|
||||
}
|
||||
|
||||
if (method !== 'POST') {
|
||||
setResponseHeader(event, 'Allow', 'POST')
|
||||
throw createError({ statusCode: 405, statusMessage: 'Method Not Allowed' })
|
||||
}
|
||||
|
||||
setResponseHeader(event, 'Content-Type', 'application/json')
|
||||
assertAigenApiAccess(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
|
||||
|
||||
if (rpcMethod === 'notifications/initialized') {
|
||||
setResponseStatus(event, 202)
|
||||
return null
|
||||
}
|
||||
|
||||
if (rpcMethod === 'initialize') {
|
||||
return jsonRpcResult(id, {
|
||||
protocolVersion: MCP_PROTOCOL_VERSION,
|
||||
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 params = body.params && typeof body.params === 'object' ? body.params as Record<string, unknown> : {}
|
||||
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)'}`)
|
||||
})
|
||||
Reference in New Issue
Block a user