/** Shared production v2 MCP assembly; normal startup installs schema0037. */ import { createMcpServerProbeAuthority, createMcpServerProbeReadAuthority } from "../../services/server-probe-authority.js" import { createProductionServerProbeExecutor } from "../../services/server-probe-execution.js" import { createServerProbeStorageHistoryReader } from "../../services/server-probe-storage-history.js" import { findRetiredServerProbeHistory } from "../../services/server-probe-retired-history.js" import { createSqlServerProbeSourceReaders } from "../../services/server-probe-storage-source.js" import { createProbeStorageHostIo } from "../../services/server-probe-storage-host-io.js" import { assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js" import { createServerProbeMcpAdapter } from "./server-probe-actions.js" import { createStorageDiagnosticCancellationRequest, PROBE_STORAGE_LIMITS, type ProbeStorageLimits } from "./diagnostic-cancellation-storage.js" import { effectiveMcpConnection } from "./store.js" import type { ActionContext } from "./action-types.js" import type { McpServiceHandler } from "./service-registry.js" export { serverProbeStorageJournalDeadline } from "../../services/server-probe-storage-host-io.js" export function createStorageServerProbeMcpAdapter(limits: ProbeStorageLimits = PROBE_STORAGE_LIMITS) { const configured = Object.freeze({ ...limits }), io = createProbeStorageHostIo(configured, "mcp") const authorityDependencies = { query: io.rows, connection: ((principal, client) => { const current = io.currentClient() if (current) return effectiveMcpConnection(principal, current) return client ? io.withBorrowed(client, () => effectiveMcpConnection(principal, client)) : io.withClient(sql => effectiveMcpConnection(principal, sql), { deadlineAt: Date.parse(principal.credentialExpiresAt) }) }) as typeof effectiveMcpConnection, verified: async () => { // Executing checks run between journal methods, never under storage locks. io.assertOutside() await assertVerifiedConfigurationAccess() }, } const adapter = createServerProbeMcpAdapter({ query: io.rows, journal: io.journal, retiredSource: lookup => findRetiredServerProbeHistory(io.rows, lookup), authority: (principal, options) => createMcpServerProbeAuthority(principal, options, authorityDependencies), historicalAuthority: (principal, options) => createMcpServerProbeReadAuthority(principal, options, authorityDependencies), history: (dependencies, execution) => createServerProbeStorageHistoryReader({ assertReadAuthority: dependencies.assertReadAuthority, options: { deadlineAt: Date.now() + 120000, signal: execution?.signal } }), executor: input => createProductionServerProbeExecutor(input, { getServer: async id => createSqlServerProbeSourceReaders({ query: io.query }, (await input.readAuthority()).scopeServerIds).getServer(id), findDestination: async (remote, explicit, scope) => createSqlServerProbeSourceReaders({ query: io.query }, scope).findDestination(remote, explicit, scope), }), }) const window = (work: () => Promise, signal?: AbortSignal, deadlineAt = Date.now() + 300000) => io.runRequest({ deadlineAt, signal }, work) const actionWindow = (context: ActionContext, work: () => Promise) => window(work, context.signal, Math.min(Date.now() + 300000, Date.parse(context.principal.credentialExpiresAt))) const service: McpServiceHandler = { ...adapter.service, current: raw => window(() => adapter.service.current(raw)), diagnosticIdentity: (raw, context) => window(() => adapter.service.diagnosticIdentity!(raw, context), context.phase === "forward" ? context.signal : undefined), execute: (raw, context) => window(() => adapter.service.execute(raw, context), context?.phase === "forward" ? context.signal : undefined), verify: (raw, result) => window(() => adapter.service.verify!(raw, result)), reconcile: (raw, authorize) => window(() => adapter.service.reconcile!(raw, authorize)), } return { ...adapter, service, target: (raw: Parameters[0], context: ActionContext) => actionWindow(context, () => context.transactionClient ? io.withBorrowed(context.transactionClient, () => adapter.target(raw, { ...context, transactionClient: io.currentClient() })) : adapter.target(raw, context)), prepare: (raw: Parameters[0], context: ActionContext) => actionWindow(context, () => adapter.prepare(raw, context)), receiptTarget: (raw: Parameters[0], context: ActionContext) => actionWindow(context, () => adapter.receiptTarget(raw, context)), readPage: (raw: Parameters[0], context: ActionContext) => actionWindow(context, () => adapter.readPage(raw, context)), cancel: createStorageDiagnosticCancellationRequest(configured), } }