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,124 @@
|
||||
import type { H3Event } from 'h3'
|
||||
import { STUDIO2_IMAGE_ENGINES, studio2EngineDefaults } from '~/shared/studio2/engine-defaults.mjs'
|
||||
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' }
|
||||
|
||||
const ENGINE_ENUM = [...STUDIO2_IMAGE_ENGINES]
|
||||
|
||||
export const MCP_TOOLS = [
|
||||
{
|
||||
name: 'aigen_engines',
|
||||
description: 'Recommended steps, cfg, and aspect for flux, krea, qwen21, and moss.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {},
|
||||
additionalProperties: false
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'aigen_generate',
|
||||
description: 'Queue an AIGen still. Returns { id, engine, steps, cfg, aspect, folderId, status }.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
prompt: { type: 'string', description: 'Image prompt (required)' },
|
||||
engine: {
|
||||
type: 'string',
|
||||
enum: ENGINE_ENUM,
|
||||
description: 'Image engine (default flux)'
|
||||
},
|
||||
aspect: { type: 'string', description: 'Aspect ratio (optional; engine default if omitted)' },
|
||||
folderId: { type: 'string', description: 'Library folder id (optional; Inbox/first folder if omitted)' },
|
||||
steps: { type: 'integer', description: 'Sampling steps (optional)' },
|
||||
cfg: { type: 'number', description: 'CFG scale (optional)' },
|
||||
seed: { type: 'number', description: 'Seed (optional; random if omitted)' }
|
||||
},
|
||||
required: ['prompt'],
|
||||
additionalProperties: false
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'aigen_job',
|
||||
description: 'Poll an AIGen job by id. Returns state and stillUrl when done.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string', description: 'Job id from aigen_generate' }
|
||||
},
|
||||
required: ['id'],
|
||||
additionalProperties: false
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
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-Headers', 'Authorization, Content-Type, Accept, MCP-Protocol-Version')
|
||||
setResponseHeader(event, 'Access-Control-Max-Age', '86400')
|
||||
if (origin) setResponseHeader(event, 'Vary', 'Origin')
|
||||
}
|
||||
|
||||
function enginesPayload() {
|
||||
const out: Record<string, { steps: number, cfg: number, aspect: string }> = {}
|
||||
for (const engine of STUDIO2_IMAGE_ENGINES) {
|
||||
out[engine] = studio2EngineDefaults(engine)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function toolTextResult(payload: unknown, isError = false) {
|
||||
return {
|
||||
content: [{ type: 'text', text: JSON.stringify(payload) }],
|
||||
...(isError ? { isError: true } : {})
|
||||
}
|
||||
}
|
||||
|
||||
function errorMessage(err: any) {
|
||||
return String(err?.statusMessage || err?.message || err || 'Tool failed')
|
||||
}
|
||||
|
||||
export async function callMcpTool(event: H3Event, name: string, args: Record<string, unknown>) {
|
||||
if (name === 'aigen_engines') {
|
||||
return toolTextResult(enginesPayload())
|
||||
}
|
||||
if (name === 'aigen_generate') {
|
||||
try {
|
||||
const result = await queueAigenGenerate(event, args)
|
||||
return toolTextResult(result)
|
||||
} catch (err: any) {
|
||||
return toolTextResult({ error: errorMessage(err) }, true)
|
||||
}
|
||||
}
|
||||
if (name === 'aigen_job') {
|
||||
try {
|
||||
const id = String(args.id || '')
|
||||
if (!id) return toolTextResult({ error: 'id is required' }, true)
|
||||
const result = readAigenJob(event, id, { absoluteStillUrl: true })
|
||||
return toolTextResult(result)
|
||||
} catch (err: any) {
|
||||
return toolTextResult({ error: errorMessage(err) }, true)
|
||||
}
|
||||
}
|
||||
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