import { readFile } from 'node:fs/promises'; import { normalizeInlineResult } from './shared/inline-video-result.js'; import { inlineVideoUri, legacyInlineVideoUri } from './shared/inline-video.ts'; import { publicToolName, legacyToolName, publicToolResult } from './shared/public-names.mjs'; import { assetReadToolResult } from './shared/asset-library.ts'; import { createInterface } from 'node:readline'; import type { JSONRPCMessage, Transport } from '@modelcontextprotocol/server'; import { readFileSync, statSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { WorkspaceStore } from './workspace.ts'; import { ensureService, VERSION } from './preview.ts'; import { tools, callTool } from './mcp-tools.ts'; export { tools, callTool } from './mcp-tools.ts'; import { createToolRegistry, releaseProfile } from './shared/mcp-registry.ts'; import { phase1Tools, finalVideoTool, validateInput, normalizeToolInput, phase1TransportTool } from './shared/phase1-tools.ts'; import { workflowResources, readWorkflowResource, workflowEntryGuide } from './shared/workflow.ts'; import { ensureEditor, editorRequest, editorHeaders, sharedEditorEnabled } from './editor-runtime.ts'; import { isMain } from './shared/entry.ts'; import { resolveProviderSetupEntry } from './shared/workspace-url.ts'; import type { Selection } from './editor-runtime.ts'; import { createAppTracking, closeTracking, type OmTracking } from './shared/tracking.ts'; import { extractCodexThreadMeta, flushCodexSessionTracking, isCompletedExportToolResult, recordCodexThreadActivity, syncActiveCodexThreads } from './shared/codex-session-tracking.ts'; import { extractHostSessionMeta, flushHostSessionTracking, recordHostSessionActivity, syncActiveHostSessions } from './shared/host-session-tracking.ts'; import { uuid } from './tracking/core.ts'; import { mcpToolNames } from './tracking/events.ts'; import { gameResultStatusIsLifecycle, gameToolFields, toolFailureFields, toolInputFields, toolResultFields } from './tracking/tool-fields.ts'; import { identifyHost, unknownHost } from './tracking/host.ts'; import type { HostContext } from './tracking/host.ts'; import { templateCatalogFor } from './prism-templates.ts'; import { templateConfig } from './shared/template-config.ts'; import { createMcpClientState, negotiateTemplatePresentation, showTemplateRecommendations, templatePresentationName, templatePresentationUriPrefix } from './shared/mcp-template-recommendations.ts'; import type { McpClientState } from './shared/mcp-template-recommendations.ts'; import { gameToolAnnotations } from './shared/game-tools.ts'; import { hasMcpEnvelope, loadModernMcpSdk, type ModernMcpSdk } from './shared/mcp-modern.ts'; import { resolvePublicOrigin } from './gateway/agent-runtimes.ts'; import { compactRuntimeUpdateNotice, maybeAutoUpdateBeforeCreation, shouldAutoUpdateForToolCall, type RuntimeUpdateNotice, type SourceUpdateApplyResult, type SourceUpdateCheckResult } from './shared/source-update.ts'; export function mcpRegistry(profile = releaseProfile) { return createToolRegistry(profile === 'phase1' ? phase1Tools : tools, profile); } export const releaseRegistry = mcpRegistry(); export const publicTools = releaseRegistry.tools; const selections = new WeakMap(); const trackingByStore = new WeakMap>(); const videoAppClients = new WeakSet(); const connectionHosts = new WeakMap(); const connectionContexts = new WeakMap>(); function trackingFor(store: WorkspaceStore) { if (store.tracking) return Promise.resolve(store.tracking); let tracking = trackingByStore.get(store); if (!tracking) { const created = createAppTracking(store.directory, 'mcp'); tracking = created; trackingByStore.set(store, tracking); // Read paths close interrupted exports too, so they report through the same tracker. void created.then(value => { store.tracking ??= value; }); } return tracking; } const runtimeRoot = fileURLToPath(new URL('./', import.meta.url)); const runtimeDirectory = statSync(runtimeRoot); const serverIcons = (['light', 'dark'] as const).map(theme => ({ src: 'data:image/svg+xml;base64,' + readFileSync(new URL(`./docs/readme/opendesign-symbol${theme === 'dark' ? '-dark' : ''}.svg`, import.meta.url)).toString('base64'), mimeType: 'image/svg+xml', sizes: ['any'], theme, })); export type McpRpcOptions = { transport?: 'stdio' | 'http'; previewOrigin?: string; stateless?: boolean; authorizeToolCall?: (tool: { name: string; annotations?: { readOnlyHint?: boolean } }, args: unknown) => void; callPhase1?: (payload: { name: string; arguments: any; trackingHost: HostContext; trackingContext?: string; channel?: 'mcp_stdio' | 'mcp_http' }) => Promise; autoUpdate?: (context: { repoRoot: string; dataDirectory: string; restartBackend: boolean }) => Promise; }; export async function rpc(message: any, store = new WorkspaceStore(), registry = releaseRegistry, connection?: { selection: Selection }, clientState?: McpClientState, options?: McpRpcOptions) { if (message?.jsonrpc === '2.0' && message.id !== undefined && message.method === 'initialize') { connectionHosts.set(connection ?? store, identifyHost(message.params?.clientInfo, process.env)); } const declared = message?.params?._meta?.['io.modelcontextprotocol/clientInfo']; const host = declared ? identifyHost(declared, process.env) : connectionHosts.get(connection ?? store) ?? unknownHost; let contexts = connectionContexts.get(connection ?? store); if (!contexts) { contexts = new Map(); connectionContexts.set(connection ?? store, contexts); } const hostKey = JSON.stringify(host); if (!contexts.has(hostKey)) { if (contexts.size >= 16) contexts.delete(contexts.keys().next().value!); contexts.set(hostKey, crypto.randomUUID()); } const trackingContext = contexts.get(hostKey)!; const name = legacyToolName(message?.params?.name); const isInternalProbe = message?.params?._meta?.['openmotion/internalProbe'] === true; if (isInternalProbe) { return invokeRpc(message, store, registry, connection, host, trackingContext, clientState, options); } const codexMeta = message?.jsonrpc === '2.0' && message.id !== undefined && (message.method === 'tools/call' || message.method === 'resources/read') ? extractCodexThreadMeta(message.params, host) : null; const hostSessionMeta = !codexMeta && message?.jsonrpc === '2.0' && message.id !== undefined && (message.method === 'tools/call' || message.method === 'resources/read') ? extractHostSessionMeta(message.params, host, process.env) : null; if (message?.jsonrpc !== '2.0' || message.id === undefined || message.method !== 'tools/call' || registry.profile !== 'phase1' || !(mcpToolNames as readonly unknown[]).includes(name)) { const response = await invokeRpc(message, store, registry, connection, host, trackingContext, clientState, options); if ((codexMeta || hostSessionMeta) && !(response as any)?.error) { const resultValue = (response as any)?.result; const content = resultValue?.structuredContent; const activityOpts = { projectId: content?.projectId ?? message.params?.arguments?.projectId, resourceUri: message.method === 'resources/read' ? message.params?.uri : undefined, exportCompleted: message.method === 'tools/call' && isCompletedExportToolResult(name, message.params?.arguments, content, resultValue?.isError), }; if (codexMeta) void recordCodexThreadActivity(store.directory, codexMeta, activityOpts).catch(() => {}); if (hostSessionMeta) void recordHostSessionActivity(store.directory, hostSessionMeta, activityOpts).catch(() => {}); } return response; } const tracking = await trackingFor(store), started = performance.now(), callId = crypto.randomUUID(); const publicName = publicToolName(message.params.name); const definition = phase1Tools.find(tool => tool.name === publicName) ?? (publicName === templatePresentationName ? clientState?.presentation?.tool : videoAppClients.has(clientState ?? connection ?? store) ? finalVideoTool : undefined); const rawArgs = message.params.arguments; const args = definition ? normalizeToolInput(definition.inputSchema, rawArgs ?? {}) : rawArgs; let accepted = false; if (definition) { try { validateInput(definition.inputSchema, args ?? {}); accepted = true; } catch { /* The invocation reports the validation failure. */ } } const fields = { tool_name: name as typeof mcpToolNames[number], call_id: callId, accepted, ...toolInputFields(publicName, args), ...gameToolFields(name, args), ...(typeof args?.requestId === 'string' && uuid.test(args.requestId) ? { operation_id: args.requestId } : {}) }; if (accepted) tracking.track('mcp_tool_started', fields, undefined, undefined, host); const response = await invokeRpc(message, store, registry, connection, host, trackingContext, clientState, options); const value = (response as any)?.result; const content = value?.structuredContent; if (codexMeta || hostSessionMeta) { const activityOpts = { projectId: content?.projectId ?? args?.projectId, exportCompleted: isCompletedExportToolResult(name, args, content, value?.isError), }; if (codexMeta) void recordCodexThreadActivity(store.directory, codexMeta, activityOpts).catch(() => {}); if (hostSessionMeta) void recordHostSessionActivity(store.directory, hostSessionMeta, activityOpts).catch(() => {}); } if (name === 'motion_open_preview' && value && !value.isError && message.params?.arguments?.target === 'providers') { const entry_from = resolveProviderSetupEntry(message.params.arguments.entry); const studio_id = entry_from === 'agent_game_publish_signed_out' ? 'game' : entry_from === 'agent_design_deploy_signed_out' ? 'design' : undefined; tracking.track('provider_setup_opened', { entry_from, presentation: message.params.arguments.presentation ?? 'foreground', embedded: message.params.arguments.embedded !== false, ...(studio_id ? { studio_id } : {}), }, undefined, undefined, host); } // Any discovery or status route may carry the internal link result: // strip it before the agent sees it and record the link generation once for both stdio and HTTP MCP. const mcpChannel = options?.transport === 'http' ? 'mcp_http' : 'mcp_stdio'; if (value && !value.isError && content?._upgradeLinkResult) { const { _upgradeLinkResult, ...visible } = content; tracking.track('upgrade_link_result', { ..._upgradeLinkResult, channel: _upgradeLinkResult.channel ?? mcpChannel, }, undefined, undefined, host); value.structuredContent = visible; value.content = [{ type: 'text', text: JSON.stringify(visible) }]; } const lifecycleStatus = gameResultStatusIsLifecycle(name); const outcome = value?.isError || !lifecycleStatus && content?.status === 'failed' ? 'failure' : !lifecycleStatus && content?.status === 'partial' ? 'partial' : value ? 'success' : 'unknown'; const importRejected = (outcome === 'partial' || outcome === 'failure') && (name === 'motion_import_assets' || name === 'motion_assets' && fields.action === 'import_to_project'); tracking.track('mcp_tool_finished', { ...fields, ...toolResultFields(publicName, args, content), ...(value?.isError ? {} : gameToolFields(name, args, content)), outcome, duration_ms: Math.min(86_400_000, Math.round(performance.now() - started)), ...(value?.isError ? toolFailureFields(content?.error) : importRejected ? { error_code: 'ASSET_IMPORT_FAILED' as const } : {}), ...(typeof content?.replayed === 'boolean' ? { replayed: content.replayed } : {}) }, undefined, undefined, host); return response; } const baseServerInfo = { name: 'openmotion', title: 'OpenMotion', version: VERSION }; const appCapability = 'io.modelcontextprotocol/ui'; const appMimeType = 'text/html;profile=mcp-app'; function configureClient(client: object, profile: string, capabilities: any, state?: McpClientState) { negotiateTemplatePresentation(state, profile, capabilities); videoAppClients.delete(client); const mimeTypes = capabilities?.extensions?.[appCapability]?.mimeTypes; if (profile === 'phase1' && Array.isArray(mimeTypes) && mimeTypes.includes(appMimeType)) videoAppClients.add(client); } export function modernMcpServer(sdk: ModernMcpSdk, store: WorkspaceStore, options: McpRpcOptions = {}) { const discovery = { serverInfo: { ...baseServerInfo, icons: serverIcons }, capabilities: { tools: {}, ...(releaseRegistry.profile === 'phase1' ? { resources: {} } : {}) }, ...(releaseRegistry.instructions ? { instructions: releaseRegistry.instructions } : {}), }; return sdk.createModernMcpServer(discovery, (message, capabilities) => { const clientState = createMcpClientState(); const needsPresentation = message.method === 'tools/list' || message.method === 'resources/list' || (message.method === 'tools/call' && publicToolName(message.params?.name) === templatePresentationName) || (message.method === 'resources/read' && typeof message.params?.uri === 'string' && message.params.uri.startsWith(templatePresentationUriPrefix)); configureClient(clientState, releaseRegistry.profile, capabilities, needsPresentation ? clientState : undefined); return rpc(message, store, releaseRegistry, { selection: {} }, clientState, { ...options, stateless: true }); }); } function withPlayableUrl(value: any, origin: string | undefined) { if (!origin || !value || typeof value !== 'object') return value; const absolute = (url: unknown) => typeof url === 'string' && /^\/(?:motion-)?api\/games\//.test(url) ? new URL(url, origin).href : url; const fields = (game: any) => ({ ...(typeof game.playUrl === 'string' ? { playUrl: absolute(game.playUrl) } : {}), ...(typeof game.roomInviteUrlPattern === 'string' ? { roomInviteUrlPattern: absolute(game.roomInviteUrlPattern) } : {}), }); return { ...value, ...fields(value), ...(value.game && typeof value.game === 'object' ? { game: { ...value.game, ...fields(value.game) } } : {}) }; } async function invokeRpc(message: any, store: WorkspaceStore, registry: ReturnType, connection?: { selection: Selection }, host: HostContext = unknownHost, trackingContext?: string, clientState?: McpClientState, options?: McpRpcOptions) { if (!message || message.jsonrpc !== '2.0' || typeof message.method !== 'string') return { jsonrpc: '2.0', id: message?.id ?? null, error: { code: -32600, message: 'Invalid request' } }; if (message.id === undefined) return null; const result = (value: unknown) => ({ jsonrpc: '2.0', id: message.id, result: value }); if (message.method === 'initialize') { configureClient(clientState ?? connection ?? store, registry.profile, message.params?.capabilities, clientState); const protocolVersion = ['2024-11-05', '2025-03-26', '2025-06-18', '2025-11-25'].includes(message.params?.protocolVersion) ? message.params.protocolVersion : '2025-11-25'; return result({ protocolVersion, capabilities: { tools: {}, ...(registry.profile === 'phase1' ? { resources: {} } : {}) }, serverInfo: { ...baseServerInfo, ...(protocolVersion === '2025-11-25' ? { icons: serverIcons } : {}) }, instructions: registry.instructions ?? workflowEntryGuide + ' Load only references needed by the current task and reuse already loaded documentId/contentHash. Reuse saved decisions for small edits. OpenMotion wraps Hyperframes Studio and its renderer. Only for tasks that require working in the editor or authoring a project, call openmotion_open_preview, read its pinned authoringGuide and open previewUrl with the host built-in browser before reporting it opened. Author native HTML compositions with seekable GSAP timelines, never custom shot JSON or canvas recording. Read openmotion_get_project before source edits and pass its revision as expectedRevision; inspect lint/timeline afterwards. Only when the user scope includes export/final delivery, query any applicable existing export to avoid duplicate jobs and export the inspected revision in the background, wait for completed, then call openmotion_verify_export with that job ID/revision and the brief audio policy. Separately inspect final visuals, sound and actual player playback before claiming full verification. MCP previewUrl defaults to embed=1; preserve its query and hash. Use sessionUrl or embedded=false only for the full OpenMotion workspace. Do not restart the host Agent or ask users to start a terminal server.' }); } const effectiveRegistry = registry.profile === 'phase1' ? createToolRegistry(registry.tools, registry.profile, [ ...(clientState?.presentation ? [clientState.presentation.tool] : []), ...(videoAppClients.has(clientState ?? connection ?? store) ? [finalVideoTool] : []), ]) : registry; if (message.method === 'ping') return result({}); if (registry.profile === 'phase1' && message.method === 'resources/list') return result({ resources: [...workflowResources(), { uri: inlineVideoUri, name: 'Final video playback', mimeType: 'text/html;profile=mcp-app' }, ...(clientState?.presentation ? [clientState.presentation.descriptor] : [])] }); if (registry.profile === 'phase1' && message.method === 'resources/read') { if (message.params?.uri === inlineVideoUri || message.params?.uri === legacyInlineVideoUri) { try { return result({ contents: [{ uri: message.params.uri, mimeType: 'text/html;profile=mcp-app', text: await readFile(new URL('./web/inline-video.html', import.meta.url), 'utf8'), _meta: { ui: { prefersBorder: true, csp: { connectDomains: [], resourceDomains: ['data:', 'blob:'] } }, 'openai/ui': { availableDisplayModes: ['inline'], preferredDisplayMode: 'inline' } } }] }); } catch { return { jsonrpc: '2.0', id: message.id, error: { code: -32603, message: 'INLINE_UI_NOT_BUILT: run npm run build:inline' } }; } } try { const uri = message.params?.uri; if (typeof uri === 'string' && uri.startsWith(templatePresentationUriPrefix)) { if (!clientState?.presentation || uri !== clientState.presentation.uri) throw new Error('Unknown or unnegotiated UI resource'); return result({ contents: [clientState.presentation.content] }); } return result({ contents: [await readWorkflowResource(uri, store.directory)] }); } catch (error) { return { jsonrpc: '2.0', id: message.id, error: { code: -32602, message: (error as Error).message } }; } } if (message.method === 'tools/list') return result({ tools: effectiveRegistry.tools }); if (message.method === 'tools/call') { try { if (registry.profile !== 'phase1') { const current = statSync(runtimeRoot, { throwIfNoEntry: false }); if (!current || current.dev !== runtimeDirectory.dev || current.ino !== runtimeDirectory.ino) throw Object.assign( new Error('The OpenMotion plugin directory was replaced or removed. Reload this internal legacy MCP connection before retrying; no project operation was applied.'), { code: 'MCP_RUNTIME_RELOAD_REQUIRED' }, ); } const name = publicToolName(message.params?.name); const exposed = effectiveRegistry.tools.find(tool => tool.name === name); if (!exposed) throw new Error('MCP_TOOL_NOT_EXPOSED: ' + String(message.params?.name)); const actionAnnotations = gameToolAnnotations(name, message.params?.arguments); options?.authorizeToolCall?.(actionAnnotations ? { ...exposed, annotations: actionAnnotations } : exposed, message.params?.arguments ?? {}); if (name === templatePresentationName && clientState?.presentation) { validateInput(exposed.inputSchema, message.params.arguments ?? {}); const value = showTemplateRecommendations(clientState.presentation, message.params.arguments); return result({ content: [{ type: 'text', text: value.onboarding ? 'The native onboarding component includes the complete welcome and three template cards. Do not append a duplicate welcome, verification status or an acknowledgment after this successful result. End the onboarding turn without an additional text reply and wait for the user. No project has been created.' : 'Preview these templates, then choose a card or reply with its number. No project has been created.' }], structuredContent: value, // Bind each result to its actual immutable resource and isolate it // from an older mounted recommendation retained by the host. _meta: { ui: { resourceUri: clientState.presentation.uri }, 'openai/widgetSessionId': value.recommendationId } }); } if (registry.profile === 'phase1') { const internalName = legacyToolName(name); const finalVideo = name === finalVideoTool.name; const definition = finalVideo ? finalVideoTool : phase1Tools.find(tool => tool.name === publicToolName(name))!; const rawInput = message.params.arguments ?? {}; const input = normalizeToolInput(definition.inputSchema, rawInput); validateInput(definition.inputSchema, input); const args = input && typeof input === 'object' && !Array.isArray(input) ? { ...input } : input; if (!options?.stateless && internalName === 'motion_open_preview' && args.target !== 'explore' && args.target !== 'templates' && args.target !== 'providers' && !args.projectId && !args.sessionId && args.action !== 'record_review') args.sessionId = selections.get(store)?.sessionId; if (args.sessionId === undefined) delete args.sessionId; if (['repository', 'hosted'].includes(templateConfig().source) && ['motion_list_templates', 'motion_get_template', 'motion_list_skills', 'motion_get_skill'].includes(internalName)) { const catalog = templateCatalogFor(store.directory); const value = internalName === 'motion_list_templates' ? await catalog.index() : internalName === 'motion_get_template' ? await catalog.detail(args.templateId) : internalName === 'motion_list_skills' ? await catalog.skillIndex() : await catalog.skillDetail(args.skillId); return result({ content: [{ type: 'text', text: JSON.stringify(value) }], structuredContent: value }); } const transportName = finalVideo ? 'openmotion_get_project' : name; const transportArgs = finalVideo ? { projectId: args.projectId, workId: args.workId, includeVideo: true } : args; let runtimeUpdate: RuntimeUpdateNotice | undefined; if (shouldAutoUpdateForToolCall(transportName, transportArgs)) { const restartBackend = !options?.callPhase1 && options?.transport !== 'http'; const updateRunner = options?.autoUpdate ?? (!options?.callPhase1 || options?.transport === 'http' ? maybeAutoUpdateBeforeCreation : undefined); if (updateRunner) { const updateOutcome = await updateRunner({ repoRoot: runtimeRoot, dataDirectory: store.directory, restartBackend }).catch(() => undefined); runtimeUpdate = compactRuntimeUpdateNotice(updateOutcome); if (runtimeUpdate && options?.transport === 'http' && !runtimeUpdate.restarted) { runtimeUpdate = { ...runtimeUpdate, restartDeferred: true }; } } } const mcpChannel: 'mcp_stdio' | 'mcp_http' = options?.transport === 'http' ? 'mcp_http' : 'mcp_stdio'; let raw: any; let responseOk = true; let previewOrigin = options?.previewOrigin; if (options?.callPhase1) { raw = await options.callPhase1({ name: transportName, arguments: transportArgs, trackingHost: host, trackingContext, channel: mcpChannel }); } else { const service = await ensureService(store, undefined, transportName), origin = new URL(service.previewUrl).origin; previewOrigin ??= resolvePublicOrigin(origin); const response = await fetch(origin + '/motion-api/phase1/call', { method: 'POST', headers: await editorHeaders(store, origin), body: JSON.stringify({ name: phase1TransportTool(service, transportName) ?? transportName, arguments: transportArgs, trackingHost: host, trackingContext, channel: mcpChannel }), signal: AbortSignal.timeout(600_000) }); raw = await response.json(); responseOk = response.ok; } const normalized = responseOk && transportName === 'openmotion_get_project' && transportArgs.workId ? normalizeInlineResult(raw) : undefined; const resultValue = publicToolResult(normalized?.structuredContent ?? raw); const value = responseOk && transportName.startsWith('openmotion_game_') ? withPlayableUrl(resultValue, previewOrigin) : resultValue; if (!responseOk) throw Object.assign(new Error(value.error || 'Tool failed'), { code: value.code, details: value.runtime, invalid_field: value.invalid_field, validation_rule: value.validation_rule, ...(value.settingsHandoff ? { settingsHandoff: value.settingsHandoff } : {}), ...(value.applied === false ? { applied: false } : {}), }); if (finalVideo && (!value.inlineWork?.playable || value.inlineWork.state !== 'completed' || !normalized?._meta?.inlineVideo?.video)) throw new Error('FINAL_VIDEO_NOT_READY: Display only after the MP4 export completes and passes verification'); if (!options?.stateless && value.projectId && value.sessionId) selections.set(store, { projectId: value.projectId, sessionId: value.sessionId }); if ((internalName === 'motion_assets' || internalName === 'motion_asset_manage') && args.action === 'read') return result(assetReadToolResult(value)); const { _inlineMedia: legacyMedia, ...visible } = value; if (finalVideo) { // Agent execution envelopes can truncate private data URLs. Keep presentation small; // the App retrieves the verified bytes through its direct server-tool connection. const presentation = { ...visible, inlineVideoTransport: 'server-tool' }; return result({ content: [{ type: 'text', text: JSON.stringify(presentation) }], structuredContent: presentation, _meta: { ui: { resourceUri: inlineVideoUri } } }); } const output = runtimeUpdate ? { ...visible, runtimeUpdate } : visible; const _inlineMedia = normalized?._meta?.inlineVideo ?? legacyMedia; return result({ content: [{ type: 'text', text: JSON.stringify(output) }], structuredContent: output, ...(_inlineMedia ? { _meta: { inlineVideo: _inlineMedia } } : {}) }); } let value; if (connection && !['motion_get_workflow', 'motion_list_templates', 'motion_get_template'].includes(legacyToolName(name))) { const editor = await ensureEditor(store.directory); const reply = await editorRequest(editor, '/editor-api/tools/call', { name: legacyToolName(name), arguments: message.params?.arguments || {}, selection: connection.selection }); connection.selection = reply.selection; value = reply.result; } else value = await callTool(name, message.params?.arguments || {}, store, await trackingFor(store)); return result({ content: [{ type: 'text', text: JSON.stringify(value) }], structuredContent: value }); } catch (error) { if (typeof (error as any)?.jsonRpcCode === 'number') { return { jsonrpc: '2.0', id: message.id, error: { code: (error as any).jsonRpcCode, message: error instanceof Error ? error.message : 'Forbidden', }, }; } const errorMessage = error instanceof Error ? error.message : 'Tool failed'; const code = (error as any).code || /^([A-Z][A-Z_]+):?/.exec(errorMessage)?.[1] || 'TOOL_FAILED'; const { invalid_field, validation_rule } = toolFailureFields(error); const runtimeFailure = ['MCP_RUNTIME_RELOAD_REQUIRED', 'EDITOR_RUNTIME_INCOMPATIBLE', 'NATIVE_RUNTIME_NOT_READY'].includes(code); const settingsHandoff = (error as { settingsHandoff?: unknown })?.settingsHandoff; return result({ isError: true, content: [{ type: 'text', text: errorMessage }], structuredContent: { ...(settingsHandoff ? { settingsHandoff } : {}), error: { code, message: errorMessage, ...(invalid_field ? { invalid_field, validation_rule } : {}), ...(settingsHandoff ? { settingsHandoff } : {}), ...(runtimeFailure && (error as any).details ? { details: (error as any).details } : {}), applied: (error as any).applied === false || runtimeFailure || /^(OPENDESIGN_SIGN_IN_REQUIRED|NATIVE_RUNTIME_NOT_READY|INVALID_|VERSION_CONFLICT|VERSION_NOT_RESTORABLE|VERSION_NOT_FOUND|PROJECT_NOT_|PROJECT_SESSION_|PROJECT_TRASHED|PROJECT_VALIDATION_FAILED|REQUEST_ID_CONFLICT|SESSION_NOT_FOUND|MCP_TOOL_NOT_EXPOSED|EXPORT_NOT_|TEMPLATE_NOT_|WORK_CLOSED|WORK_VERSION_MISMATCH|WORK_GENERATION_UNTRACKED|WORK_ID_CONFLICT|WORK_NOT_FOUND|GENERATION_CONTENT_UNCHANGED|GENERATION_ARTIFACT_INVALID|DUPLICATE_ITEM_ID|ITEM_ID_CONFLICT|DUPLICATE_FILE_PATH|CONFLICTING_FILE_PATHS|SOURCE_BATCH_TOO_LARGE|REVIEW_VERSION_MISMATCH|REVIEW_OBJECT_MISMATCH|PREVIEW_TARGET_MISMATCH)/.test(code) ? false : 'unknown' } } }); } } return { jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Method not found' } }; } if (isMain(import.meta.url)) { const store = new WorkspaceStore(); if ((await trackingFor(store)).enabled()) process.stderr.write('OpenMotion collects redacted usage information to improve product and generation quality. Disable in Studio Privacy & data settings or set OPENMOTION_TELEMETRY=0.\n'); void syncActiveCodexThreads(store.directory).catch(() => {}); void syncActiveHostSessions(store.directory).catch(() => {}); const connection = sharedEditorEnabled ? { selection: {} } : undefined; const clientState = createMcpClientState(); const modernTransport: Transport = { async start() {}, async close() {}, async send(response) { process.stdout.write(JSON.stringify(response) + '\n'); }, }; let modern: ReturnType | undefined; const lines = createInterface({ input: process.stdin, crlfDelay: Infinity }); for await (const line of lines) { if (!line.trim()) continue; let response; try { const message: JSONRPCMessage = JSON.parse(line); if (hasMcpEnvelope(message)) { if (!modern) { const sdk = await loadModernMcpSdk(); modern = sdk.serveStdio(() => modernMcpServer(sdk, store), { transport: modernTransport, legacy: 'reject', maxSubscriptions: 0, }); } modernTransport.onmessage?.(message); continue; } response = await rpc(message, store, releaseRegistry, connection, clientState); } catch { response = { jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error' } }; } if (response) process.stdout.write(JSON.stringify(response) + '\n'); } await modern?.close(); await Promise.allSettled([ closeTracking(await trackingByStore.get(store)), flushCodexSessionTracking(store.directory), flushHostSessionTracking(store.directory), ]); }