@contentrain/mcp 0.3.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +147 -238
  3. package/dist/apply-manager-CaTuu7Lj.mjs +751 -0
  4. package/dist/apply-manager-CaTuu7Lj.mjs.map +1 -0
  5. package/dist/astro-parser-RofKmKQi.mjs +166 -0
  6. package/dist/astro-parser-RofKmKQi.mjs.map +1 -0
  7. package/dist/branch-lifecycle-DB6sgeJi.mjs +97 -0
  8. package/dist/branch-lifecycle-DB6sgeJi.mjs.map +1 -0
  9. package/dist/chunk-BEJ448es.mjs +35 -0
  10. package/dist/compiler-rIC4ETHv.mjs +44509 -0
  11. package/dist/compiler-rIC4ETHv.mjs.map +1 -0
  12. package/dist/compiler-sfc.cjs-BbD04rn_.mjs +50279 -0
  13. package/dist/compiler-sfc.cjs-BbD04rn_.mjs.map +1 -0
  14. package/dist/config-CPlhDwsv.mjs +27 -0
  15. package/dist/config-CPlhDwsv.mjs.map +1 -0
  16. package/dist/context-yh3cuw5N.mjs +46 -0
  17. package/dist/context-yh3cuw5N.mjs.map +1 -0
  18. package/dist/core/apply-manager.d.mts +144 -0
  19. package/dist/core/apply-manager.d.mts.map +1 -0
  20. package/dist/core/apply-manager.mjs +6 -0
  21. package/dist/core/config.d.mts +8 -0
  22. package/dist/core/config.d.mts.map +1 -0
  23. package/dist/core/config.mjs +2 -0
  24. package/dist/core/content-manager.d.mts +53 -0
  25. package/dist/core/content-manager.d.mts.map +1 -0
  26. package/dist/core/content-manager.mjs +2 -0
  27. package/dist/core/context.d.mts +13 -0
  28. package/dist/core/context.d.mts.map +1 -0
  29. package/dist/core/context.mjs +3 -0
  30. package/dist/core/graph-builder.d.mts +12 -0
  31. package/dist/core/graph-builder.d.mts.map +1 -0
  32. package/dist/core/graph-builder.mjs +2 -0
  33. package/dist/core/meta-manager.d.mts +14 -0
  34. package/dist/core/meta-manager.d.mts.map +1 -0
  35. package/dist/core/meta-manager.mjs +2 -0
  36. package/dist/core/model-manager.d.mts +44 -0
  37. package/dist/core/model-manager.d.mts.map +1 -0
  38. package/dist/core/model-manager.mjs +2 -0
  39. package/dist/core/scan-config.d.mts +24 -0
  40. package/dist/core/scan-config.d.mts.map +1 -0
  41. package/dist/core/scan-config.mjs +2 -0
  42. package/dist/core/scanner.d.mts +25 -0
  43. package/dist/core/scanner.d.mts.map +1 -0
  44. package/dist/core/scanner.mjs +3 -0
  45. package/dist/core/validator.d.mts +23 -0
  46. package/dist/core/validator.d.mts.map +1 -0
  47. package/dist/core/validator.mjs +3 -0
  48. package/dist/detect-C8XmJ4S6.mjs +362 -0
  49. package/dist/detect-C8XmJ4S6.mjs.map +1 -0
  50. package/dist/fs-BbuGnVcG.mjs +52 -0
  51. package/dist/fs-BbuGnVcG.mjs.map +1 -0
  52. package/dist/git/branch-lifecycle.d.mts +28 -0
  53. package/dist/git/branch-lifecycle.d.mts.map +1 -0
  54. package/dist/git/branch-lifecycle.mjs +2 -0
  55. package/dist/git/transaction.d.mts +25 -0
  56. package/dist/git/transaction.d.mts.map +1 -0
  57. package/dist/git/transaction.mjs +4 -0
  58. package/dist/graph-builder-fPrDDwnu.mjs +162 -0
  59. package/dist/graph-builder-fPrDDwnu.mjs.map +1 -0
  60. package/dist/id-BdD4HvMY.mjs +12 -0
  61. package/dist/id-BdD4HvMY.mjs.map +1 -0
  62. package/dist/index.d.mts +1 -359
  63. package/dist/index.mjs +15 -2161
  64. package/dist/index.mjs.map +1 -1
  65. package/dist/meta-manager-CECrfsKm.mjs +53 -0
  66. package/dist/meta-manager-CECrfsKm.mjs.map +1 -0
  67. package/dist/model-manager-BcWWu5l2.mjs +963 -0
  68. package/dist/model-manager-BcWWu5l2.mjs.map +1 -0
  69. package/dist/node-Cbq5eR5i.mjs +300 -0
  70. package/dist/node-Cbq5eR5i.mjs.map +1 -0
  71. package/dist/scan-config-AJGxCdNd.mjs +159 -0
  72. package/dist/scan-config-AJGxCdNd.mjs.map +1 -0
  73. package/dist/scanner-BdTiv8sw.mjs +619 -0
  74. package/dist/scanner-BdTiv8sw.mjs.map +1 -0
  75. package/dist/serializer-Dueih-i2.mjs +33 -0
  76. package/dist/serializer-Dueih-i2.mjs.map +1 -0
  77. package/dist/server-CTcDpxwW.mjs +1861 -0
  78. package/dist/server-CTcDpxwW.mjs.map +1 -0
  79. package/dist/server.d.mts +7 -0
  80. package/dist/server.d.mts.map +1 -0
  81. package/dist/server.mjs +11 -0
  82. package/dist/source-map-generator-DpO-ejrt.mjs +893 -0
  83. package/dist/source-map-generator-DpO-ejrt.mjs.map +1 -0
  84. package/dist/svelte-parser-COTNG92w.mjs +225 -0
  85. package/dist/svelte-parser-COTNG92w.mjs.map +1 -0
  86. package/dist/templates/index.d.mts +8 -0
  87. package/dist/templates/index.d.mts.map +1 -0
  88. package/dist/templates/index.mjs +2 -0
  89. package/dist/templates-3HWi-PTu.mjs +824 -0
  90. package/dist/templates-3HWi-PTu.mjs.map +1 -0
  91. package/dist/transaction-BsECnmL5.mjs +128 -0
  92. package/dist/transaction-BsECnmL5.mjs.map +1 -0
  93. package/dist/tsx-parser-Bf8Firrq.mjs +2 -0
  94. package/dist/tsx-parser-CYVyBWjn.mjs +234 -0
  95. package/dist/tsx-parser-CYVyBWjn.mjs.map +1 -0
  96. package/dist/util/detect.d.mts +20 -0
  97. package/dist/util/detect.d.mts.map +1 -0
  98. package/dist/util/detect.mjs +2 -0
  99. package/dist/util/fs.d.mts +12 -0
  100. package/dist/util/fs.d.mts.map +1 -0
  101. package/dist/util/fs.mjs +2 -0
  102. package/dist/util/id.d.mts +6 -0
  103. package/dist/util/id.d.mts.map +1 -0
  104. package/dist/util/id.mjs +2 -0
  105. package/dist/util/serializer.d.mts +14 -0
  106. package/dist/util/serializer.d.mts.map +1 -0
  107. package/dist/util/serializer.mjs +2 -0
  108. package/dist/validator-ku84AyFr.mjs +755 -0
  109. package/dist/validator-ku84AyFr.mjs.map +1 -0
  110. package/dist/vue-parser-CjS4YAoQ.mjs +225 -0
  111. package/dist/vue-parser-CjS4YAoQ.mjs.map +1 -0
  112. package/package.json +107 -43
  113. package/dist/fs-D83-Ii_y.mjs +0 -75
  114. package/dist/fs-D83-Ii_y.mjs.map +0 -1
  115. package/dist/index.d.mts.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/util/json.ts","../src/errors.ts","../src/core/model/system-fields.ts","../src/core/model/manager.ts","../src/util/id.ts","../src/core/content/markdown.ts","../src/core/content/manager.ts","../src/core/asset/manager.ts","../src/core/validation/rules.ts","../src/core/validation/validator.ts","../src/core/git/merge.ts","../src/core/git/sync.ts","../src/core/writer.ts","../src/tools/model.ts","../src/tools/content.ts","../src/tools/asset.ts","../src/core/doctor.ts","../src/tools/doctor.ts","../src/server.ts","../src/index.ts"],"sourcesContent":["import { readFile, writeFile, rename } from 'node:fs/promises'\nimport { join, dirname } from 'node:path'\nimport { randomBytes } from 'node:crypto'\nimport { ensureDir } from './fs.js'\n\n/**\n * Read and parse a JSON file. Returns empty array if file doesn't exist.\n */\nexport async function readJsonFile<T = unknown>(filePath: string): Promise<T> {\n try {\n const raw = await readFile(filePath, 'utf-8')\n return JSON.parse(raw) as T\n }\n catch (err: unknown) {\n if (err instanceof Error && 'code' in err && (err as NodeJS.ErrnoException).code === 'ENOENT') {\n return [] as unknown as T\n }\n throw err\n }\n}\n\n/**\n * Write JSON to file atomically (temp file + rename).\n */\nexport async function writeJsonFile(filePath: string, data: unknown): Promise<void> {\n await ensureDir(dirname(filePath))\n const tmpPath = join(dirname(filePath), `.tmp-${randomBytes(4).toString('hex')}.json`)\n const content = JSON.stringify(data, null, 2) + '\\n'\n await writeFile(tmpPath, content, 'utf-8')\n await rename(tmpPath, filePath)\n}\n","export class ContentrainError extends Error {\n code: string\n context: Record<string, unknown>\n\n constructor(message: string, code: string, context: Record<string, unknown> = {}) {\n super(message)\n this.name = 'ContentrainError'\n this.code = code\n this.context = context\n }\n}\n\nexport class ValidationError extends ContentrainError {\n fieldErrors: Array<{ fieldId: string; message: string; rule: string }>\n\n constructor(message: string, fieldErrors: Array<{ fieldId: string; message: string; rule: string }>) {\n super(message, 'VALIDATION_ERROR', { fieldErrors })\n this.name = 'ValidationError'\n this.fieldErrors = fieldErrors\n }\n}\n\nexport class ModelNotFoundError extends ContentrainError {\n constructor(modelId: string) {\n super(`Model not found: ${modelId}`, 'MODEL_NOT_FOUND', { modelId })\n this.name = 'ModelNotFoundError'\n }\n}\n\nexport class EntryNotFoundError extends ContentrainError {\n constructor(modelId: string, entryId: string) {\n super(`Entry not found: ${entryId} in model ${modelId}`, 'ENTRY_NOT_FOUND', { modelId, entryId })\n this.name = 'EntryNotFoundError'\n }\n}\n\nexport class GitSyncError extends ContentrainError {\n constructor(message: string, context: Record<string, unknown> = {}) {\n super(message, 'GIT_SYNC_ERROR', context)\n this.name = 'GitSyncError'\n }\n}\n\nexport class MergeConflictError extends GitSyncError {\n conflictedFiles: string[]\n\n constructor(conflictedFiles: string[]) {\n super(`Merge conflict in files: ${conflictedFiles.join(', ')}`, { conflictedFiles })\n this.name = 'MergeConflictError'\n this.conflictedFiles = conflictedFiles\n }\n}\n\nexport class PushRejectedError extends GitSyncError {\n attemptsExhausted: boolean\n\n constructor(attemptsExhausted: boolean) {\n super(\n attemptsExhausted\n ? 'Push rejected: all retry attempts exhausted'\n : 'Push rejected by remote',\n { attemptsExhausted },\n )\n this.name = 'PushRejectedError'\n this.attemptsExhausted = attemptsExhausted\n }\n}\n\nexport class RelationIntegrityError extends ValidationError {\n constructor(fieldId: string, referencedModel: string, referencedId: string) {\n super(\n `Relation integrity error: ${referencedId} not found in model ${referencedModel}`,\n [{ fieldId, message: `Referenced entry ${referencedId} not found in ${referencedModel}`, rule: 'relation-integrity' }],\n )\n this.name = 'RelationIntegrityError'\n }\n}\n\nexport class AssetNotFoundError extends ContentrainError {\n constructor(assetPath: string) {\n super(`Asset not found in registry: ${assetPath}`, 'ASSET_NOT_FOUND', { assetPath })\n this.name = 'AssetNotFoundError'\n }\n}\n","import type { FieldDefinition } from '../../types/index.js'\n\n/**\n * Generate the 4 required system fields for a model.\n */\nexport function createSystemFields(modelId: string): FieldDefinition[] {\n return [\n {\n name: 'createdAt',\n fieldId: 'createdAt',\n componentId: 'date',\n options: {},\n validations: { 'required-field': { value: true } },\n fieldType: 'date',\n system: true,\n defaultField: false,\n modelId,\n },\n {\n name: 'updatedAt',\n fieldId: 'updatedAt',\n componentId: 'date',\n options: {},\n validations: { 'required-field': { value: true } },\n fieldType: 'date',\n system: true,\n defaultField: false,\n modelId,\n },\n {\n name: 'ID',\n fieldId: 'ID',\n componentId: 'single-line-text',\n options: {},\n validations: { 'required-field': { value: true } },\n fieldType: 'string',\n system: true,\n defaultField: false,\n modelId,\n },\n {\n name: 'status',\n fieldId: 'status',\n componentId: 'single-line-text',\n options: {},\n validations: { 'required-field': { value: true } },\n fieldType: 'string',\n system: true,\n defaultField: false,\n modelId,\n },\n ]\n}\n\n/**\n * Generate MD/MDX default fields (slug + content).\n */\nexport function createMdDefaultFields(modelId: string): FieldDefinition[] {\n return [\n {\n name: 'slug',\n fieldId: 'slug',\n componentId: 'slug',\n options: {},\n validations: { 'required-field': { value: true }, 'unique-field': { value: true } },\n fieldType: 'string',\n system: false,\n defaultField: true,\n modelId,\n },\n {\n name: 'content',\n fieldId: 'content',\n componentId: 'md-editor',\n options: {},\n validations: { 'required-field': { value: false } },\n fieldType: 'string',\n system: false,\n defaultField: true,\n modelId,\n },\n ]\n}\n","import { join } from 'node:path'\nimport type { ModelMetadata, FieldDefinition } from '../../types/index.js'\nimport { readJsonFile, writeJsonFile } from '../../util/json.js'\nimport { ensureDir } from '../../util/fs.js'\nimport { ModelNotFoundError } from '../../errors.js'\nimport { createSystemFields, createMdDefaultFields } from './system-fields.js'\n\nexport class ModelManager {\n constructor(private basePath: string) {}\n\n private get modelsDir(): string {\n return join(this.basePath, 'models')\n }\n\n private get metadataPath(): string {\n return join(this.modelsDir, 'metadata.json')\n }\n\n // ── Read Operations ──\n\n async list(): Promise<ModelMetadata[]> {\n return readJsonFile<ModelMetadata[]>(this.metadataPath)\n }\n\n async get(modelId: string): Promise<ModelMetadata> {\n const models = await this.list()\n const model = models.find(m => m.modelId === modelId)\n if (!model) throw new ModelNotFoundError(modelId)\n return model\n }\n\n async getFields(modelId: string): Promise<FieldDefinition[]> {\n await this.get(modelId) // ensure model exists\n const fieldsPath = join(this.modelsDir, `${modelId}.json`)\n return readJsonFile<FieldDefinition[]>(fieldsPath)\n }\n\n async describe(modelId: string): Promise<{\n model: ModelMetadata\n fields: FieldDefinition[]\n customFields: FieldDefinition[]\n }> {\n const model = await this.get(modelId)\n const fields = await this.getFields(modelId)\n const customFields = fields.filter(f => !f.system && !f.defaultField)\n return { model, fields, customFields }\n }\n\n // ── Write Operations ──\n\n async create(input: {\n name: string\n description?: string\n modelId: string\n localization: boolean\n type: 'JSON' | 'MD' | 'MDX'\n path?: string\n fields?: Omit<FieldDefinition, 'modelId' | 'system' | 'defaultField'>[]\n }): Promise<ModelMetadata> {\n const models = await this.list()\n\n if (models.some(m => m.modelId === input.modelId)) {\n throw new Error(`Model already exists: ${input.modelId}`)\n }\n\n // Create metadata entry\n const metadata: ModelMetadata = {\n name: input.name,\n description: input.description,\n modelId: input.modelId,\n path: input.path,\n localization: input.localization,\n type: input.type,\n createdBy: 'user',\n isServerless: false,\n }\n\n models.push(metadata)\n await writeJsonFile(this.metadataPath, models)\n\n // Build field definitions\n const systemFields = createSystemFields(input.modelId)\n const mdFields = (input.type === 'MD' || input.type === 'MDX')\n ? createMdDefaultFields(input.modelId)\n : []\n const customFields: FieldDefinition[] = (input.fields || []).map(f => ({\n ...f,\n modelId: input.modelId,\n system: false,\n defaultField: false,\n }))\n\n const allFields = [...systemFields, ...mdFields, ...customFields]\n await ensureDir(this.modelsDir)\n await writeJsonFile(join(this.modelsDir, `${input.modelId}.json`), allFields)\n\n // Create empty content directory and file\n const contentDir = join(this.basePath, input.modelId)\n await ensureDir(contentDir)\n\n if (input.localization) {\n // We don't know which locales yet — create dir only, files created on first content write\n }\n else {\n await writeJsonFile(join(contentDir, `${input.modelId}.json`), [])\n }\n\n return metadata\n }\n\n async addField(modelId: string, field: Omit<FieldDefinition, 'modelId' | 'system' | 'defaultField'>): Promise<FieldDefinition> {\n const fields = await this.getFields(modelId)\n\n if (fields.some(f => f.fieldId === field.fieldId)) {\n throw new Error(`Field already exists: ${field.fieldId} in model ${modelId}`)\n }\n\n const newField: FieldDefinition = {\n ...field,\n modelId,\n system: false,\n defaultField: false,\n }\n\n fields.push(newField)\n await writeJsonFile(join(this.modelsDir, `${modelId}.json`), fields)\n\n return newField\n }\n\n async removeField(modelId: string, fieldId: string): Promise<void> {\n const fields = await this.getFields(modelId)\n const field = fields.find(f => f.fieldId === fieldId)\n\n if (!field) throw new Error(`Field not found: ${fieldId} in model ${modelId}`)\n if (field.system) throw new Error(`Cannot remove system field: ${fieldId}`)\n\n const filtered = fields.filter(f => f.fieldId !== fieldId)\n await writeJsonFile(join(this.modelsDir, `${modelId}.json`), filtered)\n }\n\n async delete(modelId: string): Promise<void> {\n const models = await this.list()\n const filtered = models.filter(m => m.modelId !== modelId)\n\n if (filtered.length === models.length) {\n throw new ModelNotFoundError(modelId)\n }\n\n await writeJsonFile(this.metadataPath, filtered)\n\n // Remove field definitions\n const { removeFile, removeDir } = await import('../../util/fs.js')\n await removeFile(join(this.modelsDir, `${modelId}.json`))\n\n // Remove content directory\n await removeDir(join(this.basePath, modelId))\n }\n}\n","import { randomUUID } from 'node:crypto'\n\nconst ID_PATTERN = /^[a-f0-9]{12}$/\n\n/**\n * Generate a 12-character lowercase hexadecimal ID.\n * Matches the Contentrain platform: crypto.randomUUID().split('-').pop()\n */\nexport function generateId(): string {\n return randomUUID().split('-').pop()!\n}\n\n/**\n * Validate that a string matches the Contentrain ID format.\n */\nexport function isValidId(id: string): boolean {\n return ID_PATTERN.test(id)\n}\n","import { join } from 'node:path'\nimport { readFile, writeFile } from 'node:fs/promises'\nimport type { MDContentEntry, ModelMetadata } from '../../types/index.js'\nimport { ensureDir, removeFile } from '../../util/fs.js'\n\n/**\n * Build frontmatter string from entry data (excluding 'content' field).\n */\nfunction buildFrontmatter(entry: Record<string, unknown>): string {\n const lines: string[] = ['---']\n for (const [key, value] of Object.entries(entry)) {\n if (key === 'content') continue\n if (typeof value === 'string') {\n lines.push(`${key}: '${value}'`)\n }\n else if (typeof value === 'boolean' || typeof value === 'number') {\n lines.push(`${key}: ${value}`)\n }\n else if (Array.isArray(value)) {\n lines.push(`${key}: ${JSON.stringify(value)}`)\n }\n }\n lines.push('---')\n return lines.join('\\n')\n}\n\n/**\n * Get the markdown file extension based on model type.\n */\nfunction getExtension(type: 'MD' | 'MDX'): string {\n return type === 'MDX' ? '.mdx' : '.md'\n}\n\n/**\n * Resolve the markdown file path for an entry.\n */\nfunction resolveMarkdownPath(\n repoPath: string,\n model: ModelMetadata,\n entry: MDContentEntry,\n locale?: string,\n): string {\n const basePath = entry.path || model.path || ''\n const ext = getExtension(model.type as 'MD' | 'MDX')\n\n if (locale) {\n return join(repoPath, basePath, locale, `${entry.slug}${ext}`)\n }\n return join(repoPath, basePath, `${entry.slug}${ext}`)\n}\n\nexport class MarkdownSync {\n constructor(private repoPath: string) {}\n\n /**\n * Write a markdown file for an MD/MDX entry.\n */\n async write(\n model: ModelMetadata,\n entry: MDContentEntry,\n content: string,\n locale?: string,\n ): Promise<string> {\n const filePath = resolveMarkdownPath(this.repoPath, model, entry, locale)\n await ensureDir(join(filePath, '..'))\n\n const frontmatter = buildFrontmatter(entry)\n const body = content || ''\n const fileContent = `${frontmatter}\\n\\n${body}\\n`\n\n await writeFile(filePath, fileContent, 'utf-8')\n return filePath\n }\n\n /**\n * Read markdown content from a file.\n */\n async read(\n model: ModelMetadata,\n entry: MDContentEntry,\n locale?: string,\n ): Promise<string> {\n const filePath = resolveMarkdownPath(this.repoPath, model, entry, locale)\n try {\n const raw = await readFile(filePath, 'utf-8')\n // Extract content after frontmatter\n const match = raw.match(/^---\\n[\\s\\S]*?\\n---\\n\\n?([\\s\\S]*)$/)\n return match ? match[1].trim() : ''\n }\n catch {\n return ''\n }\n }\n\n /**\n * Delete a markdown file for an entry.\n */\n async delete(\n model: ModelMetadata,\n entry: MDContentEntry,\n locale?: string,\n ): Promise<void> {\n const filePath = resolveMarkdownPath(this.repoPath, model, entry, locale)\n await removeFile(filePath)\n }\n}\n","import { join } from 'node:path'\nimport type { ContentEntry, MDContentEntry, ModelMetadata } from '../../types/index.js'\nimport { readJsonFile, writeJsonFile } from '../../util/json.js'\nimport { ensureDir, listJsonFiles } from '../../util/fs.js'\nimport { generateId } from '../../util/id.js'\nimport { EntryNotFoundError } from '../../errors.js'\nimport { ModelManager } from '../model/manager.js'\nimport { MarkdownSync } from './markdown.js'\n\nexport class ContentManager {\n private modelManager: ModelManager\n private markdownSync: MarkdownSync\n\n constructor(\n private basePath: string,\n private repoPath: string,\n ) {\n this.modelManager = new ModelManager(basePath)\n this.markdownSync = new MarkdownSync(repoPath)\n }\n\n // ── Path Helpers ──\n\n private contentDir(modelId: string): string {\n return join(this.basePath, modelId)\n }\n\n private contentFilePath(modelId: string, locale?: string): string {\n const dir = this.contentDir(modelId)\n return locale\n ? join(dir, `${locale}.json`)\n : join(dir, `${modelId}.json`)\n }\n\n // ── Read Operations ──\n\n async list(modelId: string, locale?: string): Promise<ContentEntry[]> {\n const model = await this.modelManager.get(modelId)\n const filePath = this.resolveContentPath(model, locale)\n return readJsonFile<ContentEntry[]>(filePath)\n }\n\n async get(modelId: string, entryId: string, locale?: string): Promise<ContentEntry> {\n const entries = await this.list(modelId, locale)\n const entry = entries.find(e => e.ID === entryId)\n if (!entry) throw new EntryNotFoundError(modelId, entryId)\n return entry\n }\n\n async getLocales(modelId: string): Promise<string[]> {\n const model = await this.modelManager.get(modelId)\n if (!model.localization) return []\n const files = await listJsonFiles(this.contentDir(modelId))\n // Exclude modelId.json (non-localized file name)\n return files.filter(f => f !== modelId)\n }\n\n // ── Write Operations ──\n\n async create(\n modelId: string,\n data: Record<string, unknown>,\n options?: { locale?: string; id?: string; status?: 'draft' | 'publish'; content?: string },\n ): Promise<string> {\n const model = await this.modelManager.get(modelId)\n const id = options?.id || generateId()\n const now = new Date().toISOString()\n\n const entry: ContentEntry = {\n ID: id,\n createdAt: now,\n updatedAt: now,\n status: options?.status || 'draft',\n scheduled: false,\n ...data,\n }\n\n if (model.localization) {\n await this.createLocalized(model, entry, options?.locale, options?.content)\n }\n else {\n await this.appendEntry(model, entry, undefined, options?.content)\n }\n\n return id\n }\n\n async update(\n modelId: string,\n entryId: string,\n data: Record<string, unknown>,\n options?: { locale?: string; content?: string },\n ): Promise<ContentEntry> {\n const model = await this.modelManager.get(modelId)\n const locale = model.localization ? options?.locale : undefined\n const filePath = this.resolveContentPath(model, locale)\n const entries = await readJsonFile<ContentEntry[]>(filePath)\n\n const index = entries.findIndex(e => e.ID === entryId)\n if (index === -1) throw new EntryNotFoundError(modelId, entryId)\n\n // Preserve createdAt, update updatedAt\n const updated: ContentEntry = {\n ...entries[index],\n ...data,\n ID: entryId,\n createdAt: entries[index].createdAt,\n updatedAt: new Date().toISOString(),\n }\n\n entries[index] = updated\n await writeJsonFile(filePath, entries)\n\n // Sync markdown if MD/MDX\n if ((model.type === 'MD' || model.type === 'MDX') && 'slug' in updated) {\n const mdContent = options?.content ?? await this.markdownSync.read(model, updated as MDContentEntry, locale)\n await this.markdownSync.write(model, updated as MDContentEntry, mdContent, locale)\n }\n\n return updated\n }\n\n async delete(modelId: string, entryId: string): Promise<void> {\n const model = await this.modelManager.get(modelId)\n\n if (model.localization) {\n const locales = await this.getLocales(modelId)\n for (const locale of locales) {\n await this.deleteFromFile(model, entryId, locale)\n }\n }\n else {\n await this.deleteFromFile(model, entryId)\n }\n }\n\n // ── Private Helpers ──\n\n private resolveContentPath(model: ModelMetadata, locale?: string): string {\n if (model.localization && locale) {\n return this.contentFilePath(model.modelId, locale)\n }\n if (model.localization && !locale) {\n // Default: try to find any locale file\n return this.contentFilePath(model.modelId, model.modelId)\n }\n return this.contentFilePath(model.modelId)\n }\n\n private async createLocalized(\n model: ModelMetadata,\n entry: ContentEntry,\n locale?: string,\n content?: string,\n ): Promise<void> {\n if (locale) {\n // Create for a specific locale\n await this.appendEntry(model, entry, locale, content)\n }\n else {\n // Get existing locales or default\n const locales = await this.getLocales(model.modelId)\n if (locales.length === 0) {\n // No locale files yet — cannot determine locales\n throw new Error(\n `Model \"${model.modelId}\" is localized but no locale files exist. `\n + 'Specify a locale (e.g., locale: \"en\") to create the first entry.',\n )\n }\n // Create entry in ALL locale files with the same ID\n for (const loc of locales) {\n await this.appendEntry(model, entry, loc, content)\n }\n }\n }\n\n private async appendEntry(\n model: ModelMetadata,\n entry: ContentEntry,\n locale?: string,\n content?: string,\n ): Promise<void> {\n const filePath = this.resolveContentPath(model, locale)\n await ensureDir(join(filePath, '..'))\n\n const entries = await readJsonFile<ContentEntry[]>(filePath)\n\n // Check for duplicate ID\n if (entries.some(e => e.ID === entry.ID)) {\n throw new Error(`Duplicate ID: ${entry.ID} already exists in ${model.modelId}`)\n }\n\n entries.push(entry)\n await writeJsonFile(filePath, entries)\n\n // Create markdown file if MD/MDX\n if ((model.type === 'MD' || model.type === 'MDX') && 'slug' in entry) {\n await this.markdownSync.write(model, entry as MDContentEntry, content || '', locale)\n }\n }\n\n private async deleteFromFile(model: ModelMetadata, entryId: string, locale?: string): Promise<void> {\n const filePath = this.resolveContentPath(model, locale)\n const entries = await readJsonFile<ContentEntry[]>(filePath)\n\n const entry = entries.find(e => e.ID === entryId)\n if (!entry) return // Already deleted or doesn't exist in this locale\n\n const filtered = entries.filter(e => e.ID !== entryId)\n await writeJsonFile(filePath, filtered)\n\n // Delete markdown file if MD/MDX\n if ((model.type === 'MD' || model.type === 'MDX') && 'slug' in entry) {\n await this.markdownSync.delete(model, entry as MDContentEntry, locale)\n }\n }\n}\n","import { join } from 'node:path'\nimport { stat } from 'node:fs/promises'\nimport type { AssetEntry, WriterConfig } from '../../types/index.js'\nimport { readJsonFile, writeJsonFile } from '../../util/json.js'\nimport { fileExists } from '../../util/fs.js'\nimport { AssetNotFoundError } from '../../errors.js'\n\nexport class AssetManager {\n private assetsPath: string\n\n constructor(\n private basePath: string,\n private repoPath: string,\n private config: WriterConfig,\n ) {\n this.assetsPath = join(basePath, 'assets.json')\n }\n\n async list(): Promise<AssetEntry[]> {\n return readJsonFile<AssetEntry[]>(this.assetsPath)\n }\n\n async get(assetPath: string): Promise<AssetEntry | undefined> {\n const assets = await this.list()\n return assets.find(a => a.path === assetPath)\n }\n\n async register(input: {\n path: string\n mimetype: string\n alt?: string\n }): Promise<AssetEntry> {\n // Verify file physically exists\n const fullPath = join(this.repoPath, input.path)\n if (!await fileExists(fullPath)) {\n throw new Error(`Asset file does not exist at: ${input.path}`)\n }\n\n const assets = await this.list()\n\n // Check for duplicate\n if (assets.some(a => a.path === input.path)) {\n throw new Error(`Asset already registered: ${input.path}`)\n }\n\n // Get file size\n const stats = await stat(fullPath)\n const now = new Date().toISOString()\n\n const entry: AssetEntry = {\n path: input.path,\n mimetype: input.mimetype,\n size: stats.size,\n alt: input.alt || '',\n meta: {\n user: {\n name: this.config.author?.name || 'AI Agent',\n email: this.config.author?.email || 'agent@contentrain.io',\n },\n createdAt: now,\n updatedAt: now,\n },\n }\n\n assets.push(entry)\n await writeJsonFile(this.assetsPath, assets)\n\n return entry\n }\n\n async deregister(assetPath: string): Promise<void> {\n const assets = await this.list()\n const filtered = assets.filter(a => a.path !== assetPath)\n\n if (filtered.length === assets.length) {\n throw new AssetNotFoundError(assetPath)\n }\n\n await writeJsonFile(this.assetsPath, filtered)\n }\n\n async update(assetPath: string, updates: { alt?: string; mimetype?: string }): Promise<AssetEntry> {\n const assets = await this.list()\n const index = assets.findIndex(a => a.path === assetPath)\n\n if (index === -1) throw new AssetNotFoundError(assetPath)\n\n if (updates.alt !== undefined) assets[index].alt = updates.alt\n if (updates.mimetype !== undefined) assets[index].mimetype = updates.mimetype\n if (assets[index].meta) {\n assets[index].meta!.updatedAt = new Date().toISOString()\n }\n\n await writeJsonFile(this.assetsPath, assets)\n return assets[index]\n }\n}\n","import type { ComponentId, FieldType } from '../../types/index.js'\nimport { isValidId } from '../../util/id.js'\n\nconst VALID_STATUSES = ['draft', 'publish', 'changed'] as const\n\n/**\n * Map componentId to expected runtime JS type.\n */\nexport function getExpectedType(componentId: ComponentId, fieldType: FieldType): string {\n switch (componentId) {\n // String components\n case 'single-line-text':\n case 'multi-line-text':\n case 'email':\n case 'url':\n case 'slug':\n case 'color':\n case 'json':\n case 'md-editor':\n case 'rich-text-editor':\n case 'phone-number': // phone-number is string, NOT number\n return 'string'\n\n // Number components\n case 'integer':\n case 'decimal':\n case 'rating':\n case 'percent':\n return 'number'\n\n // Boolean components\n case 'checkbox':\n case 'switch':\n return 'boolean'\n\n // Date components — stored as ISO string\n case 'date':\n case 'date-time':\n return 'string'\n\n // Media — stored as path string\n case 'media':\n return 'string'\n\n // Relations\n case 'one-to-one':\n return 'string'\n case 'one-to-many':\n return 'array'\n\n default:\n return fieldType === 'array' ? 'array' : 'string'\n }\n}\n\n/**\n * Validate an ID matches Contentrain format (12-char lowercase hex).\n */\nexport function validateId(id: unknown): string | null {\n if (typeof id !== 'string') return 'ID must be a string'\n if (!isValidId(id)) return 'ID must be 12-character lowercase hexadecimal'\n return null\n}\n\n/**\n * Validate a status value.\n */\nexport function validateStatus(status: unknown): string | null {\n if (typeof status !== 'string') return 'status must be a string'\n if (!VALID_STATUSES.includes(status as typeof VALID_STATUSES[number])) {\n return `status must be one of: ${VALID_STATUSES.join(', ')}`\n }\n return null\n}\n\n/**\n * Validate a value matches the expected runtime type.\n */\nexport function validateFieldValue(value: unknown, expectedType: string, fieldId: string): string | null {\n if (value === null || value === undefined) return null // handled by required check\n\n switch (expectedType) {\n case 'string':\n if (typeof value !== 'string') return `${fieldId} must be a string`\n break\n case 'number':\n if (typeof value !== 'number') return `${fieldId} must be a number`\n break\n case 'boolean':\n if (typeof value !== 'boolean') return `${fieldId} must be a boolean`\n break\n case 'array':\n if (!Array.isArray(value)) return `${fieldId} must be an array`\n break\n }\n\n return null\n}\n\n/**\n * Validate integer specifically.\n */\nexport function validateInteger(value: unknown, fieldId: string): string | null {\n if (typeof value === 'number' && !Number.isInteger(value)) {\n return `${fieldId} must be an integer`\n }\n return null\n}\n","import { join } from 'node:path'\nimport type { ContentEntry, ValidationResult, ValidationFieldError } from '../../types/index.js'\nimport { readJsonFile } from '../../util/json.js'\nimport { ModelManager } from '../model/manager.js'\nimport { getExpectedType, validateFieldValue, validateInteger, validateId, validateStatus } from './rules.js'\n\nexport class Validator {\n private modelManager: ModelManager\n\n constructor(private basePath: string) {\n this.modelManager = new ModelManager(basePath)\n }\n\n /**\n * Validate content data against model field definitions.\n */\n async validate(\n modelId: string,\n data: Record<string, unknown>,\n options?: { isUpdate?: boolean },\n ): Promise<ValidationResult> {\n const errors: ValidationFieldError[] = []\n const { fields, model } = await this.modelManager.describe(modelId)\n\n const nonSystemFields = fields.filter(f => !f.system)\n\n for (const field of nonSystemFields) {\n const value = data[field.fieldId]\n\n // Required check (skip on update if field not provided)\n if (field.validations?.['required-field']?.value) {\n if (!options?.isUpdate && (value === undefined || value === null || value === '')) {\n // For MD/MDX, content field is not required in JSON\n if (field.fieldId === 'content' && (model.type === 'MD' || model.type === 'MDX')) {\n continue\n }\n errors.push({\n fieldId: field.fieldId,\n message: `${field.name} is required`,\n rule: 'required-field',\n })\n continue\n }\n }\n\n if (value === undefined || value === null) continue\n\n // Type check\n const expectedType = getExpectedType(field.componentId, field.fieldType)\n const typeError = validateFieldValue(value, expectedType, field.fieldId)\n if (typeError) {\n errors.push({ fieldId: field.fieldId, message: typeError, rule: 'type-check' })\n continue\n }\n\n // Integer check\n if (field.componentId === 'integer') {\n const intError = validateInteger(value, field.fieldId)\n if (intError) {\n errors.push({ fieldId: field.fieldId, message: intError, rule: 'integer-check' })\n }\n }\n\n // Relation integrity check\n if (field.componentId === 'one-to-one' && typeof value === 'string') {\n const refModel = field.options?.reference?.form?.reference?.value\n if (refModel) {\n const integrityError = await this.checkRelationIntegrity(refModel, value)\n if (integrityError) {\n errors.push({ fieldId: field.fieldId, message: integrityError, rule: 'relation-integrity' })\n }\n }\n }\n\n if (field.componentId === 'one-to-many' && Array.isArray(value)) {\n const refModel = field.options?.reference?.form?.reference?.value\n if (refModel) {\n for (const refId of value) {\n if (typeof refId === 'string') {\n const integrityError = await this.checkRelationIntegrity(refModel, refId)\n if (integrityError) {\n errors.push({ fieldId: field.fieldId, message: integrityError, rule: 'relation-integrity' })\n }\n }\n }\n }\n }\n\n // Media path check\n if (field.componentId === 'media' && typeof value === 'string' && value !== '') {\n const assetError = await this.checkAssetExists(value)\n if (assetError) {\n errors.push({ fieldId: field.fieldId, message: assetError, rule: 'asset-exists' })\n }\n }\n }\n\n // Unique field check\n if (!options?.isUpdate) {\n const uniqueFields = nonSystemFields.filter(f => f.validations?.['unique-field']?.value)\n for (const field of uniqueFields) {\n const value = data[field.fieldId]\n if (value === undefined || value === null) continue\n\n const uniqueError = await this.checkUniqueness(modelId, field.fieldId, value)\n if (uniqueError) {\n errors.push({ fieldId: field.fieldId, message: uniqueError, rule: 'unique-field' })\n }\n }\n }\n\n return {\n valid: errors.length === 0,\n errors,\n }\n }\n\n /**\n * Validate ID format.\n */\n validateId(id: string): ValidationResult {\n const error = validateId(id)\n if (error) {\n return { valid: false, errors: [{ fieldId: 'ID', message: error, rule: 'id-format' }] }\n }\n return { valid: true, errors: [] }\n }\n\n /**\n * Validate status value.\n */\n validateStatus(status: string): ValidationResult {\n const error = validateStatus(status)\n if (error) {\n return { valid: false, errors: [{ fieldId: 'status', message: error, rule: 'status-value' }] }\n }\n return { valid: true, errors: [] }\n }\n\n // ── Private Helpers ──\n\n private async checkRelationIntegrity(refModelId: string, refEntryId: string): Promise<string | null> {\n try {\n await this.modelManager.get(refModelId) // verify model exists\n // Try to find the entry in non-localized or any locale file\n const contentDir = join(this.basePath, refModelId)\n const { listJsonFiles } = await import('../../util/fs.js')\n const files = await listJsonFiles(contentDir)\n\n for (const file of files) {\n const entries = await readJsonFile<ContentEntry[]>(join(contentDir, `${file}.json`))\n if (entries.some(e => e.ID === refEntryId)) return null\n }\n\n return `Referenced entry ${refEntryId} not found in model ${refModelId}`\n }\n catch {\n return `Referenced model ${refModelId} not found`\n }\n }\n\n private async checkAssetExists(assetPath: string): Promise<string | null> {\n try {\n const assets = await readJsonFile<Array<{ path: string }>>(join(this.basePath, 'assets.json'))\n if (assets.some(a => a.path === assetPath)) return null\n return `Asset not found in registry: ${assetPath}`\n }\n catch {\n return `Could not read assets.json`\n }\n }\n\n private async checkUniqueness(modelId: string, fieldId: string, value: unknown): Promise<string | null> {\n try {\n const contentDir = join(this.basePath, modelId)\n const { listJsonFiles } = await import('../../util/fs.js')\n const files = await listJsonFiles(contentDir)\n\n for (const file of files) {\n const entries = await readJsonFile<ContentEntry[]>(join(contentDir, `${file}.json`))\n if (entries.some(e => e[fieldId] === value)) {\n return `${fieldId} value \"${value}\" already exists — must be unique`\n }\n }\n return null\n }\n catch {\n return null\n }\n }\n}\n","import type { ContentEntry } from '../../types/index.js'\n\n/**\n * Three-way structural merge for JSON arrays indexed by ID.\n *\n * Strategy:\n * - New entries (in ours but not base): APPEND to result\n * - Deleted entries (in base but not in ours): REMOVE from result\n * - Modified entries (same ID, different fields): apply our field-level changes over theirs\n * - Entries only in theirs (added by another writer): KEEP\n */\nexport function structuralMerge(\n base: ContentEntry[],\n ours: ContentEntry[],\n theirs: ContentEntry[],\n): ContentEntry[] {\n const baseMap = indexById(base)\n const oursMap = indexById(ours)\n\n const result: ContentEntry[] = []\n const processedIds = new Set<string>()\n\n // 1. Process entries from theirs (remote) as the base ordering\n for (const theirEntry of theirs) {\n const id = theirEntry.ID\n processedIds.add(id)\n\n const baseEntry = baseMap.get(id)\n const ourEntry = oursMap.get(id)\n\n if (!ourEntry && baseEntry) {\n // We deleted it — skip (don't include in result)\n continue\n }\n\n if (ourEntry && baseEntry) {\n // Both have it — merge field-level changes\n result.push(mergeEntry(baseEntry, ourEntry, theirEntry))\n }\n else {\n // We don't have it (and it wasn't in base) — it's theirs only, keep it\n result.push(theirEntry)\n }\n }\n\n // 2. Add our new entries (in ours but not in base and not in theirs)\n for (const ourEntry of ours) {\n if (!processedIds.has(ourEntry.ID)) {\n result.push(ourEntry)\n }\n }\n\n return result\n}\n\n/**\n * Merge a single entry using three-way field comparison.\n * Our changes take precedence over theirs when both modified the same field.\n */\nfunction mergeEntry(\n base: ContentEntry,\n ours: ContentEntry,\n theirs: ContentEntry,\n): ContentEntry {\n const merged: ContentEntry = { ...theirs }\n\n for (const key of Object.keys(ours)) {\n const baseVal = JSON.stringify(base[key])\n const ourVal = JSON.stringify(ours[key])\n const theirVal = JSON.stringify(theirs[key])\n\n if (ourVal !== baseVal && theirVal === baseVal) {\n // We changed it, they didn't — take ours\n merged[key] = ours[key]\n }\n else if (ourVal !== baseVal && theirVal !== baseVal) {\n // Both changed — our change wins (last-writer-wins for same field)\n merged[key] = ours[key]\n }\n // If only they changed, theirs is already in merged via spread\n }\n\n return merged\n}\n\nfunction indexById(entries: ContentEntry[]): Map<string, ContentEntry> {\n const map = new Map<string, ContentEntry>()\n for (const entry of entries) {\n map.set(entry.ID, entry)\n }\n return map\n}\n","import { join } from 'node:path'\nimport { tmpdir } from 'node:os'\nimport { randomBytes } from 'node:crypto'\nimport { simpleGit, type SimpleGit } from 'simple-git'\nimport type { WorktreeContext, WriterConfig, ContentEntry } from '../../types/index.js'\nimport { GitSyncError, PushRejectedError } from '../../errors.js'\nimport { readJsonFile, writeJsonFile } from '../../util/json.js'\nimport { removeDir } from '../../util/fs.js'\nimport { structuralMerge } from './merge.js'\n\nexport class GitSyncManager {\n private config: Required<Pick<WriterConfig, 'remote' | 'retryAttempts' | 'commitPrefix'>> & WriterConfig\n\n constructor(config: WriterConfig) {\n this.config = {\n ...config,\n remote: config.remote || 'origin',\n retryAttempts: config.retryAttempts || 3,\n commitPrefix: config.commitPrefix || '[contentrain]',\n }\n }\n\n /**\n * Create an isolated git worktree for safe operations.\n */\n async createWorktree(): Promise<WorktreeContext> {\n const id = randomBytes(4).toString('hex')\n const worktreePath = join(tmpdir(), `contentrain-mcp-${id}`)\n const git = simpleGit(this.config.repoPath)\n\n try {\n // Fetch latest\n await git.fetch(this.config.remote, this.config.branch)\n\n // Create worktree from remote branch\n const remoteBranch = `${this.config.remote}/${this.config.branch}`\n const localBranch = `contentrain-work-${id}`\n\n await git.raw([\n 'worktree', 'add',\n '-b', localBranch,\n worktreePath,\n remoteBranch,\n ])\n\n return {\n path: worktreePath,\n branch: localBranch,\n id,\n }\n }\n catch (err) {\n throw new GitSyncError(`Failed to create worktree: ${err}`, {\n branch: this.config.branch,\n worktreePath,\n })\n }\n }\n\n /**\n * Commit all changes in the worktree.\n */\n async commit(ctx: WorktreeContext, message: string): Promise<string> {\n const git = simpleGit(ctx.path)\n const fullMessage = `${this.config.commitPrefix} ${message}`\n\n await git.add('-A')\n\n // Check if there are actual changes\n const status = await git.status()\n if (status.files.length === 0) {\n return '' // Nothing to commit\n }\n\n const authorArgs: string[] = []\n if (this.config.author) {\n authorArgs.push('--author', `${this.config.author.name} <${this.config.author.email}>`)\n }\n\n const result = await git.commit(fullMessage, undefined, Object.fromEntries(\n authorArgs.length ? [['--author', `${this.config.author!.name} <${this.config.author!.email}>`]] : [],\n ))\n\n return result.commit\n }\n\n /**\n * Push with rebase-retry on conflict.\n */\n async pushWithRetry(ctx: WorktreeContext): Promise<void> {\n const git = simpleGit(ctx.path)\n const maxAttempts = this.config.retryAttempts\n\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n // Push our local branch to the remote target branch\n await git.push(this.config.remote, `${ctx.branch}:${this.config.branch}`)\n return // Success\n }\n catch {\n if (attempt === maxAttempts) {\n throw new PushRejectedError(true)\n }\n\n // Fetch latest and try structural merge\n try {\n await this.resolveAndRetry(ctx)\n }\n catch (mergeErr) {\n if (attempt === maxAttempts) {\n throw new GitSyncError(`Failed to resolve conflicts after ${maxAttempts} attempts`, {\n originalError: String(mergeErr),\n })\n }\n }\n }\n }\n }\n\n /**\n * Cleanup: remove the worktree.\n */\n async cleanup(ctx: WorktreeContext): Promise<void> {\n try {\n const git = simpleGit(this.config.repoPath)\n await git.raw(['worktree', 'remove', ctx.path, '--force'])\n }\n catch {\n // Force cleanup if git worktree remove fails\n await removeDir(ctx.path)\n }\n\n // Remove the temporary branch\n try {\n const git = simpleGit(this.config.repoPath)\n await git.raw(['branch', '-D', ctx.branch])\n }\n catch {\n // Branch may already be gone\n }\n }\n\n /**\n * Run a full transaction: worktree → execute → commit → push → cleanup.\n */\n async transaction<T>(\n fn: (worktreePath: string) => Promise<T>,\n message: string,\n ): Promise<T> {\n const ctx = await this.createWorktree()\n\n try {\n const result = await fn(ctx.path)\n\n const commitHash = await this.commit(ctx, message)\n if (commitHash) {\n await this.pushWithRetry(ctx)\n }\n\n return result\n }\n finally {\n await this.cleanup(ctx)\n }\n }\n\n /**\n * Run a staging transaction: worktree → execute → commit → push to staging branch → cleanup.\n * Changes are pushed to a separate branch instead of the target branch.\n */\n async stagingTransaction<T>(\n fn: (worktreePath: string) => Promise<T>,\n message: string,\n stagingBranch: string,\n ): Promise<{ result: T; hasChanges: boolean }> {\n const ctx = await this.createWorktree()\n\n try {\n const result = await fn(ctx.path)\n\n const commitHash = await this.commit(ctx, message)\n if (commitHash) {\n const git = simpleGit(ctx.path)\n try {\n await git.push(this.config.remote, `${ctx.branch}:${stagingBranch}`)\n }\n catch (err) {\n throw new GitSyncError(`Failed to push to staging branch \"${stagingBranch}\"`, {\n branch: stagingBranch,\n originalError: String(err),\n })\n }\n }\n\n return { result, hasChanges: !!commitHash }\n }\n finally {\n await this.cleanup(ctx)\n }\n }\n\n /**\n * Merge a remote staging branch into the target branch.\n * Tries fast-forward first, falls back to merge commit.\n */\n async mergeStagingToTarget(stagingBranch: string): Promise<string> {\n const git = simpleGit(this.config.repoPath)\n const remote = this.config.remote\n\n // Verify staging branch exists on remote\n await git.fetch(remote, stagingBranch)\n try {\n await git.raw(['rev-parse', '--verify', `${remote}/${stagingBranch}`])\n }\n catch {\n throw new GitSyncError(`Staging branch \"${stagingBranch}\" not found on remote \"${remote}\"`, {\n branch: stagingBranch,\n })\n }\n\n // Fetch target branch too\n await git.fetch(remote, this.config.branch)\n\n // Create worktree from target branch, merge staging into it\n const ctx = await this.createWorktree()\n\n try {\n const wtGit = simpleGit(ctx.path)\n\n // Try fast-forward first\n try {\n await wtGit.merge([`${remote}/${stagingBranch}`, '--ff-only'])\n }\n catch {\n await wtGit.merge([\n `${remote}/${stagingBranch}`,\n '-m', `${this.config.commitPrefix} apply doctor fix from ${stagingBranch}`,\n ])\n }\n\n // Push merged result to target branch\n await this.pushWithRetry(ctx)\n\n // Delete remote staging branch\n try {\n await git.push(remote, `:${stagingBranch}`)\n }\n catch {\n // Non-fatal\n }\n\n const log = await wtGit.log({ maxCount: 1 })\n return log.latest?.hash || ''\n }\n finally {\n await this.cleanup(ctx)\n }\n }\n\n /**\n * Delete a remote staging branch without merging.\n */\n async deleteStagingBranch(stagingBranch: string): Promise<void> {\n const git = simpleGit(this.config.repoPath)\n try {\n await git.push(this.config.remote, `:${stagingBranch}`)\n }\n catch (err) {\n throw new GitSyncError(`Failed to delete staging branch \"${stagingBranch}\"`, {\n branch: stagingBranch,\n originalError: String(err),\n })\n }\n }\n\n // ── Private ──\n\n /**\n * Resolve push conflicts via structural JSON merge.\n */\n private async resolveAndRetry(ctx: WorktreeContext): Promise<void> {\n const git = simpleGit(ctx.path)\n const remoteBranch = `${this.config.remote}/${this.config.branch}`\n\n // Fetch latest remote state\n await git.fetch(this.config.remote, this.config.branch)\n\n // Find merge base\n const mergeBase = await git.raw(['merge-base', ctx.branch, remoteBranch])\n const baseCommit = mergeBase.trim()\n\n // Get list of conflicting files (JSON files in contentrain/)\n const diffOutput = await git.raw(['diff', '--name-only', `${remoteBranch}...${ctx.branch}`])\n const changedFiles = diffOutput.trim().split('\\n').filter((f: string) => f.endsWith('.json'))\n\n // For each changed JSON file, do structural merge\n for (const file of changedFiles) {\n if (!file.includes('contentrain/') && !file.startsWith('contentrain/')) continue\n\n try {\n // Read all three versions\n const basePath = join(ctx.path, file)\n const base = await this.readGitVersion<ContentEntry[]>(git, baseCommit, file)\n const ours = await readJsonFile<ContentEntry[]>(basePath)\n const theirsRaw = await git.raw(['show', `${remoteBranch}:${file}`])\n const theirs = JSON.parse(theirsRaw) as ContentEntry[]\n\n // Structural merge\n const merged = structuralMerge(base, ours, theirs)\n await writeJsonFile(basePath, merged)\n }\n catch {\n // If structural merge fails for a file, keep our version\n }\n }\n\n // Reset to remote and apply our merged state\n await git.raw(['reset', '--soft', remoteBranch])\n await git.add('-A')\n\n const status = await git.status()\n if (status.files.length > 0) {\n await git.commit(`${this.config.commitPrefix} merge: resolve concurrent changes`)\n }\n }\n\n private async readGitVersion<T>(git: SimpleGit, commit: string, filePath: string): Promise<T> {\n try {\n const raw = await git.raw(['show', `${commit}:${filePath}`])\n return JSON.parse(raw) as T\n }\n catch {\n return [] as unknown as T\n }\n }\n}\n","import { join } from 'node:path'\nimport { mkdtemp, cp } from 'node:fs/promises'\nimport { tmpdir } from 'node:os'\nimport type { WriterConfig } from '../types/index.js'\nimport { ModelManager } from './model/manager.js'\nimport { ContentManager } from './content/manager.js'\nimport { AssetManager } from './asset/manager.js'\nimport { Validator } from './validation/validator.js'\nimport { GitSyncManager } from './git/sync.js'\nimport { removeDir } from '../util/fs.js'\n\n/**\n * Main orchestrator — coordinates all managers and git sync.\n *\n * Three modes:\n * 1. Read mode: reads from repoPath directly (no git sync)\n * 2. Transaction mode: creates a worktree, applies changes, commits + pushes\n * 3. Dry-run mode: copies contentrain dir to temp, applies changes there, then discards\n */\nexport class ContentrainWriter {\n private config: WriterConfig\n private contentrainDir: string\n\n // Direct-mode managers (operate on repoPath — reads only)\n readonly model: ModelManager\n readonly content: ContentManager\n readonly asset: AssetManager\n readonly validator: Validator\n private gitSync: GitSyncManager\n\n constructor(config: WriterConfig) {\n this.config = config\n this.contentrainDir = config.contentrainDir || 'contentrain'\n const basePath = join(config.repoPath, this.contentrainDir)\n\n this.model = new ModelManager(basePath)\n this.content = new ContentManager(basePath, config.repoPath)\n this.asset = new AssetManager(basePath, config.repoPath, config)\n this.validator = new Validator(basePath)\n this.gitSync = new GitSyncManager(config)\n }\n\n /**\n * Execute operations in a git-synced transaction.\n * Creates worktree → runs operations → validates → commits → pushes.\n * In dry-run mode, copies to a temp directory and discards after execution.\n */\n async transaction<T>(\n fn: (managers: TransactionManagers) => Promise<T>,\n options?: { message?: string },\n ): Promise<T> {\n if (this.config.dryRun) {\n return this.dryRunTransaction(fn)\n }\n\n return this.gitSync.transaction(\n async (worktreePath: string) => {\n const managers = this.createManagers(worktreePath)\n return fn(managers)\n },\n options?.message || 'content update',\n )\n }\n\n /**\n * Execute operations and push to a staging branch instead of the target branch.\n * Returns the result and whether any changes were pushed.\n */\n async stagingTransaction<T>(\n fn: (managers: TransactionManagers) => Promise<T>,\n options: { message: string; stagingBranch: string },\n ): Promise<{ result: T; hasChanges: boolean }> {\n if (this.config.dryRun) {\n const result = await this.dryRunTransaction(fn)\n return { result, hasChanges: false }\n }\n\n return this.gitSync.stagingTransaction(\n async (worktreePath: string) => {\n const managers = this.createManagers(worktreePath)\n return fn(managers)\n },\n options.message,\n options.stagingBranch,\n )\n }\n\n /**\n * Merge a staging branch into the target branch.\n */\n async applyStagingBranch(stagingBranch: string): Promise<string> {\n return this.gitSync.mergeStagingToTarget(stagingBranch)\n }\n\n /**\n * Delete a staging branch without merging.\n */\n async discardStagingBranch(stagingBranch: string): Promise<void> {\n return this.gitSync.deleteStagingBranch(stagingBranch)\n }\n\n /**\n * Dry-run: copy contentrain dir to temp, run operations there, then discard.\n * The original repository is never modified.\n */\n private async dryRunTransaction<T>(\n fn: (managers: TransactionManagers) => Promise<T>,\n ): Promise<T> {\n const tempRepoPath = await mkdtemp(join(tmpdir(), 'contentrain-dry-'))\n const tempContentrainDir = join(tempRepoPath, this.contentrainDir)\n const sourceContentrainDir = join(this.config.repoPath, this.contentrainDir)\n\n try {\n await cp(sourceContentrainDir, tempContentrainDir, { recursive: true })\n const managers = this.createManagers(tempRepoPath)\n return await fn(managers)\n }\n finally {\n await removeDir(tempRepoPath)\n }\n }\n\n /**\n * Create managers for a specific repo path (used in transactions).\n */\n private createManagers(repoPath: string): TransactionManagers {\n const basePath = join(repoPath, this.contentrainDir)\n return {\n basePath,\n repoPath,\n model: new ModelManager(basePath),\n content: new ContentManager(basePath, repoPath),\n asset: new AssetManager(basePath, repoPath, this.config),\n validator: new Validator(basePath),\n }\n }\n}\n\nexport interface TransactionManagers {\n basePath: string\n repoPath: string\n model: ModelManager\n content: ContentManager\n asset: AssetManager\n validator: Validator\n}\n","import { z } from 'zod'\nimport type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { ContentrainWriter } from '../core/writer.js'\n\nexport function registerModelTools(server: McpServer, writer: ContentrainWriter): void {\n // ── List Models ──\n server.registerTool(\n 'contentrain_list_models',\n {\n description: 'List all Contentrain models with their metadata (type, localization, etc.)',\n },\n async () => {\n try {\n const models = await writer.model.list()\n return {\n content: [{ type: 'text' as const, text: JSON.stringify(models, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Describe Model ──\n server.registerTool(\n 'contentrain_describe_model',\n {\n description: 'Get detailed schema for a model including all field definitions, types, validations, and options. Call this BEFORE creating or updating content to understand required fields.',\n inputSchema: {\n modelId: z.string().describe('The model ID to describe'),\n },\n },\n async ({ modelId }) => {\n try {\n const desc = await writer.model.describe(modelId)\n return {\n content: [{ type: 'text' as const, text: JSON.stringify(desc, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Create Model ──\n server.registerTool(\n 'contentrain_create_model',\n {\n description: 'Create a new Contentrain model with field definitions. System fields (ID, createdAt, updatedAt, status) are auto-added. For MD/MDX models, slug and content fields are also auto-added.',\n inputSchema: {\n name: z.string().describe('Display name of the model'),\n modelId: z.string().regex(/^[a-z0-9-]+$/).describe('Unique model ID in kebab-case'),\n type: z.enum(['JSON', 'MD', 'MDX']).describe('Content type'),\n localization: z.boolean().describe('Whether the model supports multiple languages'),\n description: z.string().optional().describe('Model description'),\n path: z.string().optional().describe('Base path for MD/MDX files'),\n fields: z.array(z.object({\n name: z.string(),\n fieldId: z.string(),\n componentId: z.string(),\n fieldType: z.string(),\n options: z.record(z.unknown()).optional(),\n validations: z.record(z.unknown()).optional(),\n })).optional().describe('Custom field definitions (system fields are auto-added)'),\n },\n },\n async (params) => {\n try {\n const result = await writer.transaction(async (tx) => {\n return tx.model.create({\n name: params.name,\n modelId: params.modelId,\n type: params.type,\n localization: params.localization,\n description: params.description,\n path: params.path,\n fields: params.fields as any,\n })\n }, { message: `create model: ${params.modelId}` })\n\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: true, model: result }, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Add Field ──\n server.registerTool(\n 'contentrain_add_field',\n {\n description: 'Add a new field to an existing model.',\n inputSchema: {\n modelId: z.string().describe('Target model ID'),\n name: z.string().describe('Display name of the field'),\n fieldId: z.string().describe('Unique field identifier'),\n componentId: z.string().describe('UI component (single-line-text, integer, one-to-one, media, etc.)'),\n fieldType: z.string().describe('Data type (string, number, boolean, relation, media, date, array)'),\n options: z.record(z.unknown()).optional().describe('Field options (title-field, default-value, reference, etc.)'),\n validations: z.record(z.unknown()).optional().describe('Field validations (required-field, unique-field)'),\n },\n },\n async (params) => {\n try {\n const result = await writer.transaction(async (tx) => {\n return tx.model.addField(params.modelId, {\n name: params.name,\n fieldId: params.fieldId,\n componentId: params.componentId as any,\n fieldType: params.fieldType as any,\n options: (params.options || {}) as any,\n validations: (params.validations || {}) as any,\n })\n }, { message: `add field ${params.fieldId} to ${params.modelId}` })\n\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: true, field: result }, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Delete Model ──\n server.registerTool(\n 'contentrain_delete_model',\n {\n description: 'Delete a model and all its content. WARNING: This is destructive and cannot be undone.',\n annotations: { destructiveHint: true },\n inputSchema: {\n modelId: z.string().describe('Model ID to delete'),\n },\n },\n async ({ modelId }) => {\n try {\n await writer.transaction(async (tx) => {\n await tx.model.delete(modelId)\n }, { message: `delete model: ${modelId}` })\n\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: true, deleted: modelId }) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n}\n","import { z } from 'zod'\nimport type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { ContentrainWriter } from '../core/writer.js'\n\nexport function registerContentTools(server: McpServer, writer: ContentrainWriter): void {\n // ── List Content ──\n server.registerTool(\n 'contentrain_list_content',\n {\n description: 'List all content entries for a model. For localized models, specify a locale.',\n annotations: { readOnlyHint: true },\n inputSchema: {\n modelId: z.string().describe('Model ID to list content from'),\n locale: z.string().optional().describe('Locale code (e.g., \"en\", \"tr\") for localized models'),\n },\n },\n async ({ modelId, locale }) => {\n try {\n const entries = await writer.content.list(modelId, locale)\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ total: entries.length, entries }, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Get Content ──\n server.registerTool(\n 'contentrain_get_content',\n {\n description: 'Get a single content entry by ID.',\n annotations: { readOnlyHint: true },\n inputSchema: {\n modelId: z.string().describe('Model ID'),\n entryId: z.string().describe('Entry ID (12-char hex)'),\n locale: z.string().optional().describe('Locale code for localized models'),\n },\n },\n async ({ modelId, entryId, locale }) => {\n try {\n const entry = await writer.content.get(modelId, entryId, locale)\n return {\n content: [{ type: 'text' as const, text: JSON.stringify(entry, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Create Content ──\n server.registerTool(\n 'contentrain_create_content',\n {\n description: 'Create a new content entry. System fields (ID, createdAt, updatedAt, status) are auto-generated. For localized models, specify locale to create in that language. Call contentrain_describe_model first to know required fields.',\n inputSchema: {\n modelId: z.string().describe('Target model ID'),\n data: z.record(z.unknown()).describe('Content data object with field values matching model schema'),\n locale: z.string().optional().describe('Locale code (e.g., \"en\") — required for localized models'),\n status: z.enum(['draft', 'publish']).optional().describe('Entry status (default: \"draft\")'),\n content: z.string().optional().describe('Markdown body content (only for MD/MDX models)'),\n },\n },\n async (params) => {\n try {\n const result = await writer.transaction(async (tx) => {\n const validation = await tx.validator.validate(params.modelId, params.data as Record<string, unknown>)\n if (!validation.valid) {\n throw new Error(`Validation failed:\\n${validation.errors.map(e => ` - ${e.fieldId}: ${e.message}`).join('\\n')}`)\n }\n\n return tx.content.create(params.modelId, params.data as Record<string, unknown>, {\n locale: params.locale,\n status: params.status,\n content: params.content,\n })\n }, { message: `create ${params.modelId} entry` })\n\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: true, id: result }, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Update Content ──\n server.registerTool(\n 'contentrain_update_content',\n {\n description: 'Update an existing content entry. Only provide fields you want to change. updatedAt is auto-set.',\n inputSchema: {\n modelId: z.string().describe('Model ID'),\n entryId: z.string().describe('Entry ID to update (12-char hex)'),\n data: z.record(z.unknown()).describe('Fields to update'),\n locale: z.string().optional().describe('Locale code for localized models'),\n content: z.string().optional().describe('Updated markdown body (only for MD/MDX models)'),\n },\n },\n async (params) => {\n try {\n const result = await writer.transaction(async (tx) => {\n const validation = await tx.validator.validate(\n params.modelId,\n params.data as Record<string, unknown>,\n { isUpdate: true },\n )\n if (!validation.valid) {\n throw new Error(`Validation failed:\\n${validation.errors.map(e => ` - ${e.fieldId}: ${e.message}`).join('\\n')}`)\n }\n\n return tx.content.update(params.modelId, params.entryId, params.data as Record<string, unknown>, {\n locale: params.locale,\n content: params.content,\n })\n }, { message: `update ${params.modelId}/${params.entryId}` })\n\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: true, entry: result }, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Delete Content ──\n server.registerTool(\n 'contentrain_delete_content',\n {\n description: 'Delete a content entry by ID. For localized models, removes from all locale files. For MD/MDX, also deletes markdown files.',\n annotations: { destructiveHint: true },\n inputSchema: {\n modelId: z.string().describe('Model ID'),\n entryId: z.string().describe('Entry ID to delete (12-char hex)'),\n },\n },\n async ({ modelId, entryId }) => {\n try {\n await writer.transaction(async (tx) => {\n await tx.content.delete(modelId, entryId)\n }, { message: `delete ${modelId}/${entryId}` })\n\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: true, deleted: entryId }) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Validate Content ──\n server.registerTool(\n 'contentrain_validate',\n {\n description: 'Validate content data against model schema without writing. Use for dry-run checks.',\n annotations: { readOnlyHint: true },\n inputSchema: {\n modelId: z.string().describe('Model ID to validate against'),\n data: z.record(z.unknown()).describe('Content data to validate'),\n },\n },\n async ({ modelId, data }) => {\n try {\n const result = await writer.validator.validate(modelId, data as Record<string, unknown>)\n return {\n content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n}\n","import { z } from 'zod'\nimport type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { ContentrainWriter } from '../core/writer.js'\n\nexport function registerAssetTools(server: McpServer, writer: ContentrainWriter): void {\n // ── List Assets ──\n server.registerTool(\n 'contentrain_list_assets',\n {\n description: 'List all registered assets in assets.json.',\n annotations: { readOnlyHint: true },\n },\n async () => {\n try {\n const assets = await writer.asset.list()\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ total: assets.length, assets }, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Register Asset ──\n server.registerTool(\n 'contentrain_register_asset',\n {\n description: 'Register a new asset in assets.json. The file must already exist at the specified path in the repository.',\n inputSchema: {\n path: z.string().describe('Relative path to the asset file (e.g., \"contentrain/static/hero.png\")'),\n mimetype: z.string().describe('MIME type (e.g., \"image/png\", \"image/jpeg\", \"application/pdf\")'),\n alt: z.string().optional().describe('Alt text for the asset'),\n },\n },\n async (params) => {\n try {\n const result = await writer.transaction(async (tx) => {\n return tx.asset.register({\n path: params.path,\n mimetype: params.mimetype,\n alt: params.alt,\n })\n }, { message: `register asset: ${params.path}` })\n\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: true, asset: result }, null, 2) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n // ── Deregister Asset ──\n server.registerTool(\n 'contentrain_deregister_asset',\n {\n description: 'Remove an asset from assets.json registry. Does NOT delete the physical file.',\n annotations: { destructiveHint: true },\n inputSchema: {\n path: z.string().describe('Asset path to deregister'),\n },\n },\n async ({ path }) => {\n try {\n await writer.transaction(async (tx) => {\n await tx.asset.deregister(path)\n }, { message: `deregister asset: ${path}` })\n\n return {\n content: [{ type: 'text' as const, text: JSON.stringify({ success: true, deregistered: path }) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n}\n","import { join } from 'node:path'\nimport { readFile } from 'node:fs/promises'\nimport type {\n ModelMetadata,\n FieldDefinition,\n ContentEntry,\n AssetEntry,\n MDContentEntry,\n} from '../types/index.js'\nimport { readJsonFile, writeJsonFile } from '../util/json.js'\nimport { listJsonFiles, fileExists } from '../util/fs.js'\nimport { isValidId, generateId } from '../util/id.js'\nimport { getExpectedType, validateFieldValue, validateInteger } from './validation/rules.js'\n\n// ── Types ──\n\nexport type IssueSeverity = 'error' | 'warning'\n\nexport type IssueCategory =\n | 'structure'\n | 'id'\n | 'schema'\n | 'relation'\n | 'asset'\n | 'sync'\n\nexport interface DoctorIssue {\n severity: IssueSeverity\n category: IssueCategory\n modelId?: string\n entryId?: string\n field?: string\n file?: string\n message: string\n fix?: string\n}\n\nexport interface DoctorResult {\n summary: {\n total: number\n errors: number\n warnings: number\n fixed: number\n models: number\n entries: number\n }\n issues: DoctorIssue[]\n}\n\nexport interface DoctorOptions {\n fix?: boolean\n modelId?: string\n}\n\n// Internal tracking for loaded content files\ninterface ContentFile {\n modelId: string\n fileName: string\n filePath: string\n entries: ContentEntry[]\n dirty: boolean\n}\n\n// ── Doctor Engine ──\n\nexport class Doctor {\n private issues: DoctorIssue[] = []\n private fixed = 0\n private totalEntries = 0\n private scannedEntries = 0\n\n // ID remap: oldId → newId (for relation propagation and locale consistency)\n private idRemap = new Map<string, string>()\n\n // All loaded content files (for cross-model relation propagation)\n private contentFiles: ContentFile[] = []\n\n // All field definitions by modelId (for relation field lookup)\n private fieldsByModel = new Map<string, FieldDefinition[]>()\n\n constructor(\n private basePath: string,\n private repoPath: string,\n ) {}\n\n async diagnose(options: DoctorOptions = {}): Promise<DoctorResult> {\n this.issues = []\n this.fixed = 0\n this.totalEntries = 0\n this.scannedEntries = 0\n this.idRemap = new Map()\n this.contentFiles = []\n this.fieldsByModel = new Map()\n\n const fix = options.fix ?? false\n\n // 1. Check metadata.json is valid\n const models = await this.checkMetadata()\n if (!models) {\n return this.buildResult(0)\n }\n\n // 2. Filter to specific model if requested\n const targetModels = options.modelId\n ? models.filter(m => m.modelId === options.modelId)\n : models\n\n if (options.modelId && targetModels.length === 0) {\n this.addIssue('error', 'structure', `Model \"${options.modelId}\" not found in metadata.json`)\n return this.buildResult(models.length)\n }\n\n // 3. Check structure: field files exist for all models\n await this.checkFieldFiles(models, targetModels)\n\n // 4. Load assets once for media checks\n const assets = await this.loadAssets()\n\n // 5. Load ALL content files upfront (needed for relation propagation in fix mode)\n await this.loadAllContentFiles(fix ? models : targetModels)\n\n // 6. Load ALL field definitions (needed for relation propagation)\n for (const model of models) {\n try {\n const fields = await readJsonFile<FieldDefinition[]>(join(this.basePath, 'models', `${model.modelId}.json`))\n this.fieldsByModel.set(model.modelId, fields)\n }\n catch {\n // Already reported in checkFieldFiles\n }\n }\n\n // 7. Count scanned entries (only target models)\n for (const cf of this.contentFiles) {\n if (targetModels.some(m => m.modelId === cf.modelId)) {\n this.scannedEntries += cf.entries.length\n }\n }\n\n // 8. For each target model, run all checks (collects ID remaps but doesn't write yet)\n for (const model of targetModels) {\n this.checkModelStructure(model)\n this.checkModelContent(model, assets, fix)\n // MD/MDX sync needs async file checks\n if (model.type === 'MD' || model.type === 'MDX') {\n const modelFiles = this.contentFiles.filter(f => f.modelId === model.modelId)\n await this.checkMarkdownSync(model, modelFiles)\n }\n }\n\n // 9. Check orphan content directories\n if (!options.modelId) {\n await this.checkOrphanDirectories(models)\n }\n\n // 10. Propagate ID changes to all relation fields across ALL models\n if (fix && this.idRemap.size > 0) {\n this.propagateIdChanges()\n }\n\n // 11. Write all dirty content files\n if (fix) {\n await this.writeDirtyFiles()\n }\n\n return this.buildResult(targetModels.length)\n }\n\n // ── Content Loading ──\n\n private async loadAllContentFiles(models: ModelMetadata[]): Promise<void> {\n for (const model of models) {\n const contentDir = join(this.basePath, model.modelId)\n const fileNames = await listJsonFiles(contentDir)\n\n for (const fileName of fileNames) {\n const filePath = join(contentDir, `${fileName}.json`)\n let entries: ContentEntry[]\n try {\n const raw = await readFile(filePath, 'utf-8')\n entries = JSON.parse(raw) as ContentEntry[]\n if (!Array.isArray(entries)) {\n this.addIssue('error', 'structure', 'Content file is not an array', model.modelId, undefined, undefined, `${model.modelId}/${fileName}.json`)\n continue\n }\n }\n catch {\n this.addIssue('error', 'structure', 'Invalid JSON in content file', model.modelId, undefined, undefined, `${model.modelId}/${fileName}.json`)\n continue\n }\n\n this.totalEntries += entries.length\n this.contentFiles.push({\n modelId: model.modelId,\n fileName,\n filePath,\n entries,\n dirty: false,\n })\n }\n }\n }\n\n // ── Structural Checks ──\n\n private async checkMetadata(): Promise<ModelMetadata[] | null> {\n const metadataPath = join(this.basePath, 'models', 'metadata.json')\n try {\n const raw = await readFile(metadataPath, 'utf-8')\n try {\n const models = JSON.parse(raw) as ModelMetadata[]\n if (!Array.isArray(models)) {\n this.addIssue('error', 'structure', 'metadata.json is not an array', undefined, undefined, undefined, 'models/metadata.json')\n return null\n }\n\n // Check for duplicate modelIds in metadata\n const seen = new Map<string, number>()\n for (const m of models) {\n const count = (seen.get(m.modelId) ?? 0) + 1\n seen.set(m.modelId, count)\n }\n for (const [modelId, count] of seen) {\n if (count > 1) {\n this.addIssue('error', 'structure', `Duplicate modelId \"${modelId}\" in metadata.json (${count} times)`, modelId, undefined, undefined, 'models/metadata.json')\n }\n }\n\n return models\n }\n catch {\n this.addIssue('error', 'structure', 'metadata.json contains invalid JSON', undefined, undefined, undefined, 'models/metadata.json')\n return null\n }\n }\n catch {\n this.addIssue('error', 'structure', 'metadata.json not found — is this a Contentrain project?', undefined, undefined, undefined, 'models/metadata.json')\n return null\n }\n }\n\n private async checkFieldFiles(allModels: ModelMetadata[], targetModels: ModelMetadata[]): Promise<void> {\n for (const model of targetModels) {\n const fieldPath = join(this.basePath, 'models', `${model.modelId}.json`)\n if (!await fileExists(fieldPath)) {\n this.addIssue('error', 'structure', `Field definition file missing for model \"${model.modelId}\"`, model.modelId, undefined, undefined, `models/${model.modelId}.json`)\n }\n }\n\n // Check for field files without metadata entry\n const metaIds = new Set(allModels.map(m => m.modelId))\n const fieldFiles = await listJsonFiles(join(this.basePath, 'models'))\n for (const file of fieldFiles) {\n if (file === 'metadata') continue\n if (!metaIds.has(file)) {\n this.addIssue('warning', 'structure', `Field definition file \"${file}.json\" has no matching model in metadata.json`, file, undefined, undefined, `models/${file}.json`)\n }\n }\n }\n\n private async checkOrphanDirectories(models: ModelMetadata[]): Promise<void> {\n const modelIds = new Set(models.map(m => m.modelId))\n const { readdir, stat } = await import('node:fs/promises')\n\n try {\n const entries = await readdir(this.basePath)\n for (const entry of entries) {\n if (entry === 'models' || entry === 'assets.json' || entry === 'static' || entry.startsWith('.')) continue\n const fullPath = join(this.basePath, entry)\n const s = await stat(fullPath)\n if (s.isDirectory() && !modelIds.has(entry)) {\n this.addIssue('warning', 'structure', `Content directory \"${entry}/\" has no matching model in metadata.json`, entry)\n }\n }\n }\n catch {\n // basePath doesn't exist or can't be read\n }\n }\n\n // ── Model Checks ──\n\n private checkModelStructure(model: ModelMetadata): void {\n const fields = this.fieldsByModel.get(model.modelId)\n if (!fields) return\n\n // Check system fields exist in field definition\n const requiredSystemIds = ['createdAt', 'updatedAt', 'ID', 'status']\n for (const sysId of requiredSystemIds) {\n if (!fields.some(f => f.fieldId === sysId && f.system)) {\n this.addIssue('error', 'schema', `Missing system field \"${sysId}\" in field definitions`, model.modelId, undefined, undefined, `models/${model.modelId}.json`)\n }\n }\n\n if (model.type === 'MD' || model.type === 'MDX') {\n if (!fields.some(f => f.fieldId === 'slug')) {\n this.addIssue('error', 'schema', 'MD/MDX model missing required \"slug\" field', model.modelId, undefined, undefined, `models/${model.modelId}.json`)\n }\n if (!fields.some(f => f.fieldId === 'content')) {\n this.addIssue('warning', 'schema', 'MD/MDX model missing \"content\" field definition', model.modelId, undefined, undefined, `models/${model.modelId}.json`)\n }\n }\n }\n\n private checkModelContent(model: ModelMetadata, assets: AssetEntry[], fix: boolean): void {\n const fields = this.fieldsByModel.get(model.modelId)\n if (!fields) return\n\n const modelFiles = this.contentFiles.filter(f => f.modelId === model.modelId)\n\n if (modelFiles.length === 0 && !model.localization) {\n this.addIssue('warning', 'structure', `No content files found for model \"${model.modelId}\"`, model.modelId)\n return\n }\n\n for (const cf of modelFiles) {\n // Duplicate ID detection within same file\n const fileIdSet = new Map<string, number>()\n for (const entry of cf.entries) {\n const count = (fileIdSet.get(entry.ID) ?? 0) + 1\n fileIdSet.set(entry.ID, count)\n }\n\n for (const [id, count] of fileIdSet) {\n if (count > 1) {\n this.addIssue('error', 'id', `Duplicate ID \"${id}\" appears ${count} times in same file`, model.modelId, id, undefined, `${model.modelId}/${cf.fileName}.json`,\n fix ? 'Will regenerate duplicate IDs and update relations' : undefined)\n\n if (fix) {\n this.fixDuplicateIds(cf, id)\n }\n }\n }\n\n // Check each entry\n for (const entry of cf.entries) {\n this.checkEntry(model, fields, entry, assets, `${model.modelId}/${cf.fileName}.json`, fix, cf)\n }\n\n // Check unique constraints\n this.checkUniqueConstraints(model, fields, cf.entries, `${model.modelId}/${cf.fileName}.json`)\n }\n\n // Localized model: verify ID consistency across locale files\n if (model.localization && modelFiles.length > 1) {\n this.checkLocaleIdConsistency(model, modelFiles)\n }\n }\n\n // ── Entry-Level Checks ──\n\n private checkEntry(\n model: ModelMetadata,\n fields: FieldDefinition[],\n entry: ContentEntry,\n assets: AssetEntry[],\n file: string,\n fix: boolean,\n cf: ContentFile,\n ): void {\n const entryId = entry.ID\n\n // ID format\n if (!entryId || typeof entryId !== 'string') {\n this.addIssue('error', 'id', 'Entry missing ID field', model.modelId, undefined, 'ID', file)\n return\n }\n if (!isValidId(entryId)) {\n // Reuse existing remap for locale consistency: same old ID → same new ID across all locale files\n const existingNewId = this.idRemap.get(entryId)\n const newId = fix ? (existingNewId || generateId()) : undefined\n this.addIssue('error', 'id', `Invalid ID format \"${entryId}\" — must be 12-char lowercase hex`, model.modelId, entryId, 'ID', file,\n fix ? `Regenerated to \"${newId}\" and updating all relations` : undefined)\n if (fix && newId) {\n this.idRemap.set(entryId, newId)\n entry.ID = newId\n cf.dirty = true\n if (!existingNewId) this.fixed++ // only count once per unique old ID\n }\n }\n\n // System fields presence\n if (!entry.createdAt) {\n this.addIssue('error', 'schema', 'Missing createdAt', model.modelId, entryId, 'createdAt', file,\n fix ? 'Will set to current timestamp' : undefined)\n if (fix) {\n entry.createdAt = new Date().toISOString()\n cf.dirty = true\n this.fixed++\n }\n }\n if (!entry.updatedAt) {\n this.addIssue('error', 'schema', 'Missing updatedAt', model.modelId, entryId, 'updatedAt', file,\n fix ? 'Will set to current timestamp' : undefined)\n if (fix) {\n entry.updatedAt = new Date().toISOString()\n cf.dirty = true\n this.fixed++\n }\n }\n if (!entry.status) {\n this.addIssue('error', 'schema', 'Missing status', model.modelId, entryId, 'status', file,\n fix ? 'Will set to \"draft\"' : undefined)\n if (fix) {\n entry.status = 'draft'\n cf.dirty = true\n this.fixed++\n }\n }\n else if (!['draft', 'publish', 'changed'].includes(entry.status)) {\n this.addIssue('error', 'schema', `Invalid status \"${entry.status}\" — must be draft, publish, or changed`, model.modelId, entryId, 'status', file,\n fix ? 'Will set to \"draft\"' : undefined)\n if (fix) {\n entry.status = 'draft'\n cf.dirty = true\n this.fixed++\n }\n }\n\n // Validate createdAt/updatedAt are valid ISO dates\n if (entry.createdAt && isNaN(Date.parse(entry.createdAt))) {\n this.addIssue('error', 'schema', `Invalid createdAt date format \"${entry.createdAt}\"`, model.modelId, entryId, 'createdAt', file)\n }\n if (entry.updatedAt && isNaN(Date.parse(entry.updatedAt))) {\n this.addIssue('error', 'schema', `Invalid updatedAt date format \"${entry.updatedAt}\"`, model.modelId, entryId, 'updatedAt', file)\n }\n\n // Field-level checks (non-system fields)\n const nonSystemFields = fields.filter(f => !f.system)\n for (const field of nonSystemFields) {\n const value = entry[field.fieldId]\n\n // Required check\n if (field.validations?.['required-field']?.value) {\n if (value === undefined || value === null || value === '') {\n if (field.fieldId === 'content' && (model.type === 'MD' || model.type === 'MDX')) continue\n this.addIssue('error', 'schema', `Required field \"${field.fieldId}\" is empty`, model.modelId, entryId, field.fieldId, file)\n continue\n }\n }\n\n if (value === undefined || value === null) continue\n\n // Type check\n const expectedType = getExpectedType(field.componentId, field.fieldType)\n const typeError = validateFieldValue(value, expectedType, field.fieldId)\n if (typeError) {\n this.addIssue('error', 'schema', `Type mismatch: ${typeError}`, model.modelId, entryId, field.fieldId, file)\n continue\n }\n\n // Integer check\n if (field.componentId === 'integer') {\n const intError = validateInteger(value, field.fieldId)\n if (intError) {\n this.addIssue('error', 'schema', intError, model.modelId, entryId, field.fieldId, file)\n }\n }\n\n // Relation integrity\n if (field.componentId === 'one-to-one' && typeof value === 'string') {\n this.checkRelation(model.modelId, entryId, field, value, file)\n }\n if (field.componentId === 'one-to-many' && Array.isArray(value)) {\n for (const refId of value) {\n if (typeof refId === 'string') {\n this.checkRelation(model.modelId, entryId, field, refId, file)\n }\n else {\n this.addIssue('error', 'relation', 'one-to-many array contains non-string value', model.modelId, entryId, field.fieldId, file)\n }\n }\n }\n\n // Relation field missing reference config\n if ((field.componentId === 'one-to-one' || field.componentId === 'one-to-many') && !field.options?.reference?.form?.reference?.value) {\n this.addIssue('warning', 'relation', `Relation field \"${field.fieldId}\" missing options.reference.form.reference.value`, model.modelId, undefined, field.fieldId, `models/${model.modelId}.json`)\n }\n\n // Asset check\n if (field.componentId === 'media' && typeof value === 'string' && value !== '') {\n if (!assets.some(a => a.path === value)) {\n this.addIssue('error', 'asset', `Media path \"${value}\" not found in assets.json`, model.modelId, entryId, field.fieldId, file)\n }\n }\n }\n }\n\n private checkRelation(\n modelId: string,\n entryId: string,\n field: FieldDefinition,\n refId: string,\n file: string,\n ): void {\n const refModelId = field.options?.reference?.form?.reference?.value\n if (!refModelId) return\n\n // Validate referenced ID format\n if (!isValidId(refId)) {\n this.addIssue('error', 'relation', `Relation references invalid ID \"${refId}\"`, modelId, entryId, field.fieldId, file)\n return\n }\n\n // Check referenced entry exists in loaded content\n const refFiles = this.contentFiles.filter(f => f.modelId === refModelId)\n const found = refFiles.some(rf => rf.entries.some(e => e.ID === refId))\n\n if (!found) {\n this.addIssue('error', 'relation', `Referenced entry \"${refId}\" not found in model \"${refModelId}\"`, modelId, entryId, field.fieldId, file)\n }\n }\n\n // ── Cross-Entry Checks ──\n\n private checkUniqueConstraints(\n model: ModelMetadata,\n fields: FieldDefinition[],\n entries: ContentEntry[],\n file: string,\n ): void {\n const uniqueFields = fields.filter(f => f.validations?.['unique-field']?.value && !f.system)\n for (const field of uniqueFields) {\n const seen = new Map<unknown, string>()\n for (const entry of entries) {\n const value = entry[field.fieldId]\n if (value === undefined || value === null || value === '') continue\n const key = typeof value === 'object' ? JSON.stringify(value) : value\n const existing = seen.get(key)\n if (existing) {\n this.addIssue('error', 'schema', `Unique constraint violated: \"${field.fieldId}\" value \"${value}\" duplicated between entries ${existing} and ${entry.ID}`, model.modelId, entry.ID, field.fieldId, file)\n }\n else {\n seen.set(key, entry.ID)\n }\n }\n }\n }\n\n private checkLocaleIdConsistency(\n model: ModelMetadata,\n modelFiles: ContentFile[],\n ): void {\n if (modelFiles.length < 2) return\n\n const referenceFile = modelFiles[0]\n const referenceIds = new Set(referenceFile.entries.map(e => e.ID))\n\n for (let i = 1; i < modelFiles.length; i++) {\n const otherFile = modelFiles[i]\n const otherIds = new Set(otherFile.entries.map(e => e.ID))\n\n for (const id of referenceIds) {\n if (!otherIds.has(id)) {\n this.addIssue('error', 'id', `Entry \"${id}\" exists in ${referenceFile.fileName}.json but missing in ${otherFile.fileName}.json`, model.modelId, id, undefined, `${model.modelId}/${otherFile.fileName}.json`)\n }\n }\n\n for (const id of otherIds) {\n if (!referenceIds.has(id)) {\n this.addIssue('error', 'id', `Entry \"${id}\" exists in ${otherFile.fileName}.json but missing in ${referenceFile.fileName}.json`, model.modelId, id, undefined, `${model.modelId}/${referenceFile.fileName}.json`)\n }\n }\n }\n }\n\n // ── MD/MDX Sync ──\n\n private async checkMarkdownSync(model: ModelMetadata, modelFiles: ContentFile[]): Promise<void> {\n for (const cf of modelFiles) {\n const isLocalized = model.localization && cf.fileName !== model.modelId\n const locale = isLocalized ? cf.fileName : undefined\n\n for (const entry of cf.entries) {\n if (!('slug' in entry) || !entry.slug) {\n this.addIssue('error', 'sync', 'MD/MDX entry missing \"slug\" field', model.modelId, entry.ID, 'slug', `${model.modelId}/${cf.fileName}.json`)\n continue\n }\n\n const mdEntry = entry as MDContentEntry\n const ext = model.type === 'MDX' ? '.mdx' : '.md'\n const baseMdPath = mdEntry.path || model.path || ''\n const mdPath = locale\n ? join(this.repoPath, baseMdPath, locale, `${mdEntry.slug}${ext}`)\n : join(this.repoPath, baseMdPath, `${mdEntry.slug}${ext}`)\n\n if (!await fileExists(mdPath)) {\n this.addIssue('error', 'sync', `Markdown file missing for entry \"${mdEntry.slug}\"`, model.modelId, entry.ID, undefined, mdPath.replace(this.repoPath + '/', ''))\n }\n\n // Check content body is NOT stored in JSON index\n if ('content' in entry && entry.content && typeof entry.content === 'string' && (entry.content as string).length > 0) {\n this.addIssue('warning', 'sync', 'MD/MDX JSON index should not contain \"content\" body — body belongs in markdown file', model.modelId, entry.ID, 'content', `${model.modelId}/${cf.fileName}.json`)\n }\n }\n }\n }\n\n // ── Asset Checks ──\n\n private async loadAssets(): Promise<AssetEntry[]> {\n try {\n return await readJsonFile<AssetEntry[]>(join(this.basePath, 'assets.json'))\n }\n catch {\n this.addIssue('warning', 'structure', 'assets.json not found or invalid')\n return []\n }\n }\n\n // ── Fix: ID Propagation ──\n\n /**\n * After all ID fixes are collected in idRemap, propagate changes\n * to ALL relation fields across ALL loaded content files.\n *\n * This is the critical two-way binding: when entry A's ID changes,\n * every relation field in every model that points to A must be updated.\n */\n private propagateIdChanges(): void {\n if (this.idRemap.size === 0) return\n\n for (const cf of this.contentFiles) {\n const fields = this.fieldsByModel.get(cf.modelId)\n if (!fields) continue\n\n // Find relation fields in this model\n const relationFields = fields.filter(f =>\n f.componentId === 'one-to-one' || f.componentId === 'one-to-many',\n )\n if (relationFields.length === 0) continue\n\n for (const entry of cf.entries) {\n for (const field of relationFields) {\n const value = entry[field.fieldId]\n if (value === undefined || value === null) continue\n\n if (field.componentId === 'one-to-one' && typeof value === 'string') {\n const newId = this.idRemap.get(value)\n if (newId) {\n this.addIssue('warning', 'relation',\n `Updated relation \"${field.fieldId}\": \"${value}\" → \"${newId}\"`,\n cf.modelId, entry.ID, field.fieldId, `${cf.modelId}/${cf.fileName}.json`,\n 'Propagated ID change to relation')\n entry[field.fieldId] = newId\n cf.dirty = true\n this.fixed++\n }\n }\n\n if (field.componentId === 'one-to-many' && Array.isArray(value)) {\n let changed = false\n const updated = (value as string[]).map(refId => {\n const newId = this.idRemap.get(refId)\n if (newId) {\n changed = true\n return newId\n }\n return refId\n })\n if (changed) {\n this.addIssue('warning', 'relation',\n `Updated one-to-many relation \"${field.fieldId}\": propagated ${this.idRemap.size} ID change(s)`,\n cf.modelId, entry.ID, field.fieldId, `${cf.modelId}/${cf.fileName}.json`,\n 'Propagated ID changes to relation array')\n entry[field.fieldId] = updated\n cf.dirty = true\n this.fixed++\n }\n }\n }\n }\n }\n }\n\n // ── Fix: Duplicate IDs ──\n\n private fixDuplicateIds(cf: ContentFile, duplicateId: string): void {\n let first = true\n for (const entry of cf.entries) {\n if (entry.ID === duplicateId) {\n if (first) {\n first = false\n continue\n }\n const newId = generateId()\n this.idRemap.set(`${duplicateId}:dup:${newId}`, newId) // track but don't remap the original\n entry.ID = newId\n cf.dirty = true\n this.fixed++\n }\n }\n }\n\n // ── Write Back ──\n\n private async writeDirtyFiles(): Promise<void> {\n for (const cf of this.contentFiles) {\n if (cf.dirty) {\n await writeJsonFile(cf.filePath, cf.entries)\n }\n }\n }\n\n // ── Utilities ──\n\n private addIssue(\n severity: IssueSeverity,\n category: IssueCategory,\n message: string,\n modelId?: string,\n entryId?: string,\n field?: string,\n file?: string,\n fix?: string,\n ): void {\n this.issues.push({ severity, category, modelId, entryId, field, file, message, fix })\n }\n\n private buildResult(modelCount: number): DoctorResult {\n // Issues with a `fix` descriptor were repaired — don't count them as errors\n const unfixedErrors = this.issues.filter(i => i.severity === 'error' && !i.fix).length\n return {\n summary: {\n total: this.issues.length,\n errors: unfixedErrors,\n warnings: this.issues.filter(i => i.severity === 'warning').length,\n fixed: this.fixed,\n models: modelCount,\n entries: this.scannedEntries,\n },\n issues: this.issues,\n }\n }\n}\n","import { z } from 'zod'\nimport { join } from 'node:path'\nimport type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { ContentrainWriter } from '../core/writer.js'\nimport type { WriterConfig } from '../types/index.js'\nimport { Doctor, type DoctorResult } from '../core/doctor.js'\n\nexport function registerDoctorTools(server: McpServer, writer: ContentrainWriter, config: WriterConfig): void {\n const targetBranch = config.branch || 'contentrain'\n const stagingPrefix = `${targetBranch}--doctor-fix/`\n\n function isStagingBranch(branch: string): boolean {\n return branch.startsWith(stagingPrefix)\n }\n\n server.registerTool(\n 'contentrain_doctor',\n {\n description: [\n 'Run a full diagnostic scan on all Contentrain models, content, and assets.',\n 'Detects: duplicate/invalid IDs, type mismatches, broken relations, missing assets,',\n 'locale ID inconsistencies, orphan directories, MD/MDX sync issues, unique constraint',\n 'violations, missing system fields, and invalid JSON files.',\n 'Use fix=true to auto-repair fixable issues — changes are pushed to a staging branch',\n 'for review. Use contentrain_doctor_apply to merge the staging branch into the target.',\n 'Use modelId to scope the scan to a single model.',\n ].join(' '),\n annotations: { readOnlyHint: false },\n inputSchema: {\n fix: z.boolean().optional().default(false).describe('Auto-fix repairable issues (duplicate IDs, missing timestamps, invalid status). Pushes to a staging branch for review.'),\n modelId: z.string().optional().describe('Scope scan to a specific model ID. Omit to scan all models.'),\n },\n },\n async ({ fix, modelId }) => {\n try {\n if (fix) {\n const stagingBranch = `${stagingPrefix}${Date.now()}`\n const { result, hasChanges } = await writer.stagingTransaction(async (tx) => {\n const doctor = new Doctor(tx.basePath, tx.repoPath)\n return doctor.diagnose({ fix: true, modelId })\n }, {\n message: `doctor fix${modelId ? ` ${modelId}` : ''}`,\n stagingBranch,\n })\n\n return {\n content: [{\n type: 'text' as const,\n text: formatResult(result, hasChanges ? stagingBranch : undefined),\n }],\n }\n }\n\n // Read-only mode: scan directly\n const contentrainDir = config.contentrainDir || 'contentrain'\n const basePath = join(config.repoPath, contentrainDir)\n const doctor = new Doctor(basePath, config.repoPath)\n const result = await doctor.diagnose({ fix: false, modelId })\n\n return {\n content: [{ type: 'text' as const, text: formatResult(result) }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n server.registerTool(\n 'contentrain_doctor_apply',\n {\n description: [\n 'Apply a staged doctor fix by merging the staging branch into the target branch.',\n 'Use the stagingBranch value returned by contentrain_doctor fix=true.',\n 'Attempts fast-forward merge first, falls back to merge commit.',\n 'Deletes the staging branch after successful merge.',\n ].join(' '),\n annotations: { readOnlyHint: false },\n inputSchema: {\n branch: z.string().describe(`The staging branch name returned by contentrain_doctor fix=true (e.g., \"${stagingPrefix}1234567890\")`),\n },\n },\n async ({ branch }) => {\n try {\n if (!isStagingBranch(branch)) {\n return {\n content: [{ type: 'text' as const, text: `Error: branch must start with \"${stagingPrefix}\" — only doctor staging branches for the \"${targetBranch}\" target can be applied.` }],\n isError: true,\n }\n }\n\n const commitHash = await writer.applyStagingBranch(branch)\n\n return {\n content: [{\n type: 'text' as const,\n text: `✅ Doctor fix applied to \"${targetBranch}\".\\nCommit: ${commitHash}\\nStaging branch \"${branch}\" has been deleted.`,\n }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n\n server.registerTool(\n 'contentrain_doctor_discard',\n {\n description: 'Discard a staged doctor fix by deleting the staging branch without merging.',\n annotations: { readOnlyHint: false },\n inputSchema: {\n branch: z.string().describe(`The staging branch name to discard (e.g., \"${stagingPrefix}1234567890\")`),\n },\n },\n async ({ branch }) => {\n try {\n if (!isStagingBranch(branch)) {\n return {\n content: [{ type: 'text' as const, text: `Error: branch must start with \"${stagingPrefix}\" — only doctor staging branches can be discarded.` }],\n isError: true,\n }\n }\n\n await writer.discardStagingBranch(branch)\n\n return {\n content: [{ type: 'text' as const, text: `Staging branch \"${branch}\" has been deleted.` }],\n }\n }\n catch (err) {\n return { content: [{ type: 'text' as const, text: `Error: ${err}` }], isError: true }\n }\n },\n )\n}\n\nfunction formatResult(result: DoctorResult, stagingBranch?: string): string {\n const { summary, issues } = result\n\n const lines: string[] = []\n\n // Header\n if (summary.errors === 0 && summary.warnings === 0 && summary.fixed === 0) {\n lines.push('✅ No issues found!')\n }\n else if (summary.errors === 0 && summary.fixed > 0) {\n lines.push('✅ All fixable issues have been repaired.')\n }\n else if (summary.errors === 0) {\n lines.push('⚠️ No errors, but some warnings found.')\n }\n else {\n lines.push('❌ Issues detected.')\n }\n\n // Summary\n lines.push('')\n lines.push(`Models scanned: ${summary.models}`)\n lines.push(`Entries scanned: ${summary.entries}`)\n lines.push(`Errors: ${summary.errors}`)\n lines.push(`Warnings: ${summary.warnings}`)\n if (summary.fixed > 0) {\n lines.push(`Fixed: ${summary.fixed}`)\n }\n\n // Staging branch info\n if (stagingBranch) {\n lines.push('')\n lines.push(`📦 Staging branch: ${stagingBranch}`)\n lines.push('→ Review the changes, then run contentrain_doctor_apply to merge into the target branch.')\n lines.push('→ Or run contentrain_doctor_discard to discard without applying.')\n }\n\n // Issues grouped by category\n if (issues.length > 0) {\n lines.push('')\n\n const grouped = new Map<string, typeof issues>()\n for (const issue of issues) {\n const key = issue.category\n const arr = grouped.get(key) ?? []\n arr.push(issue)\n grouped.set(key, arr)\n }\n\n const categoryLabels: Record<string, string> = {\n structure: '📁 Structure',\n id: '🔑 IDs',\n schema: '📋 Schema',\n relation: '🔗 Relations',\n asset: '🖼️ Assets',\n sync: '📝 MD/MDX Sync',\n }\n\n for (const [category, categoryIssues] of grouped) {\n lines.push(`\\n### ${categoryLabels[category] || category}`)\n for (const issue of categoryIssues) {\n // Fixed issues get ✅ instead of ❌/⚠️\n const icon = issue.fix ? '✅' : issue.severity === 'error' ? '❌' : '⚠️'\n const location = [issue.modelId, issue.entryId, issue.field].filter(Boolean).join(' → ')\n const filePart = issue.file ? ` (${issue.file})` : ''\n const fixPart = issue.fix ? ` [FIX: ${issue.fix}]` : ''\n lines.push(` ${icon} ${location ? `[${location}]` : ''}${filePart} ${issue.message}${fixPart}`)\n }\n }\n }\n\n // Also include raw JSON for programmatic consumption\n lines.push('\\n---\\n')\n lines.push(JSON.stringify(result, null, 2))\n\n return lines.join('\\n')\n}\n","import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport { ContentrainWriter } from './core/writer.js'\nimport type { WriterConfig } from './types/index.js'\nimport { registerModelTools } from './tools/model.js'\nimport { registerContentTools } from './tools/content.js'\nimport { registerAssetTools } from './tools/asset.js'\nimport { registerDoctorTools } from './tools/doctor.js'\n\nexport function createServer(config: WriterConfig): McpServer {\n const server = new McpServer({\n name: 'contentrain',\n version: '0.1.1',\n })\n\n const writer = new ContentrainWriter(config)\n\n // Register all tools\n registerModelTools(server, writer)\n registerContentTools(server, writer)\n registerAssetTools(server, writer)\n registerDoctorTools(server, writer, config)\n\n return server\n}\n","#!/usr/bin/env node\n\nimport { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'\nimport { createServer } from './server.js'\n\n// Parse config from environment variables or CLI args\nconst repoPath = process.env.CONTENTRAIN_REPO_PATH || process.cwd()\nconst branch = process.env.CONTENTRAIN_BRANCH || 'contentrain'\nconst remote = process.env.CONTENTRAIN_REMOTE || 'origin'\nconst contentrainDir = process.env.CONTENTRAIN_DIR || 'contentrain'\nconst dryRun = process.env.CONTENTRAIN_DRY_RUN === 'true'\nconst authorName = process.env.CONTENTRAIN_AUTHOR_NAME\nconst authorEmail = process.env.CONTENTRAIN_AUTHOR_EMAIL\n\nconst server = createServer({\n repoPath,\n branch,\n remote,\n contentrainDir,\n dryRun,\n author: authorName && authorEmail\n ? { name: authorName, email: authorEmail }\n : undefined,\n})\n\nasync function main(): Promise<void> {\n const transport = new StdioServerTransport()\n await server.connect(transport)\n}\n\nmain().catch((err) => {\n console.error('Failed to start Contentrain MCP server:', err)\n process.exit(1)\n})\n\n// Re-export for programmatic use\nexport { createServer } from './server.js'\nexport { ContentrainWriter } from './core/writer.js'\nexport { Doctor } from './core/doctor.js'\nexport type * from './types/index.js'\n"],"mappings":";;;;;;;;;;;;;;AAQA,eAAsB,aAA0B,UAA8B;AAC5E,KAAI;EACF,MAAM,MAAM,MAAM,SAAS,UAAU,QAAQ;AAC7C,SAAO,KAAK,MAAM,IAAI;UAEjB,KAAc;AACnB,MAAI,eAAe,SAAS,UAAU,OAAQ,IAA8B,SAAS,SACnF,QAAO,EAAE;AAEX,QAAM;;;;;;AAOV,eAAsB,cAAc,UAAkB,MAA8B;AAClF,OAAM,UAAU,QAAQ,SAAS,CAAC;CAClC,MAAM,UAAU,KAAK,QAAQ,SAAS,EAAE,QAAQ,YAAY,EAAE,CAAC,SAAS,MAAM,CAAC,OAAO;AAEtF,OAAM,UAAU,SADA,KAAK,UAAU,MAAM,MAAM,EAAE,GAAG,MACd,QAAQ;AAC1C,OAAM,OAAO,SAAS,SAAS;;;;AC7BjC,IAAa,mBAAb,cAAsC,MAAM;CAC1C;CACA;CAEA,YAAY,SAAiB,MAAc,UAAmC,EAAE,EAAE;AAChF,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,OAAO;AACZ,OAAK,UAAU;;;AAcnB,IAAa,qBAAb,cAAwC,iBAAiB;CACvD,YAAY,SAAiB;AAC3B,QAAM,oBAAoB,WAAW,mBAAmB,EAAE,SAAS,CAAC;AACpE,OAAK,OAAO;;;AAIhB,IAAa,qBAAb,cAAwC,iBAAiB;CACvD,YAAY,SAAiB,SAAiB;AAC5C,QAAM,oBAAoB,QAAQ,YAAY,WAAW,mBAAmB;GAAE;GAAS;GAAS,CAAC;AACjG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AgB,aAAa,QAAiC;CAC5D,MAAM,SAAS,IAAI,UAAU;EAC3B,MAAM;EACN,SAAS;EACV,CAAC;CAEF,MAAM,SAAS,IAAI,kBAAkB,OAAO;AAG5C,oBAAmB,QAAQ,OAAO;AAClC,sBAAqB,QAAQ,OAAO;AACpC,oBAAmB,QAAQ,OAAO;AAClC,qBAAoB,QAAQ,QAAQ,OAAO;AAE3C,QAAO;;;;AChBT,MAAM,WAAW,QAAQ,IAAI,yBAAyB,QAAQ,KAAK;AACnE,MAAM,SAAS,QAAQ,IAAI,sBAAsB;AACjD,MAAM,SAAS,QAAQ,IAAI,sBAAsB;AACjD,MAAM,iBAAiB,QAAQ,IAAI,mBAAmB;AACtD,MAAM,SAAS,QAAQ,IAAI,wBAAwB;AACnD,MAAM,aAAa,QAAQ,IAAI;AAC/B,MAAM,cAAc,QAAQ,IAAI;AAEhC,MAAM,SAAS,aAAa;CAC1B;CACA;CACA;CACA;CACA;CACA,QAAQ,cAAc,cAClB;EAAE,MAAM;EAAY,OAAO;EAAa,GACxC,KAAA;CACL,CAAC;AAEF,eAAe,OAAsB;CACnC,MAAM,YAAY,IAAI,sBAAsB;AAC5C,OAAM,OAAO,QAAQ,UAAU;;AAGjC,MAAM,CAAC,OAAO,QAAQ;AACpB,SAAQ,MAAM,2CAA2C,IAAI;AAC7D,SAAQ,KAAK,EAAE;EACf"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { resolve } from 'node:path'\nimport { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'\nimport { createServer } from './server.js'\n\nconst projectRoot = resolve(process.env['CONTENTRAIN_PROJECT_ROOT'] ?? process.cwd())\nconst server = createServer(projectRoot)\nconst transport = new StdioServerTransport()\n\nawait server.connect(transport)\n"],"mappings":";;;;;;;;;;;;;;AAMA,MAAM,SAAS,aADK,QAAQ,QAAQ,IAAI,+BAA+B,QAAQ,KAAK,CAAC,CAC7C;AACxC,MAAM,YAAY,IAAI,sBAAsB;AAE5C,MAAM,OAAO,QAAQ,UAAU"}
@@ -0,0 +1,53 @@
1
+ import { a as readJson, s as writeJson, t as contentrainDir } from "./fs-BbuGnVcG.mjs";
2
+ import { join } from "node:path";
3
+ import { rm } from "node:fs/promises";
4
+ //#region src/core/meta-manager.ts
5
+ function metaPath(projectRoot, model, opts) {
6
+ const base = join(contentrainDir(projectRoot), "meta", model.id);
7
+ if (model.kind === "document" && opts.slug) return join(base, opts.slug, `${opts.locale ?? "en"}.json`);
8
+ return join(base, `${opts.locale ?? "en"}.json`);
9
+ }
10
+ async function readMeta(projectRoot, model, opts) {
11
+ return readJson(metaPath(projectRoot, model, opts));
12
+ }
13
+ async function writeMeta(projectRoot, model, opts, meta) {
14
+ const filePath = metaPath(projectRoot, model, opts);
15
+ if (model.kind === "collection" && opts.entryId) {
16
+ const existing = await readJson(filePath) ?? {};
17
+ existing[opts.entryId] = meta;
18
+ await writeJson(filePath, existing);
19
+ } else await writeJson(filePath, meta);
20
+ }
21
+ async function deleteMeta(projectRoot, model, opts) {
22
+ if (model.kind === "collection" && opts.entryId) {
23
+ const filePath = metaPath(projectRoot, model, opts);
24
+ const existing = await readJson(filePath);
25
+ if (existing && opts.entryId in existing) {
26
+ delete existing[opts.entryId];
27
+ if (Object.keys(existing).length > 0) await writeJson(filePath, existing);
28
+ else await rm(filePath, { force: true });
29
+ }
30
+ } else if (model.kind === "document" && opts.slug) {
31
+ const slugDir = join(contentrainDir(projectRoot), "meta", model.id, opts.slug);
32
+ if (opts.locale) await rm(join(slugDir, `${opts.locale}.json`), { force: true });
33
+ else try {
34
+ await rm(slugDir, {
35
+ recursive: true,
36
+ force: true
37
+ });
38
+ } catch {}
39
+ } else if (opts.locale) await rm(metaPath(projectRoot, model, opts), { force: true });
40
+ else {
41
+ const metaDir = join(contentrainDir(projectRoot), "meta", model.id);
42
+ try {
43
+ await rm(metaDir, {
44
+ recursive: true,
45
+ force: true
46
+ });
47
+ } catch {}
48
+ }
49
+ }
50
+ //#endregion
51
+ export { readMeta as n, writeMeta as r, deleteMeta as t };
52
+
53
+ //# sourceMappingURL=meta-manager-CECrfsKm.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"meta-manager-CECrfsKm.mjs","names":[],"sources":["../src/core/meta-manager.ts"],"sourcesContent":["import type { ModelDefinition, EntryMeta } from '@contentrain/types'\nimport { join } from 'node:path'\nimport { rm } from 'node:fs/promises'\nimport { contentrainDir, readJson, writeJson } from '../util/fs.js'\n\ninterface MetaOpts {\n locale?: string\n entryId?: string\n slug?: string\n}\n\nfunction metaPath(projectRoot: string, model: ModelDefinition, opts: MetaOpts): string {\n const base = join(contentrainDir(projectRoot), 'meta', model.id)\n\n if (model.kind === 'document' && opts.slug) {\n return join(base, opts.slug, `${opts.locale ?? 'en'}.json`)\n }\n\n return join(base, `${opts.locale ?? 'en'}.json`)\n}\n\nexport async function readMeta(\n projectRoot: string,\n model: ModelDefinition,\n opts: MetaOpts,\n): Promise<EntryMeta | Record<string, EntryMeta> | null> {\n return readJson(metaPath(projectRoot, model, opts))\n}\n\nexport async function writeMeta(\n projectRoot: string,\n model: ModelDefinition,\n opts: MetaOpts,\n meta: EntryMeta,\n): Promise<void> {\n const filePath = metaPath(projectRoot, model, opts)\n\n if (model.kind === 'collection' && opts.entryId) {\n const existing = await readJson<Record<string, EntryMeta>>(filePath) ?? {}\n existing[opts.entryId] = meta\n await writeJson(filePath, existing)\n } else {\n await writeJson(filePath, meta)\n }\n}\n\nexport async function deleteMeta(\n projectRoot: string,\n model: ModelDefinition,\n opts: MetaOpts,\n): Promise<void> {\n if (model.kind === 'collection' && opts.entryId) {\n const filePath = metaPath(projectRoot, model, opts)\n const existing = await readJson<Record<string, EntryMeta>>(filePath)\n if (existing && opts.entryId in existing) {\n delete existing[opts.entryId]\n if (Object.keys(existing).length > 0) {\n await writeJson(filePath, existing)\n } else {\n await rm(filePath, { force: true })\n }\n }\n } else if (model.kind === 'document' && opts.slug) {\n const slugDir = join(contentrainDir(projectRoot), 'meta', model.id, opts.slug)\n if (opts.locale) {\n await rm(join(slugDir, `${opts.locale}.json`), { force: true })\n } else {\n try {\n await rm(slugDir, { recursive: true, force: true })\n } catch {\n // directory may not exist\n }\n }\n } else if (opts.locale) {\n const filePath = metaPath(projectRoot, model, opts)\n await rm(filePath, { force: true })\n } else {\n // No locale specified — remove all meta for this model (non-i18n or full cleanup)\n const metaDir = join(contentrainDir(projectRoot), 'meta', model.id)\n try {\n await rm(metaDir, { recursive: true, force: true })\n } catch {\n // directory may not exist\n }\n }\n}\n"],"mappings":";;;;AAWA,SAAS,SAAS,aAAqB,OAAwB,MAAwB;CACrF,MAAM,OAAO,KAAK,eAAe,YAAY,EAAE,QAAQ,MAAM,GAAG;AAEhE,KAAI,MAAM,SAAS,cAAc,KAAK,KACpC,QAAO,KAAK,MAAM,KAAK,MAAM,GAAG,KAAK,UAAU,KAAK,OAAO;AAG7D,QAAO,KAAK,MAAM,GAAG,KAAK,UAAU,KAAK,OAAO;;AAGlD,eAAsB,SACpB,aACA,OACA,MACuD;AACvD,QAAO,SAAS,SAAS,aAAa,OAAO,KAAK,CAAC;;AAGrD,eAAsB,UACpB,aACA,OACA,MACA,MACe;CACf,MAAM,WAAW,SAAS,aAAa,OAAO,KAAK;AAEnD,KAAI,MAAM,SAAS,gBAAgB,KAAK,SAAS;EAC/C,MAAM,WAAW,MAAM,SAAoC,SAAS,IAAI,EAAE;AAC1E,WAAS,KAAK,WAAW;AACzB,QAAM,UAAU,UAAU,SAAS;OAEnC,OAAM,UAAU,UAAU,KAAK;;AAInC,eAAsB,WACpB,aACA,OACA,MACe;AACf,KAAI,MAAM,SAAS,gBAAgB,KAAK,SAAS;EAC/C,MAAM,WAAW,SAAS,aAAa,OAAO,KAAK;EACnD,MAAM,WAAW,MAAM,SAAoC,SAAS;AACpE,MAAI,YAAY,KAAK,WAAW,UAAU;AACxC,UAAO,SAAS,KAAK;AACrB,OAAI,OAAO,KAAK,SAA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