Fix Claude Desktop MCP handshake on /mcp.
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>
This commit is contained in:
+51
-17
@@ -1,10 +1,14 @@
|
||||
import type { H3Event } from 'h3'
|
||||
import { STUDIO2_IMAGE_ENGINES, studio2EngineDefaults } from '~/shared/studio2/engine-defaults.mjs'
|
||||
import { aigenBearerAuthorized } from '~/server/utils/aigenApiAuth'
|
||||
import { assertAigenApiAccess } from '~/server/utils/aigenApi'
|
||||
import { queueAigenGenerate } from '~/server/utils/aigenGenerate'
|
||||
import { readAigenJob } from '~/server/utils/aigenJob'
|
||||
|
||||
export const MCP_PROTOCOL_VERSION = '2025-03-26'
|
||||
export const MCP_SERVER_INFO = { name: 'aigen', version: '1.0.0' }
|
||||
export const MCP_SUPPORTED_PROTOCOL_VERSIONS = ['2025-03-26', '2025-06-18', '2025-11-25'] as const
|
||||
export const MCP_RESOURCE = 'https://aigen.carrgarage.com/mcp'
|
||||
|
||||
const ENGINE_ENUM = [...STUDIO2_IMAGE_ENGINES]
|
||||
|
||||
@@ -57,12 +61,58 @@ export const MCP_TOOLS = [
|
||||
export function applyMcpCors(event: H3Event) {
|
||||
const origin = String(getHeader(event, 'origin') || '').trim()
|
||||
setResponseHeader(event, 'Access-Control-Allow-Origin', origin || '*')
|
||||
setResponseHeader(event, 'Access-Control-Allow-Methods', 'POST, OPTIONS')
|
||||
setResponseHeader(event, 'Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
|
||||
setResponseHeader(event, 'Access-Control-Allow-Headers', 'Authorization, Content-Type, Accept, MCP-Protocol-Version')
|
||||
setResponseHeader(event, 'Access-Control-Expose-Headers', 'Mcp-Session-Id')
|
||||
setResponseHeader(event, 'Access-Control-Max-Age', '86400')
|
||||
if (origin) setResponseHeader(event, 'Vary', 'Origin')
|
||||
}
|
||||
|
||||
export function mcpOauthProtectedResource() {
|
||||
return {
|
||||
resource: MCP_RESOURCE,
|
||||
authorization_servers: [] as string[]
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveMcpProtocolVersion(requested: unknown) {
|
||||
const version = String(requested || '').trim()
|
||||
if ((MCP_SUPPORTED_PROTOCOL_VERSIONS as readonly string[]).includes(version)) return version
|
||||
return MCP_PROTOCOL_VERSION
|
||||
}
|
||||
|
||||
export function jsonRpcError(id: unknown, code: number, message: string) {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
id: id ?? null,
|
||||
error: { code, message }
|
||||
}
|
||||
}
|
||||
|
||||
export function jsonRpcResult(id: unknown, result: unknown) {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
id: id ?? null,
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
/** 401 with JSON-RPC body — never a Nuxt error page. */
|
||||
export function mcpUnauthorized(event: H3Event, id: unknown = null) {
|
||||
applyMcpCors(event)
|
||||
setResponseStatus(event, 401)
|
||||
setResponseHeader(event, 'Content-Type', 'application/json')
|
||||
return jsonRpcError(id, -32001, 'Unauthorized')
|
||||
}
|
||||
|
||||
export function mcpBearerOk(event: H3Event) {
|
||||
return aigenBearerAuthorized(event)
|
||||
}
|
||||
|
||||
export function mcpAssertAccess(event: H3Event) {
|
||||
assertAigenApiAccess(event)
|
||||
}
|
||||
|
||||
function enginesPayload() {
|
||||
const out: Record<string, { steps: number, cfg: number, aspect: string }> = {}
|
||||
for (const engine of STUDIO2_IMAGE_ENGINES) {
|
||||
@@ -106,19 +156,3 @@ export async function callMcpTool(event: H3Event, name: string, args: Record<str
|
||||
}
|
||||
return toolTextResult({ error: `Unknown tool: ${name}` }, true)
|
||||
}
|
||||
|
||||
export function jsonRpcError(id: unknown, code: number, message: string) {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
id: id ?? null,
|
||||
error: { code, message }
|
||||
}
|
||||
}
|
||||
|
||||
export function jsonRpcResult(id: unknown, result: unknown) {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
id: id ?? null,
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user