import { parseModuleCliOptions } from '@open-mercato/shared/lib/cli/parseModuleCliOptions' import type { ModuleCli } from '@open-mercato/shared/modules/registry' import { createRequestContainer } from '@open-mercato/shared/lib/di/container' import { resetServerLoggerCache } from '@open-mercato/shared/lib/logger' import { installNextSubpathResolveHook } from './lib/next-subpath-resolve-hook' import { resolveMcpHost } from './lib/mcp-host-resolution' /** * Ensure app bootstrap is called before creating DI container. * Uses the shared generated-bootstrap loader so the command works both from * the monorepo app and from standalone apps installed through npm packages. */ async function ensureBootstrap(): Promise { // These commands run outside Next.js, but the API route modules the // operation runner imports on a tool call say `from 'next/server'`, which // plain Node cannot resolve (#6118). Install the retry-with-`.js` hook before // anything below can pull a route module in. installNextSubpathResolveHook() // First check if DI is already available try { const { getDiRegistrars } = await import('@open-mercato/shared/lib/di/container') getDiRegistrars() return // DI already available } catch { // DI not available, need to bootstrap } try { const { bootstrapFromAppRoot } = await import('@open-mercato/shared/lib/bootstrap/dynamicLoader') await bootstrapFromAppRoot() } catch (error) { console.error('[MCP] Bootstrap failed:', error instanceof Error ? error.message : error) // Continue - some contexts may not have bootstrap available } } function parseArgs(rest: string[]): Record { return parseModuleCliOptions(rest) } type CliArgs = Record const EXPOSURE_FLAG_KEYS = ['tools', 'exclude-tools', 'mutation-mode', 'no-agent-tools'] as const const EXPOSURE_HELP_LINES = [ 'Exposure options (override OM_MCP_* env vars):', ' --tools Allowlist of tool names or prefix globs (e.g. customers.*,search)', ' --exclude-tools Denylist of tool names or prefix globs', ' --mutation-mode direct | deny | approval (default: OM_MCP_MUTATION_MODE, then direct)', ' --no-agent-tools Hide tools that wrap AI agents', ] type ExposureCliArgsResult = { ok: true; cliArgs: CliArgs } | { ok: false; error: string } async function readExposureCliArgs(args: CliArgs): Promise { const cliArgs: CliArgs = {} for (const key of EXPOSURE_FLAG_KEYS) { const value = args[key] if (value !== undefined) cliArgs[key] = value } const { findMcpExposureConfigError } = await import('./lib/mcp-exposure') const error = findMcpExposureConfigError(process.env, cliArgs) if (error) return { ok: false, error } const rawMode = cliArgs['mutation-mode'] if (typeof rawMode === 'string') cliArgs['mutation-mode'] = rawMode.trim().toLowerCase() return { ok: true, cliArgs } } function printExposureError(command: string, error: string): void { console.error(`[${command}] ${error}`) console.error('') for (const line of EXPOSURE_HELP_LINES) console.error(line) process.exitCode = 1 } const mcpServe: ModuleCli = { command: 'mcp:serve', async run(rest) { // stdout carries the MCP JSON-RPC protocol (StdioServerTransport) — route all // facade logs to stderr and rebuild any pino root created earlier in this process. process.env.OM_LOG_DESTINATION = 'stderr' resetServerLoggerCache() const args = parseArgs(rest) // Prefer the OPEN_MERCATO_API_KEY env var so the secret never has to be // placed on the command line (argv is world-readable via ps / /proc). // The --api-key flag stays supported for backward compatibility. const apiKey = String(args['api-key'] ?? args.apiKey ?? '') || process.env.OPEN_MERCATO_API_KEY || null const tenantId = String(args.tenant ?? args.tenantId ?? '') || null const organizationId = String(args.org ?? args.organizationId ?? '') || null const userId = String(args.user ?? args.userId ?? '') || null const debug = args.debug === true || args.debug === 'true' const allowUnauthenticatedSuperadmin = args['allow-unauthenticated-superadmin'] === true || args['allow-unauthenticated-superadmin'] === 'true' const exposure = await readExposureCliArgs(args) if (!exposure.ok) { printExposureError('mcp:serve', exposure.error) return } // Either API key or tenant is required if (!apiKey && !tenantId) { console.error('Usage: mercato ai_assistant mcp:serve [options]') console.error('') console.error('Authentication (choose one):') console.error(' OPEN_MERCATO_API_KEY env API key secret (recommended — keeps the secret off argv)') console.error(' --api-key API key secret for authentication (visible in process listings)') console.error(' --tenant Tenant ID (for manual context)') console.error('') console.error('Options (with --tenant):') console.error(' --org Organization ID (optional)') console.error(' --user User ID for ACL (required unless --allow-unauthenticated-superadmin is set)') console.error('') console.error('Common options:') console.error(' --debug Enable debug logging') console.error(' --allow-unauthenticated-superadmin') console.error(' DEV/TEST ONLY: run as superadmin with no per-user ACL when') console.error(' no --user (and no --api-key) is supplied. Never use in production.') console.error('') for (const line of EXPOSURE_HELP_LINES) console.error(line) console.error('') console.error('Examples:') console.error(' OPEN_MERCATO_API_KEY=omk_xxxx.yyyy... mercato ai_assistant mcp:serve') console.error(' mercato ai_assistant mcp:serve --api-key omk_xxxx.yyyy...') console.error(' mercato ai_assistant mcp:serve --tenant 123e4567-e89b-12d3-a456-426614174000 --user ') console.error(' mercato ai_assistant mcp:serve --tenant 123e4567-e89b-12d3-a456-426614174000 --allow-unauthenticated-superadmin') console.error(' mercato ai_assistant mcp:serve --tools "customers.*,search" --mutation-mode deny') return } await ensureBootstrap() const container = await createRequestContainer() const { runMcpServer } = await import('./lib/mcp-server') if (apiKey) { await runMcpServer({ config: { name: 'open-mercato-mcp', version: '0.1.0', debug, }, container, apiKeySecret: apiKey, cliArgs: exposure.cliArgs, }) } else { await runMcpServer({ config: { name: 'open-mercato-mcp', version: '0.1.0', debug, }, container, context: { tenantId, organizationId, userId, }, allowUnauthenticatedSuperadmin, cliArgs: exposure.cliArgs, }) } }, } const MCP_DEFAULT_PORT = 3001 const mcpServeHttp: ModuleCli = { command: 'mcp:serve-http', async run(rest) { const args = parseArgs(rest) if (args.help === true) { console.error('Usage: mercato ai_assistant mcp:serve-http [options]') console.error('') console.error('Options:') console.error(` --port HTTP port (default ${MCP_DEFAULT_PORT})`) console.error(' --debug Enable debug logging') console.error('') for (const line of EXPOSURE_HELP_LINES) console.error(line) return } const portArg = parseInt(String(args.port ?? ''), 10) const port = !portArg || isNaN(portArg) ? MCP_DEFAULT_PORT : portArg const debug = args.debug === true || args.debug === 'true' const exposure = await readExposureCliArgs(args) if (!exposure.ok) { printExposureError('mcp:serve-http', exposure.error) return } const host = resolveMcpHost( typeof args.host === 'string' ? args.host : undefined, process.env.MCP_HTTP_HOST, ) await ensureBootstrap() const container = await createRequestContainer() const { runMcpHttpServer } = await import('./lib/http-server') await runMcpHttpServer({ config: { name: 'open-mercato-mcp', version: '0.1.0', debug, }, container, port, cliArgs: exposure.cliArgs, host, }) }, } const mcpDev: ModuleCli = { command: 'mcp:dev', async run() { const exposure = await readExposureCliArgs({}) if (!exposure.ok) { printExposureError('mcp:dev', exposure.error) return } await ensureBootstrap() const { runMcpDevServer } = await import('./lib/mcp-dev-server') await runMcpDevServer() }, } const mcpEnsureApiKey: ModuleCli = { command: 'mcp:ensure-api-key', async run(rest) { const args = parseArgs(rest) // A bare `--file` (no value) parses to boolean true — treat it as missing // rather than provisioning into a file literally named "true". const fileArg = typeof args.file === 'string' ? args.file.trim() : '' const filePath = fileArg || process.env.MCP_SERVER_API_KEY_FILE?.trim() || process.env.MCP_API_KEY_FILE?.trim() || '' if (!filePath) { console.error('Usage: mercato ai_assistant mcp:ensure-api-key --file [options]') console.error('') console.error('Ensures a valid MCP server API key exists in the database and that its') console.error('plaintext secret is stored in (the idempotency anchor). The secret') console.error('is never printed; consumers such as the OpenCode container read the file.') console.error('') console.error('Options:') console.error(' --file Secret file path (or MCP_SERVER_API_KEY_FILE env)') console.error(' --name API key name (default __mcp_server__)') console.error(' --email Owner user email (default OM_INIT_SUPERADMIN_EMAIL, then superadmin@acme.com)') console.error(' --rotate Force rotation even when the file secret is still valid') process.exitCode = 1 return } const keyName = typeof args.name === 'string' && args.name.length > 0 ? args.name : undefined const ownerEmail = typeof args.email === 'string' && args.email.length > 0 ? args.email : undefined const rotate = args.rotate === true || args.rotate === 'true' await ensureBootstrap() const container = await createRequestContainer() const em = container.resolve('em') const rbac = container.resolve('rbacService') as import('@open-mercato/core/modules/auth/services/rbacService').RbacService const { ensureMcpApiKey } = await import('./lib/mcp-ensure-api-key') const result = await ensureMcpApiKey({ em, filePath, keyName, ownerEmail, rotate, rbac }) const message = result.status === 'valid' ? 'Existing key is valid' : 'New key created' console.log(`[mcp:ensure-api-key] ${message} (id=${result.keyId}, prefix=${result.keyPrefix})`) }, } const listTools: ModuleCli = { command: 'mcp:list-tools', async run(rest) { const args = parseArgs(rest) const verbose = args.verbose === true || args.verbose === 'true' if (args.help === true) { console.error('Usage: mercato ai_assistant mcp:list-tools [options]') console.error('') console.error('Options:') console.error(' --verbose Print descriptions and required features') console.error(' --effective Apply the MCP exposure config (OM_MCP_* env + flags below)') console.error(' --as-api-key Also apply the ACL of this API key, as mcp:serve-http does') console.error(' (implies --effective; the secret is never printed)') console.error('') for (const line of EXPOSURE_HELP_LINES) console.error(line) return } const apiKeyArg = args['as-api-key'] if (apiKeyArg === true || (typeof apiKeyArg === 'string' && !apiKeyArg.trim())) { printExposureError('mcp:list-tools', '--as-api-key requires an API key secret.') return } const apiKeySecret = typeof apiKeyArg === 'string' ? apiKeyArg.trim() : null const effective = args.effective === true || args.effective === 'true' || apiKeySecret !== null const exposure = effective ? await readExposureCliArgs(args) : null if (exposure && !exposure.ok) { printExposureError('mcp:list-tools', exposure.error) return } // Ensure bootstrap runs so modules are registered for API discovery await ensureBootstrap() const { loadAllModuleTools } = await import('./lib/tool-loader') await loadAllModuleTools() const { getToolRegistry } = await import('./lib/tool-registry') const registry = getToolRegistry() let toolNames = registry.listToolNames() if (exposure) { const { describeMcpExposureConfig, filterToolsForCaller, isToolExposed, resolveMcpExposureConfig, } = await import('./lib/mcp-exposure') const exposureConfig = resolveMcpExposureConfig(process.env, exposure.cliArgs) const allTools = Array.from(registry.getTools().values()) let effectiveTools = allTools.filter((tool) => isToolExposed(tool, exposureConfig)) if (apiKeySecret) { const container = await createRequestContainer() const { resolveApiKeyListCaller } = await import('./lib/http-server') const caller = await resolveApiKeyListCaller(container, apiKeySecret) if (!caller) { console.error('[mcp:list-tools] Invalid or expired API key') process.exitCode = 1 return } effectiveTools = filterToolsForCaller(allTools, exposureConfig, caller) } console.log(`\nExposure config: ${JSON.stringify(describeMcpExposureConfig(exposureConfig, allTools))}`) console.log(`Principal: ${apiKeySecret ? 'api key ACL' : 'none (exposure config only)'}`) toolNames = effectiveTools.map((tool) => tool.name) } if (toolNames.length === 0) { if (exposure) { console.log('\nNo MCP tools are exposed for this configuration.\n') return } console.log('\nNo MCP tools registered.') console.log('Tools can be registered by modules using registerMcpTool().\n') return } console.log( exposure ? `\nEffective MCP Tools (${toolNames.length} of ${registry.listToolNames().length}):\n` : `\nRegistered MCP Tools (${toolNames.length}):\n`, ) // Group tools by module const byModule = new Map() for (const name of toolNames) { const [module] = name.split('.') const list = byModule.get(module) ?? [] list.push(name) byModule.set(module, list) } // Sort modules alphabetically const sortedModules = Array.from(byModule.keys()).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)) for (const moduleName of sortedModules) { const tools = byModule.get(moduleName)! console.log(`${moduleName} (${tools.length} tools):`) for (const name of tools.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))) { const tool = registry.getTool(name) if (!tool) continue if (verbose) { console.log(` ${name}`) console.log(` ${tool.description}`) if (tool.requiredFeatures?.length) { console.log(` Requires: ${tool.requiredFeatures.join(', ')}`) } } else { console.log(` - ${name}`) } } console.log('') } }, } const entityGraph: ModuleCli = { command: 'entity-graph', async run(rest) { const args = parseArgs(rest) const format = String(args.format ?? 'triples') as 'json' | 'triples' const entity = args.entity ? String(args.entity) : undefined const moduleName = args.module ? String(args.module) : undefined await ensureBootstrap() const { getOrm } = await import('@open-mercato/shared/lib/db/mikro') const { extractEntityGraph, formatGraphAsTriples, filterGraphByEntity, filterGraphByModule } = await import( './lib/entity-graph' ) console.log('[Entity Graph] Extracting from MikroORM metadata...') const orm = await getOrm() const graph = await extractEntityGraph(orm) // Apply filters let edges = graph.edges if (entity) { edges = filterGraphByEntity(graph, entity) console.log(`[Entity Graph] Filtered by entity: ${entity}`) } if (moduleName) { const filteredGraph = { ...graph, edges } edges = filterGraphByModule(filteredGraph, moduleName) console.log(`[Entity Graph] Filtered by module: ${moduleName}`) } const filteredGraph = { ...graph, edges } if (format === 'json') { console.log(JSON.stringify(filteredGraph, null, 2)) } else { const triples = formatGraphAsTriples(filteredGraph) console.log('') for (const triple of triples) { console.log(triple) } } console.log(`\n[Entity Graph] ${graph.nodes.length} entities, ${edges.length} relationships`) }, } const runPendingActionCleanup: ModuleCli = { command: 'run-pending-action-cleanup', async run() { await ensureBootstrap() const container = await createRequestContainer() const { runPendingActionCleanup: runCleanup } = await import( './workers/ai-pending-action-cleanup' ) const em = container.resolve('em') const summary = await runCleanup({ em, container }) console.log('[ai-pending-action-cleanup] Sweep complete:', summary) }, } const testTools: ModuleCli = { command: 'test-tools', async run(rest) { const args = parseArgs(rest) const json = args.json === true || args.json === 'true' const moduleFilter = typeof args.module === 'string' && args.module.length > 0 ? args.module : null const includeMutations = args['no-mutations'] !== true && args['no-mutations'] !== 'true' const tenantId = typeof args.tenant === 'string' && args.tenant.length > 0 ? args.tenant : null const organizationId = typeof args.org === 'string' && args.org.length > 0 ? args.org : null await ensureBootstrap() const { runToolTests } = await import('./lib/tool-test-runner') const report = await runToolTests({ tenantId, organizationId, moduleFilter, includeMutations, }) if (json) { // Wrap in markers so a Playwright spec (or any caller) can extract the // JSON payload without being thrown off by bootstrap log lines emitted // to stdout by other modules during DI container creation. console.log('---TOOL_TEST_REPORT_BEGIN---') console.log(JSON.stringify(report)) console.log('---TOOL_TEST_REPORT_END---') } else { console.log('') console.log( `AI tool test report — tenant=${report.tenantId ?? ''} org=${ report.organizationId ?? '' }`, ) console.log( `total=${report.total} pass=${report.passed} fail=${report.failed} skip=${report.skipped}`, ) const byModule = new Map() for (const record of report.records) { const list = byModule.get(record.module) ?? [] list.push(record) byModule.set(record.module, list) } const sortedModules = Array.from(byModule.keys()).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)) for (const moduleId of sortedModules) { const list = byModule.get(moduleId)! console.log('') console.log(`${moduleId} (${list.length}):`) for (const record of list) { const marker = record.status === 'pass' ? '✓' : record.status === 'fail' ? '✗' : '·' const reason = record.reason ? ` — ${record.reason}` : '' const mutation = record.isMutation ? ' [mutation]' : '' console.log( ` ${marker} ${record.tool}${mutation} (${record.durationMs}ms)${reason}`, ) } } console.log('') } if (report.failed > 0) { process.exitCode = 1 } }, } const runTokenUsagePrune: ModuleCli = { command: 'run-token-usage-prune', async run() { await ensureBootstrap() const container = await createRequestContainer() const { runTokenUsagePrune: runPrune } = await import( './workers/ai-token-usage-prune' ) const em = container.resolve('em') const summary = await runPrune({ em }) console.log('[ai-token-usage-prune] Prune complete:', summary) }, } const runAgentRunPrune: ModuleCli = { command: 'run-agent-run-prune', async run() { await ensureBootstrap() const container = await createRequestContainer() const { runAgentRunPrune: runPrune } = await import( './workers/ai-agent-run-prune' ) const em = container.resolve('em') const summary = await runPrune({ em }) console.log('[ai-agent-run-prune] Prune complete:', summary) }, } export default [ mcpServe, mcpServeHttp, mcpDev, mcpEnsureApiKey, listTools, entityGraph, runPendingActionCleanup, runTokenUsagePrune, runAgentRunPrune, testTools, ]