@productbrain/mcp 0.0.1-beta.2 → 0.0.1-beta.21
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.
- package/.env.mcp.example +8 -13
- package/dist/chunk-CXYNWTRQ.js +1425 -0
- package/dist/chunk-CXYNWTRQ.js.map +1 -0
- package/dist/chunk-N47IMYAQ.js +4546 -0
- package/dist/chunk-N47IMYAQ.js.map +1 -0
- package/dist/chunk-XBMI6QHR.js +100 -0
- package/dist/chunk-XBMI6QHR.js.map +1 -0
- package/dist/cli/index.js +1 -1
- package/dist/http.js +374 -0
- package/dist/http.js.map +1 -0
- package/dist/index.js +46 -4222
- package/dist/index.js.map +1 -1
- package/dist/setup-GZ5OZ5OP.js +365 -0
- package/dist/setup-GZ5OZ5OP.js.map +1 -0
- package/dist/smart-capture-2N7E4OAJ.js +14 -0
- package/package.json +5 -1
- package/dist/chunk-DGUM43GV.js +0 -11
- package/dist/setup-V6HIAYXL.js +0 -227
- package/dist/setup-V6HIAYXL.js.map +0 -1
- /package/dist/{chunk-DGUM43GV.js.map → smart-capture-2N7E4OAJ.js.map} +0 -0
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/tools/knowledge.ts","../src/analytics.ts","../src/client.ts","../src/tools/labels.ts","../src/tools/health.ts","../src/tools/verify.ts","../src/tools/smart-capture.ts","../src/tools/architecture.ts","../src/tools/workflows.ts","../src/workflows/definitions.ts","../src/tools/gitchain.ts","../src/resources/index.ts","../src/prompts/index.ts"],"sourcesContent":["import { readFileSync } from \"node:fs\";\nimport { resolve } from \"node:path\";\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\n\nimport { registerKnowledgeTools } from \"./tools/knowledge.js\";\nimport { registerLabelTools } from \"./tools/labels.js\";\nimport { registerHealthTools } from \"./tools/health.js\";\nimport { registerVerifyTools } from \"./tools/verify.js\";\nimport { registerSmartCaptureTools } from \"./tools/smart-capture.js\";\nimport { registerArchitectureTools } from \"./tools/architecture.js\";\nimport { registerWorkflowTools } from \"./tools/workflows.js\";\nimport { registerGitChainTools } from \"./tools/gitchain.js\";\nimport { registerResources } from \"./resources/index.js\";\nimport { registerPrompts } from \"./prompts/index.js\";\nimport { getWorkspaceId, bootstrapCloudMode } from \"./client.js\";\nimport { initAnalytics, trackSessionStarted, shutdownAnalytics } from \"./analytics.js\";\n\nif (!process.env.CONVEX_SITE_URL && !process.env.PRODUCTBRAIN_API_KEY) {\n try {\n const envPath = resolve(process.cwd(), \".env.mcp\");\n for (const line of readFileSync(envPath, \"utf-8\").split(\"\\n\")) {\n const trimmed = line.trim();\n if (!trimmed || trimmed.startsWith(\"#\")) continue;\n const eqIdx = trimmed.indexOf(\"=\");\n if (eqIdx === -1) continue;\n process.env[trimmed.slice(0, eqIdx)] ??= trimmed.slice(eqIdx + 1);\n }\n } catch {\n // .env.mcp not found — rely on env vars being set by the launcher\n }\n}\n\nbootstrapCloudMode();\n\nconst SERVER_VERSION = \"1.0.0\";\n\ninitAnalytics();\n\nlet workspaceId: string;\ntry {\n workspaceId = await getWorkspaceId();\n} catch (err: any) {\n const hint =\n !process.env.PRODUCTBRAIN_API_KEY && !process.env.CONVEX_SITE_URL\n ? \"\\n[MCP] Hint: Set PRODUCTBRAIN_API_KEY in your MCP config env block, or run `npx productbrain setup`.\"\n : \"\";\n process.stderr.write(`[MCP] Startup failed: ${err.message}${hint}\\n`);\n process.exit(1);\n}\n\ntrackSessionStarted(\n process.env.WORKSPACE_SLUG ?? \"unknown\",\n workspaceId,\n SERVER_VERSION,\n);\n\nconst server = new McpServer(\n {\n name: \"Product Brain\",\n version: SERVER_VERSION,\n },\n {\n capabilities: { logging: {} },\n instructions: [\n \"Product Brain — the single source of truth for product knowledge.\",\n \"Terminology, standards, and core data all live here — no need to check external docs.\",\n \"\",\n \"Terminology & naming: For 'what is X?' or naming questions, fetch `productbrain://terminology`\",\n \"or use the `name-check` prompt to validate names against the glossary.\",\n \"\",\n \"Workflow:\",\n \" 1. Verify: call `health` to confirm connectivity.\",\n \" 2. Terminology: fetch `productbrain://terminology` or use `name-check` prompt for naming questions.\",\n \" 3. Discover: use `kb-search` to find entries by text, or `list-entries` to browse a collection.\",\n \" 4. Drill in: use `get-entry` for full details — data, labels, relations, history.\",\n \" 5. Capture: use `smart-capture` to create entries — it auto-links related entries and\",\n \" returns a quality scorecard in one call. Use `create-entry` only when you need\",\n \" full control over every field.\",\n \" 6. Connect: use `suggest-links` then `relate-entries` to build the graph.\",\n \" 7. Quality: use `quality-check` to assess entry completeness.\",\n \" 8. Debug: use `mcp-audit` to see what backend calls happened this session.\",\n \"\",\n \"Always prefer `smart-capture` over `create-entry` or `quick-capture` for new entries.\",\n \"Always prefer kb-search or list-entries before get-entry — discover, then drill in.\",\n \"\",\n \"Orientation:\",\n \" When you need to understand the system — architecture, data model, rules,\",\n \" or analytics — fetch the `productbrain://orientation` resource first.\",\n \" It gives you the map. Then use the appropriate tool to drill in.\",\n ].join(\"\\n\"),\n },\n);\n\nregisterKnowledgeTools(server);\nregisterLabelTools(server);\nregisterHealthTools(server);\nregisterVerifyTools(server);\nregisterSmartCaptureTools(server);\nregisterArchitectureTools(server);\nregisterWorkflowTools(server);\nregisterGitChainTools(server);\nregisterResources(server);\nregisterPrompts(server);\n\nconst transport = new StdioServerTransport();\nawait server.connect(transport);\n\nasync function gracefulShutdown() {\n await shutdownAnalytics();\n process.exit(0);\n}\n\nprocess.on(\"SIGINT\", gracefulShutdown);\nprocess.on(\"SIGTERM\", gracefulShutdown);\n","import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport { mcpQuery, mcpMutation } from \"../client.js\";\n\nfunction extractPreview(data: any, maxLen: number): string {\n if (!data || typeof data !== \"object\") return \"\";\n const raw = data.description ?? data.canonical ?? data.detail ?? \"\";\n if (typeof raw !== \"string\" || !raw) return \"\";\n return raw.length > maxLen ? raw.substring(0, maxLen) + \"...\" : raw;\n}\n\nexport function registerKnowledgeTools(server: McpServer) {\n\n server.registerTool(\n \"list-collections\",\n {\n title: \"Browse Collections\",\n description:\n \"List every knowledge collection in the workspace — glossary, business rules, tracking events, standards, etc. \" +\n \"Returns each collection's slug, name, description, and field schema. \" +\n \"Start here before create-entry so you know which collections exist and what fields they expect.\",\n annotations: { readOnlyHint: true },\n },\n async () => {\n const collections = await mcpQuery<any[]>(\"kb.listCollections\");\n\n if (collections.length === 0) {\n return { content: [{ type: \"text\" as const, text: \"No collections found in this workspace.\" }] };\n }\n\n const formatted = collections\n .map((c) => {\n const fieldList = c.fields\n .map((f: any) => ` - \\`${f.key}\\` (${f.type}${f.required ? \", required\" : \"\"}${f.searchable ? \", searchable\" : \"\"})`)\n .join(\"\\n\");\n return `## ${c.name} (\\`${c.slug}\\`)\\n${c.description || \"_No description_\"}\\n\\n**Fields:**\\n${fieldList}`;\n })\n .join(\"\\n\\n---\\n\\n\");\n\n return {\n content: [{ type: \"text\" as const, text: `# Knowledge Collections (${collections.length})\\n\\n${formatted}` }],\n };\n }\n );\n\n server.registerTool(\n \"list-entries\",\n {\n title: \"Browse Entries\",\n description:\n \"List entries in a collection, with optional filters for status, tag, or label. \" +\n \"Returns entry IDs, names, status, and a data preview. \" +\n \"Use list-collections first to discover available collection slugs.\",\n inputSchema: {\n collection: z.string().optional().describe(\"Collection slug, e.g. 'glossary', 'tracking-events', 'business-rules'\"),\n status: z.string().optional().describe(\"Filter: draft | active | verified | deprecated\"),\n tag: z.string().optional().describe(\"Filter by internal tag, e.g. 'health:ambiguous'\"),\n label: z.string().optional().describe(\"Filter by label slug — matches entries across all collections\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ collection, status, tag, label }) => {\n let entries: any[];\n\n if (label) {\n entries = await mcpQuery<any[]>(\"kb.listEntriesByLabel\", { labelSlug: label });\n if (status) entries = entries.filter((e: any) => e.status === status);\n } else {\n entries = await mcpQuery<any[]>(\"kb.listEntries\", {\n collectionSlug: collection,\n status,\n tag,\n });\n }\n\n if (entries.length === 0) {\n return { content: [{ type: \"text\" as const, text: \"No entries match the given filters.\" }] };\n }\n\n const formatted = entries\n .map((e) => {\n const id = e.entryId ? `**${e.entryId}:** ` : \"\";\n const dataPreview = e.data\n ? Object.entries(e.data)\n .slice(0, 4)\n .map(([k, v]) => ` ${k}: ${typeof v === \"string\" ? v.substring(0, 120) : JSON.stringify(v)}`)\n .join(\"\\n\")\n : \"\";\n return `- ${id}${e.name} \\`${e.status}\\`${dataPreview ? `\\n${dataPreview}` : \"\"}`;\n })\n .join(\"\\n\\n\");\n\n const scope = collection ? ` in \\`${collection}\\`` : \"\";\n return {\n content: [{ type: \"text\" as const, text: `# Entries${scope} (${entries.length})\\n\\n${formatted}` }],\n };\n }\n );\n\n server.registerTool(\n \"get-entry\",\n {\n title: \"Look Up Entry\",\n description:\n \"Retrieve a single knowledge entry by its human-readable ID (e.g. 'T-SUPPLIER', 'BR-001', 'EVT-workspace_created'). \" +\n \"Returns the full record: all data fields, labels, relations, and change history. \" +\n \"Use kb-search or list-entries first to discover entry IDs.\",\n inputSchema: {\n entryId: z.string().describe(\"Entry ID, e.g. 'T-SUPPLIER', 'BR-001', 'EVT-workspace_created'\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ entryId }) => {\n const entry = await mcpQuery<any>(\"kb.getEntry\", { entryId });\n\n if (!entry) {\n return { content: [{ type: \"text\" as const, text: `Entry \\`${entryId}\\` not found. Try kb-search to find the right ID.` }] };\n }\n\n const lines: string[] = [\n `# ${entry.entryId ? `${entry.entryId}: ` : \"\"}${entry.name}`,\n \"\",\n `**Status:** ${entry.status}`,\n ];\n\n if (entry.data && typeof entry.data === \"object\") {\n lines.push(\"\");\n for (const [key, val] of Object.entries(entry.data)) {\n const display = typeof val === \"string\" ? val : JSON.stringify(val);\n lines.push(`**${key}:** ${display}`);\n }\n }\n\n if (entry.tags?.length > 0) {\n lines.push(\"\", `**Tags:** ${entry.tags.join(\", \")}`);\n }\n\n if (entry.labels?.length > 0) {\n lines.push(\"\", `**Labels:** ${entry.labels.map((l: any) => `\\`${l.slug ?? l.name}\\``).join(\", \")}`);\n }\n\n if (entry.relations?.length > 0) {\n lines.push(\"\", \"## Relations\");\n for (const r of entry.relations) {\n const arrow = r.direction === \"outgoing\" ? \"\\u2192\" : \"\\u2190\";\n const other = r.otherEntryId ? `${r.otherEntryId}: ${r.otherName}` : (r.otherName ?? \"unknown\");\n lines.push(`- ${arrow} **${r.type}** ${other}`);\n }\n }\n\n if (entry.history?.length > 0) {\n lines.push(\"\", \"## History (last 10)\");\n for (const h of entry.history.slice(-10)) {\n const date = new Date(h.timestamp).toISOString().split(\"T\")[0];\n lines.push(`- ${date}: ${h.event}${h.changedBy ? ` _(${h.changedBy})_` : \"\"}`);\n }\n }\n\n return { content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }] };\n }\n );\n\n const governedCollections = new Set([\n \"glossary\", \"business-rules\", \"principles\", \"standards\", \"strategy\",\n ]);\n\n server.registerTool(\n \"create-entry\",\n {\n title: \"Create Entry\",\n description:\n \"Create a new knowledge entry. Provide the collection slug, a display name, status, and data matching the collection's field schema. \" +\n \"Call list-collections first to see available collections and their field definitions. \" +\n \"Governed collections (glossary, business-rules, principles, standards, strategy) require status 'draft' — \" +\n \"to promote to 'active' or 'verified', raise a tension or use update-entry after approval.\",\n inputSchema: {\n collection: z.string().describe(\"Collection slug, e.g. 'tracking-events', 'standards', 'glossary'\"),\n entryId: z.string().optional().describe(\"Human-readable ID, e.g. 'EVT-workspace_created', 'STD-posthog-events'\"),\n name: z.string().describe(\"Display name\"),\n status: z.string().default(\"draft\").describe(\"Lifecycle status: draft | active | verified | deprecated\"),\n data: z.record(z.unknown()).describe(\"Data object — keys must match the collection's field definitions\"),\n order: z.number().optional().describe(\"Manual sort order within the collection\"),\n },\n annotations: { destructiveHint: false },\n },\n async ({ collection, entryId, name, status, data, order }) => {\n if (governedCollections.has(collection) && status !== \"draft\" && status !== \"deprecated\") {\n return {\n content: [{\n type: \"text\" as const,\n text: `# Governance Required\\n\\n` +\n `The \\`${collection}\\` collection is governed. New entries must be created with status \\`draft\\`.\\n\\n` +\n `**How to proceed:**\\n` +\n `1. Create the entry with status \\`draft\\` (treated as a proposal)\\n` +\n `2. Raise a tension in the \\`tensions\\` collection to request promotion\\n` +\n `3. After approval, use \\`update-entry\\` to change status to \\`active\\` or \\`verified\\``,\n }],\n };\n }\n\n try {\n const id = await mcpMutation<string>(\"kb.createEntry\", {\n collectionSlug: collection,\n entryId,\n name,\n status,\n data,\n order,\n });\n\n return {\n content: [{ type: \"text\" as const, text: `# Entry Created\\n\\n**${entryId ?? name}** added to \\`${collection}\\` as \\`${status}\\`.\\n\\nInternal ID: ${id}` }],\n };\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : String(error);\n if (msg.includes(\"Duplicate entry\") || msg.includes(\"already exists\")) {\n return {\n content: [{\n type: \"text\" as const,\n text: `# Cannot Create — Duplicate Detected\\n\\n${msg}\\n\\n` +\n `**What to do:**\\n` +\n `- Use \\`get-entry\\` to inspect the existing entry\\n` +\n `- Use \\`update-entry\\` to modify it\\n` +\n `- If a genuinely new entry is needed, raise a tension to propose it`,\n }],\n };\n }\n throw error;\n }\n }\n );\n\n server.registerTool(\n \"update-entry\",\n {\n title: \"Update Entry\",\n description:\n \"Update an existing entry by its human-readable ID. Only provide the fields you want to change — data fields are merged with existing values. \" +\n \"Creates a draft version by default. Set autoPublish=true to publish immediately. \" +\n \"Use get-entry first to see current values. SOS-020: Cannot update tension status via MCP — process decides (use Product Brain UI after approval).\",\n inputSchema: {\n entryId: z.string().describe(\"Entry ID to update, e.g. 'T-SUPPLIER', 'BR-001'\"),\n name: z.string().optional().describe(\"New display name\"),\n status: z.string().optional().describe(\"New status: draft | active | verified | deprecated\"),\n data: z.record(z.unknown()).optional().describe(\"Fields to update (merged with existing data)\"),\n order: z.number().optional().describe(\"New sort order\"),\n autoPublish: z.boolean().optional().default(false).describe(\"If true, publishes the change immediately. If false (default), creates a draft version.\"),\n },\n annotations: { idempotentHint: true, destructiveHint: false },\n },\n async ({ entryId, name, status, data, order, autoPublish }) => {\n try {\n const id = await mcpMutation<string>(\"kb.updateEntry\", {\n entryId,\n name,\n status,\n data,\n order,\n autoPublish,\n });\n\n const mode = autoPublish ? 'published' : 'saved as draft';\n return {\n content: [{ type: \"text\" as const, text: `# Entry Updated\\n\\n**${entryId}** has been ${mode}.\\n\\nInternal ID: ${id}` }],\n };\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : String(error);\n if (msg.includes(\"SOS-020\")) {\n return {\n content: [{\n type: \"text\" as const,\n text: `# SOS-020: Tension Status Cannot Be Changed via MCP\\n\\n` +\n `Tension status (open, in-progress, closed) must be changed through the defined process, not via MCP.\\n\\n` +\n `**What you can do:**\\n` +\n `- Create tensions: \\`create-entry collection=tensions name=\"...\" status=open\\`\\n` +\n `- List tensions: \\`list-entries collection=tensions\\`\\n` +\n `- Update non-status fields (raised, date, priority, description) via \\`update-entry\\`\\n` +\n `- After process approval, a human uses the Product Brain UI to change status\\n\\n` +\n `Process criteria (TBD): e.g. 3+ users approved, or 7 days without valid concerns.`,\n }],\n };\n }\n throw error;\n }\n }\n );\n\n server.registerTool(\n \"kb-search\",\n {\n title: \"Search Knowledge Base\",\n description:\n \"Full-text search across all knowledge entries. Returns entry names, collection, status, and a description preview. \" +\n \"Scope results to a specific collection (e.g. collection='business-rules') or filter by status (e.g. status='active'). \" +\n \"Use this to discover entries before calling get-entry for full details.\",\n inputSchema: {\n query: z.string().describe(\"Search text (min 2 characters)\"),\n collection: z.string().optional().describe(\"Scope to a collection slug, e.g. 'business-rules', 'glossary', 'tracking-events'\"),\n status: z.string().optional().describe(\"Filter by status: draft | active | verified | deprecated\"),\n },\n annotations: { readOnlyHint: true, openWorldHint: true },\n },\n async ({ query, collection, status }) => {\n const scope = collection ? ` in \\`${collection}\\`` : \"\";\n await server.sendLoggingMessage({ level: \"info\", data: `Searching${scope} for \"${query}\"...`, logger: \"product-os\" });\n\n const [results, collections] = await Promise.all([\n mcpQuery<any[]>(\"kb.searchEntries\", { query, collectionSlug: collection, status }),\n mcpQuery<any[]>(\"kb.listCollections\"),\n ]);\n\n if (results.length === 0) {\n return { content: [{ type: \"text\" as const, text: `No results for \"${query}\"${scope}. Try a broader search or check list-collections for available data.` }] };\n }\n\n const collMap = new Map<string, { name: string; slug: string }>();\n for (const c of collections) {\n collMap.set(c._id, { name: c.name, slug: c.slug });\n }\n\n const countsBySlug = new Map<string, number>();\n for (const e of results) {\n const col = collMap.get(e.collectionId);\n const slug = col?.slug ?? \"unknown\";\n countsBySlug.set(slug, (countsBySlug.get(slug) ?? 0) + 1);\n }\n const collSummary = [...countsBySlug.entries()]\n .sort((a, b) => b[1] - a[1])\n .map(([slug, count]) => `${count} ${slug}`)\n .join(\", \");\n\n const formatted = results\n .map((e) => {\n const id = e.entryId ? `**${e.entryId}:** ` : \"\";\n const col = collMap.get(e.collectionId);\n const colTag = col ? ` [${col.slug}]` : \"\";\n const desc = extractPreview(e.data, 150);\n const preview = desc ? `\\n ${desc}` : \"\";\n return `- ${id}${e.name} \\`${e.status}\\`${colTag}${preview}`;\n })\n .join(\"\\n\");\n\n const header = `# Search Results for \"${query}\"${scope} (${results.length} match${results.length === 1 ? \"\" : \"es\"})\\n\\n**By collection:** ${collSummary}`;\n const footer = `_Tip: Use \\`collection\\` param to scope search. Use get-entry with an entry ID for full details._`;\n\n return {\n content: [{ type: \"text\" as const, text: `${header}\\n\\n${formatted}\\n\\n${footer}` }],\n };\n }\n );\n\n server.registerTool(\n \"get-history\",\n {\n title: \"Entry Change History\",\n description:\n \"Get the audit trail for an entry — when it was created, updated, status-changed, etc. \" +\n \"Returns timestamped events with change details.\",\n inputSchema: {\n entryId: z.string().describe(\"Entry ID, e.g. 'T-SUPPLIER', 'BR-001'\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ entryId }) => {\n const history = await mcpQuery<any[]>(\"kb.listEntryHistory\", { entryId });\n\n if (history.length === 0) {\n return { content: [{ type: \"text\" as const, text: `No history found for \\`${entryId}\\`.` }] };\n }\n\n const formatted = history\n .map((h) => {\n const date = new Date(h.timestamp).toISOString();\n const changes = h.changes ? ` — ${JSON.stringify(h.changes)}` : \"\";\n return `- **${date}** ${h.event}${h.changedBy ? ` _(${h.changedBy})_` : \"\"}${changes}`;\n })\n .join(\"\\n\");\n\n return {\n content: [{ type: \"text\" as const, text: `# History for \\`${entryId}\\` (${history.length} events)\\n\\n${formatted}` }],\n };\n }\n );\n\n server.registerTool(\n \"relate-entries\",\n {\n title: \"Link Two Entries\",\n description:\n \"Create a typed relation between two entries, building the knowledge graph. \" +\n \"Use get-entry to see existing relations before adding new ones.\\n\\n\" +\n \"Recommended relation types (extensible — any string is accepted):\\n\" +\n \"- related_to, depends_on, replaces, conflicts_with, references, confused_with\\n\" +\n \"- governs — a rule constrains behavior of a feature\\n\" +\n \"- defines_term_for — a glossary term is canonical vocabulary for a feature/area\\n\" +\n \"- belongs_to — a feature belongs to a product area or parent concept\\n\" +\n \"- informs — a decision or insight informs a feature\\n\" +\n \"- surfaces_tension_in — a tension exists within a feature area\\n\\n\" +\n \"Check glossary for relation type definitions if unsure which to use.\",\n inputSchema: {\n from: z.string().describe(\"Source entry ID, e.g. 'T-SUPPLIER'\"),\n to: z.string().describe(\"Target entry ID, e.g. 'BR-001'\"),\n type: z.string().describe(\"Relation type — use a recommended type or any descriptive string\"),\n },\n annotations: { destructiveHint: false },\n },\n async ({ from, to, type }) => {\n await mcpMutation(\"kb.createEntryRelation\", {\n fromEntryId: from,\n toEntryId: to,\n type,\n });\n\n return {\n content: [{ type: \"text\" as const, text: `# Relation Created\\n\\n**${from}** \\u2014[${type}]\\u2192 **${to}**` }],\n };\n }\n );\n\n server.registerTool(\n \"find-related\",\n {\n title: \"Find Related Entries\",\n description:\n \"Navigate the knowledge graph — find all entries related to a given entry. \" +\n \"Shows incoming references (what points to this entry), outgoing references (what this entry points to), or both. \" +\n \"Use after get-entry to explore connections, or to answer 'what depends on X?' or 'what references Y?'\",\n inputSchema: {\n entryId: z.string().describe(\"Entry ID, e.g. 'GT-019', 'SOS-006'\"),\n direction: z.enum([\"incoming\", \"outgoing\", \"both\"]).default(\"both\")\n .describe(\"Filter: 'incoming' = what references this entry, 'outgoing' = what this entry references\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ entryId, direction }) => {\n const relations = await mcpQuery<any[]>(\"kb.listEntryRelations\", { entryId });\n\n if (relations.length === 0) {\n return { content: [{ type: \"text\" as const, text: `No relations found for \\`${entryId}\\`. Use relate-entries to create connections.` }] };\n }\n\n const sourceEntry = await mcpQuery<any>(\"kb.getEntry\", { entryId });\n if (!sourceEntry) {\n return { content: [{ type: \"text\" as const, text: `Entry \\`${entryId}\\` not found. Try kb-search to find the right ID.` }] };\n }\n const sourceInternalId = sourceEntry._id;\n\n const MAX_RELATIONS = 25;\n const truncated = relations.length > MAX_RELATIONS;\n const capped = relations.slice(0, MAX_RELATIONS);\n\n const otherIds = new Set<string>();\n for (const r of capped) {\n const otherId = r.fromId === sourceInternalId ? r.toId : r.fromId;\n otherIds.add(otherId);\n }\n\n const otherEntries = new Map<string, { entryId?: string; name: string; collectionId: string }>();\n for (const id of otherIds) {\n const entry = await mcpQuery<any>(\"kb.getEntry\", { id });\n if (entry) {\n otherEntries.set(entry._id, { entryId: entry.entryId, name: entry.name, collectionId: entry.collectionId });\n }\n }\n\n const collections = await mcpQuery<any[]>(\"kb.listCollections\");\n const collMap = new Map<string, string>();\n for (const c of collections) collMap.set(c._id, c.slug);\n\n const lines: string[] = [`# Relations for ${entryId}: ${sourceEntry.name}`, \"\"];\n\n const enriched = capped.map((r) => {\n const isOutgoing = r.fromId === sourceInternalId;\n const otherId = isOutgoing ? r.toId : r.fromId;\n const other = otherEntries.get(otherId);\n const otherLabel = other?.entryId ? `${other.entryId}: ${other.name}` : (other?.name ?? \"(deleted)\");\n const colSlug = other ? (collMap.get(other.collectionId) ?? \"unknown\") : \"unknown\";\n return { isOutgoing, type: r.type, otherLabel, colSlug };\n });\n\n const outgoing = enriched.filter((r) => r.isOutgoing);\n const incoming = enriched.filter((r) => !r.isOutgoing);\n\n if ((direction === \"outgoing\" || direction === \"both\") && outgoing.length > 0) {\n lines.push(`## Outgoing (${outgoing.length})`);\n for (const r of outgoing) {\n lines.push(`- \\u2192 **${r.type}** ${r.otherLabel} [${r.colSlug}]`);\n }\n lines.push(\"\");\n }\n\n if ((direction === \"incoming\" || direction === \"both\") && incoming.length > 0) {\n lines.push(`## Incoming (${incoming.length})`);\n for (const r of incoming) {\n lines.push(`- \\u2190 **${r.type}** ${r.otherLabel} [${r.colSlug}]`);\n }\n lines.push(\"\");\n }\n\n const shown = direction === \"outgoing\" ? outgoing : direction === \"incoming\" ? incoming : enriched;\n if (shown.length === 0) {\n lines.push(`No ${direction} relations found for \\`${entryId}\\`.`);\n }\n\n if (truncated) {\n lines.push(`_Showing first ${MAX_RELATIONS} of ${relations.length} relations. Use get-entry for the full picture._`);\n }\n\n return { content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }] };\n }\n );\n\n server.registerTool(\n \"gather-context\",\n {\n title: \"Gather Full Context\",\n description:\n \"Assemble the full knowledge context around an entry by traversing the knowledge graph. \" +\n \"Returns all related entries grouped by collection — glossary terms, business rules, tensions, decisions, features, etc. \" +\n \"Use this when starting work on a feature or investigating an area to get the complete picture in one call.\\n\\n\" +\n \"Example: gather-context entryId='FEAT-001' returns all glossary terms, business rules, and tensions linked to that feature.\",\n inputSchema: {\n entryId: z.string().describe(\"Entry ID, e.g. 'FEAT-001', 'GT-019', 'BR-007'\"),\n maxHops: z.number().min(1).max(3).default(2)\n .describe(\"How many relation hops to traverse (1=direct only, 2=default, 3=wide net)\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ entryId, maxHops }) => {\n const result = await mcpQuery<any>(\"kb.gatherContext\", { entryId, maxHops });\n\n if (!result?.root) {\n return { content: [{ type: \"text\" as const, text: `Entry \\`${entryId}\\` not found. Try kb-search to find the right ID.` }] };\n }\n\n if (result.related.length === 0) {\n return {\n content: [{\n type: \"text\" as const,\n text: `# Context for ${result.root.entryId}: ${result.root.name}\\n\\n` +\n `_No relations found._ This entry is not yet connected to the knowledge graph.\\n\\n` +\n `Use \\`suggest-links\\` to discover potential connections, or \\`relate-entries\\` to link manually.`,\n }],\n };\n }\n\n const byCollection = new Map<string, typeof result.related>();\n for (const entry of result.related) {\n const key = entry.collectionName;\n if (!byCollection.has(key)) byCollection.set(key, []);\n byCollection.get(key)!.push(entry);\n }\n\n const lines: string[] = [\n `# Context for ${result.root.entryId}: ${result.root.name}`,\n `_${result.totalRelations} related entries across ${byCollection.size} collections (${result.hopsTraversed} hops traversed)_`,\n \"\",\n ];\n\n for (const [collName, entries] of byCollection) {\n lines.push(`## ${collName} (${entries.length})`);\n for (const e of entries) {\n const arrow = e.relationDirection === \"outgoing\" ? \"\\u2192\" : \"\\u2190\";\n const hopLabel = e.hop > 1 ? ` (hop ${e.hop})` : \"\";\n const id = e.entryId ? `${e.entryId}: ` : \"\";\n lines.push(`- ${arrow} **${e.relationType}** ${id}${e.name}${hopLabel}`);\n }\n lines.push(\"\");\n }\n\n return { content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }] };\n }\n );\n\n server.registerTool(\n \"suggest-links\",\n {\n title: \"Suggest Links\",\n description:\n \"Discover potential connections for an entry by scanning the knowledge base for related content. \" +\n \"Returns ranked suggestions based on text similarity — review them and use relate-entries to create the ones that make sense.\\n\\n\" +\n \"This is a discovery tool, not auto-linking. Always review suggestions before linking.\",\n inputSchema: {\n entryId: z.string().describe(\"Entry ID to find suggestions for, e.g. 'FEAT-001'\"),\n limit: z.number().min(1).max(20).default(10)\n .describe(\"Max number of suggestions to return\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ entryId, limit }) => {\n const entry = await mcpQuery<any>(\"kb.getEntry\", { entryId });\n if (!entry) {\n return { content: [{ type: \"text\" as const, text: `Entry \\`${entryId}\\` not found. Try kb-search to find the right ID.` }] };\n }\n\n const searchTerms = [entry.name];\n if (entry.data?.description) searchTerms.push(entry.data.description);\n if (entry.data?.canonical) searchTerms.push(entry.data.canonical);\n if (entry.data?.rationale) searchTerms.push(entry.data.rationale);\n if (entry.data?.rule) searchTerms.push(entry.data.rule);\n\n const queryText = searchTerms\n .join(\" \")\n .replace(/[^\\w\\s]/g, \" \")\n .split(/\\s+/)\n .filter((w) => w.length > 3)\n .slice(0, 8)\n .join(\" \");\n\n if (!queryText) {\n return { content: [{ type: \"text\" as const, text: `Entry \\`${entryId}\\` has too little text content to generate suggestions.` }] };\n }\n\n const results = await mcpQuery<any[]>(\"kb.searchEntries\", { query: queryText });\n if (!results || results.length === 0) {\n return { content: [{ type: \"text\" as const, text: `No suggestions found for \\`${entryId}\\`. The knowledge base may need more entries.` }] };\n }\n\n const existingRelations = await mcpQuery<any[]>(\"kb.listEntryRelations\", { entryId });\n const relatedIds = new Set(\n existingRelations.flatMap((r) => [r.fromId, r.toId])\n );\n\n const collections = await mcpQuery<any[]>(\"kb.listCollections\");\n const collMap = new Map<string, string>();\n for (const c of collections) collMap.set(c._id, c.slug);\n\n const suggestions = results\n .filter((r) => r._id !== entry._id && !relatedIds.has(r._id))\n .slice(0, limit)\n .map((r) => ({\n entryId: r.entryId,\n name: r.name,\n collection: collMap.get(r.collectionId) ?? \"unknown\",\n preview: extractPreview(r.data, 80),\n }));\n\n if (suggestions.length === 0) {\n return { content: [{ type: \"text\" as const, text: `No new link suggestions for \\`${entryId}\\` — it may already be well-connected, or no similar entries exist.` }] };\n }\n\n const lines = [\n `# Link Suggestions for ${entryId}: ${entry.name}`,\n `_${suggestions.length} potential connections found. Review and use \\`relate-entries\\` to create the ones that make sense._`,\n \"\",\n ];\n\n for (let i = 0; i < suggestions.length; i++) {\n const s = suggestions[i];\n const preview = s.preview ? ` — ${s.preview}` : \"\";\n lines.push(`${i + 1}. **${s.entryId ?? \"(no ID)\"}**: ${s.name} [${s.collection}]${preview}`);\n }\n\n lines.push(\"\");\n lines.push(\"**To link:** `relate-entries from='FEAT-001' to='GT-019' type='defines_term_for'`\");\n lines.push(\"\");\n lines.push(\"_Recommended relation types: governs, defines_term_for, belongs_to, informs, surfaces_tension_in, related_to, depends_on_\");\n\n return { content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }] };\n }\n );\n\n server.registerTool(\n \"quick-capture\",\n {\n title: \"Quick Capture\",\n description:\n \"Quickly capture a knowledge entry with just a name and description — no need to look up the full field schema first. \" +\n \"Always creates as 'draft' status with sensible defaults for remaining fields. \" +\n \"Use update-entry later to fill in details. Embodies 'Capture Now, Curate Later' (PRI-81cbdq).\",\n inputSchema: {\n collection: z.string().describe(\"Collection slug, e.g. 'business-rules', 'glossary', 'tensions', 'decisions'\"),\n name: z.string().describe(\"Display name for the entry\"),\n description: z.string().describe(\"Short description — the essential context to capture now\"),\n entryId: z.string().optional().describe(\"Optional human-readable ID (e.g. 'SOS-020', 'GT-031')\"),\n },\n annotations: { destructiveHint: false },\n },\n async ({ collection, name, description, entryId }) => {\n const col = await mcpQuery<any>(\"kb.getCollection\", { slug: collection });\n if (!col) {\n return { content: [{ type: \"text\" as const, text: `Collection \\`${collection}\\` not found. Use list-collections to see available collections.` }] };\n }\n\n const data: Record<string, unknown> = {};\n const emptyFields: string[] = [];\n\n for (const field of col.fields ?? []) {\n const key = field.key as string;\n if (key === \"description\" || key === \"canonical\" || key === \"detail\") {\n data[key] = description;\n } else if (field.type === \"array\" || field.type === \"multi-select\") {\n data[key] = [];\n emptyFields.push(key);\n } else if (field.type === \"select\") {\n data[key] = field.options?.[0] ?? \"\";\n emptyFields.push(key);\n } else {\n data[key] = \"\";\n emptyFields.push(key);\n }\n }\n\n if (!data.description && !data.canonical && !data.detail) {\n data.description = description;\n }\n\n try {\n const id = await mcpMutation<string>(\"kb.createEntry\", {\n collectionSlug: collection,\n entryId,\n name,\n status: \"draft\",\n data,\n });\n\n const emptyNote = emptyFields.length > 0\n ? `\\n\\n**Fields to fill later** (via \\`update-entry\\`):\\n${emptyFields.map((f) => `- \\`${f}\\``).join(\"\\n\")}`\n : \"\";\n\n return {\n content: [{\n type: \"text\" as const,\n text: `# Quick Capture — Done\\n\\n**${entryId ?? name}** added to \\`${collection}\\` as \\`draft\\`.\\n\\nInternal ID: ${id}${emptyNote}\\n\\n_Use \\`update-entry\\` to fill in details when ready. Use \\`get-entry\\` to review._`,\n }],\n };\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : String(error);\n if (msg.includes(\"Duplicate\") || msg.includes(\"already exists\")) {\n return {\n content: [{\n type: \"text\" as const,\n text: `# Cannot Capture — Duplicate Detected\\n\\n${msg}\\n\\nUse \\`get-entry\\` to inspect the existing entry, or \\`update-entry\\` to modify it.`,\n }],\n };\n }\n throw error;\n }\n }\n );\n\n server.registerTool(\n \"load-context-for-task\",\n {\n title: \"Load Context for Task\",\n description:\n \"Auto-load relevant domain knowledge for a task in a single call. \" +\n \"Pass a natural-language task description; the tool searches the KB, traverses the knowledge graph, \" +\n \"and returns a ranked set of entries (business rules, glossary terms, decisions, features, etc.) \" +\n \"grouped by collection with a confidence score.\\n\\n\" +\n \"Use this at the start of a conversation to ground the agent in domain context before writing code or making recommendations.\\n\\n\" +\n \"Confidence levels:\\n\" +\n \"- high: 3+ direct KB matches — strong domain coverage\\n\" +\n \"- medium: 1-2 direct matches — partial coverage, may want to drill deeper\\n\" +\n \"- low: no direct matches but related entries found via graph traversal\\n\" +\n \"- none: no relevant entries found — KB may not cover this area yet\",\n inputSchema: {\n taskDescription: z.string().describe(\"Natural-language description of the task or user message\"),\n maxResults: z.number().min(1).max(25).default(10).optional()\n .describe(\"Max entries to return (default 10)\"),\n maxHops: z.number().min(1).max(3).default(2).optional()\n .describe(\"Graph traversal depth from each search hit (default 2)\"),\n },\n annotations: { readOnlyHint: true, openWorldHint: true },\n },\n async ({ taskDescription, maxResults, maxHops }) => {\n await server.sendLoggingMessage({\n level: \"info\",\n data: `Loading context for task: \"${taskDescription.substring(0, 80)}...\"`,\n logger: \"product-brain\",\n });\n\n const result = await mcpQuery<{\n entries: Array<{\n entryId?: string;\n name: string;\n collectionSlug: string;\n collectionName: string;\n descriptionPreview: string;\n codePaths: string[];\n hop: number;\n relationType?: string;\n }>;\n confidence: string;\n searchTerms: string;\n totalFound: number;\n }>(\"kb.loadContextForTask\", {\n taskDescription,\n maxResults: maxResults ?? 10,\n maxHops: maxHops ?? 2,\n });\n\n if (result.confidence === \"none\" || result.entries.length === 0) {\n return {\n content: [{\n type: \"text\" as const,\n text: `# Context Loaded\\n\\n**Confidence:** None\\n\\n` +\n `No KB context found for this task. The knowledge base may not cover this area yet.\\n\\n` +\n `_Consider capturing domain knowledge discovered during this task via \\`smart-capture\\`._`,\n }],\n };\n }\n\n const byCollection = new Map<string, typeof result.entries>();\n for (const entry of result.entries) {\n const key = entry.collectionName;\n if (!byCollection.has(key)) byCollection.set(key, []);\n byCollection.get(key)!.push(entry);\n }\n\n const lines: string[] = [\n `# Context Loaded`,\n `**Confidence:** ${result.confidence.charAt(0).toUpperCase() + result.confidence.slice(1)}`,\n `**Matched:** ${result.entries.length} entries across ${byCollection.size} collection${byCollection.size === 1 ? \"\" : \"s\"}`,\n \"\",\n ];\n\n for (const [collName, entries] of byCollection) {\n lines.push(`### ${collName} (${entries.length})`);\n for (const e of entries) {\n const id = e.entryId ? `**${e.entryId}:** ` : \"\";\n const hopLabel = e.hop > 0 ? ` _(hop ${e.hop}${e.relationType ? `, ${e.relationType}` : \"\"})_` : \"\";\n const preview = e.descriptionPreview ? `\\n ${e.descriptionPreview}` : \"\";\n const codePaths = e.codePaths.length > 0 ? `\\n Code: ${e.codePaths.join(\", \")}` : \"\";\n lines.push(`- ${id}${e.name}${hopLabel}${preview}${codePaths}`);\n }\n lines.push(\"\");\n }\n\n lines.push(`_Use \\`get-entry\\` for full details on any entry._`);\n\n return {\n content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }],\n };\n }\n );\n\n server.registerTool(\n \"review-rules\",\n {\n title: \"Review Business Rules\",\n description:\n \"Surface all active business rules for a domain, formatted for compliance review. \" +\n \"Use when reviewing code, designs, or decisions against Product Brain governance. \" +\n \"Optionally provide context (what you're building or reviewing) to help focus the review. \" +\n \"This is the tool form of the review-against-rules prompt.\",\n inputSchema: {\n domain: z.string().describe(\"Business rule domain, e.g. 'AI & MCP Integration', 'Governance & Decision-Making'\"),\n context: z.string().optional().describe(\"What you're reviewing — code change, design decision, file path, etc.\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ domain, context }) => {\n const entries = await mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"business-rules\" });\n\n const domainLower = domain.toLowerCase();\n const rules = entries.filter((e) => {\n const ruleDomain = (e.data?.domain ?? \"\") as string;\n return ruleDomain.toLowerCase() === domainLower || ruleDomain.toLowerCase().includes(domainLower);\n });\n\n if (rules.length === 0) {\n const allDomains = [...new Set(entries.map((e) => e.data?.domain).filter(Boolean))];\n return {\n content: [{\n type: \"text\" as const,\n text: `# No Rules Found for \"${domain}\"\\n\\nAvailable domains:\\n${allDomains.map((d) => `- ${d}`).join(\"\\n\")}\\n\\n_Try one of the domains above, or use kb-search to find rules by keyword._`,\n }],\n };\n }\n\n const header = context\n ? `# Business Rules: ${domain}\\n\\n**Review context:** ${context}\\n\\nFor each rule, assess: compliant, at risk, violation, or not applicable.\\n`\n : `# Business Rules: ${domain}\\n`;\n\n const formatted = rules\n .map((r) => {\n const id = r.entryId ? `**${r.entryId}:** ` : \"\";\n const severity = r.data?.severity ? ` | Severity: ${r.data.severity}` : \"\";\n const desc = r.data?.description ?? \"\";\n const impact = r.data?.dataImpact ? `\\n Data impact: ${r.data.dataImpact}` : \"\";\n const related = (r.data?.relatedRules as string[] ?? []).length > 0\n ? `\\n Related: ${(r.data.relatedRules as string[]).join(\", \")}`\n : \"\";\n return `### ${id}${r.name} \\`${r.status}\\`${severity}\\n\\n${desc}${impact}${related}`;\n })\n .join(\"\\n\\n---\\n\\n\");\n\n const footer = `\\n_${rules.length} rule${rules.length === 1 ? \"\" : \"s\"} in this domain. Use \\`get-entry\\` for full details. Use the \\`draft-rule-from-context\\` prompt to propose new rules._`;\n\n return {\n content: [{ type: \"text\" as const, text: `${header}\\n${formatted}\\n${footer}` }],\n };\n }\n );\n}\n","/**\n * PostHog analytics for SynergyOS maintainers — tracks MCP usage (sessions, tool calls).\n * Not user-facing. Key is injected at build time via SYNERGYOS_POSTHOG_KEY.\n * Override with POSTHOG_MCP_KEY for self-hosted deployments.\n */\n\nimport { userInfo } from \"node:os\";\nimport { PostHog } from \"posthog-node\";\n\nlet client: PostHog | null = null;\nlet distinctId = \"anonymous\";\n\nconst POSTHOG_HOST = \"https://eu.i.posthog.com\";\n\n/** Injected at build time: SYNERGYOS_POSTHOG_KEY env when running `npm run build`/publish. */\ndeclare const __SYNERGYOS_POSTHOG_KEY__: string;\n\n/** Only write to stderr when MCP_DEBUG=1 for quieter default DX. */\nfunction log(msg: string): void {\n if (process.env.MCP_DEBUG === \"1\") {\n process.stderr.write(msg);\n }\n}\n\nexport function initAnalytics(): void {\n const apiKey = process.env.POSTHOG_MCP_KEY || __SYNERGYOS_POSTHOG_KEY__;\n if (!apiKey) {\n log(\"[MCP-ANALYTICS] No PostHog key — tracking disabled (set SYNERGYOS_POSTHOG_KEY at build time for publish)\\n\");\n return;\n }\n\n client = new PostHog(apiKey, {\n host: POSTHOG_HOST,\n flushAt: 1,\n flushInterval: 5000,\n });\n distinctId = process.env.MCP_USER_ID || fallbackDistinctId();\n\n log(`[MCP-ANALYTICS] Initialized — host=${POSTHOG_HOST} distinctId=${distinctId}\\n`);\n}\n\nfunction fallbackDistinctId(): string {\n try {\n return userInfo().username;\n } catch {\n return `os-${process.pid}`;\n }\n}\n\nexport function trackSessionStarted(\n workspaceSlug: string,\n workspaceId: string,\n serverVersion: string,\n): void {\n if (!client) return;\n client.capture({\n distinctId,\n event: \"mcp_session_started\",\n properties: {\n workspace_slug: workspaceSlug,\n workspace_id: workspaceId,\n server_version: serverVersion,\n source: \"mcp-server\",\n $groups: { workspace: workspaceId },\n },\n });\n}\n\nexport function trackToolCall(\n fn: string,\n status: \"ok\" | \"error\",\n durationMs: number,\n workspaceId: string,\n errorMsg?: string,\n): void {\n const properties: Record<string, unknown> = {\n tool: fn,\n status,\n duration_ms: durationMs,\n workspace_slug: process.env.WORKSPACE_SLUG ?? \"unknown\",\n source: \"mcp-server\",\n $groups: { workspace: workspaceId },\n };\n if (errorMsg) properties.error = errorMsg;\n\n if (!client) return;\n client.capture({\n distinctId,\n event: \"mcp_tool_called\",\n properties,\n });\n}\n\nexport async function shutdownAnalytics(): Promise<void> {\n await client?.shutdown();\n}\n","/**\n * MCP client — communicates with the Convex HTTP Action gateway.\n *\n * Two configuration modes:\n *\n * 1. **Cloud mode** (single key):\n * PRODUCTBRAIN_API_KEY=pb_sk_...\n * The cloud URL is resolved automatically. Workspace is inferred from the key.\n *\n * 2. **Self-hosted mode** (three vars):\n * CONVEX_SITE_URL — base URL of the Convex deployment (*.convex.site)\n * MCP_API_KEY — secret key matching MCP_API_KEY set in the Convex dashboard env vars\n * WORKSPACE_SLUG — slug of the workspace to operate on\n */\n\nimport { trackToolCall } from \"./analytics.js\";\n\nconst DEFAULT_CLOUD_URL = \"https://trustworthy-kangaroo-277.convex.site\";\n\nlet cachedWorkspaceId: string | null = null;\nlet cloudMode = false;\n\nexport interface AuditEntry {\n ts: string;\n fn: string;\n workspace: string;\n status: 'ok' | 'error';\n durationMs: number;\n error?: string;\n}\n\nconst AUDIT_BUFFER_SIZE = 50;\nconst auditBuffer: AuditEntry[] = [];\n\n/**\n * Bootstrap cloud mode: if PRODUCTBRAIN_API_KEY is set, map it to the\n * internal env vars so the rest of the client works transparently.\n */\nexport function bootstrapCloudMode(): void {\n const pbKey = process.env.PRODUCTBRAIN_API_KEY;\n if (pbKey?.startsWith(\"pb_sk_\")) {\n const cloudUrl = process.env.PRODUCTBRAIN_URL ?? DEFAULT_CLOUD_URL;\n process.env.CONVEX_SITE_URL ??= cloudUrl;\n process.env.MCP_API_KEY ??= pbKey;\n cloudMode = true;\n }\n}\n\nfunction getEnv(key: string): string {\n const value = process.env[key];\n if (!value) throw new Error(`${key} environment variable is required`);\n return value;\n}\n\n/** Only write audit to stderr on errors, or when MCP_DEBUG=1. Keeps default DX quiet. */\nfunction shouldLogAudit(status: 'ok' | 'error'): boolean {\n return status === 'error' || process.env.MCP_DEBUG === '1';\n}\n\nfunction audit(fn: string, status: 'ok' | 'error', durationMs: number, errorMsg?: string): void {\n const ts = new Date().toISOString();\n const workspace = cachedWorkspaceId ?? 'unresolved';\n\n const entry: AuditEntry = { ts, fn, workspace, status, durationMs };\n if (errorMsg) entry.error = errorMsg;\n auditBuffer.push(entry);\n if (auditBuffer.length > AUDIT_BUFFER_SIZE) auditBuffer.shift();\n\n trackToolCall(fn, status, durationMs, workspace, errorMsg);\n\n if (!shouldLogAudit(status)) return;\n\n const base = `[MCP-AUDIT] ${ts} fn=${fn} workspace=${workspace} status=${status} duration=${durationMs}ms`;\n if (status === 'error' && errorMsg) {\n process.stderr.write(`${base} error=${JSON.stringify(errorMsg)}\\n`);\n } else {\n process.stderr.write(`${base}\\n`);\n }\n}\n\n/** Returns a snapshot of the audit ring buffer (most recent last). */\nexport function getAuditLog(): readonly AuditEntry[] {\n return auditBuffer;\n}\n\n/**\n * Low-level call to the HTTP Action gateway. Does NOT inject workspaceId.\n * Use mcpQuery / mcpMutation for workspace-scoped calls.\n */\nexport async function mcpCall<T>(fn: string, args: Record<string, unknown> = {}): Promise<T> {\n const siteUrl = getEnv('CONVEX_SITE_URL').replace(/\\/$/, '');\n const apiKey = getEnv('MCP_API_KEY');\n\n const start = Date.now();\n\n let res: Response;\n try {\n res = await fetch(`${siteUrl}/api/mcp`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${apiKey}`,\n },\n body: JSON.stringify({ fn, args }),\n });\n } catch (err: any) {\n audit(fn, 'error', Date.now() - start, err.message);\n throw new Error(`MCP call \"${fn}\" network error: ${err.message}`);\n }\n\n const json = await res.json() as { data?: T; error?: string };\n\n if (!res.ok || json.error) {\n audit(fn, 'error', Date.now() - start, json.error);\n throw new Error(`MCP call \"${fn}\" failed (${res.status}): ${json.error ?? 'unknown error'}`);\n }\n\n audit(fn, 'ok', Date.now() - start);\n return json.data as T;\n}\n\n/** Resolve and cache the workspace ID. Cloud mode resolves from the API key; self-hosted uses WORKSPACE_SLUG. */\nexport async function getWorkspaceId(): Promise<string> {\n if (cachedWorkspaceId) return cachedWorkspaceId;\n\n if (cloudMode) {\n // Cloud keys: the backend resolves workspace from the key itself\n const workspace = await mcpCall<{ _id: string; name: string; slug: string } | null>(\n 'resolveWorkspace',\n { slug: \"__cloud__\" },\n );\n if (!workspace) {\n throw new Error(\"Cloud key is valid but no workspace is associated. Run `npx productbrain setup` again.\");\n }\n cachedWorkspaceId = workspace._id;\n return cachedWorkspaceId;\n }\n\n const slug = getEnv('WORKSPACE_SLUG');\n const workspace = await mcpCall<{ _id: string; name: string; slug: string } | null>(\n 'resolveWorkspace',\n { slug },\n );\n\n if (!workspace) {\n throw new Error(`Workspace with slug \"${slug}\" not found`);\n }\n\n cachedWorkspaceId = workspace._id;\n return cachedWorkspaceId;\n}\n\n/** Call a query scoped to the current workspace. */\nexport async function mcpQuery<T>(fn: string, args: Record<string, unknown> = {}): Promise<T> {\n const workspaceId = await getWorkspaceId();\n return mcpCall<T>(fn, { ...args, workspaceId });\n}\n\n/** Call a mutation scoped to the current workspace. */\nexport async function mcpMutation<T>(fn: string, args: Record<string, unknown> = {}): Promise<T> {\n const workspaceId = await getWorkspaceId();\n return mcpCall<T>(fn, { ...args, workspaceId });\n}\n","import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport { mcpQuery, mcpMutation } from \"../client.js\";\n\nexport function registerLabelTools(server: McpServer) {\n\n server.registerTool(\n \"list-labels\",\n {\n title: \"Browse Labels\",\n description:\n \"List all workspace labels with their groups and hierarchy. \" +\n \"Labels can be applied to any entry across any collection for cross-domain filtering. \" +\n \"Similar to labels in Linear or GitHub.\",\n annotations: { readOnlyHint: true },\n },\n async () => {\n const labels = await mcpQuery<any[]>(\"kb.listLabels\");\n\n if (labels.length === 0) {\n return { content: [{ type: \"text\" as const, text: \"No labels defined in this workspace yet.\" }] };\n }\n\n const groups = labels.filter((l) => l.isGroup);\n const ungrouped = labels.filter((l) => !l.isGroup && !l.parentId);\n const children = (parentId: string) => labels.filter((l) => l.parentId === parentId);\n\n const lines: string[] = [\"# Workspace Labels\"];\n\n for (const group of groups) {\n lines.push(`\\n## ${group.name}`);\n if (group.description) lines.push(`_${group.description}_`);\n for (const child of children(group._id)) {\n const color = child.color ? ` ${child.color}` : \"\";\n lines.push(` - \\`${child.slug}\\` ${child.name}${color}`);\n }\n }\n\n if (ungrouped.length > 0) {\n lines.push(\"\\n## Ungrouped\");\n for (const label of ungrouped) {\n const color = label.color ? ` ${label.color}` : \"\";\n lines.push(`- \\`${label.slug}\\` ${label.name}${color}${label.description ? ` — _${label.description}_` : \"\"}`);\n }\n }\n\n return {\n content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }],\n };\n }\n );\n\n server.registerTool(\n \"manage-labels\",\n {\n title: \"Manage Labels\",\n description:\n \"Create, update, or delete a workspace label. Labels support hierarchy (groups with children). \" +\n \"Use list-labels first to see what exists.\",\n inputSchema: {\n action: z.enum([\"create\", \"update\", \"delete\"]).describe(\"What to do\"),\n slug: z.string().describe(\"Label slug (machine name), e.g. 'p1-critical', 'needs-review'\"),\n name: z.string().optional().describe(\"Display name (required for create)\"),\n color: z.string().optional().describe(\"Hex color, e.g. '#ef4444'\"),\n description: z.string().optional().describe(\"What this label means\"),\n parentSlug: z.string().optional().describe(\"Parent group slug for label hierarchy\"),\n isGroup: z.boolean().optional().describe(\"True if this is a group container, not a taggable label\"),\n order: z.number().optional().describe(\"Sort order within its group\"),\n },\n },\n async ({ action, slug, name, color, description, parentSlug, isGroup, order }) => {\n if (action === \"create\") {\n if (!name) {\n return { content: [{ type: \"text\" as const, text: \"Cannot create a label without a name.\" }] };\n }\n\n let parentId: string | undefined;\n if (parentSlug) {\n const labels = await mcpQuery<any[]>(\"kb.listLabels\");\n const parent = labels.find((l: any) => l.slug === parentSlug);\n if (!parent) {\n return { content: [{ type: \"text\" as const, text: `Parent label \\`${parentSlug}\\` not found. Use list-labels to see available groups.` }] };\n }\n parentId = parent._id;\n }\n\n await mcpMutation(\"kb.createLabel\", { slug, name, color, description, parentId, isGroup, order });\n return { content: [{ type: \"text\" as const, text: `# Label Created\\n\\n**${name}** (\\`${slug}\\`)` }] };\n }\n\n if (action === \"update\") {\n await mcpMutation(\"kb.updateLabel\", { slug, name, color, description, isGroup, order });\n return { content: [{ type: \"text\" as const, text: `# Label Updated\\n\\n\\`${slug}\\` has been updated.` }] };\n }\n\n if (action === \"delete\") {\n await mcpMutation(\"kb.deleteLabel\", { slug });\n return { content: [{ type: \"text\" as const, text: `# Label Deleted\\n\\n\\`${slug}\\` removed from all entries and deleted.` }] };\n }\n\n return { content: [{ type: \"text\" as const, text: \"Unknown action.\" }] };\n }\n );\n\n server.registerTool(\n \"label-entry\",\n {\n title: \"Tag Entry with Label\",\n description:\n \"Apply or remove a label from a knowledge entry. Labels work across all collections for cross-domain filtering. \" +\n \"Use list-labels to see available label slugs.\",\n inputSchema: {\n action: z.enum([\"apply\", \"remove\"]).describe(\"Apply or remove the label\"),\n entryId: z.string().describe(\"Entry ID, e.g. 'T-SUPPLIER', 'EVT-workspace_created'\"),\n label: z.string().describe(\"Label slug to apply/remove\"),\n },\n },\n async ({ action, entryId, label }) => {\n if (action === \"apply\") {\n await mcpMutation(\"kb.applyLabel\", { entryId, labelSlug: label });\n return { content: [{ type: \"text\" as const, text: `Label \\`${label}\\` applied to **${entryId}**.` }] };\n }\n\n await mcpMutation(\"kb.removeLabel\", { entryId, labelSlug: label });\n return { content: [{ type: \"text\" as const, text: `Label \\`${label}\\` removed from **${entryId}**.` }] };\n }\n );\n}\n","import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport { mcpQuery, getWorkspaceId, getAuditLog, type AuditEntry } from \"../client.js\";\n\ntype CallCategory = \"read\" | \"search\" | \"write\" | \"label\" | \"meta\";\n\nconst CALL_CATEGORIES: Record<string, CallCategory> = {\n \"kb.getEntry\": \"read\",\n \"kb.listEntries\": \"read\",\n \"kb.listEntryHistory\": \"read\",\n \"kb.listEntryRelations\": \"read\",\n \"kb.listEntriesByLabel\": \"read\",\n \"kb.searchEntries\": \"search\",\n \"kb.createEntry\": \"write\",\n \"kb.updateEntry\": \"write\",\n \"kb.createEntryRelation\": \"write\",\n \"kb.applyLabel\": \"label\",\n \"kb.removeLabel\": \"label\",\n \"kb.createLabel\": \"label\",\n \"kb.updateLabel\": \"label\",\n \"kb.deleteLabel\": \"label\",\n \"kb.listCollections\": \"meta\",\n \"kb.getCollection\": \"meta\",\n \"kb.listLabels\": \"meta\",\n \"resolveWorkspace\": \"meta\",\n};\n\nfunction categorize(fn: string): CallCategory {\n return CALL_CATEGORIES[fn] ?? \"meta\";\n}\n\nfunction formatDuration(ms: number): string {\n if (ms < 60_000) return `${Math.round(ms / 1000)}s`;\n const mins = Math.floor(ms / 60_000);\n const secs = Math.round((ms % 60_000) / 1000);\n return `${mins}m ${secs}s`;\n}\n\nfunction buildSessionSummary(log: readonly AuditEntry[]): string {\n if (log.length === 0) return \"\";\n\n const byCategory = new Map<CallCategory, Map<string, number>>();\n let errorCount = 0;\n let writeCreates = 0;\n let writeUpdates = 0;\n\n for (const entry of log) {\n const cat = categorize(entry.fn);\n if (!byCategory.has(cat)) byCategory.set(cat, new Map());\n const fnCounts = byCategory.get(cat)!;\n fnCounts.set(entry.fn, (fnCounts.get(entry.fn) ?? 0) + 1);\n\n if (entry.status === \"error\") errorCount++;\n if (entry.fn === \"kb.createEntry\" && entry.status === \"ok\") writeCreates++;\n if (entry.fn === \"kb.updateEntry\" && entry.status === \"ok\") writeUpdates++;\n }\n\n const firstTs = new Date(log[0].ts).getTime();\n const lastTs = new Date(log[log.length - 1].ts).getTime();\n const duration = formatDuration(lastTs - firstTs);\n\n const lines: string[] = [`# Session Summary (${duration})\\n`];\n\n const categoryLabels: [CallCategory, string][] = [\n [\"read\", \"Reads\"],\n [\"search\", \"Searches\"],\n [\"write\", \"Writes\"],\n [\"label\", \"Labels\"],\n [\"meta\", \"Meta\"],\n ];\n\n for (const [cat, label] of categoryLabels) {\n const fnCounts = byCategory.get(cat);\n if (!fnCounts || fnCounts.size === 0) continue;\n const total = [...fnCounts.values()].reduce((a, b) => a + b, 0);\n const detail = [...fnCounts.entries()]\n .sort((a, b) => b[1] - a[1])\n .map(([fn, count]) => `${fn.replace(\"kb.\", \"\")} x${count}`)\n .join(\", \");\n lines.push(`- **${label}:** ${total} call${total === 1 ? \"\" : \"s\"} (${detail})`);\n }\n\n lines.push(`- **Errors:** ${errorCount}`);\n\n if (writeCreates > 0 || writeUpdates > 0) {\n lines.push(\"\");\n lines.push(\"## Knowledge Contribution\");\n if (writeCreates > 0) lines.push(`- ${writeCreates} entr${writeCreates === 1 ? \"y\" : \"ies\"} created`);\n if (writeUpdates > 0) lines.push(`- ${writeUpdates} entr${writeUpdates === 1 ? \"y\" : \"ies\"} updated`);\n }\n\n return lines.join(\"\\n\");\n}\n\nexport function registerHealthTools(server: McpServer) {\n\n server.registerTool(\n \"health\",\n {\n title: \"Health Check\",\n description:\n \"Verify that Product Brain is running and can reach its backend. \" +\n \"Returns workspace status, collection count, entry count, and latency. \" +\n \"Use this to confirm connectivity before doing real work.\",\n annotations: { readOnlyHint: true },\n },\n async () => {\n const start = Date.now();\n const errors: string[] = [];\n\n let workspaceId: string | undefined;\n try {\n workspaceId = await getWorkspaceId();\n } catch (e: any) {\n errors.push(`Workspace resolution failed: ${e.message}`);\n }\n\n let collections: any[] = [];\n try {\n collections = await mcpQuery<any[]>(\"kb.listCollections\");\n } catch (e: any) {\n errors.push(`Collection fetch failed: ${e.message}`);\n }\n\n let totalEntries = 0;\n if (collections.length > 0) {\n try {\n const entries = await mcpQuery<any[]>(\"kb.listEntries\", {});\n totalEntries = entries.length;\n } catch (e: any) {\n errors.push(`Entry count failed: ${e.message}`);\n }\n }\n\n const durationMs = Date.now() - start;\n const healthy = errors.length === 0;\n\n const lines = [\n `# ${healthy ? \"Healthy\" : \"Degraded\"}`,\n \"\",\n `**Workspace:** ${workspaceId ?? \"unresolved\"}`,\n `**Collections:** ${collections.length}`,\n `**Entries:** ${totalEntries}`,\n `**Latency:** ${durationMs}ms`,\n ];\n\n if (errors.length > 0) {\n lines.push(\"\", \"## Errors\");\n for (const err of errors) {\n lines.push(`- ${err}`);\n }\n }\n\n return { content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }] };\n }\n );\n\n server.registerTool(\n \"mcp-audit\",\n {\n title: \"Session Audit Log\",\n description:\n \"Show a session summary (reads, writes, searches, contributions) and the last N backend calls. \" +\n \"Useful for debugging, tracing tool behavior, and seeing what you contributed to the knowledge base.\",\n inputSchema: {\n limit: z.number().min(1).max(50).default(20).describe(\"How many recent calls to show (max 50)\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ limit }) => {\n const log = getAuditLog();\n const recent = log.slice(-limit);\n\n if (recent.length === 0) {\n return { content: [{ type: \"text\" as const, text: \"No calls recorded yet this session.\" }] };\n }\n\n const summary = buildSessionSummary(log);\n\n const logLines = [`# Audit Log (last ${recent.length} of ${log.length} total)\\n`];\n for (const entry of recent) {\n const icon = entry.status === \"ok\" ? \"\\u2713\" : \"\\u2717\";\n const errPart = entry.error ? ` \\u2014 ${entry.error}` : \"\";\n logLines.push(`${icon} \\`${entry.fn}\\` ${entry.durationMs}ms ${entry.status}${errPart}`);\n }\n\n return {\n content: [{ type: \"text\" as const, text: `${summary}\\n\\n---\\n\\n${logLines.join(\"\\n\")}` }],\n };\n }\n );\n}\n","import { existsSync, readFileSync } from \"node:fs\";\nimport { resolve } from \"node:path\";\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport { mcpQuery, mcpMutation } from \"../client.js\";\n\n// ── Types ──────────────────────────────────────────────────────────────\n\ntype MappingKind = \"file\" | \"schema\" | \"conceptual\";\ntype CheckResult = \"verified\" | \"drifted\" | \"unverifiable\";\n\ninterface MappingCheck {\n entryId: string;\n entryName: string;\n field: string;\n kind: MappingKind;\n result: CheckResult;\n reason: string;\n currentStatus: string;\n}\n\ninterface RefCheck {\n entryId: string;\n entryName: string;\n refValue: string;\n found: boolean;\n}\n\n// ── Project Root Resolution ────────────────────────────────────────────\n\nfunction resolveProjectRoot(): string | null {\n const candidates = [\n process.env.WORKSPACE_PATH,\n process.cwd(),\n resolve(process.cwd(), \"..\"),\n ].filter(Boolean) as string[];\n\n for (const dir of candidates) {\n const resolved = resolve(dir);\n if (existsSync(resolve(resolved, \"convex/schema.ts\"))) return resolved;\n }\n return null;\n}\n\n// ── Schema Parser ──────────────────────────────────────────────────────\n\nfunction parseConvexSchema(schemaPath: string): Map<string, Set<string>> {\n const content = readFileSync(schemaPath, \"utf-8\");\n const tables = new Map<string, Set<string>>();\n let currentTable: string | null = null;\n\n for (const line of content.split(\"\\n\")) {\n const tableMatch = line.match(/^\\t(\\w+):\\s*defineTable\\(/);\n if (tableMatch) {\n currentTable = tableMatch[1];\n tables.set(currentTable, new Set());\n continue;\n }\n\n if (currentTable && /^\\t[}\\)]/.test(line) && !/^\\t\\t/.test(line)) {\n currentTable = null;\n continue;\n }\n\n if (currentTable) {\n const fieldMatch = line.match(/^\\t\\t(\\w+):\\s*v\\./);\n if (fieldMatch) tables.get(currentTable)!.add(fieldMatch[1]);\n }\n }\n\n return tables;\n}\n\n// ── Code Mapping Classifiers ───────────────────────────────────────────\n\nfunction cleanFieldRef(field: string): string {\n return field\n .replace(/\\s*\\(.*\\)\\s*$/, \"\")\n .replace(/\\s*=\\s*.*$/, \"\")\n .trim();\n}\n\nfunction splitMultiRefs(field: string): string[] {\n if (field.includes(\" + \")) return field.split(/\\s*\\+\\s*/);\n return [field];\n}\n\nconst FILE_EXT_RE = /\\.(ts|js|svelte|json|css|md|jsx|tsx|mjs|cjs)(?:\\s|$)/i;\n\nfunction classifyRef(cleaned: string): MappingKind {\n if (cleaned.includes(\"/\") || FILE_EXT_RE.test(cleaned) || /^\\.\\w+/.test(cleaned)) {\n return \"file\";\n }\n if (/^\\w+\\.\\w+$/.test(cleaned)) return \"schema\";\n if (/^\\w+\\s+table$/i.test(cleaned)) return \"schema\";\n return \"conceptual\";\n}\n\n// ── Verification Checkers ──────────────────────────────────────────────\n\nfunction checkFileRef(ref: string, root: string): { result: CheckResult; reason: string } {\n const filePart = ref.split(/\\s+/)[0];\n const fullPath = resolve(root, filePart);\n if (existsSync(fullPath)) return { result: \"verified\", reason: \"exists\" };\n return { result: \"drifted\", reason: `not found: ${filePart}` };\n}\n\nfunction checkSchemaRef(\n ref: string,\n schema: Map<string, Set<string>>,\n): { result: CheckResult; reason: string } {\n const tableOnlyMatch = ref.match(/^(\\w+)\\s+table$/i);\n if (tableOnlyMatch) {\n const table = tableOnlyMatch[1];\n if (schema.has(table)) return { result: \"verified\", reason: `table \"${table}\" exists` };\n return { result: \"drifted\", reason: `table \"${table}\" not found in schema` };\n }\n\n const dotIdx = ref.indexOf(\".\");\n if (dotIdx > 0) {\n const table = ref.slice(0, dotIdx);\n const field = ref.slice(dotIdx + 1);\n if (!schema.has(table)) return { result: \"drifted\", reason: `table \"${table}\" not found in schema` };\n if (!schema.get(table)!.has(field)) return { result: \"drifted\", reason: `field \"${field}\" not found in table \"${table}\"` };\n return { result: \"verified\", reason: `${table}.${field} exists` };\n }\n\n return { result: \"unverifiable\", reason: \"could not parse schema reference\" };\n}\n\n// ── Trust Report Formatter ─────────────────────────────────────────────\n\nfunction formatTrustReport(\n collection: string,\n entryCount: number,\n mappings: MappingCheck[],\n refs: RefCheck[],\n fixes: string[],\n mode: string,\n schemaTableCount: number,\n projectRoot: string,\n): string {\n const verified = mappings.filter((c) => c.result === \"verified\").length;\n const drifted = mappings.filter((c) => c.result === \"drifted\").length;\n const unverifiable = mappings.filter((c) => c.result === \"unverifiable\").length;\n\n const refsValid = refs.filter((c) => c.found).length;\n const refsBroken = refs.filter((c) => !c.found).length;\n\n const totalChecks = mappings.length + refs.length;\n const totalPassed = verified + refsValid;\n const trustScore = totalChecks > 0 ? Math.round((totalPassed / totalChecks) * 100) : 100;\n\n const lines: string[] = [\n `# Trust Report: ${collection} (${entryCount} entries scanned)`,\n \"\",\n ];\n\n if (mappings.length > 0) {\n lines.push(\n \"## Code Mapping Verification\",\n `- ${mappings.length} mappings checked`,\n `- ${verified} verified (${Math.round((verified / mappings.length) * 100)}%)`,\n `- ${drifted} drifted (file/schema not found)`,\n `- ${unverifiable} unverifiable (conceptual — skipped)`,\n \"\",\n );\n }\n\n if (drifted > 0) {\n lines.push(\"### Drifted Mappings\");\n for (const mc of mappings.filter((c) => c.result === \"drifted\")) {\n lines.push(`- **${mc.entryId}** (${mc.entryName}): \\`${mc.field}\\` — ${mc.reason}`);\n }\n lines.push(\"\");\n }\n\n if (unverifiable > 0) {\n lines.push(\"### Unverifiable (Conceptual)\");\n for (const mc of mappings.filter((c) => c.result === \"unverifiable\")) {\n lines.push(`- **${mc.entryId}** (${mc.entryName}): \\`${mc.field}\\``);\n }\n lines.push(\"\");\n }\n\n if (refs.length > 0) {\n lines.push(\n \"## Cross-Reference Verification\",\n `- ${refs.length} references checked`,\n `- ${refsValid} valid (${refs.length > 0 ? Math.round((refsValid / refs.length) * 100) : 0}%)`,\n `- ${refsBroken} broken`,\n \"\",\n );\n }\n\n if (refsBroken > 0) {\n lines.push(\"### Broken References\");\n for (const rc of refs.filter((c) => !c.found)) {\n lines.push(`- **${rc.entryId}** (${rc.entryName}): relatedRules \\`${rc.refValue}\\` — entry not found`);\n }\n lines.push(\"\");\n }\n\n lines.push(`## Trust Score: ${trustScore}% (${totalPassed} of ${totalChecks} checks passed)`);\n\n if (mode === \"fix\" && fixes.length > 0) {\n lines.push(\n \"\",\n \"## Applied Fixes\",\n `Updated codeMapping status from \\`aligned\\` → \\`drifted\\` on ${fixes.length} entr${fixes.length === 1 ? \"y\" : \"ies\"}:`,\n );\n for (const eid of fixes) lines.push(`- ${eid}`);\n } else if (mode === \"report\" && drifted > 0) {\n const fixable = mappings.filter((c) => c.result === \"drifted\" && c.currentStatus === \"aligned\").length;\n if (fixable > 0) {\n lines.push(\"\", `_${fixable} mapping(s) marked \"aligned\" but actually drifted. Run with mode=\"fix\" to update._`);\n }\n }\n\n lines.push(\"\", \"---\", `_Schema: ${schemaTableCount} tables parsed from convex/schema.ts. Project root: ${projectRoot}_`);\n\n return lines.join(\"\\n\");\n}\n\n// ── Tool Registration ──────────────────────────────────────────────────\n\nexport function registerVerifyTools(server: McpServer) {\n server.registerTool(\n \"verify\",\n {\n title: \"Verify Knowledge Base\",\n description:\n \"Verify knowledge entries against the actual codebase. Checks glossary code mappings \" +\n \"(do referenced files and schema fields still exist?) and validates cross-references \" +\n \"(do relatedRules point to real entries?). Produces a trust report with a trust score. \" +\n \"Use mode='fix' to auto-update drifted codeMapping statuses.\",\n inputSchema: {\n collection: z.string().default(\"glossary\")\n .describe(\"Collection slug to verify (default: glossary)\"),\n mode: z.enum([\"report\", \"fix\"]).default(\"report\")\n .describe(\"'report' = read-only trust report. 'fix' = also update drifted codeMapping statuses.\"),\n },\n annotations: { readOnlyHint: false },\n },\n async ({ collection, mode }) => {\n const projectRoot = resolveProjectRoot();\n if (!projectRoot) {\n return {\n content: [{\n type: \"text\" as const,\n text: \"# Verification Failed\\n\\n\" +\n \"Cannot find project root (looked for `convex/schema.ts` in cwd and parent directory).\\n\\n\" +\n \"Set `WORKSPACE_PATH` in `.env.mcp` to the absolute path of the Product Brain project root.\",\n }],\n };\n }\n\n const schema = parseConvexSchema(resolve(projectRoot, \"convex/schema.ts\"));\n\n await server.sendLoggingMessage({\n level: \"info\",\n data: `Verifying \"${collection}\" against ${schema.size} schema tables at ${projectRoot}`,\n logger: \"product-os\",\n });\n\n const scopedEntries = await mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: collection });\n\n if (scopedEntries.length === 0) {\n return {\n content: [{ type: \"text\" as const, text: `No entries found in \\`${collection}\\`. Nothing to verify.` }],\n };\n }\n\n // Build cross-reference lookup: fetch all entries to check relatedRules\n let allEntryIds: Set<string>;\n try {\n const allEntries = await mcpQuery<any[]>(\"kb.listEntries\", {});\n allEntryIds = new Set(allEntries.map((e: any) => e.entryId).filter(Boolean));\n } catch {\n allEntryIds = new Set(scopedEntries.map((e: any) => e.entryId).filter(Boolean));\n }\n\n const mappingChecks: MappingCheck[] = [];\n const refChecks: RefCheck[] = [];\n\n for (const entry of scopedEntries) {\n const eid = entry.entryId ?? entry.name;\n const ename = entry.name;\n\n // ── Code mapping verification ──\n const codeMappings: any[] = entry.data?.codeMapping ?? [];\n for (const cm of codeMappings) {\n const rawField: string = cm.field ?? \"\";\n for (const rawRef of splitMultiRefs(rawField)) {\n const cleaned = cleanFieldRef(rawRef);\n if (!cleaned) continue;\n\n const kind = classifyRef(cleaned);\n let result: CheckResult;\n let reason: string;\n\n if (kind === \"file\") {\n ({ result, reason } = checkFileRef(cleaned, projectRoot));\n } else if (kind === \"schema\") {\n ({ result, reason } = checkSchemaRef(cleaned, schema));\n } else {\n result = \"unverifiable\";\n reason = \"conceptual reference\";\n }\n\n mappingChecks.push({\n entryId: eid, entryName: ename, field: rawRef.trim(),\n kind, result, reason, currentStatus: cm.status ?? \"unknown\",\n });\n }\n }\n\n // ── Cross-reference verification (relatedRules only) ──\n const MAX_CROSS_REFS = 50;\n const relatedRules: string[] = entry.data?.relatedRules ?? [];\n for (const ruleId of relatedRules) {\n if (refChecks.length >= MAX_CROSS_REFS) break;\n refChecks.push({\n entryId: eid, entryName: ename, refValue: ruleId,\n found: allEntryIds.has(ruleId),\n });\n }\n }\n\n // ── Fix mode: update aligned → drifted ──\n const fixes: string[] = [];\n if (mode === \"fix\") {\n const driftedByEntry = new Map<string, Set<string>>();\n for (const mc of mappingChecks) {\n if (mc.result === \"drifted\" && mc.currentStatus === \"aligned\") {\n if (!driftedByEntry.has(mc.entryId)) driftedByEntry.set(mc.entryId, new Set());\n driftedByEntry.get(mc.entryId)!.add(mc.field);\n }\n }\n\n for (const [eid, driftedFields] of driftedByEntry) {\n const entry = scopedEntries.find((e: any) => (e.entryId ?? e.name) === eid);\n if (!entry?.entryId) continue;\n\n const updated = (entry.data?.codeMapping ?? []).map((cm: any) =>\n cm.status === \"aligned\" && driftedFields.has(cm.field)\n ? { ...cm, status: \"drifted\" }\n : cm,\n );\n\n await mcpMutation(\"kb.updateEntry\", {\n entryId: entry.entryId,\n data: { codeMapping: updated },\n });\n fixes.push(entry.entryId);\n }\n }\n\n const report = formatTrustReport(\n collection, scopedEntries.length,\n mappingChecks, refChecks, fixes, mode,\n schema.size, projectRoot,\n );\n\n return { content: [{ type: \"text\" as const, text: report }] };\n },\n );\n}\n","/**\n * Smart capture — ARCH-node-mcp (Core layer)\n * KB: FEAT-MCP-001 (MCP Server), ARCH-flow-smart-capture\n * Rules: SOS-016 (governed draft-first), SOS-015 (unique IDs within scope)\n */\n\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport { mcpQuery, mcpMutation } from \"../client.js\";\n\n// ── Collection Workflow Profiles ────────────────────────────────────────────\n\ninterface FieldDefault {\n key: string;\n value: unknown | \"today\" | \"infer\";\n}\n\ninterface QualityCheck {\n id: string;\n label: string;\n check: (ctx: CaptureContext) => boolean;\n suggestion?: (ctx: CaptureContext) => string;\n}\n\ninterface CollectionProfile {\n idPrefix: string;\n governedDraft: boolean;\n defaults: FieldDefault[];\n descriptionField: string;\n recommendedRelationTypes: string[];\n qualityChecks: QualityCheck[];\n inferField?: (ctx: CaptureContext) => Record<string, unknown>;\n}\n\ninterface CaptureContext {\n collection: string;\n name: string;\n description: string;\n context?: string;\n data: Record<string, unknown>;\n entryId: string;\n linksCreated: LinkResult[];\n linksSuggested: LinkSuggestion[];\n collectionFields: Array<{ key: string; type: string; required?: boolean }>;\n}\n\ninterface LinkResult {\n targetEntryId: string;\n targetName: string;\n targetCollection: string;\n relationType: string;\n}\n\ninterface LinkSuggestion {\n entryId?: string;\n name: string;\n collection: string;\n reason: string;\n preview: string;\n}\n\nconst AREA_KEYWORDS: Record<string, string[]> = {\n \"Architecture\": [\"convex\", \"schema\", \"database\", \"migration\", \"api\", \"backend\", \"infrastructure\", \"scaling\", \"performance\"],\n \"Knowledge Base\": [\"knowledge\", \"glossary\", \"entry\", \"collection\", \"terminology\", \"drift\", \"graph\"],\n \"AI & MCP Integration\": [\"mcp\", \"ai\", \"cursor\", \"agent\", \"tool\", \"llm\", \"prompt\", \"context\"],\n \"Developer Experience\": [\"dx\", \"developer\", \"ide\", \"workflow\", \"friction\", \"ceremony\"],\n \"Governance & Decision-Making\": [\"governance\", \"decision\", \"rule\", \"policy\", \"compliance\", \"approval\"],\n \"Analytics & Tracking\": [\"analytics\", \"posthog\", \"tracking\", \"event\", \"metric\", \"funnel\"],\n \"Security\": [\"security\", \"auth\", \"api key\", \"permission\", \"access\", \"token\"],\n};\n\nfunction inferArea(text: string): string {\n const lower = text.toLowerCase();\n let bestArea = \"\";\n let bestScore = 0;\n for (const [area, keywords] of Object.entries(AREA_KEYWORDS)) {\n const score = keywords.filter((kw) => lower.includes(kw)).length;\n if (score > bestScore) {\n bestScore = score;\n bestArea = area;\n }\n }\n return bestArea;\n}\n\nfunction inferDomain(text: string): string {\n return inferArea(text) || \"\";\n}\n\nconst COMMON_CHECKS: Record<string, QualityCheck> = {\n clearName: {\n id: \"clear-name\",\n label: \"Clear, specific name (not vague)\",\n check: (ctx) => ctx.name.length > 10 && ![\"new tension\", \"new entry\", \"untitled\", \"test\"].includes(ctx.name.toLowerCase()),\n suggestion: () => \"Rename to something specific — describe the actual problem or concept.\",\n },\n hasDescription: {\n id: \"has-description\",\n label: \"Description provided (>50 chars)\",\n check: (ctx) => ctx.description.length > 50,\n suggestion: () => \"Add a fuller description explaining context and impact.\",\n },\n hasRelations: {\n id: \"has-relations\",\n label: \"At least 1 relation created\",\n check: (ctx) => ctx.linksCreated.length >= 1,\n suggestion: () => \"Use `suggest-links` and `relate-entries` to connect this entry to related knowledge.\",\n },\n diverseRelations: {\n id: \"diverse-relations\",\n label: \"Relations span multiple collections\",\n check: (ctx) => {\n const colls = new Set(ctx.linksCreated.map((l) => l.targetCollection));\n return colls.size >= 2;\n },\n suggestion: () => \"Try linking to entries in different collections (glossary, business-rules, strategy).\",\n },\n};\n\nconst PROFILES: Map<string, CollectionProfile> = new Map([\n [\"tensions\", {\n idPrefix: \"TEN\",\n governedDraft: false,\n descriptionField: \"description\",\n defaults: [\n { key: \"priority\", value: \"medium\" },\n { key: \"date\", value: \"today\" },\n { key: \"raised\", value: \"infer\" },\n { key: \"severity\", value: \"infer\" },\n ],\n recommendedRelationTypes: [\"surfaces_tension_in\", \"references\", \"belongs_to\", \"related_to\"],\n inferField: (ctx: CaptureContext) => {\n const fields: Record<string, unknown> = {};\n const text = `${ctx.name} ${ctx.description}`;\n const area = inferArea(text);\n if (area) fields.raised = area;\n if (text.toLowerCase().includes(\"critical\") || text.toLowerCase().includes(\"blocker\")) {\n fields.severity = \"critical\";\n } else if (text.toLowerCase().includes(\"bottleneck\") || text.toLowerCase().includes(\"scaling\") || text.toLowerCase().includes(\"breaking\")) {\n fields.severity = \"high\";\n } else {\n fields.severity = \"medium\";\n }\n if (area) fields.affectedArea = area;\n return fields;\n },\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n COMMON_CHECKS.hasRelations,\n {\n id: \"has-severity\",\n label: \"Severity specified\",\n check: (ctx) => !!ctx.data.severity && ctx.data.severity !== \"\",\n suggestion: (ctx) => {\n const text = `${ctx.name} ${ctx.description}`.toLowerCase();\n const inferred = text.includes(\"critical\") ? \"critical\" : text.includes(\"bottleneck\") ? \"high\" : \"medium\";\n return `Set severity — suggest: ${inferred} (based on description keywords).`;\n },\n },\n {\n id: \"has-affected-area\",\n label: \"Affected area identified\",\n check: (ctx) => !!ctx.data.affectedArea && ctx.data.affectedArea !== \"\",\n suggestion: (ctx) => {\n const area = inferArea(`${ctx.name} ${ctx.description}`);\n return area\n ? `Set affectedArea — suggest: \"${area}\" (inferred from content).`\n : \"Specify which product area or domain this tension impacts.\";\n },\n },\n ],\n }],\n\n [\"business-rules\", {\n idPrefix: \"SOS\",\n governedDraft: true,\n descriptionField: \"description\",\n defaults: [\n { key: \"severity\", value: \"medium\" },\n { key: \"domain\", value: \"infer\" },\n ],\n recommendedRelationTypes: [\"governs\", \"references\", \"conflicts_with\", \"related_to\"],\n inferField: (ctx: CaptureContext) => {\n const fields: Record<string, unknown> = {};\n const domain = inferDomain(`${ctx.name} ${ctx.description}`);\n if (domain) fields.domain = domain;\n return fields;\n },\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n COMMON_CHECKS.hasRelations,\n {\n id: \"has-rationale\",\n label: \"Rationale provided\",\n check: (ctx) => typeof ctx.data.rationale === \"string\" && ctx.data.rationale.length > 10,\n suggestion: () => \"Add a rationale explaining why this rule exists via `update-entry`.\",\n },\n {\n id: \"has-domain\",\n label: \"Domain specified\",\n check: (ctx) => !!ctx.data.domain && ctx.data.domain !== \"\",\n suggestion: (ctx) => {\n const domain = inferDomain(`${ctx.name} ${ctx.description}`);\n return domain\n ? `Set domain — suggest: \"${domain}\" (inferred from content).`\n : \"Specify the business domain this rule belongs to.\";\n },\n },\n ],\n }],\n\n [\"glossary\", {\n idPrefix: \"GT\",\n governedDraft: true,\n descriptionField: \"canonical\",\n defaults: [\n { key: \"category\", value: \"infer\" },\n ],\n recommendedRelationTypes: [\"defines_term_for\", \"confused_with\", \"related_to\", \"references\"],\n inferField: (ctx: CaptureContext) => {\n const fields: Record<string, unknown> = {};\n const area = inferArea(`${ctx.name} ${ctx.description}`);\n if (area) {\n const categoryMap: Record<string, string> = {\n \"Architecture\": \"Platform & Architecture\",\n \"Knowledge Base\": \"Knowledge Management\",\n \"AI & MCP Integration\": \"AI & Developer Tools\",\n \"Developer Experience\": \"AI & Developer Tools\",\n \"Governance & Decision-Making\": \"Governance & Process\",\n \"Analytics & Tracking\": \"Platform & Architecture\",\n \"Security\": \"Platform & Architecture\",\n };\n fields.category = categoryMap[area] ?? \"\";\n }\n return fields;\n },\n qualityChecks: [\n COMMON_CHECKS.clearName,\n {\n id: \"has-canonical\",\n label: \"Canonical definition provided (>20 chars)\",\n check: (ctx) => {\n const canonical = ctx.data.canonical;\n return typeof canonical === \"string\" && canonical.length > 20;\n },\n suggestion: () => \"Add a clear canonical definition — this is the single source of truth for this term.\",\n },\n COMMON_CHECKS.hasRelations,\n {\n id: \"has-category\",\n label: \"Category assigned\",\n check: (ctx) => !!ctx.data.category && ctx.data.category !== \"\",\n suggestion: () => \"Assign a category (e.g., 'Platform & Architecture', 'Governance & Process').\",\n },\n ],\n }],\n\n [\"decisions\", {\n idPrefix: \"DEC\",\n governedDraft: false,\n descriptionField: \"rationale\",\n defaults: [\n { key: \"date\", value: \"today\" },\n { key: \"decidedBy\", value: \"infer\" },\n ],\n recommendedRelationTypes: [\"informs\", \"references\", \"replaces\", \"related_to\"],\n inferField: (ctx: CaptureContext) => {\n const fields: Record<string, unknown> = {};\n const area = inferArea(`${ctx.name} ${ctx.description}`);\n if (area) fields.decidedBy = area;\n return fields;\n },\n qualityChecks: [\n COMMON_CHECKS.clearName,\n {\n id: \"has-rationale\",\n label: \"Rationale provided (>30 chars)\",\n check: (ctx) => {\n const rationale = ctx.data.rationale;\n return typeof rationale === \"string\" && rationale.length > 30;\n },\n suggestion: () => \"Explain why this decision was made — what was considered and rejected?\",\n },\n COMMON_CHECKS.hasRelations,\n {\n id: \"has-date\",\n label: \"Decision date recorded\",\n check: (ctx) => !!ctx.data.date && ctx.data.date !== \"\",\n suggestion: () => \"Record when this decision was made.\",\n },\n ],\n }],\n\n [\"features\", {\n idPrefix: \"FEAT\",\n governedDraft: false,\n descriptionField: \"description\",\n defaults: [] as FieldDefault[],\n recommendedRelationTypes: [\"belongs_to\", \"depends_on\", \"surfaces_tension_in\", \"related_to\"],\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n COMMON_CHECKS.hasRelations,\n {\n id: \"has-owner\",\n label: \"Owner assigned\",\n check: (ctx: CaptureContext) => !!ctx.data.owner && ctx.data.owner !== \"\",\n suggestion: () => \"Assign an owner team or product area.\",\n },\n {\n id: \"has-rationale\",\n label: \"Rationale documented\",\n check: (ctx: CaptureContext) => !!ctx.data.rationale && String(ctx.data.rationale).length > 20,\n suggestion: () => \"Explain why this feature matters — what problem does it solve?\",\n },\n ],\n } satisfies CollectionProfile],\n]);\n\nconst FALLBACK_PROFILE: CollectionProfile = {\n idPrefix: \"\",\n governedDraft: false,\n descriptionField: \"description\",\n defaults: [],\n recommendedRelationTypes: [\"related_to\", \"references\"],\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n COMMON_CHECKS.hasRelations,\n ],\n};\n\n// ── ID Generation ───────────────────────────────────────────────────────────\n\nfunction generateEntryId(prefix: string): string {\n if (!prefix) return \"\";\n const chars = \"abcdefghijklmnopqrstuvwxyz0123456789\";\n let suffix = \"\";\n for (let i = 0; i < 6; i++) {\n suffix += chars[Math.floor(Math.random() * chars.length)];\n }\n return `${prefix}-${suffix}`;\n}\n\n// ── Auto-Linking Logic ──────────────────────────────────────────────────────\n\nfunction extractSearchTerms(name: string, description: string): string {\n const text = `${name} ${description}`;\n return text\n .replace(/[^\\w\\s]/g, \" \")\n .split(/\\s+/)\n .filter((w) => w.length > 3)\n .slice(0, 8)\n .join(\" \");\n}\n\nfunction computeLinkConfidence(\n candidate: { name: string; data?: any; entryId?: string },\n sourceName: string,\n sourceDescription: string,\n sourceCollection: string,\n candidateCollection: string,\n): number {\n const text = `${sourceName} ${sourceDescription}`.toLowerCase();\n const candidateName = candidate.name.toLowerCase();\n let score = 0;\n\n if (text.includes(candidateName) && candidateName.length > 3) {\n score += 40;\n }\n\n const candidateWords = candidateName.split(/\\s+/).filter((w) => w.length > 3);\n const matchingWords = candidateWords.filter((w) => text.includes(w));\n score += (matchingWords.length / Math.max(candidateWords.length, 1)) * 30;\n\n const HUB_COLLECTIONS = new Set([\"strategy\", \"features\"]);\n if (HUB_COLLECTIONS.has(candidateCollection)) {\n score += 15;\n }\n\n if (candidateCollection !== sourceCollection) {\n score += 10;\n }\n\n return Math.min(score, 100);\n}\n\nfunction inferRelationType(\n sourceCollection: string,\n targetCollection: string,\n profile: CollectionProfile,\n): string {\n const typeMap: Record<string, Record<string, string>> = {\n tensions: {\n glossary: \"surfaces_tension_in\",\n \"business-rules\": \"references\",\n strategy: \"belongs_to\",\n features: \"surfaces_tension_in\",\n decisions: \"references\",\n },\n \"business-rules\": {\n glossary: \"references\",\n features: \"governs\",\n strategy: \"belongs_to\",\n tensions: \"references\",\n },\n glossary: {\n features: \"defines_term_for\",\n \"business-rules\": \"references\",\n strategy: \"references\",\n },\n decisions: {\n features: \"informs\",\n \"business-rules\": \"references\",\n strategy: \"references\",\n tensions: \"references\",\n },\n };\n\n return typeMap[sourceCollection]?.[targetCollection]\n ?? profile.recommendedRelationTypes[0]\n ?? \"related_to\";\n}\n\n// ── Quality Scoring ─────────────────────────────────────────────────────────\n\ninterface QualityResult {\n score: number;\n maxScore: number;\n checks: Array<{ id: string; label: string; passed: boolean; suggestion?: string }>;\n}\n\nfunction scoreQuality(ctx: CaptureContext, profile: CollectionProfile): QualityResult {\n const checks = profile.qualityChecks.map((qc) => {\n const passed = qc.check(ctx);\n return {\n id: qc.id,\n label: qc.label,\n passed,\n suggestion: passed ? undefined : qc.suggestion?.(ctx),\n };\n });\n\n const passed = checks.filter((c) => c.passed).length;\n const total = checks.length;\n const score = total > 0 ? Math.round((passed / total) * 10) : 10;\n\n return { score, maxScore: 10, checks };\n}\n\nexport function formatQualityReport(result: QualityResult): string {\n const lines: string[] = [`## Quality: ${result.score}/${result.maxScore}`];\n for (const check of result.checks) {\n const icon = check.passed ? \"[x]\" : \"[ ]\";\n const suggestion = check.passed ? \"\" : ` -- ${check.suggestion ?? \"\"}`;\n lines.push(`${icon} ${check.label}${suggestion}`);\n }\n return lines.join(\"\\n\");\n}\n\n// ── Exported: quality-check for existing entries ────────────────────────────\n\nexport async function checkEntryQuality(entryId: string): Promise<{ text: string; quality: QualityResult }> {\n const entry = await mcpQuery<any>(\"kb.getEntry\", { entryId });\n if (!entry) {\n return {\n text: `Entry \\`${entryId}\\` not found. Try kb-search to find the right ID.`,\n quality: { score: 0, maxScore: 10, checks: [] },\n };\n }\n\n const collections = await mcpQuery<any[]>(\"kb.listCollections\");\n const collMap = new Map<string, string>();\n for (const c of collections) collMap.set(c._id, c.slug);\n const collectionSlug = collMap.get(entry.collectionId) ?? \"unknown\";\n\n const profile = PROFILES.get(collectionSlug) ?? FALLBACK_PROFILE;\n\n const relations = await mcpQuery<any[]>(\"kb.listEntryRelations\", { entryId });\n const linksCreated: LinkResult[] = [];\n for (const r of relations) {\n const otherId = r.fromId === entry._id ? r.toId : r.fromId;\n linksCreated.push({\n targetEntryId: otherId,\n targetName: \"\",\n targetCollection: \"\",\n relationType: r.type,\n });\n }\n\n const descField = profile.descriptionField;\n const description = typeof entry.data?.[descField] === \"string\" ? entry.data[descField] : \"\";\n\n const ctx: CaptureContext = {\n collection: collectionSlug,\n name: entry.name,\n description,\n data: entry.data ?? {},\n entryId: entry.entryId ?? \"\",\n linksCreated,\n linksSuggested: [],\n collectionFields: [],\n };\n\n const quality = scoreQuality(ctx, profile);\n\n const lines: string[] = [\n `# Quality Check: ${entry.entryId ?? entry.name}`,\n `**${entry.name}** in \\`${collectionSlug}\\` [${entry.status}]`,\n \"\",\n formatQualityReport(quality),\n ];\n\n if (quality.score < 10) {\n const failedChecks = quality.checks.filter((c) => !c.passed && c.suggestion);\n if (failedChecks.length > 0) {\n lines.push(\"\");\n lines.push(`_To improve: use \\`update-entry\\` to fill missing fields, or \\`relate-entries\\` to add connections._`);\n }\n }\n\n return { text: lines.join(\"\\n\"), quality };\n}\n\n// ── Tool Registration ───────────────────────────────────────────────────────\n\nconst GOVERNED_COLLECTIONS = new Set([\n \"glossary\", \"business-rules\", \"principles\", \"standards\", \"strategy\", \"features\",\n]);\n\nconst AUTO_LINK_CONFIDENCE_THRESHOLD = 35;\nconst MAX_AUTO_LINKS = 5;\nconst MAX_SUGGESTIONS = 5;\n\nexport function registerSmartCaptureTools(server: McpServer) {\n\n server.registerTool(\n \"smart-capture\",\n {\n title: \"Smart Capture\",\n description:\n \"One-call knowledge capture: creates an entry, auto-links related entries, and returns a quality scorecard. \" +\n \"Replaces the multi-step workflow of create-entry + suggest-links + relate-entries. \" +\n \"Provide a collection, name, and description — everything else is inferred or auto-filled.\\n\\n\" +\n \"Supported collections with smart profiles: tensions, business-rules, glossary, decisions.\\n\" +\n \"All other collections use sensible defaults (same as quick-capture + auto-linking).\\n\\n\" +\n \"Always creates as 'draft' for governed collections. Embodies 'Capture Now, Curate Later' (PRI-81cbdq).\",\n inputSchema: {\n collection: z.string().describe(\"Collection slug, e.g. 'tensions', 'business-rules', 'glossary', 'decisions'\"),\n name: z.string().describe(\"Display name — be specific (e.g. 'Convex adjacency list won't scale for graph traversal')\"),\n description: z.string().describe(\"Full context — what's happening, why it matters, what you observed\"),\n context: z.string().optional().describe(\"Optional additional context (e.g. 'Observed during gather-context calls taking 700ms+')\"),\n entryId: z.string().optional().describe(\"Optional custom entry ID (e.g. 'TEN-my-id'). Auto-generated if omitted.\"),\n },\n annotations: { destructiveHint: false },\n },\n async ({ collection, name, description, context, entryId }) => {\n const profile = PROFILES.get(collection) ?? FALLBACK_PROFILE;\n\n // 1. Resolve collection schema\n const col = await mcpQuery<any>(\"kb.getCollection\", { slug: collection });\n if (!col) {\n return {\n content: [{ type: \"text\" as const, text: `Collection \\`${collection}\\` not found. Use \\`list-collections\\` to see available collections.` }],\n };\n }\n\n // 2. Build data with profile defaults + inference\n const data: Record<string, unknown> = {};\n const today = new Date().toISOString().split(\"T\")[0];\n\n for (const field of col.fields ?? []) {\n const key = field.key as string;\n if (key === profile.descriptionField) {\n data[key] = description;\n } else if (field.type === \"array\" || field.type === \"multi-select\") {\n data[key] = [];\n } else {\n data[key] = \"\";\n }\n }\n\n for (const def of profile.defaults) {\n if (def.value === \"today\") {\n data[def.key] = today;\n } else if (def.value !== \"infer\") {\n data[def.key] = def.value;\n }\n }\n\n if (profile.inferField) {\n const inferred = profile.inferField({\n collection, name, description, context, data, entryId: \"\",\n linksCreated: [], linksSuggested: [], collectionFields: col.fields ?? [],\n });\n for (const [key, val] of Object.entries(inferred)) {\n if (val !== undefined && val !== \"\") {\n data[key] = val;\n }\n }\n }\n\n if (!data[profile.descriptionField] && !data.description && !data.canonical) {\n data[profile.descriptionField || \"description\"] = description;\n }\n\n // 3. Determine status\n const status = GOVERNED_COLLECTIONS.has(collection) ? \"draft\" : \"draft\";\n\n // 4. Generate entry ID\n const finalEntryId = entryId ?? generateEntryId(profile.idPrefix);\n\n // 5. Create entry\n let internalId: string;\n try {\n internalId = await mcpMutation<string>(\"kb.createEntry\", {\n collectionSlug: collection,\n entryId: finalEntryId || undefined,\n name,\n status,\n data,\n createdBy: \"smart-capture\",\n });\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : String(error);\n if (msg.includes(\"Duplicate\") || msg.includes(\"already exists\")) {\n return {\n content: [{\n type: \"text\" as const,\n text: `# Cannot Capture — Duplicate Detected\\n\\n${msg}\\n\\nUse \\`get-entry\\` to inspect the existing entry, or \\`update-entry\\` to modify it.`,\n }],\n };\n }\n throw error;\n }\n\n // 6. Discover and auto-link related entries\n const linksCreated: LinkResult[] = [];\n const linksSuggested: LinkSuggestion[] = [];\n\n const searchQuery = extractSearchTerms(name, description);\n if (searchQuery) {\n const [searchResults, allCollections] = await Promise.all([\n mcpQuery<any[]>(\"kb.searchEntries\", { query: searchQuery }),\n mcpQuery<any[]>(\"kb.listCollections\"),\n ]);\n\n const collMap = new Map<string, string>();\n for (const c of allCollections) collMap.set(c._id, c.slug);\n\n const candidates = (searchResults ?? [])\n .filter((r) => r.entryId !== finalEntryId && r._id !== internalId)\n .map((r) => ({\n ...r,\n collSlug: collMap.get(r.collectionId) ?? \"unknown\",\n confidence: computeLinkConfidence(r, name, description, collection, collMap.get(r.collectionId) ?? \"unknown\"),\n }))\n .sort((a, b) => b.confidence - a.confidence);\n\n // Auto-link high-confidence matches\n for (const c of candidates) {\n if (linksCreated.length >= MAX_AUTO_LINKS) break;\n if (c.confidence < AUTO_LINK_CONFIDENCE_THRESHOLD) break;\n if (!c.entryId || !finalEntryId) continue;\n\n const relationType = inferRelationType(collection, c.collSlug, profile);\n try {\n await mcpMutation(\"kb.createEntryRelation\", {\n fromEntryId: finalEntryId,\n toEntryId: c.entryId,\n type: relationType,\n });\n linksCreated.push({\n targetEntryId: c.entryId,\n targetName: c.name,\n targetCollection: c.collSlug,\n relationType,\n });\n } catch {\n // Relation creation failed (e.g. entry not found) — skip silently\n }\n }\n\n // Collect suggestions for remaining candidates\n const linkedIds = new Set(linksCreated.map((l) => l.targetEntryId));\n for (const c of candidates) {\n if (linksSuggested.length >= MAX_SUGGESTIONS) break;\n if (linkedIds.has(c.entryId)) continue;\n if (c.confidence < 10) continue;\n\n const preview = extractPreview(c.data, 80);\n const reason = c.confidence >= AUTO_LINK_CONFIDENCE_THRESHOLD\n ? \"high relevance (already linked)\"\n : `\"${c.name.toLowerCase().split(/\\s+/).filter((w: string) => `${name} ${description}`.toLowerCase().includes(w) && w.length > 3).slice(0, 2).join('\", \"')}\" appears in content`;\n\n linksSuggested.push({\n entryId: c.entryId,\n name: c.name,\n collection: c.collSlug,\n reason,\n preview,\n });\n }\n }\n\n // 7. Score quality\n const captureCtx: CaptureContext = {\n collection,\n name,\n description,\n context,\n data,\n entryId: finalEntryId,\n linksCreated,\n linksSuggested,\n collectionFields: col.fields ?? [],\n };\n const quality = scoreQuality(captureCtx, profile);\n\n // 8. Format response\n const lines: string[] = [\n `# Captured: ${finalEntryId || name}`,\n `**${name}** added to \\`${collection}\\` as \\`${status}\\``,\n ];\n\n if (linksCreated.length > 0) {\n lines.push(\"\");\n lines.push(`## Auto-linked (${linksCreated.length})`);\n for (const link of linksCreated) {\n lines.push(`- -> **${link.relationType}** ${link.targetEntryId}: ${link.targetName} [${link.targetCollection}]`);\n }\n }\n\n if (linksSuggested.length > 0) {\n lines.push(\"\");\n lines.push(\"## Suggested links (review and use relate-entries)\");\n for (let i = 0; i < linksSuggested.length; i++) {\n const s = linksSuggested[i];\n const preview = s.preview ? ` — ${s.preview}` : \"\";\n lines.push(`${i + 1}. **${s.entryId ?? \"(no ID)\"}**: ${s.name} [${s.collection}]${preview}`);\n }\n }\n\n lines.push(\"\");\n lines.push(formatQualityReport(quality));\n\n const failedChecks = quality.checks.filter((c) => !c.passed);\n if (failedChecks.length > 0) {\n lines.push(\"\");\n lines.push(`_To improve: \\`update-entry entryId=\"${finalEntryId}\"\\` to fill missing fields._`);\n }\n\n return { content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }] };\n }\n );\n\n // ── Quality Check Tool ──────────────────────────────────────────────────\n\n server.registerTool(\n \"quality-check\",\n {\n title: \"Quality Check\",\n description:\n \"Score an existing knowledge entry against collection-specific quality criteria. \" +\n \"Returns a scorecard (X/10) with specific, actionable suggestions for improvement. \" +\n \"Checks: name clarity, description completeness, relation connectedness, and collection-specific fields.\\n\\n\" +\n \"Use after creating entries to assess their quality, or to audit existing entries.\",\n inputSchema: {\n entryId: z.string().describe(\"Entry ID to check, e.g. 'TEN-graph-db', 'GT-019', 'SOS-006'\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ entryId }) => {\n const result = await checkEntryQuality(entryId);\n return { content: [{ type: \"text\" as const, text: result.text }] };\n }\n );\n}\n\n// ── Helpers ────────────────────────────────────────────────────────────────\n\nfunction extractPreview(data: any, maxLen: number): string {\n if (!data || typeof data !== \"object\") return \"\";\n const raw = data.description ?? data.canonical ?? data.detail ?? data.rule ?? \"\";\n if (typeof raw !== \"string\" || !raw) return \"\";\n return raw.length > maxLen ? raw.substring(0, maxLen) + \"...\" : raw;\n}\n","/**\n * Architecture tools — ARCH-node-mcp (Core layer)\n *\n * Tools: show-architecture, explore-layer, show-flow, seed-architecture, check-architecture\n * KB refs: FEAT-ARCH-001 (Architecture Explorer), FEAT-dep-health (Dep Health Scanner),\n * SOS-dbl1n9 (dependency direction rule), GT-wk426u (Fitness Function)\n */\n\nimport { existsSync, readFileSync, readdirSync, statSync } from \"node:fs\";\nimport { resolve, relative, dirname, normalize } from \"node:path\";\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport { mcpQuery, mcpMutation } from \"../client.js\";\n\nconst COLLECTION_SLUG = \"architecture\";\n\nconst COLLECTION_FIELDS = [\n { key: \"archType\", label: \"Architecture Type\", type: \"select\", required: true, options: [\"template\", \"layer\", \"node\", \"flow\"], searchable: true },\n { key: \"templateRef\", label: \"Template Entry ID\", type: \"text\", searchable: false },\n { key: \"layerRef\", label: \"Layer Entry ID\", type: \"text\", searchable: false },\n { key: \"description\", label: \"Description\", type: \"text\", searchable: true },\n { key: \"color\", label: \"Color\", type: \"text\" },\n { key: \"icon\", label: \"Icon\", type: \"text\" },\n { key: \"sourceNode\", label: \"Source Node (flows)\", type: \"text\" },\n { key: \"targetNode\", label: \"Target Node (flows)\", type: \"text\" },\n { key: \"filePaths\", label: \"File Paths\", type: \"text\", searchable: true },\n { key: \"owner\", label: \"Owner (circle/role)\", type: \"text\", searchable: true },\n { key: \"layerOrder\", label: \"Layer Order (for templates)\", type: \"text\" },\n { key: \"rationale\", label: \"Why Here? (placement rationale)\", type: \"text\", searchable: true },\n { key: \"dependsOn\", label: \"Allowed Dependencies (layers this can import from)\", type: \"text\" },\n];\n\ninterface ArchEntry {\n _id: string;\n entryId: string;\n name: string;\n status: string;\n order: number;\n data: Record<string, unknown>;\n tags?: string[];\n}\n\nasync function ensureCollection(): Promise<void> {\n const collections = await mcpQuery<any[]>(\"kb.listCollections\");\n if (collections.some((c: any) => c.slug === COLLECTION_SLUG)) return;\n\n await mcpMutation(\"kb.createCollection\", {\n slug: COLLECTION_SLUG,\n name: \"Architecture\",\n icon: \"🏗️\",\n description:\n \"System architecture map — templates, layers, nodes, and flows. \" +\n \"Visualized in the Architecture Explorer and via MCP architecture tools.\",\n fields: COLLECTION_FIELDS,\n });\n}\n\nasync function listArchEntries(): Promise<ArchEntry[]> {\n return mcpQuery<ArchEntry[]>(\"kb.listEntries\", { collectionSlug: COLLECTION_SLUG });\n}\n\nfunction byTag(entries: ArchEntry[], archType: string): ArchEntry[] {\n return entries\n .filter((e) => e.tags?.includes(`archType:${archType}`) || e.data?.archType === archType)\n .sort((a, b) => (a.order ?? 0) - (b.order ?? 0));\n}\n\n// ── HTML renderer for MCP Apps ──────────────────────────────────────────────\n\nfunction renderArchitectureHtml(\n layers: ArchEntry[],\n nodes: ArchEntry[],\n flows: ArchEntry[],\n templateName: string,\n): string {\n const layerHtml = layers.map((layer) => {\n const layerNodes = nodes.filter(\n (n) => n.data?.layerRef === layer.entryId\n );\n const nodeCards = layerNodes.map((n) => `\n <div class=\"node\" title=\"${escHtml(String(n.data?.description ?? ''))}\">\n <span class=\"node-icon\">${escHtml(String(n.data?.icon ?? '◻'))}</span>\n <span class=\"node-name\">${escHtml(n.name)}</span>\n </div>\n `).join(\"\");\n\n return `\n <div class=\"layer\" style=\"--layer-color: ${escHtml(String(layer.data?.color ?? '#666'))}\">\n <div class=\"layer-label\">\n <span class=\"layer-dot\"></span>\n <span class=\"layer-name\">${escHtml(layer.name)}</span>\n <span class=\"layer-count\">${layerNodes.length}</span>\n </div>\n <div class=\"layer-desc\">${escHtml(String(layer.data?.description ?? ''))}</div>\n <div class=\"nodes\">${nodeCards || '<span class=\"empty\">No components</span>'}</div>\n </div>\n `;\n }).join(\"\");\n\n return `<!DOCTYPE html>\n<html><head><meta charset=\"utf-8\"><style>\n*{margin:0;padding:0;box-sizing:border-box}\nbody{font-family:-apple-system,system-ui,sans-serif;background:#1a1a2e;color:#e0e0e0;padding:16px}\nh1{font-size:14px;font-weight:600;color:#a0a0c0;margin-bottom:12px;letter-spacing:.04em}\n.layer{border-left:3px solid var(--layer-color);padding:8px 12px;margin-bottom:8px;background:rgba(255,255,255,.03);border-radius:0 6px 6px 0}\n.layer-label{display:flex;align-items:center;gap:8px;margin-bottom:4px}\n.layer-dot{width:8px;height:8px;border-radius:50%;background:var(--layer-color)}\n.layer-name{font-size:12px;font-weight:600;color:#fff;letter-spacing:.03em}\n.layer-count{font-size:10px;color:var(--layer-color);background:rgba(255,255,255,.06);padding:1px 6px;border-radius:8px}\n.layer-desc{font-size:11px;color:#888;margin-bottom:6px}\n.nodes{display:flex;flex-wrap:wrap;gap:6px}\n.node{display:flex;align-items:center;gap:4px;background:rgba(255,255,255,.06);border:1px solid rgba(255,255,255,.08);border-radius:4px;padding:4px 8px;font-size:11px;cursor:default;transition:border-color .15s}\n.node:hover{border-color:var(--layer-color)}\n.node-icon{font-size:12px}\n.node-name{color:#ddd}\n.empty{font-size:11px;color:#555;font-style:italic}\n.flows{margin-top:12px;border-top:1px solid rgba(255,255,255,.06);padding-top:8px}\n.flow{font-size:11px;color:#888;padding:2px 0}\n.flow-arrow{color:#6366f1;margin:0 4px}\n</style></head><body>\n<h1>${escHtml(templateName)}</h1>\n${layerHtml}\n${flows.length > 0 ? `<div class=\"flows\"><div style=\"font-size:10px;color:#666;margin-bottom:4px;letter-spacing:.06em\">DATA FLOWS</div>${flows.map((f) => `<div class=\"flow\">${escHtml(f.name)}<span class=\"flow-arrow\">→</span><span style=\"color:#aaa\">${escHtml(String(f.data?.description ?? ''))}</span></div>`).join(\"\")}</div>` : \"\"}\n</body></html>`;\n}\n\nfunction escHtml(s: string): string {\n return s.replace(/&/g, \"&\").replace(/</g, \"<\").replace(/>/g, \">\").replace(/\"/g, \""\");\n}\n\nfunction formatLayerText(layer: ArchEntry, nodes: ArchEntry[]): string {\n const layerNodes = nodes.filter((n) => n.data?.layerRef === layer.entryId);\n const nodeList = layerNodes.map((n) => {\n const desc = n.data?.description ? ` — ${n.data.description}` : \"\";\n const owner = n.data?.owner ? ` (${n.data.owner})` : \"\";\n return ` - ${n.data?.icon ?? \"◻\"} **${n.name}**${desc}${owner}`;\n }).join(\"\\n\");\n return `### ${layer.name}\\n${layer.data?.description ?? \"\"}\\n\\n${nodeList || \" _No components_\"}`;\n}\n\n// ── Seed data: Product OS default architecture ──────────────────────────────\n\nconst SEED_TEMPLATE = {\n entryId: \"ARCH-tpl-product-os\",\n name: \"Product OS Default\",\n data: {\n archType: \"template\",\n description: \"Default 4-layer architecture: Auth → Infrastructure → Core → Features, with an outward Integration layer\",\n layerOrder: JSON.stringify([\"auth\", \"infrastructure\", \"core\", \"features\", \"integration\"]),\n },\n};\n\nconst SEED_LAYERS = [\n { entryId: \"ARCH-layer-auth\", name: \"Auth\", order: 0, data: { archType: \"layer\", templateRef: \"ARCH-tpl-product-os\", color: \"#22c55e\", description: \"Authentication, user management, and workspace-scoped access control\", icon: \"🔐\", dependsOn: \"none\", rationale: \"Foundation layer. Auth depends on nothing — it is the first gate. No layer may bypass auth. All other layers depend on auth to know who the user is and which workspace they belong to.\" } },\n { entryId: \"ARCH-layer-infra\", name: \"Infrastructure\", order: 1, data: { archType: \"layer\", templateRef: \"ARCH-tpl-product-os\", color: \"#ec4899\", description: \"Real-time data, event tracking, and AI model infrastructure\", icon: \"⚙️\", dependsOn: \"Auth\", rationale: \"Infrastructure sits on top of Auth. It provides the database, analytics, and AI plumbing that Core and Features consume. Infra may import from Auth (needs workspace context) but never from Core or Features.\" } },\n { entryId: \"ARCH-layer-core\", name: \"Core\", order: 2, data: { archType: \"layer\", templateRef: \"ARCH-tpl-product-os\", color: \"#8b5cf6\", description: \"Business logic, knowledge graph, workflow engines, and MCP tooling\", icon: \"🧠\", dependsOn: \"Auth, Infrastructure\", rationale: \"Core contains business logic and engines that are UI-agnostic. It may import from Auth and Infra. Features depend on Core, but Core must never depend on Features — this is what keeps engines reusable across different UIs.\" } },\n { entryId: \"ARCH-layer-features\", name: \"Features\", order: 3, data: { archType: \"layer\", templateRef: \"ARCH-tpl-product-os\", color: \"#6366f1\", description: \"User-facing pages, components, and feature modules\", icon: \"✦\", dependsOn: \"Auth, Infrastructure, Core\", rationale: \"Features are the user-facing layer — SvelteKit routes, components, and page-level logic. Features may import from any lower layer but nothing above may import from Features. This is the outermost application layer.\" } },\n { entryId: \"ARCH-layer-integration\", name: \"Integration\", order: 4, data: { archType: \"layer\", templateRef: \"ARCH-tpl-product-os\", color: \"#f59e0b\", description: \"Outward connections to external tools, IDEs, and services\", icon: \"🔌\", dependsOn: \"Core\", rationale: \"Integration is a lateral/outward layer — it connects to external systems (IDE, GitHub, Linear). It depends on Core (to expose knowledge) but sits outside the main stack. External tools call into Core via Integration, never directly into Features.\" } },\n];\n\nconst SEED_NODES = [\n // Auth layer\n { entryId: \"ARCH-node-clerk\", name: \"Clerk\", order: 0, data: { archType: \"node\", layerRef: \"ARCH-layer-auth\", color: \"#22c55e\", icon: \"🔑\", description: \"Authentication provider — sign-in/sign-up pages, session management, organization-level access control. UserSync.svelte is cross-cutting layout glue (not mapped here).\", filePaths: \"src/routes/sign-in/, src/routes/sign-up/\", owner: \"Platform\", rationale: \"Auth layer because Clerk is the identity gate. Every request flows through auth first. No other layer provides identity.\" } },\n { entryId: \"ARCH-node-workspace\", name: \"Workspace Scoping\", order: 1, data: { archType: \"node\", layerRef: \"ARCH-layer-auth\", color: \"#22c55e\", icon: \"🏢\", description: \"Multi-tenancy anchor — all data is workspace-scoped via workspaceId\", filePaths: \"src/lib/stores/workspace.ts, convex/workspaces.ts\", owner: \"Platform\", rationale: \"Auth layer because workspace scoping is the second gate after identity. All queries require workspaceId, making this foundational.\" } },\n\n // Infrastructure layer\n { entryId: \"ARCH-node-convex\", name: \"Convex\", order: 0, data: { archType: \"node\", layerRef: \"ARCH-layer-infra\", color: \"#ec4899\", icon: \"⚡\", description: \"Reactive database with real-time sync, serverless functions, and type-safe API generation. Unified Collections + Entries model\", filePaths: \"convex/schema.ts, convex/entries.ts, convex/http.ts\", owner: \"Platform\", rationale: \"Infrastructure because Convex is raw persistence and reactivity plumbing. It stores data but has no business logic opinions. Core and Features consume it.\" } },\n { entryId: \"ARCH-node-posthog\", name: \"PostHog\", order: 1, data: { archType: \"node\", layerRef: \"ARCH-layer-infra\", color: \"#ec4899\", icon: \"📊\", description: \"Product analytics — workspace-scoped events, feature flags, session replay\", filePaths: \"src/lib/analytics.ts, src/lib/components/PostHogWorkspaceSync.svelte\", owner: \"Platform\", rationale: \"Infrastructure because PostHog is analytics plumbing — event collection and aggregation. It has no knowledge of business domains.\" } },\n { entryId: \"ARCH-node-openrouter\", name: \"OpenRouter\", order: 2, data: { archType: \"node\", layerRef: \"ARCH-layer-infra\", color: \"#ec4899\", icon: \"🤖\", description: \"AI model routing for ChainWork artifact generation — streaming responses with format-aware prompts\", filePaths: \"src/routes/api/chainwork/generate/+server.ts\", owner: \"ChainWork\", rationale: \"Infrastructure because OpenRouter is an AI model gateway — it routes prompts to models. The strategy logic lives in ChainWork Engine (Core); this is just the pipe.\" } },\n\n // Core layer\n { entryId: \"ARCH-node-mcp\", name: \"MCP Server\", order: 0, data: { archType: \"node\", layerRef: \"ARCH-layer-core\", color: \"#8b5cf6\", icon: \"🔧\", description: \"20+ tools exposing the knowledge graph as AI-consumable operations — smart-capture, context assembly, verification, quality checks\", filePaths: \"packages/mcp-server/src/index.ts, packages/mcp-server/src/tools/\", owner: \"AI DX\", rationale: \"Core layer because the MCP server encodes business operations — smart-capture with auto-linking, quality scoring, governance rules. It depends on Infra (Convex) but never touches UI routes. Why not Infrastructure? Because it has domain opinions. Why not Features? Because it has no UI — any client (Cursor, CLI, API) can call it.\" } },\n { entryId: \"ARCH-node-knowledge-graph\", name: \"Knowledge Graph\", order: 1, data: { archType: \"node\", layerRef: \"ARCH-layer-core\", color: \"#8b5cf6\", icon: \"🕸️\", description: \"20 collections, 170+ entries with typed cross-collection relations. Smart capture, auto-linking, quality scoring\", filePaths: \"convex/mcpKnowledge.ts, convex/entries.ts\", owner: \"Knowledge\", rationale: \"Core layer because the knowledge graph IS the domain model — collections, entries, relations, versioning, quality scoring. Glossary DATA, business rules DATA, tension DATA all live here. Why not Features? Because the data model exists independently of any page. The Glossary page in Features is just one way to visualize terms that Core owns. Think: Core owns the dictionary, Features owns the dictionary app.\" } },\n { entryId: \"ARCH-node-governance\", name: \"Governance Engine\", order: 2, data: { archType: \"node\", layerRef: \"ARCH-layer-core\", color: \"#8b5cf6\", icon: \"⚖️\", description: \"Circles, roles, consent-based decision-making, tension processing with IDM-inspired async workflows\", filePaths: \"convex/versioning.ts, src/lib/components/versioning/\", owner: \"Governance\", rationale: \"Core layer because governance logic (draft→publish workflows, consent-based decisions, tension status rules) is business process that multiple UIs consume. Why not Features? Because the versioning system and proposal flow are reusable engines — the Governance Pages in Features are just one rendering of these rules.\" } },\n { entryId: \"ARCH-node-chainwork-engine\", name: \"ChainWork Engine\", order: 3, data: { archType: \"node\", layerRef: \"ARCH-layer-core\", color: \"#8b5cf6\", icon: \"⛓\", description: \"Guided strategy creation through 5-step coherence chain — AI-generated artifacts with scoring and achievements\", filePaths: \"src/lib/components/chainwork/config.ts, src/lib/components/chainwork/scoring.ts\", owner: \"ChainWork\", rationale: \"Core layer because the coherence chain logic, scoring algorithm, and quality gates are business rules. config.ts defines chain steps, scoring.ts computes quality — these could power a CLI or API. Why not Features? Because the ChainWork UI wizard in Features is just one skin over this engine.\" } },\n\n // Features layer\n { entryId: \"ARCH-node-command-center\", name: \"Command Center\", order: 0, data: { archType: \"node\", layerRef: \"ARCH-layer-features\", color: \"#6366f1\", icon: \"⬡\", description: \"Calm home screen — daily orientation, triage mode, pulse metrics, momentum tracking\", filePaths: \"src/routes/+page.svelte, src/lib/components/command-center/\", owner: \"Command Center\", rationale: \"Features layer because the Command Center is a SvelteKit page — PulseMetrics, NeedsAttention, DailyBriefing are UI components that assemble data from Core queries. Why not Core? Because it has no reusable business logic or engines — it is pure layout and presentation. If you deleted this page, no business rule would break.\" } },\n { entryId: \"ARCH-node-chainwork-ui\", name: \"ChainWork UI\", order: 1, data: { archType: \"node\", layerRef: \"ARCH-layer-features\", color: \"#6366f1\", icon: \"⛓\", description: \"Multi-step wizard for strategy artifact creation — setup, chain steps, quality gates, output\", filePaths: \"src/routes/chainwork/, src/lib/components/chainwork/\", owner: \"ChainWork\", rationale: \"Features layer because the ChainWork UI is the wizard interface — step navigation, form inputs, output rendering. Why not Core? Because the scoring logic and chain config ARE in Core (ChainWork Engine). This is the presentation skin over that engine. Delete this page and the engine still works via MCP.\" } },\n { entryId: \"ARCH-node-glossary\", name: \"Glossary\", order: 2, data: { archType: \"node\", layerRef: \"ARCH-layer-features\", color: \"#6366f1\", icon: \"Aa\", description: \"Canonical vocabulary management — term detail, code drift detection, inline term linking\", filePaths: \"src/routes/glossary/, src/lib/components/glossary/\", owner: \"Knowledge\", rationale: \"Features layer — NOT Core or Infrastructure — because this is the Glossary PAGE: the SvelteKit route for browsing, editing, and viewing terms. Why not Core? Because Core owns the glossary DATA (Knowledge Graph) and term-linking logic. The MCP server also accesses glossary terms without any page. Why not Infrastructure? Because glossary is domain-specific vocabulary, not generic plumbing. The page is one consumer of the data — Core owns the dictionary, Features owns the dictionary app.\" } },\n { entryId: \"ARCH-node-tensions\", name: \"Tensions\", order: 3, data: { archType: \"node\", layerRef: \"ARCH-layer-features\", color: \"#6366f1\", icon: \"⚡\", description: \"Tension capture and processing — raise, triage, resolve through governance workflow\", filePaths: \"src/routes/tensions/, src/lib/components/tensions/\", owner: \"Governance\", rationale: \"Features layer because this is the Tensions PAGE — the UI for raising, listing, and viewing tensions. Why not Core? Because tension data, status rules (SOS-020), and processing logic already live in Core (Governance Engine). This page is a form + list view that reads from and writes to Core. Delete it and the governance engine still processes tensions via MCP.\" } },\n { entryId: \"ARCH-node-strategy\", name: \"Strategy\", order: 4, data: { archType: \"node\", layerRef: \"ARCH-layer-features\", color: \"#6366f1\", icon: \"▣\", description: \"Product strategy pages — vision, ecosystem, product areas, decision frameworks, sequencing\", filePaths: \"src/routes/strategy/, src/routes/bridge/, src/routes/topology/\", owner: \"Strategy\", rationale: \"Features layer because Strategy is a set of SvelteKit pages (strategy, bridge, topology) that visualize strategy entries. Why not Core? Because the strategy data (vision, principles, ecosystem layers) lives in the Knowledge Graph (Core). These pages render and allow inline editing — they consume Core downward.\" } },\n { entryId: \"ARCH-node-governance-ui\", name: \"Governance Pages\", order: 5, data: { archType: \"node\", layerRef: \"ARCH-layer-features\", color: \"#6366f1\", icon: \"◈\", description: \"Roles, circles, principles, policies, decisions, proposals, business rules\", filePaths: \"src/routes/roles/, src/routes/circles/, src/routes/decisions/, src/routes/proposals/\", owner: \"Governance\", rationale: \"Features layer because these are governance PAGES — list views and detail views for roles, circles, decisions, proposals. Why not Core? Because the governance ENGINE (versioning, consent, IDM) IS in Core. These pages are the user-facing window into governance data. The logic doesn't live here, only the rendering.\" } },\n { entryId: \"ARCH-node-artifacts\", name: \"Artifacts\", order: 6, data: { archType: \"node\", layerRef: \"ARCH-layer-features\", color: \"#6366f1\", icon: \"📄\", description: \"Strategy artifacts from ChainWork — pitches, briefs, one-pagers linked to the knowledge graph\", filePaths: \"src/routes/artifacts/\", owner: \"ChainWork\", rationale: \"Features layer because the Artifacts page is a list/detail view for strategy artifacts produced by ChainWork. Why not Core? Because artifact data and scoring live in Core (ChainWork Engine). This page just renders the output and links back to the knowledge graph.\" } },\n\n // Integration layer\n { entryId: \"ARCH-node-cursor\", name: \"Cursor IDE\", order: 0, data: { archType: \"node\", layerRef: \"ARCH-layer-integration\", color: \"#f59e0b\", icon: \"🖥️\", description: \"AI-assisted development with MCP-powered knowledge context — smart capture, verification, context assembly in the editor\", filePaths: \".cursor/mcp.json, .cursor/rules/\", owner: \"AI DX\", rationale: \"Integration layer because Cursor is an external tool that connects INTO our system via MCP. Why not Core or Features? Because Cursor itself is not our code — it's a consumer. The .cursor/ config files define how it talks to us, but Cursor lives outside our deployment boundary.\" } },\n { entryId: \"ARCH-node-github\", name: \"GitHub\", order: 1, data: { archType: \"node\", layerRef: \"ARCH-layer-integration\", color: \"#f59e0b\", icon: \"🐙\", description: \"Code repository, PR reviews with governance context, CI/CD\", owner: \"Platform\", rationale: \"Integration layer because GitHub is an external service. Why not Infrastructure? Because Infra is about plumbing we control (database, analytics). GitHub is a third-party that hooks into our Core (PR reviews checking governance rules) but is not part of our deployed application.\" } },\n { entryId: \"ARCH-node-linear\", name: \"Linear (planned)\", order: 2, data: { archType: \"node\", layerRef: \"ARCH-layer-integration\", color: \"#f59e0b\", icon: \"📐\", description: \"Issue tracking, roadmap sync, tension-to-issue pipeline (planned integration)\", owner: \"Platform\", rationale: \"Integration layer because Linear is an external issue tracker. Why not Infrastructure? Because Infra is generic plumbing we run. Linear is a third-party tool we connect to. The planned pipeline bridges tensions (Core) to Linear issues — a classic outward integration pattern.\" } },\n];\n\nconst SEED_FLOWS = [\n { entryId: \"ARCH-flow-smart-capture\", name: \"Smart Capture Flow\", order: 0, data: { archType: \"flow\", sourceNode: \"ARCH-node-cursor\", targetNode: \"ARCH-node-knowledge-graph\", description: \"Developer/AI calls smart-capture via MCP → entry created with auto-linking and quality score → stored in Knowledge Graph\", color: \"#8b5cf6\" } },\n { entryId: \"ARCH-flow-governance\", name: \"Governance Flow\", order: 1, data: { archType: \"flow\", sourceNode: \"ARCH-node-tensions\", targetNode: \"ARCH-node-governance\", description: \"Tension raised → appears in Command Center → triaged → processed via IDM → decision logged\", color: \"#6366f1\" } },\n { entryId: \"ARCH-flow-chainwork\", name: \"ChainWork Strategy Flow\", order: 2, data: { archType: \"flow\", sourceNode: \"ARCH-node-chainwork-ui\", targetNode: \"ARCH-node-artifacts\", description: \"Leader opens ChainWork → walks coherence chain → AI generates artifact → scored and published to knowledge graph\", color: \"#f59e0b\" } },\n { entryId: \"ARCH-flow-knowledge-trust\", name: \"Knowledge Trust Flow\", order: 3, data: { archType: \"flow\", sourceNode: \"ARCH-node-mcp\", targetNode: \"ARCH-node-glossary\", description: \"MCP verify tool checks entries against codebase → file existence, schema references validated → trust scores updated\", color: \"#22c55e\" } },\n { entryId: \"ARCH-flow-analytics\", name: \"Analytics Flow\", order: 4, data: { archType: \"flow\", sourceNode: \"ARCH-node-command-center\", targetNode: \"ARCH-node-posthog\", description: \"Feature views and actions tracked → workspace-scoped events → PostHog group analytics → Command Center metrics\", color: \"#ec4899\" } },\n];\n\n// ── Tool Registration ───────────────────────────────────────────────────────\n\nexport function registerArchitectureTools(server: McpServer) {\n\n server.registerTool(\n \"show-architecture\",\n {\n title: \"Show Architecture\",\n description:\n \"Render the system architecture map — layered visualization showing where every component lives. \" +\n \"Returns layers (Auth → Infrastructure → Core → Features → Integration) with component nodes and data flows. \" +\n \"Optionally filter by a specific template.\",\n inputSchema: {\n template: z.string().optional().describe(\"Template entry ID to filter by (default: first available template)\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ template }) => {\n await ensureCollection();\n const all = await listArchEntries();\n\n const templates = byTag(all, \"template\");\n const activeTemplate = template\n ? templates.find((t) => t.entryId === template)\n : templates[0];\n\n const templateName = activeTemplate?.name ?? \"System Architecture\";\n const templateId = activeTemplate?.entryId;\n\n const layers = byTag(all, \"layer\")\n .filter((l) => !templateId || l.data?.templateRef === templateId);\n const nodes = byTag(all, \"node\");\n const flows = byTag(all, \"flow\");\n\n if (layers.length === 0) {\n return {\n content: [{\n type: \"text\" as const,\n text: \"# Architecture Explorer\\n\\nNo architecture data found. Use `seed-architecture` to populate the default Product OS architecture.\",\n }],\n };\n }\n\n const textLayers = layers.map((l) => formatLayerText(l, nodes)).join(\"\\n\\n\");\n const textFlows = flows.length > 0\n ? \"\\n\\n---\\n\\n## Data Flows\\n\\n\" + flows.map((f) =>\n `- **${f.name}**: ${f.data?.description ?? \"\"}`\n ).join(\"\\n\")\n : \"\";\n\n const text = `# ${templateName}\\n\\n${textLayers}${textFlows}`;\n\n const html = renderArchitectureHtml(layers, nodes, flows, templateName);\n\n return {\n content: [\n { type: \"text\" as const, text },\n { type: \"resource\" as const, resource: { uri: `ui://product-os/architecture`, mimeType: \"text/html\", text: html } },\n ],\n };\n }\n );\n\n server.registerTool(\n \"explore-layer\",\n {\n title: \"Explore Architecture Layer\",\n description:\n \"Drill into a specific architecture layer to see all its component nodes with descriptions, \" +\n \"team ownership, file paths, and linked entry counts.\",\n inputSchema: {\n layer: z.string().describe(\"Layer name or entry ID, e.g. 'Core' or 'ARCH-layer-core'\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ layer }) => {\n await ensureCollection();\n const all = await listArchEntries();\n\n const layers = byTag(all, \"layer\");\n const target = layers.find(\n (l) => l.name.toLowerCase() === layer.toLowerCase() || l.entryId === layer\n );\n\n if (!target) {\n const available = layers.map((l) => `\\`${l.name}\\``).join(\", \");\n return {\n content: [{\n type: \"text\" as const,\n text: `Layer \"${layer}\" not found. Available layers: ${available}`,\n }],\n };\n }\n\n const nodes = byTag(all, \"node\").filter((n) => n.data?.layerRef === target.entryId);\n const flows = byTag(all, \"flow\").filter(\n (f) => nodes.some((n) => n.entryId === f.data?.sourceNode || n.entryId === f.data?.targetNode)\n );\n\n const depRule = target.data?.dependsOn\n ? `\\n**Dependency rule:** May import from ${target.data.dependsOn === \"none\" ? \"nothing (foundation layer)\" : target.data.dependsOn}.\\n`\n : \"\";\n const layerRationale = target.data?.rationale\n ? `\\n> ${target.data.rationale}\\n`\n : \"\";\n\n const nodeDetail = nodes.map((n) => {\n const lines = [`#### ${n.data?.icon ?? \"◻\"} ${n.name}`];\n if (n.data?.description) lines.push(String(n.data.description));\n if (n.data?.owner) lines.push(`**Owner:** ${n.data.owner}`);\n if (n.data?.filePaths) lines.push(`**Files:** \\`${n.data.filePaths}\\``);\n if (n.data?.rationale) lines.push(`\\n**Why here?** ${n.data.rationale}`);\n return lines.join(\"\\n\");\n }).join(\"\\n\\n\");\n\n const flowLines = flows.length > 0\n ? \"\\n\\n### Connected Flows\\n\\n\" + flows.map((f) => `- ${f.name}: ${f.data?.description ?? \"\"}`).join(\"\\n\")\n : \"\";\n\n return {\n content: [{\n type: \"text\" as const,\n text: `# ${target.data?.icon ?? \"\"} ${target.name} Layer\\n\\n${target.data?.description ?? \"\"}${depRule}${layerRationale}\\n**${nodes.length} components**\\n\\n${nodeDetail}${flowLines}`,\n }],\n };\n }\n );\n\n server.registerTool(\n \"show-flow\",\n {\n title: \"Show Architecture Flow\",\n description:\n \"Visualize a specific data flow path between architecture nodes — shows source, target, and description.\",\n inputSchema: {\n flow: z.string().describe(\"Flow name or entry ID, e.g. 'Smart Capture Flow' or 'ARCH-flow-smart-capture'\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ flow }) => {\n await ensureCollection();\n const all = await listArchEntries();\n\n const flows = byTag(all, \"flow\");\n const target = flows.find(\n (f) => f.name.toLowerCase() === flow.toLowerCase() || f.entryId === flow\n );\n\n if (!target) {\n const available = flows.map((f) => `\\`${f.name}\\``).join(\", \");\n return {\n content: [{\n type: \"text\" as const,\n text: `Flow \"${flow}\" not found. Available flows: ${available}`,\n }],\n };\n }\n\n const nodes = byTag(all, \"node\");\n const source = nodes.find((n) => n.entryId === target.data?.sourceNode);\n const dest = nodes.find((n) => n.entryId === target.data?.targetNode);\n\n const lines = [\n `# ${target.name}`,\n \"\",\n `**${source?.data?.icon ?? \"?\"} ${source?.name ?? \"Unknown\"}** → **${dest?.data?.icon ?? \"?\"} ${dest?.name ?? \"Unknown\"}**`,\n \"\",\n String(target.data?.description ?? \"\"),\n ];\n\n if (source) {\n lines.push(\"\", `### Source: ${source.name}`, String(source.data?.description ?? \"\"));\n if (source.data?.filePaths) lines.push(`Files: \\`${source.data.filePaths}\\``);\n }\n if (dest) {\n lines.push(\"\", `### Target: ${dest.name}`, String(dest.data?.description ?? \"\"));\n if (dest.data?.filePaths) lines.push(`Files: \\`${dest.data.filePaths}\\``);\n }\n\n return { content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }] };\n }\n );\n\n server.registerTool(\n \"seed-architecture\",\n {\n title: \"Seed Architecture Data\",\n description:\n \"Populate the architecture collection with the default Product OS architecture map. \" +\n \"Creates the template, layers, nodes, and flows. Safe to re-run — skips existing entries.\",\n annotations: { readOnlyHint: false },\n },\n async () => {\n await ensureCollection();\n const existing = await listArchEntries();\n const existingIds = new Set(existing.map((e) => e.entryId));\n\n let created = 0;\n let updated = 0;\n let unchanged = 0;\n\n const allSeeds = [\n { ...SEED_TEMPLATE, order: 0, status: \"active\" },\n ...SEED_LAYERS.map((l) => ({ ...l, status: \"active\" as const })),\n ...SEED_NODES.map((n) => ({ ...n, status: \"active\" as const })),\n ...SEED_FLOWS.map((f) => ({ ...f, status: \"active\" as const })),\n ];\n\n for (const seed of allSeeds) {\n if (existingIds.has(seed.entryId)) {\n const existingEntry = existing.find((e) => e.entryId === seed.entryId);\n const existingData = (existingEntry?.data ?? {}) as Record<string, unknown>;\n const seedData = seed.data as Record<string, unknown>;\n const hasChanges = Object.keys(seedData).some(\n (k) => seedData[k] !== undefined && existingData[k] !== seedData[k],\n );\n if (hasChanges) {\n const mergedData = { ...existingData, ...seedData };\n await mcpMutation(\"kb.updateEntry\", {\n entryId: seed.entryId,\n data: mergedData,\n });\n updated++;\n } else {\n unchanged++;\n }\n continue;\n }\n await mcpMutation(\"kb.createEntry\", {\n collectionSlug: COLLECTION_SLUG,\n entryId: seed.entryId,\n name: seed.name,\n status: seed.status,\n data: seed.data,\n order: seed.order ?? 0,\n });\n created++;\n }\n\n return {\n content: [{\n type: \"text\" as const,\n text: `# Architecture Seeded\\n\\n**Created:** ${created} entries\\n**Updated:** ${updated} (merged new fields)\\n**Unchanged:** ${unchanged}\\n\\nUse \\`show-architecture\\` to view the map.`,\n }],\n };\n }\n );\n\n // ── check-architecture: Live Dependency Health Scanner ───────────────\n\n server.registerTool(\n \"check-architecture\",\n {\n title: \"Check Architecture Health\",\n description:\n \"Scan the codebase for dependency direction violations. Reads architecture layers, nodes, \" +\n \"and their file paths from the knowledge base, then parses TypeScript/Svelte imports and \" +\n \"checks them against the layer dependency rules (Auth ← Infra ← Core ← Features; \" +\n \"Integration → Core only). Returns a structured violation report.\",\n annotations: { readOnlyHint: true },\n },\n async () => {\n const projectRoot = resolveProjectRoot();\n if (!projectRoot) {\n return {\n content: [{\n type: \"text\" as const,\n text: \"# Scan Failed\\n\\nCannot find project root (looked for `convex/schema.ts` in cwd and parent). \" +\n \"Set `WORKSPACE_PATH` env var to the absolute path of the Product OS project root.\",\n }],\n };\n }\n\n await ensureCollection();\n const all = await listArchEntries();\n const layers = byTag(all, \"layer\");\n const nodes = byTag(all, \"node\");\n\n const result = scanDependencies(projectRoot, layers, nodes);\n\n return {\n content: [{\n type: \"text\" as const,\n text: formatScanReport(result),\n }],\n };\n }\n );\n}\n\n// ── Scanner Engine — FEAT-dep-health, enforces SOS-dbl1n9 (dependency direction) ──\n\ninterface Violation {\n sourceNode: string;\n sourceLayer: string;\n sourceFile: string;\n importPath: string;\n targetNode: string;\n targetLayer: string;\n rule: string;\n}\n\ninterface ScanResult {\n violations: Violation[];\n filesScanned: number;\n importsChecked: number;\n unmappedImports: number;\n nodeResults: Map<string, { violations: Violation[]; filesScanned: number }>;\n}\n\nfunction resolveProjectRoot(): string | null {\n const candidates = [\n process.env.WORKSPACE_PATH,\n process.cwd(),\n resolve(process.cwd(), \"..\"),\n ].filter(Boolean) as string[];\n\n for (const dir of candidates) {\n const resolved = resolve(dir);\n if (existsSync(resolve(resolved, \"convex/schema.ts\"))) return resolved;\n }\n return null;\n}\n\nfunction scanDependencies(\n projectRoot: string,\n layers: ArchEntry[],\n nodes: ArchEntry[],\n): ScanResult {\n const layerMap = new Map<string, ArchEntry>();\n for (const l of layers) layerMap.set(l.entryId, l);\n\n const allowedDeps = buildAllowedDeps(layers);\n const nodePathPrefixes = buildNodePrefixes(nodes);\n\n const violations: Violation[] = [];\n const nodeResults = new Map<string, { violations: Violation[]; filesScanned: number }>();\n let totalFiles = 0;\n let totalImports = 0;\n let unmapped = 0;\n\n for (const node of nodes) {\n const layerRef = String(node.data?.layerRef ?? \"\");\n const layer = layerMap.get(layerRef);\n if (!layer) continue;\n\n const filePaths = parseFilePaths(node);\n const nodeViolations: Violation[] = [];\n let nodeFileCount = 0;\n\n for (const fp of filePaths) {\n const absPath = resolve(projectRoot, fp);\n const files = collectFiles(absPath);\n\n for (const file of files) {\n nodeFileCount++;\n totalFiles++;\n const relFile = relative(projectRoot, file);\n const imports = parseImports(file);\n\n for (const imp of imports) {\n totalImports++;\n const resolved = resolveImport(imp, file, projectRoot);\n if (!resolved) { unmapped++; continue; }\n\n const targetNode = findNodeByPath(resolved, nodePathPrefixes);\n if (!targetNode) { unmapped++; continue; }\n\n const targetLayerRef = String(targetNode.data?.layerRef ?? \"\");\n const targetLayer = layerMap.get(targetLayerRef);\n if (!targetLayer) continue;\n\n if (targetLayerRef === layerRef) continue;\n\n const allowed = allowedDeps.get(layerRef);\n if (allowed && !allowed.has(targetLayerRef)) {\n const v: Violation = {\n sourceNode: node.name,\n sourceLayer: layer.name,\n sourceFile: relFile,\n importPath: imp,\n targetNode: targetNode.name,\n targetLayer: targetLayer.name,\n rule: `${layer.name} cannot import from ${targetLayer.name}`,\n };\n violations.push(v);\n nodeViolations.push(v);\n }\n }\n }\n }\n\n nodeResults.set(node.entryId, { violations: nodeViolations, filesScanned: nodeFileCount });\n }\n\n return { violations, filesScanned: totalFiles, importsChecked: totalImports, unmappedImports: unmapped, nodeResults };\n}\n\nfunction buildAllowedDeps(layers: ArchEntry[]): Map<string, Set<string>> {\n const nameToId = new Map<string, string>();\n for (const l of layers) nameToId.set(l.name.toLowerCase(), l.entryId);\n\n const allowed = new Map<string, Set<string>>();\n for (const layer of layers) {\n const deps = String(layer.data?.dependsOn ?? \"none\");\n const set = new Set<string>();\n if (deps !== \"none\") {\n for (const dep of deps.split(\",\").map((d) => d.trim().toLowerCase())) {\n const id = nameToId.get(dep);\n if (id) set.add(id);\n }\n }\n allowed.set(layer.entryId, set);\n }\n return allowed;\n}\n\nfunction buildNodePrefixes(nodes: ArchEntry[]): { prefix: string; node: ArchEntry }[] {\n const entries: { prefix: string; node: ArchEntry }[] = [];\n for (const node of nodes) {\n for (const fp of parseFilePaths(node)) {\n entries.push({ prefix: normalize(fp), node });\n }\n }\n entries.sort((a, b) => b.prefix.length - a.prefix.length);\n return entries;\n}\n\nfunction parseFilePaths(node: ArchEntry): string[] {\n const raw = node.data?.filePaths;\n if (!raw || typeof raw !== \"string\") return [];\n return raw.split(\",\").map((p) => p.trim()).filter(Boolean);\n}\n\nfunction collectFiles(absPath: string): string[] {\n if (!existsSync(absPath)) return [];\n const stat = statSync(absPath);\n if (stat.isFile()) {\n return isScannableFile(absPath) ? [absPath] : [];\n }\n if (!stat.isDirectory()) return [];\n\n const results: string[] = [];\n const walk = (dir: string) => {\n for (const entry of readdirSync(dir, { withFileTypes: true })) {\n if (entry.name.startsWith(\".\") || entry.name === \"node_modules\") continue;\n const full = resolve(dir, entry.name);\n if (entry.isDirectory()) walk(full);\n else if (isScannableFile(full)) results.push(full);\n }\n };\n walk(absPath);\n return results;\n}\n\nfunction isScannableFile(p: string): boolean {\n return /\\.(ts|js|svelte)$/.test(p) && !p.endsWith(\".d.ts\");\n}\n\nfunction parseImports(filePath: string): string[] {\n try {\n const content = readFileSync(filePath, \"utf-8\");\n const re = /(?:^|\\n)\\s*import\\s+(?:[\\s\\S]*?\\s+from\\s+)?['\"]([^'\"]+)['\"]/g;\n const imports: string[] = [];\n let match: RegExpExecArray | null;\n while ((match = re.exec(content)) !== null) {\n imports.push(match[1]);\n }\n return imports;\n } catch {\n return [];\n }\n}\n\nconst EXTENSIONS = [\".ts\", \".js\", \".svelte\", \"/index.ts\", \"/index.js\", \"/index.svelte\"];\n\nfunction tryResolveWithExtension(absPath: string): string | null {\n if (existsSync(absPath) && statSync(absPath).isFile()) return absPath;\n for (const ext of EXTENSIONS) {\n const withExt = absPath + ext;\n if (existsSync(withExt)) return withExt;\n }\n return null;\n}\n\nfunction resolveImport(imp: string, fromFile: string, root: string): string | null {\n let rel: string | null = null;\n if (imp.startsWith(\"$lib/\")) rel = imp.replace(\"$lib/\", \"src/lib/\");\n else if (imp.startsWith(\"$convex/\")) rel = imp.replace(\"$convex/\", \"convex/\");\n else if (imp.startsWith(\"$env/\") || imp.startsWith(\"$app/\")) return null;\n else if (imp.startsWith(\"./\") || imp.startsWith(\"../\")) {\n const fromDir = dirname(fromFile);\n const abs = resolve(fromDir, imp);\n rel = relative(root, abs);\n }\n if (!rel) return null;\n\n const abs = resolve(root, rel);\n const actual = tryResolveWithExtension(abs);\n return actual ? relative(root, actual) : rel;\n}\n\nfunction findNodeByPath(\n filePath: string,\n prefixes: { prefix: string; node: ArchEntry }[],\n): ArchEntry | null {\n const normalized = normalize(filePath);\n for (const { prefix, node } of prefixes) {\n if (normalized.startsWith(prefix)) return node;\n }\n return null;\n}\n\nfunction formatScanReport(result: ScanResult): string {\n const lines: string[] = [];\n\n if (result.violations.length === 0) {\n lines.push(\n `# Architecture Health Check Passed`,\n \"\",\n `**0 violations** across ${result.filesScanned} files (${result.importsChecked} imports checked, ${result.unmappedImports} unmapped).`,\n \"\",\n \"All imports respect the layer dependency rules.\",\n );\n } else {\n lines.push(\n `# Architecture Health Check — ${result.violations.length} Violation${result.violations.length === 1 ? \"\" : \"s\"}`,\n \"\",\n `Scanned ${result.filesScanned} files, checked ${result.importsChecked} imports, found **${result.violations.length} violation${result.violations.length === 1 ? \"\" : \"s\"}** (${result.unmappedImports} unmapped).`,\n \"\",\n );\n\n const byNode = new Map<string, Violation[]>();\n for (const v of result.violations) {\n if (!byNode.has(v.sourceNode)) byNode.set(v.sourceNode, []);\n byNode.get(v.sourceNode)!.push(v);\n }\n\n for (const [nodeName, vs] of byNode) {\n lines.push(`## ${nodeName} (${vs[0].sourceLayer})`);\n for (const v of vs) {\n lines.push(`- \\`${v.sourceFile}\\` imports \\`${v.importPath}\\` → **${v.targetNode}** (${v.targetLayer}) — ${v.rule}`);\n }\n lines.push(\"\");\n }\n }\n\n lines.push(\"---\", \"\");\n\n const nodeEntries = [...result.nodeResults.entries()];\n const cleanCount = nodeEntries.filter(([, r]) => r.violations.length === 0 && r.filesScanned > 0).length;\n const dirtyCount = nodeEntries.filter(([, r]) => r.violations.length > 0).length;\n const emptyCount = nodeEntries.filter(([, r]) => r.filesScanned === 0).length;\n\n lines.push(\n `**Summary:** ${cleanCount} clean nodes, ${dirtyCount} with violations, ${emptyCount} with no files.`,\n );\n\n return lines.join(\"\\n\");\n}\n","/**\n * Workflow tools — ARCH-node-mcp (Core layer)\n * KB: FEAT-MCP-001 (MCP Server)\n *\n * Tools for discovering and running Chainwork workflows via MCP.\n * The actual facilitation happens in the agent — these tools provide\n * the definitions, state tracking, and KB integration.\n */\n\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport { mcpMutation } from \"../client.js\";\nimport {\n getWorkflow,\n listWorkflows,\n type WorkflowDefinition,\n} from \"../workflows/definitions.js\";\n\nfunction formatWorkflowCard(wf: WorkflowDefinition): string {\n const roundList = wf.rounds\n .map((r) => ` ${r.num}. ${r.label} (${r.type}, ~${r.maxDurationHint ?? \"?\"})`)\n .join(\"\\n\");\n\n return (\n `## ${wf.icon} ${wf.name}\\n` +\n `**ID**: \\`${wf.id}\\`\\n` +\n `${wf.shortDescription}\\n\\n` +\n `**Rounds** (${wf.rounds.length}):\\n${roundList}\\n\\n` +\n `**Output**: Creates entries in \\`${wf.kbOutputCollection}\\` collection.\\n` +\n `_Use the \\`run-workflow\\` prompt with workflow=\"${wf.id}\" to start._`\n );\n}\n\nexport function registerWorkflowTools(server: McpServer) {\n\n server.registerTool(\n \"list-workflows\",\n {\n title: \"List Workflows\",\n description:\n \"List all available Chainwork workflows — retro, shape-a-bet, IDM, etc. \" +\n \"Each workflow is a structured multi-round facilitation ceremony that the agent runs in Facilitator Mode. \" +\n \"Use the `run-workflow` prompt to actually launch one.\",\n annotations: { readOnlyHint: true },\n },\n async () => {\n const workflows = listWorkflows();\n\n if (workflows.length === 0) {\n return {\n content: [{\n type: \"text\" as const,\n text: \"No workflows registered yet. Check back after the workflow definitions are configured.\",\n }],\n };\n }\n\n const cards = workflows.map(formatWorkflowCard).join(\"\\n\\n---\\n\\n\");\n\n return {\n content: [{\n type: \"text\" as const,\n text:\n `# Available Chainwork Workflows\\n\\n` +\n `${workflows.length} workflow(s) available. Use the \\`run-workflow\\` prompt to launch one.\\n\\n` +\n `---\\n\\n${cards}`,\n }],\n };\n },\n );\n\n server.registerTool(\n \"workflow-checkpoint\",\n {\n title: \"Workflow Checkpoint\",\n description:\n \"Record the output of a workflow round. Captures the round's data to the Knowledge Base \" +\n \"as a structured entry. Use this during Facilitator Mode after completing each round \" +\n \"to persist progress — so if the conversation is interrupted, work is not lost.\\n\\n\" +\n \"At workflow completion, this tool can also generate the final summary entry.\",\n inputSchema: {\n workflowId: z.string().describe(\"Workflow ID (e.g., 'retro')\"),\n roundId: z.string().describe(\"Round ID (e.g., 'what-went-well')\"),\n output: z.string().describe(\"The round's output — synthesized by the facilitator from the conversation\"),\n isFinal: z.boolean().optional().describe(\n \"If true, this is the final checkpoint and triggers the summary KB entry creation\",\n ),\n summaryName: z.string().optional().describe(\n \"Name for the final KB entry (required when isFinal=true)\",\n ),\n summaryDescription: z.string().optional().describe(\n \"Full description/rationale for the final KB entry (required when isFinal=true)\",\n ),\n },\n annotations: { destructiveHint: false },\n },\n async ({ workflowId, roundId, output, isFinal, summaryName, summaryDescription }) => {\n const wf = getWorkflow(workflowId);\n if (!wf) {\n return {\n content: [{\n type: \"text\" as const,\n text:\n `Workflow \"${workflowId}\" not found. ` +\n `Available: ${listWorkflows().map((w) => w.id).join(\", \")}.\\n\\n` +\n `This checkpoint was NOT saved. Continue the conversation — the facilitator has the context.`,\n }],\n };\n }\n\n const round = wf.rounds.find((r) => r.id === roundId);\n if (!round) {\n return {\n content: [{\n type: \"text\" as const,\n text:\n `Round \"${roundId}\" not found in workflow \"${workflowId}\". ` +\n `Available rounds: ${wf.rounds.map((r) => r.id).join(\", \")}.\\n\\n` +\n `This checkpoint was NOT saved. The conversation context is preserved — continue facilitating.`,\n }],\n };\n }\n\n const lines: string[] = [\n `## Checkpoint: Round ${round.num} — ${round.label}`,\n `Workflow: ${wf.name} (\\`${wf.id}\\`)`,\n \"\",\n ];\n\n if (isFinal && summaryName && summaryDescription) {\n try {\n const entryId = await mcpMutation<string>(\"kb.createEntry\", {\n collectionSlug: wf.kbOutputCollection,\n name: summaryName,\n status: \"draft\",\n data: {\n [wf.kbOutputTemplate.descriptionField]: summaryDescription,\n workflowType: wf.id,\n completedRound: roundId,\n date: new Date().toISOString().split(\"T\")[0],\n },\n createdBy: `workflow:${wf.id}`,\n });\n\n lines.push(\n `**KB Entry Created**: \\`${entryId}\\``,\n `Collection: \\`${wf.kbOutputCollection}\\``,\n `Name: ${summaryName}`,\n \"\",\n `The retro is now captured in the Knowledge Base. `,\n `Use \\`suggest-links\\` on this entry to connect it to related knowledge.`,\n );\n } catch (err: unknown) {\n const msg = err instanceof Error ? err.message : String(err);\n lines.push(\n `**KB capture failed**: ${msg}`,\n \"\",\n `The retro output is preserved in this conversation. `,\n `You can manually create the entry later using \\`smart-capture\\` with:`,\n `- Collection: \\`${wf.kbOutputCollection}\\``,\n `- Name: ${summaryName}`,\n `- Description: (copy from the conversation summary above)`,\n );\n }\n } else {\n lines.push(\n `Round ${round.num} output recorded.`,\n `Output: ${output.substring(0, 200)}${output.length > 200 ? \"...\" : \"\"}`,\n );\n\n const currentIdx = wf.rounds.findIndex((r) => r.id === roundId);\n if (currentIdx < wf.rounds.length - 1) {\n const next = wf.rounds[currentIdx + 1];\n lines.push(\n \"\",\n `**Next**: Round ${next.num} — ${next.label}`,\n `_${next.instruction}_`,\n );\n } else {\n lines.push(\n \"\",\n `**All rounds complete.** Call this tool again with \\`isFinal: true\\` to create the KB entry.`,\n );\n }\n }\n\n return { content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }] };\n },\n );\n}\n","/**\n * Chainwork Workflow Definitions — ARCH-node-mcp (Core layer)\n * KB: GT-006 (MCP), FEAT-MCP-001\n *\n * Each workflow is a typed chain of rounds with facilitation instructions.\n * The agent uses these to drive structured conversations in Facilitator Mode.\n */\n\nexport interface WorkflowRound {\n id: string;\n num: string;\n label: string;\n type: \"open\" | \"choice\" | \"synthesis\" | \"commit\" | \"close\";\n instruction: string;\n facilitatorGuidance: string;\n questions?: WorkflowQuestion[];\n outputSchema: {\n field: string;\n description: string;\n format: \"freetext\" | \"list\" | \"choice\" | \"structured\";\n };\n kbCollection?: string;\n maxDurationHint?: string;\n}\n\nexport interface WorkflowQuestion {\n id: string;\n prompt: string;\n options?: { id: string; label: string }[];\n allowMultiple?: boolean;\n required?: boolean;\n}\n\nexport interface WorkflowDefinition {\n id: string;\n name: string;\n shortDescription: string;\n icon: string;\n facilitatorPreamble: string;\n rounds: WorkflowRound[];\n kbOutputCollection: string;\n kbOutputTemplate: {\n nameTemplate: string;\n descriptionField: string;\n };\n errorRecovery: string;\n}\n\n// ── Retro Workflow ──────────────────────────────────────────────────────────\n\nconst RETRO_WORKFLOW: WorkflowDefinition = {\n id: \"retro\",\n name: \"Retrospective\",\n shortDescription:\n \"Structured team retrospective — reflect, surface patterns, commit to actions. Outputs a decision entry in the Knowledge Base.\",\n icon: \"◎\",\n facilitatorPreamble: `You are now in **Facilitator Mode**. You are not a coding assistant — you are a facilitator running a structured retrospective.\n\n## Your Behavior\n\n1. **Guide, don't solve.** Ask questions, reflect back, synthesize. Never jump to solutions.\n2. **One round at a time.** Complete each round fully before moving to the next. No skipping.\n3. **Use structured questions** (AskQuestion tool) for choices and multi-select. Use open conversation for reflection.\n4. **Create a Plan** at the start showing all rounds as tasks. Update it as you progress.\n5. **Synthesize between rounds.** After collecting input, reflect back what you heard before moving on.\n6. **Never go silent.** If something fails, say what happened and what to do next.\n7. **Capture to KB** at the end using the Product OS smart-capture tool.\n8. **Match the energy.** Be warm but structured. This is a ceremony, not a checklist.\n\n## Communication Style\n\n- Start each round with its number, name, and a brief instruction\n- Use quotes and emphasis to reflect back what the participant said\n- End each round with a brief synthesis before transitioning\n- When a round is complete, mark it as done in the Plan\n- If the participant seems stuck, offer prompts — never pressure\n- If a tool call fails, explain what happened and offer an alternative path\n\n## Error Recovery\n\nIf at any point a tool call or MCP operation fails:\n1. Tell the participant what you were trying to do\n2. Explain what went wrong (briefly, no stack traces)\n3. Offer a manual alternative (e.g., \"I'll capture this in the conversation instead\")\n4. Continue the workflow — never halt completely\n\n## Plan Structure\n\nCreate a Cursor Plan with these rounds as tasks. Mark each in_progress as you enter it, completed when done.`,\n\n rounds: [\n {\n id: \"set-stage\",\n num: \"01\",\n label: \"Set the Stage\",\n type: \"choice\",\n instruction:\n \"Before we dive in, let's frame what we're reflecting on. What's the scope of this retro?\",\n facilitatorGuidance:\n \"Start warm. Ask the participant to pick or describe what they're retro-ing. Confirm the scope before proceeding. If they gave context already in their initial message, use it — don't make them repeat.\",\n questions: [\n {\n id: \"scope\",\n prompt:\n \"What are we reflecting on? Pick one or describe your own.\",\n options: [\n { id: \"last-week\", label: \"Last week's work\" },\n { id: \"last-sprint\", label: \"Last sprint/cycle\" },\n { id: \"specific-project\", label: \"A specific project or feature\" },\n { id: \"process\", label: \"A process or workflow\" },\n { id: \"custom\", label: \"Something else — let me describe it\" },\n ],\n },\n ],\n outputSchema: {\n field: \"scope\",\n description: \"What timeframe or project is being retro'd\",\n format: \"freetext\",\n },\n maxDurationHint: \"2 min\",\n },\n {\n id: \"what-went-well\",\n num: \"02\",\n label: \"What Went Well\",\n type: \"open\",\n instruction:\n \"Let's start with the good. What went well? What are you proud of? What should we do more of?\",\n facilitatorGuidance:\n \"Celebrate. Reflect back each win with genuine emphasis. Ask follow-ups like 'What made that work?' or 'Who else contributed to that?' Collect at least 3 items before synthesizing. Don't rush past the positive — teams skip this too fast.\",\n outputSchema: {\n field: \"wentWell\",\n description: \"List of things that went well\",\n format: \"list\",\n },\n maxDurationHint: \"5 min\",\n },\n {\n id: \"what-didnt-go-well\",\n num: \"03\",\n label: \"What Didn't Go Well\",\n type: \"open\",\n instruction:\n \"Now the harder part. What didn't go well? What frustrated you? Where did things break down?\",\n facilitatorGuidance:\n \"Create safety. Acknowledge that this is harder. Don't judge or immediately problem-solve — just listen and capture. Ask 'What was the impact of that?' and 'When did you first notice it?'. Reflect back without softening. Collect at least 3 items.\",\n outputSchema: {\n field: \"didntGoWell\",\n description: \"List of things that didn't go well\",\n format: \"list\",\n },\n maxDurationHint: \"5 min\",\n },\n {\n id: \"patterns\",\n num: \"04\",\n label: \"Patterns & Insights\",\n type: \"synthesis\",\n instruction:\n \"Looking at what went well and what didn't — what patterns do you see? What's the deeper insight?\",\n facilitatorGuidance:\n \"This is YOUR moment as facilitator. Synthesize what you've heard across rounds 2 and 3. Surface themes, connections, and contradictions. Propose 2-3 patterns and ask the participant to react. This round transforms raw observations into actionable insights. Don't let the participant skip the 'why' — push for root causes.\",\n outputSchema: {\n field: \"patterns\",\n description: \"Synthesized patterns and insights\",\n format: \"structured\",\n },\n maxDurationHint: \"5 min\",\n },\n {\n id: \"actions\",\n num: \"05\",\n label: \"Actions & Commitments\",\n type: \"commit\",\n instruction:\n \"Based on these patterns, what will we actually change? Be specific — who does what, by when?\",\n facilitatorGuidance:\n \"Push for specificity. 'Be better at X' is not an action. 'Randy will set up a 15-min weekly check-in by Friday' is. Each action needs an owner and a deadline. Aim for 2-4 concrete actions. Use AskQuestion to confirm the final list. These become the retro's output.\",\n questions: [\n {\n id: \"action-confirm\",\n prompt: \"Are these actions concrete enough to actually happen?\",\n options: [\n { id: \"yes\", label: \"Yes — these are clear and actionable\" },\n { id: \"refine\", label: \"Let me refine some of these\" },\n { id: \"add\", label: \"I want to add more\" },\n ],\n },\n ],\n outputSchema: {\n field: \"actions\",\n description: \"Committed actions with owners and deadlines\",\n format: \"structured\",\n },\n maxDurationHint: \"5 min\",\n },\n {\n id: \"close\",\n num: \"06\",\n label: \"Close & Capture\",\n type: \"close\",\n instruction:\n \"One last thing — in one sentence, what's the single most important thing you're taking away from this retro?\",\n facilitatorGuidance:\n \"Keep it brief. One sentence reflection. Then summarize the entire retro: scope, key wins, key pain points, patterns identified, actions committed. Ask if they want to save this to the Knowledge Base. If yes, use smart-capture to create a decision/tension entry. Thank them for the retro.\",\n outputSchema: {\n field: \"takeaway\",\n description: \"Single-sentence takeaway\",\n format: \"freetext\",\n },\n kbCollection: \"decisions\",\n maxDurationHint: \"2 min\",\n },\n ],\n\n kbOutputCollection: \"decisions\",\n kbOutputTemplate: {\n nameTemplate: \"Retro: {scope} — {date}\",\n descriptionField: \"rationale\",\n },\n\n errorRecovery: `If anything goes wrong during the retro:\n\n1. **MCP tool failure**: Skip the KB capture step. Summarize everything in the conversation instead. Suggest the participant runs \\`smart-capture\\` manually later.\n2. **AskQuestion not available**: Fall back to numbered options in plain text. \"Reply with 1, 2, or 3.\"\n3. **Plan creation fails**: Continue without the Plan. The conversation IS the record.\n4. **Participant goes off-topic**: Gently redirect: \"That's valuable — let's capture it. For now, let's stay with [current round].\"\n5. **Participant wants to stop**: Respect it. Summarize what you have so far. Offer to save partial results to KB.\n\nThe retro must never fail silently. Always communicate state.`,\n};\n\n// ── Workflow Registry ───────────────────────────────────────────────────────\n\nconst WORKFLOWS = new Map<string, WorkflowDefinition>([\n [\"retro\", RETRO_WORKFLOW],\n]);\n\nexport function getWorkflow(id: string): WorkflowDefinition | undefined {\n return WORKFLOWS.get(id);\n}\n\nexport function listWorkflows(): WorkflowDefinition[] {\n return Array.from(WORKFLOWS.values());\n}\n\nexport function getWorkflowIds(): string[] {\n return Array.from(WORKFLOWS.keys());\n}\n","/**\n * GitChain MCP tools — chain lifecycle operations.\n *\n * These tools expose GitChain operations to AI agents and external consumers\n * via the MCP protocol. They call the Convex HTTP gateway routes registered\n * under the `gitchain.*` namespace.\n *\n * KB: GT-vaw0re (Git Chain), FEAT-CW-001 (ChainWork Engine)\n */\n\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport { mcpQuery, mcpMutation } from \"../client.js\";\n\nfunction linkSummary(links: Record<string, string>): string {\n return Object.entries(links)\n .map(([id, content]) => {\n const filled = typeof content === \"string\" && content.length > 0;\n const preview = filled\n ? content.substring(0, 80) + (content.length > 80 ? \"...\" : \"\")\n : \"(empty)\";\n return ` - **${id}**: ${preview}`;\n })\n .join(\"\\n\");\n}\n\nexport function registerGitChainTools(server: McpServer) {\n // ─── chain-create ────────────────────────────────────────────────\n\n server.registerTool(\n \"chain-create\",\n {\n title: \"Create Chain\",\n description:\n \"Create a new strategic chain in the knowledge base. \" +\n \"Chains are versioned knowledge artifacts that follow a chain type definition \" +\n \"(e.g. Strategy Coherence: Problem → Insight → Choice → Action → Outcome). \" +\n \"Returns the chain's entry ID for subsequent operations.\",\n inputSchema: {\n title: z.string().describe(\"Title for the chain, e.g. 'Q1 Growth Strategy'\"),\n chainTypeId: z\n .string()\n .default(\"strategy-coherence\")\n .describe(\n \"Chain type ID. Use 'strategy-coherence' for 5-link strategy chains \" +\n \"(Problem → Insight → Choice → Action → Outcome) or 'idm-proposal' for \" +\n \"IDM governance chains (Tension → Proposal → Objections → Integration → Decision)\"\n ),\n description: z\n .string()\n .optional()\n .describe(\"Optional description of what this chain is about\"),\n author: z\n .string()\n .optional()\n .describe(\"Who is creating this chain (clerkUserId or person name). Defaults to 'mcp'.\"),\n },\n },\n async ({ title, chainTypeId, description, author }) => {\n const result = await mcpMutation<{ _id: string; entryId: string }>(\n \"gitchain.createChain\",\n { title, chainTypeId, description, author }\n );\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Chain Created\\n\\n` +\n `- **Entry ID:** \\`${result.entryId}\\`\\n` +\n `- **Title:** ${title}\\n` +\n `- **Type:** ${chainTypeId}\\n` +\n `- **Status:** draft\\n\\n` +\n `Use \\`chain-edit\\` with chainEntryId=\\`${result.entryId}\\` to start filling in links.`,\n },\n ],\n };\n }\n );\n\n // ─── chain-get ───────────────────────────────────────────────────\n\n server.registerTool(\n \"chain-get\",\n {\n title: \"Get Chain\",\n description:\n \"Retrieve a chain by its entry ID. Returns the full chain content, \" +\n \"coherence scores, link fill status, and version info. \" +\n \"Use chain-list first to discover chain entry IDs.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID, e.g. 'CH-A1B2C3'\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ chainEntryId }) => {\n const chain = await mcpQuery<any>(\"gitchain.getChain\", { chainEntryId });\n\n if (!chain) {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `Chain \"${chainEntryId}\" not found.`,\n },\n ],\n };\n }\n\n const scoreSection = chain.scores\n ? `\\n## Coherence: ${chain.coherenceScore}%\\n\\n` +\n chain.scores.sections\n .map((s: any) => `- **${s.key}**: ${s.power}/100 (${\"★\".repeat(Math.min(s.stars ?? 0, 5))}${\"☆\".repeat(Math.max(0, 5 - (s.stars ?? 0)))})`)\n .join(\"\\n\")\n : \"\";\n\n const text =\n `# ${chain.name}\\n\\n` +\n `- **Entry ID:** \\`${chain.entryId}\\`\\n` +\n `- **Type:** ${chain.chainTypeName}\\n` +\n `- **Status:** ${chain.status}\\n` +\n `- **Links filled:** ${chain.filledCount}/${chain.totalCount}\\n` +\n `- **Version:** ${chain.currentVersion}\\n` +\n `- **Created by:** ${chain.createdBy ?? \"unknown\"}\\n` +\n `- **History events:** ${chain.historyCount}\\n` +\n scoreSection +\n `\\n\\n## Links\\n\\n` +\n linkSummary(chain.links);\n\n return { content: [{ type: \"text\" as const, text }] };\n }\n );\n\n // ─── chain-edit ──────────────────────────────────────────────────\n\n server.registerTool(\n \"chain-edit\",\n {\n title: \"Edit Chain Link\",\n description:\n \"Edit a specific link in a chain. Each chain has named links \" +\n \"(e.g. for strategy-coherence: problem, insight, choice, action, outcome). \" +\n \"The link content is replaced entirely — pass the full new text.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID, e.g. 'CH-A1B2C3'\"),\n linkId: z\n .string()\n .describe(\n \"Which link to edit. For strategy-coherence: problem, insight, choice, action, outcome. \" +\n \"For idm-proposal: tension, proposal, objections, integration, decision.\"\n ),\n content: z.string().describe(\"The full new content for this link\"),\n author: z\n .string()\n .optional()\n .describe(\"Who is making this edit. Defaults to 'mcp'.\"),\n },\n },\n async ({ chainEntryId, linkId, content, author }) => {\n const result = await mcpMutation<{\n _id: string;\n entryId: string;\n linkId: string;\n status: string;\n }>(\"gitchain.editLink\", { chainEntryId, linkId, content, author });\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Link Updated\\n\\n` +\n `- **Chain:** \\`${result.entryId}\\`\\n` +\n `- **Link:** ${result.linkId}\\n` +\n `- **Chain status:** ${result.status}\\n` +\n `- **Content length:** ${content.length} chars\\n\\n` +\n `Use \\`chain-get\\` to see the full chain with updated scores.`,\n },\n ],\n };\n }\n );\n\n // ─── chain-list ──────────────────────────────────────────────────\n\n server.registerTool(\n \"chain-list\",\n {\n title: \"List Chains\",\n description:\n \"List all chains in the workspace, optionally filtered by chain type or status. \" +\n \"Returns entry IDs, titles, link fill progress, and coherence scores.\",\n inputSchema: {\n chainTypeId: z\n .string()\n .optional()\n .describe(\"Filter by chain type: 'strategy-coherence' or 'idm-proposal'\"),\n status: z\n .string()\n .optional()\n .describe(\"Filter by status: 'draft' or 'active'\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ chainTypeId, status }) => {\n const chains = await mcpQuery<any[]>(\"gitchain.listChains\", {\n chainTypeId,\n status,\n });\n\n if (chains.length === 0) {\n return {\n content: [\n {\n type: \"text\" as const,\n text: \"No chains found. Use `chain-create` to create one.\",\n },\n ],\n };\n }\n\n const formatted = chains\n .map(\n (c) =>\n `- **\\`${c.entryId}\\`** ${c.name} — ${c.chainTypeId} · ` +\n `${c.filledCount}/${c.totalCount} links · ` +\n `coherence: ${c.coherenceScore}% · ` +\n `status: ${c.status}`\n )\n .join(\"\\n\");\n\n return {\n content: [\n {\n type: \"text\" as const,\n text: `# Chains (${chains.length})\\n\\n${formatted}`,\n },\n ],\n };\n }\n );\n\n // ─── chain-history ───────────────────────────────────────────────\n\n server.registerTool(\n \"chain-history\",\n {\n title: \"Chain History\",\n description:\n \"View the edit history of a chain — all creation, update, and version events. \" +\n \"Shows who changed what and when.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID, e.g. 'CH-A1B2C3'\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ chainEntryId }) => {\n const history = await mcpQuery<any[]>(\"gitchain.getHistory\", {\n chainEntryId,\n });\n\n if (history.length === 0) {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `No history found for chain \"${chainEntryId}\".`,\n },\n ],\n };\n }\n\n const formatted = history\n .sort((a: any, b: any) => b.timestamp - a.timestamp)\n .map((h: any) => {\n const date = new Date(h.timestamp).toISOString().replace(\"T\", \" \").substring(0, 19);\n return `- **${date}** [${h.event}] by ${h.changedBy ?? \"unknown\"} — ${h.note ?? \"\"}`;\n })\n .join(\"\\n\");\n\n return {\n content: [\n {\n type: \"text\" as const,\n text: `# History for ${chainEntryId} (${history.length} events)\\n\\n${formatted}`,\n },\n ],\n };\n }\n );\n\n // ─── chain-commit ──────────────────────────────────────────────\n\n server.registerTool(\n \"chain-commit\",\n {\n title: \"Commit Chain\",\n description:\n \"Create a version snapshot (commit) of the current chain state. \" +\n \"Records all link content, computes coherence score, and tracks which links changed. \" +\n \"Commit messages should follow: type(link): description. \" +\n \"Types: edit, refine, rewrite, integrate, revert.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID, e.g. 'CH-A1B2C3'\"),\n commitMessage: z\n .string()\n .describe(\n \"Commit message following convention: type(link): description. \" +\n \"Example: 'edit(outcome): Add Q1 revenue target'\"\n ),\n author: z\n .string()\n .optional()\n .describe(\"Who is committing. Defaults to 'mcp'.\"),\n },\n },\n async ({ chainEntryId, commitMessage, author }) => {\n const result = await mcpMutation<{\n entryId: string;\n version: number;\n coherenceScore: number;\n linksModified: string[];\n commitLintWarning: string | null;\n }>(\"gitchain.commitChain\", { chainEntryId, commitMessage, author });\n\n const warning = result.commitLintWarning\n ? `\\n\\n> **Lint warning:** ${result.commitLintWarning}`\n : \"\";\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Committed v${result.version}\\n\\n` +\n `- **Chain:** \\`${result.entryId}\\`\\n` +\n `- **Version:** ${result.version}\\n` +\n `- **Message:** ${commitMessage}\\n` +\n `- **Coherence:** ${result.coherenceScore}%\\n` +\n `- **Links modified:** ${result.linksModified.length > 0 ? result.linksModified.join(\", \") : \"none\"}\\n` +\n warning,\n },\n ],\n };\n }\n );\n\n // ─── chain-commits ─────────────────────────────────────────────\n\n server.registerTool(\n \"chain-commits\",\n {\n title: \"List Chain Commits\",\n description:\n \"List all version snapshots (commits) for a chain, newest first. \" +\n \"Shows version number, commit message, author, which links were modified, and status.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID, e.g. 'CH-A1B2C3'\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ chainEntryId }) => {\n const commits = await mcpQuery<any[]>(\"gitchain.listCommits\", {\n chainEntryId,\n });\n\n if (commits.length === 0) {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `No commits found for chain \"${chainEntryId}\". Use \\`chain-commit\\` to create the first snapshot.`,\n },\n ],\n };\n }\n\n const formatted = commits\n .map((c: any) => {\n const date = new Date(c.createdAt)\n .toISOString()\n .replace(\"T\", \" \")\n .substring(0, 19);\n const msg = c.commitMessage ?? c.changeNote ?? \"(no message)\";\n const links =\n c.linksModified && c.linksModified.length > 0\n ? ` [${c.linksModified.join(\", \")}]`\n : \"\";\n return `- **v${c.version}** ${date} by ${c.author} — ${msg}${links} (${c.versionStatus})`;\n })\n .join(\"\\n\");\n\n return {\n content: [\n {\n type: \"text\" as const,\n text: `# Commits for ${chainEntryId} (${commits.length})\\n\\n${formatted}`,\n },\n ],\n };\n }\n );\n\n // ─── chain-diff ────────────────────────────────────────────────\n\n server.registerTool(\n \"chain-diff\",\n {\n title: \"Diff Chain Versions\",\n description:\n \"Compare two versions of a chain. Shows which links changed, \" +\n \"word-level diffs for modified links, and the coherence score delta. \" +\n \"Use chain-commits first to see available version numbers.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID, e.g. 'CH-A1B2C3'\"),\n versionA: z.number().describe(\"The earlier version number\"),\n versionB: z.number().describe(\"The later version number\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ chainEntryId, versionA, versionB }) => {\n const diff = await mcpMutation<any>(\"gitchain.diffVersions\", {\n chainEntryId,\n versionA,\n versionB,\n });\n\n let text =\n `# Diff: v${versionA} → v${versionB}\\n\\n` +\n `- **Chain:** \\`${diff.chainEntryId}\\`\\n` +\n `- **Coherence:** ${diff.coherenceBefore}% → ${diff.coherenceAfter}% (${diff.coherenceDelta >= 0 ? \"+\" : \"\"}${diff.coherenceDelta})\\n` +\n `- **Links changed:** ${diff.linksChanged.length > 0 ? diff.linksChanged.join(\", \") : \"none\"}\\n`;\n\n for (const ld of diff.linkDiffs) {\n if (ld.status === \"unchanged\") continue;\n\n text += `\\n## ${ld.linkId} (${ld.status})\\n\\n`;\n\n const wordDiff = diff.wordDiffs[ld.linkId];\n if (wordDiff && wordDiff.length > 0) {\n for (const w of wordDiff) {\n if (w.type === \"delete\") {\n text += `~~${w.value.substring(0, 200)}~~`;\n } else if (w.type === \"insert\") {\n text += `**${w.value.substring(0, 200)}**`;\n } else {\n text += w.value.substring(0, 200);\n }\n }\n text += \"\\n\";\n }\n }\n\n return { content: [{ type: \"text\" as const, text }] };\n }\n );\n\n // ─── chain-gate ────────────────────────────────────────────────\n\n server.registerTool(\n \"chain-gate\",\n {\n title: \"Chain Coherence Gate\",\n description:\n \"Run the coherence gate on a chain. Checks: coherence score >= 70%, \" +\n \"all links filled, commit message follows convention. \" +\n \"Returns pass/fail with detailed check results. \" +\n \"This is the same gate that blocks publishing.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID, e.g. 'CH-A1B2C3'\"),\n commitMessage: z\n .string()\n .optional()\n .describe(\"Optional commit message to lint\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ chainEntryId, commitMessage }) => {\n const gate = await mcpQuery<any>(\"gitchain.runGate\", {\n chainEntryId,\n commitMessage,\n });\n\n const checkLines = gate.checks\n .map(\n (c: any) =>\n `- ${c.pass ? \"PASS\" : \"FAIL\"} **${c.name}**: ${c.detail}`\n )\n .join(\"\\n\");\n\n const icon = gate.pass ? \"PASS\" : \"BLOCKED\";\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Gate: ${icon}\\n\\n` +\n `- **Score:** ${gate.score}%\\n` +\n `- **Threshold:** ${gate.threshold}%\\n\\n` +\n `## Checks\\n\\n${checkLines}`,\n },\n ],\n };\n }\n );\n\n // ─── chain-branch ──────────────────────────────────────────────\n\n server.registerTool(\n \"chain-branch\",\n {\n title: \"Create or List Chain Branches\",\n description:\n \"Create a new branch for isolated editing, or list all branches on a chain. \" +\n \"Branches fork from the current published version of main. \" +\n \"Use chain-merge to merge a branch back.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID, e.g. 'CH-A1B2C3'\"),\n action: z\n .enum([\"create\", \"list\"])\n .describe(\"'create' to make a new branch, 'list' to see all branches\"),\n name: z\n .string()\n .optional()\n .describe(\"Branch name for 'create' action. Auto-generated if not provided.\"),\n author: z\n .string()\n .optional()\n .describe(\"Who is creating the branch. Defaults to 'mcp'.\"),\n },\n },\n async ({ chainEntryId, action, name, author }) => {\n if (action === \"create\") {\n const result = await mcpMutation<{\n branchId: string;\n name: string;\n baseVersion: number;\n }>(\"gitchain.createBranch\", { chainEntryId, name, author });\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Branch Created\\n\\n` +\n `- **Name:** ${result.name}\\n` +\n `- **Based on:** v${result.baseVersion}\\n` +\n `- **Chain:** \\`${chainEntryId}\\`\\n\\n` +\n `Edit links and commit on this branch, then use \\`chain-merge\\` to land changes.`,\n },\n ],\n };\n }\n\n const branches = await mcpMutation<any[]>(\"gitchain.listBranches\", {\n chainEntryId,\n });\n\n if (branches.length === 0) {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `No branches found for chain \"${chainEntryId}\".`,\n },\n ],\n };\n }\n\n const formatted = branches\n .map(\n (b: any) =>\n `- **${b.name}** (${b.status}) — based on v${b.baseVersion}, by ${b.createdBy}`\n )\n .join(\"\\n\");\n\n return {\n content: [\n {\n type: \"text\" as const,\n text: `# Branches for ${chainEntryId} (${branches.length})\\n\\n${formatted}`,\n },\n ],\n };\n }\n );\n\n // ─── chain-conflicts ──────────────────────────────────────────\n\n server.registerTool(\n \"chain-conflicts\",\n {\n title: \"Check Branch Conflicts\",\n description:\n \"Check if a branch has conflicts with other active branches. \" +\n \"Conflicts occur when two branches modify the same chain link. \" +\n \"Always check before merging.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID\"),\n branchName: z\n .string()\n .describe(\"The branch name to check for conflicts\"),\n },\n annotations: { readOnlyHint: true },\n },\n async ({ chainEntryId, branchName }) => {\n const result = await mcpMutation<any>(\"gitchain.checkConflicts\", {\n chainEntryId,\n branchName,\n });\n\n if (!result.hasConflicts) {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `# No Conflicts\\n\\nBranch \"${branchName}\" on \\`${chainEntryId}\\` has no conflicts with other active branches. Safe to merge.`,\n },\n ],\n };\n }\n\n const conflictLines = result.conflicts\n .map(\n (c: any) =>\n `- **${c.linkId}** — modified by: ${c.branches.map((b: any) => `${b.branchName} (${b.author})`).join(\", \")}`\n )\n .join(\"\\n\");\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Conflicts Detected\\n\\n` +\n `Branch \"${branchName}\" conflicts with other branches on these links:\\n\\n` +\n conflictLines +\n `\\n\\nResolve conflicts before merging. One branch must update its link content to avoid overwriting.`,\n },\n ],\n };\n }\n );\n\n // ─── chain-merge ───────────────────────────────────────────────\n\n server.registerTool(\n \"chain-merge\",\n {\n title: \"Merge Branch\",\n description:\n \"Merge a branch back into main. Runs the coherence gate before merging. \" +\n \"The branch is closed after merge. \" +\n \"Check for conflicts with chain-conflicts first.\",\n inputSchema: {\n chainEntryId: z\n .string()\n .describe(\"The chain's entry ID\"),\n branchName: z\n .string()\n .describe(\"The branch to merge\"),\n strategy: z\n .enum([\"merge_commit\", \"squash\"])\n .optional()\n .describe(\"Merge strategy: 'merge_commit' (default) preserves history, 'squash' collapses into one version\"),\n author: z\n .string()\n .optional()\n .describe(\"Who is merging. Defaults to 'mcp'.\"),\n },\n },\n async ({ chainEntryId, branchName, strategy, author }) => {\n const result = await mcpMutation<{\n entryId: string;\n branchName: string;\n version: number;\n strategy: string;\n }>(\"gitchain.mergeBranch\", {\n chainEntryId,\n branchName,\n strategy,\n author,\n });\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Branch Merged\\n\\n` +\n `- **Chain:** \\`${result.entryId}\\`\\n` +\n `- **Branch:** ${result.branchName} (now closed)\\n` +\n `- **Version:** v${result.version}\\n` +\n `- **Strategy:** ${result.strategy}\\n\\n` +\n `Main is now at v${result.version}. The branch has been closed.`,\n },\n ],\n };\n }\n );\n\n // ─── chain-comment ─────────────────────────────────────────────\n\n server.registerTool(\n \"chain-comment\",\n {\n title: \"Comment on Chain Version\",\n description:\n \"Add a threaded comment on a specific chain version, optionally targeting a specific link. \" +\n \"Comments support threading via parentId. Use chain-comments to see existing comments.\",\n inputSchema: {\n chainEntryId: z.string().describe(\"The chain's entry ID\"),\n action: z\n .enum([\"add\", \"resolve\", \"list\"])\n .describe(\"'add' a comment, 'resolve' a comment, or 'list' all comments\"),\n versionNumber: z\n .number()\n .optional()\n .describe(\"Version number to comment on (required for 'add')\"),\n linkId: z\n .string()\n .optional()\n .describe(\"Which link this comment targets (optional)\"),\n body: z\n .string()\n .optional()\n .describe(\"Comment text (required for 'add')\"),\n commentId: z\n .string()\n .optional()\n .describe(\"Comment ID to resolve (required for 'resolve')\"),\n author: z.string().optional().describe(\"Who is commenting. Defaults to 'mcp'.\"),\n },\n },\n async ({ chainEntryId, action, versionNumber, linkId, body, commentId, author }) => {\n if (action === \"add\") {\n if (!versionNumber) throw new Error(\"versionNumber is required for 'add'\");\n if (!body) throw new Error(\"body is required for 'add'\");\n\n const result = await mcpMutation<any>(\"gitchain.addComment\", {\n chainEntryId,\n versionNumber,\n linkId,\n body,\n author,\n });\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Comment Added\\n\\n` +\n `- **Chain:** \\`${result.chainEntryId}\\`\\n` +\n `- **Version:** v${result.versionNumber}\\n` +\n (result.linkId ? `- **Link:** ${result.linkId}\\n` : \"\") +\n `- **Body:** ${body?.substring(0, 200)}`,\n },\n ],\n };\n }\n\n if (action === \"resolve\") {\n if (!commentId) throw new Error(\"commentId is required for 'resolve'\");\n\n await mcpMutation(\"gitchain.resolveComment\", { commentId });\n\n return {\n content: [\n {\n type: \"text\" as const,\n text: `Comment resolved.`,\n },\n ],\n };\n }\n\n // list\n const comments = await mcpMutation<any[]>(\"gitchain.listComments\", {\n chainEntryId,\n versionNumber,\n });\n\n if (comments.length === 0) {\n return {\n content: [\n {\n type: \"text\" as const,\n text: `No comments found for chain \"${chainEntryId}\"${versionNumber ? ` v${versionNumber}` : \"\"}.`,\n },\n ],\n };\n }\n\n const formatted = comments\n .map((c: any) => {\n const date = new Date(c.createdAt).toISOString().replace(\"T\", \" \").substring(0, 19);\n const resolved = c.resolved ? \" (RESOLVED)\" : \"\";\n const link = c.linkId ? ` [${c.linkId}]` : \"\";\n return `- **v${c.version}${link}** ${date} by ${c.author}${resolved}: ${c.body.substring(0, 150)}`;\n })\n .join(\"\\n\");\n\n const unresolvedCount = comments.filter((c: any) => !c.resolved).length;\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Comments for ${chainEntryId} (${comments.length}, ${unresolvedCount} unresolved)\\n\\n${formatted}`,\n },\n ],\n };\n }\n );\n\n // ─── chain-revert ──────────────────────────────────────────────\n\n server.registerTool(\n \"chain-revert\",\n {\n title: \"Revert Chain\",\n description:\n \"Safely revert a chain to a previous version. Creates a NEW version \" +\n \"that restores the content of the target version — history is preserved, \" +\n \"nothing is destroyed. Use chain-commits to see available versions.\",\n inputSchema: {\n chainEntryId: z.string().describe(\"The chain's entry ID\"),\n toVersion: z.number().describe(\"The version number to revert to\"),\n author: z.string().optional().describe(\"Who is reverting. Defaults to 'mcp'.\"),\n },\n },\n async ({ chainEntryId, toVersion, author }) => {\n const result = await mcpMutation<{\n entryId: string;\n revertedTo: number;\n newVersion: number;\n linksModified: string[];\n }>(\"gitchain.revertChain\", { chainEntryId, toVersion, author });\n\n return {\n content: [\n {\n type: \"text\" as const,\n text:\n `# Reverted\\n\\n` +\n `- **Chain:** \\`${result.entryId}\\`\\n` +\n `- **Reverted to:** v${result.revertedTo}\\n` +\n `- **New version:** v${result.newVersion}\\n` +\n `- **Links affected:** ${result.linksModified.length > 0 ? result.linksModified.join(\", \") : \"none\"}\\n\\n` +\n `History is preserved — this created a new version, not a destructive reset.`,\n },\n ],\n };\n }\n );\n}\n","import { McpServer, ResourceTemplate } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { mcpQuery } from \"../client.js\";\n\nfunction formatEntryMarkdown(entry: any): string {\n const id = entry.entryId ? `${entry.entryId}: ` : \"\";\n const lines = [`## ${id}${entry.name} [${entry.status}]`];\n if (entry.data && typeof entry.data === \"object\") {\n for (const [key, val] of Object.entries(entry.data)) {\n if (val && key !== \"rawData\") {\n lines.push(`**${key}**: ${typeof val === \"string\" ? val : JSON.stringify(val)}`);\n }\n }\n }\n return lines.join(\"\\n\");\n}\n\nfunction buildOrientationMarkdown(\n collections: any[] | null,\n trackingEvents: any[] | null,\n standards: any[] | null,\n businessRules: any[] | null,\n): string {\n const sections: string[] = [\"# Product Brain — Orientation\"];\n\n // Architecture\n sections.push(\n \"## Architecture\\n\" +\n \"```\\n\" +\n \"Cursor (stdio) → MCP Server (mcp-server/src/index.ts)\\n\" +\n \" → POST /api/mcp with Bearer token\\n\" +\n \" → Convex HTTP Action (convex/http.ts)\\n\" +\n \" → internalQuery / internalMutation (convex/mcpKnowledge.ts)\\n\" +\n \" → Convex DB (workspace-scoped)\\n\" +\n \"```\\n\" +\n \"Security: API key auth on every request, workspace-scoped data, internal functions blocked from external clients.\\n\" +\n \"Key files: `packages/mcp-server/src/client.ts` (HTTP client + audit), `convex/schema.ts` (schema).\",\n );\n\n // Data model\n if (collections) {\n const collList = collections\n .map((c) => {\n const prefix = c.icon ? `${c.icon} ` : \"\";\n return `- ${prefix}**${c.name}** (\\`${c.slug}\\`) — ${c.description || \"no description\"}`;\n })\n .join(\"\\n\");\n sections.push(\n `## Data Model (${collections.length} collections)\\n` +\n \"Unified entries model: collections define field schemas, entries hold data in a flexible `data` field.\\n\" +\n \"Tags for filtering (e.g. `severity:high`), relations via `entryRelations`, history via `entryHistory`, labels via `labels` + `entryLabels`.\\n\\n\" +\n collList + \"\\n\\n\" +\n \"Use `list-collections` for field schemas, `get-entry` for full records.\",\n );\n } else {\n sections.push(\n \"## Data Model\\n\" +\n \"Could not load collections — use `list-collections` to browse manually.\",\n );\n }\n\n // Business rules\n const rulesCount = businessRules ? `${businessRules.length} entries` : \"not loaded — collection may not exist yet\";\n sections.push(\n \"## Business Rules\\n\" +\n `Collection: \\`business-rules\\` (${rulesCount}).\\n` +\n \"Find rules: `kb-search` for text search, `list-entries collection=business-rules` to browse.\\n\" +\n \"Check compliance: use the `review-against-rules` prompt (pass a domain).\\n\" +\n \"Draft a new rule: use the `draft-rule-from-context` prompt.\",\n );\n\n // Analytics / tracking\n const eventsCount = trackingEvents ? `${trackingEvents.length} events` : \"not loaded — collection may not exist yet\";\n const conventionNote = standards\n ? \"Naming convention: `object_action` in snake_case with past-tense verbs (from standards collection).\"\n : \"Naming convention: `object_action` in snake_case with past-tense verbs.\";\n sections.push(\n \"## Analytics & Tracking\\n\" +\n `Event catalog: \\`tracking-events\\` collection (${eventsCount}).\\n` +\n `${conventionNote}\\n` +\n \"Implementation: `src/lib/analytics.ts`. Workspace-scoped events MUST use `withWorkspaceGroup()`.\\n\" +\n \"Browse: `list-entries collection=tracking-events`. Full setup: `docs/posthog-setup.md`.\",\n );\n\n // Knowledge Graph\n sections.push(\n \"## Knowledge Graph\\n\" +\n \"Entries are connected via typed relations (`entryRelations` table). Relations are bidirectional and collection-agnostic — any entry can link to any other entry.\\n\\n\" +\n \"**Recommended relation types** (extensible — any string accepted):\\n\" +\n \"- `governs` — a rule constrains behavior of a feature\\n\" +\n \"- `defines_term_for` — a glossary term is canonical vocabulary for a feature/area\\n\" +\n \"- `belongs_to` — a feature belongs to a product area or parent concept\\n\" +\n \"- `informs` — a decision or insight informs a feature\\n\" +\n \"- `surfaces_tension_in` — a tension exists within a feature area\\n\" +\n \"- `related_to`, `depends_on`, `replaces`, `conflicts_with`, `references`, `confused_with`\\n\\n\" +\n \"Each relation type is defined as a glossary entry (prefix `GT-REL-*`) to prevent terminology drift.\\n\\n\" +\n \"**Tools:**\\n\" +\n \"- `gather-context` — get the full context around any entry (multi-hop graph traversal)\\n\" +\n \"- `suggest-links` — discover potential connections for an entry\\n\" +\n \"- `relate-entries` — create a typed link between two entries\\n\" +\n \"- `find-related` — list direct relations for an entry\\n\\n\" +\n \"**Convention:** When creating or updating entries in governed collections, always use `suggest-links` to discover and create relevant relations.\",\n );\n\n // Creating Knowledge\n sections.push(\n \"## Creating Knowledge\\n\" +\n \"Use `smart-capture` as the primary tool for creating new entries. It handles the full workflow in one call:\\n\" +\n \"1. Creates the entry with collection-aware defaults (auto-fills dates, infers domains, sets priority)\\n\" +\n \"2. Auto-links related entries from across the knowledge base (up to 5 confident matches)\\n\" +\n \"3. Returns a quality scorecard (X/10) with actionable improvement suggestions\\n\\n\" +\n \"**Smart profiles** exist for: `tensions`, `business-rules`, `glossary`, `decisions`.\\n\" +\n \"All other collections use sensible defaults.\\n\\n\" +\n \"Example: `smart-capture collection='tensions' name='...' description='...'`\\n\\n\" +\n \"Use `quality-check` to score existing entries retroactively.\\n\" +\n \"Use `create-entry` only when you need full control over every field.\\n\" +\n \"Use `quick-capture` for minimal ceremony without auto-linking.\",\n );\n\n // Where to go next\n sections.push(\n \"## Where to Go Next\\n\" +\n \"- **Create entry** → `smart-capture` tool (auto-links + quality score in one call)\\n\" +\n \"- **Full context** → `gather-context` tool (start here when working on a feature)\\n\" +\n \"- **Discover links** → `suggest-links` tool\\n\" +\n \"- **Quality audit** → `quality-check` tool\\n\" +\n \"- **Terminology** → `name-check` prompt or `productbrain://terminology` resource\\n\" +\n \"- **Schema details** → `productbrain://collections` resource or `list-collections` tool\\n\" +\n \"- **Labels** → `productbrain://labels` resource or `list-labels` tool\\n\" +\n \"- **Any collection** → `productbrain://{slug}/entries` resource\\n\" +\n \"- **Log a decision** → `draft-decision-record` prompt\\n\" +\n \"- **Architecture map** → `show-architecture` tool (layered system visualization)\\n\" +\n \"- **Explore a layer** → `explore-layer` tool (drill into Auth, Core, Features, etc.)\\n\" +\n \"- **Health check** → `health` tool\\n\" +\n \"- **Debug MCP calls** → `mcp-audit` tool\",\n );\n\n return sections.join(\"\\n\\n---\\n\\n\");\n}\n\nexport function registerResources(server: McpServer) {\n // Orientation: single-call system map for AI developers\n server.resource(\n \"kb-orientation\",\n \"productbrain://orientation\",\n async (uri) => {\n const [collectionsResult, eventsResult, standardsResult, rulesResult] = await Promise.allSettled([\n mcpQuery<any[]>(\"kb.listCollections\"),\n mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"tracking-events\" }),\n mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"standards\" }),\n mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"business-rules\" }),\n ]);\n\n const collections = collectionsResult.status === \"fulfilled\" ? collectionsResult.value : null;\n const trackingEvents = eventsResult.status === \"fulfilled\" ? eventsResult.value : null;\n const standards = standardsResult.status === \"fulfilled\" ? standardsResult.value : null;\n const businessRules = rulesResult.status === \"fulfilled\" ? rulesResult.value : null;\n\n return {\n contents: [{\n uri: uri.href,\n text: buildOrientationMarkdown(collections, trackingEvents, standards, businessRules),\n mimeType: \"text/markdown\",\n }],\n };\n }\n );\n\n // Terminology: glossary + standards summary for deep-dives\n server.resource(\n \"kb-terminology\",\n \"productbrain://terminology\",\n async (uri) => {\n const [glossaryResult, standardsResult] = await Promise.allSettled([\n mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"glossary\" }),\n mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"standards\" }),\n ]);\n\n const lines: string[] = [\"# Product Brain — Terminology\"];\n\n if (glossaryResult.status === \"fulfilled\") {\n if (glossaryResult.value.length > 0) {\n const terms = glossaryResult.value\n .map((t) => `- **${t.name}** (${t.entryId ?? \"—\"}) [${t.status}]: ${t.data?.canonical ?? t.data?.description ?? \"\"}`)\n .join(\"\\n\");\n lines.push(`## Glossary (${glossaryResult.value.length} terms)\\n\\n${terms}`);\n } else {\n lines.push(\"## Glossary\\n\\nNo glossary terms yet. Use `create-entry` with collection `glossary` to add terms.\");\n }\n } else {\n lines.push(\"## Glossary\\n\\nCould not load glossary — use `list-entries collection=glossary` to browse manually.\");\n }\n\n if (standardsResult.status === \"fulfilled\") {\n if (standardsResult.value.length > 0) {\n const stds = standardsResult.value\n .map((s) => `- **${s.name}** (${s.entryId ?? \"—\"}) [${s.status}]: ${s.data?.description ?? \"\"}`)\n .join(\"\\n\");\n lines.push(`## Standards (${standardsResult.value.length} entries)\\n\\n${stds}`);\n } else {\n lines.push(\"## Standards\\n\\nNo standards yet. Use `create-entry` with collection `standards` to add standards.\");\n }\n } else {\n lines.push(\"## Standards\\n\\nCould not load standards — use `list-entries collection=standards` to browse manually.\");\n }\n\n return {\n contents: [{ uri: uri.href, text: lines.join(\"\\n\\n---\\n\\n\"), mimeType: \"text/markdown\" }],\n };\n }\n );\n\n server.resource(\n \"kb-collections\",\n \"productbrain://collections\",\n async (uri) => {\n const collections = await mcpQuery<any[]>(\"kb.listCollections\");\n\n if (collections.length === 0) {\n return { contents: [{ uri: uri.href, text: \"No collections in this workspace.\", mimeType: \"text/markdown\" }] };\n }\n\n const formatted = collections\n .map((c) => {\n const fieldList = c.fields\n .map((f: any) => ` - \\`${f.key}\\` (${f.type}${f.required ? \", required\" : \"\"}${f.searchable ? \", searchable\" : \"\"})`)\n .join(\"\\n\");\n return `## ${c.icon ?? \"\"} ${c.name} (\\`${c.slug}\\`)\\n${c.description || \"\"}\\n\\n**Fields:**\\n${fieldList}`;\n })\n .join(\"\\n\\n---\\n\\n\");\n\n return {\n contents: [{ uri: uri.href, text: `# Knowledge Collections (${collections.length})\\n\\n${formatted}`, mimeType: \"text/markdown\" }],\n };\n }\n );\n\n server.resource(\n \"kb-collection-entries\",\n new ResourceTemplate(\"productbrain://{slug}/entries\", {\n list: async () => {\n const collections = await mcpQuery<any[]>(\"kb.listCollections\");\n return {\n resources: collections.map((c) => ({\n uri: `productbrain://${c.slug}/entries`,\n name: `${c.icon ?? \"\"} ${c.name}`.trim(),\n })),\n };\n },\n }),\n async (uri, { slug }) => {\n const entries = await mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: slug as string });\n const formatted = entries.map(formatEntryMarkdown).join(\"\\n\\n---\\n\\n\");\n\n return {\n contents: [{\n uri: uri.href,\n text: formatted || \"No entries in this collection.\",\n mimeType: \"text/markdown\",\n }],\n };\n }\n );\n\n server.resource(\n \"kb-labels\",\n \"productbrain://labels\",\n async (uri) => {\n const labels = await mcpQuery<any[]>(\"kb.listLabels\");\n\n if (labels.length === 0) {\n return { contents: [{ uri: uri.href, text: \"No labels in this workspace.\", mimeType: \"text/markdown\" }] };\n }\n\n const groups = labels.filter((l) => l.isGroup);\n const ungrouped = labels.filter((l) => !l.isGroup && !l.parentId);\n const children = (parentId: string) => labels.filter((l) => l.parentId === parentId);\n\n const lines: string[] = [];\n for (const group of groups) {\n lines.push(`## ${group.name}`);\n for (const child of children(group._id)) {\n lines.push(`- \\`${child.slug}\\` ${child.name}${child.color ? ` (${child.color})` : \"\"}`);\n }\n }\n if (ungrouped.length > 0) {\n lines.push(\"## Ungrouped\");\n for (const l of ungrouped) {\n lines.push(`- \\`${l.slug}\\` ${l.name}${l.color ? ` (${l.color})` : \"\"}`);\n }\n }\n\n return {\n contents: [{ uri: uri.href, text: `# Workspace Labels (${labels.length})\\n\\n${lines.join(\"\\n\")}`, mimeType: \"text/markdown\" }],\n };\n }\n );\n}\n","import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\nimport { mcpQuery } from \"../client.js\";\nimport { getWorkflow, listWorkflows } from \"../workflows/definitions.js\";\n\nexport function registerPrompts(server: McpServer) {\n server.prompt(\n \"review-against-rules\",\n \"Review code or a design decision against all business rules for a given domain. Fetches the rules and asks you to do a structured compliance review.\",\n { domain: z.string().describe(\"Business rule domain (e.g. 'Identity & Access', 'Governance & Decision-Making')\") },\n async ({ domain }) => {\n const entries = await mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"business-rules\" });\n const rules = entries.filter((e) => e.data?.domain === domain);\n\n if (rules.length === 0) {\n return {\n messages: [\n {\n role: \"user\" as const,\n content: {\n type: \"text\" as const,\n text: `No business rules found for domain \"${domain}\". Use the list-entries tool with collection \"business-rules\" to see available domains.`,\n },\n },\n ],\n };\n }\n\n const rulesText = rules\n .map(\n (r) =>\n `### ${r.entryId ?? \"\"}: ${r.name}\\n` +\n `Status: ${r.status} | Severity: ${r.data?.severity ?? \"unknown\"}\\n` +\n `Description: ${r.data?.description ?? \"\"}\\n` +\n `Data Impact: ${r.data?.dataImpact ?? \"\"}\\n` +\n `Platforms: ${(r.data?.platforms ?? []).join(\", \")}\\n` +\n (r.data?.conflictWith ? `CONFLICT: ${r.data.conflictWith.rule} — ${r.data.conflictWith.nature}\\n` : \"\")\n )\n .join(\"\\n---\\n\\n\");\n\n return {\n messages: [\n {\n role: \"user\" as const,\n content: {\n type: \"text\" as const,\n text:\n `Review the current code or design against the following business rules for the \"${domain}\" domain.\\n\\n` +\n `For each rule, assess:\\n` +\n `1. Is the current implementation compliant?\\n` +\n `2. Are there potential violations or edge cases?\\n` +\n `3. What specific changes would be needed for compliance?\\n\\n` +\n `Business Rules:\\n\\n${rulesText}\\n\\n` +\n `Provide a structured review with a compliance status for each rule (COMPLIANT / AT RISK / VIOLATION / NOT APPLICABLE).`,\n },\n },\n ],\n };\n }\n );\n\n server.prompt(\n \"name-check\",\n \"Check variable names, field names, or API names against the glossary for terminology alignment. Flags drift from canonical terms.\",\n { names: z.string().describe(\"Comma-separated list of names to check (e.g. 'vendor_id, compliance_level, formulator_type')\") },\n async ({ names }) => {\n const terms = await mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"glossary\" });\n\n const glossaryContext = terms\n .map(\n (t) =>\n `${t.name} (${t.entryId ?? \"\"}) [${t.status}]: ${t.data?.canonical ?? \"\"}` +\n (t.data?.confusedWith?.length > 0 ? ` — Often confused with: ${t.data.confusedWith.join(\", \")}` : \"\") +\n (t.data?.codeMapping?.length > 0\n ? `\\n Code mappings: ${t.data.codeMapping.map((m: any) => `${m.platform}:${m.field}`).join(\", \")}`\n : \"\")\n )\n .join(\"\\n\");\n\n return {\n messages: [\n {\n role: \"user\" as const,\n content: {\n type: \"text\" as const,\n text:\n `Check the following names against the glossary for terminology alignment:\\n\\n` +\n `Names to check: ${names}\\n\\n` +\n `Glossary (canonical terms):\\n${glossaryContext}\\n\\n` +\n `For each name:\\n` +\n `1. Does it match a canonical term? If so, which one?\\n` +\n `2. Is there terminology drift? (e.g. using \"vendor\" instead of \"supplier\", \"compliance\" instead of \"conformance\")\\n` +\n `3. Suggest the canonical alternative if drift is detected.\\n` +\n `4. Flag any names that don't have a corresponding glossary term (might need one).\\n\\n` +\n `Format as a table: Name | Status | Canonical Form | Action Needed`,\n },\n },\n ],\n };\n }\n );\n\n server.prompt(\n \"draft-decision-record\",\n \"Draft a structured decision record from a description of what was decided. Includes context from recent decisions and relevant rules.\",\n { context: z.string().describe(\"Description of the decision (e.g. 'We decided to use MRSL v3.1 as the conformance baseline because...')\") },\n async ({ context }) => {\n const recentDecisions = await mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"decisions\" });\n const sorted = [...recentDecisions]\n .sort((a, b) => ((b.data?.date ?? \"\") > (a.data?.date ?? \"\") ? 1 : -1))\n .slice(0, 5);\n\n const recentContext = sorted.length > 0\n ? sorted.map((d) => `- [${d.status}] ${d.name} (${d.data?.date ?? \"no date\"})`).join(\"\\n\")\n : \"No previous decisions recorded.\";\n\n return {\n messages: [\n {\n role: \"user\" as const,\n content: {\n type: \"text\" as const,\n text:\n `Draft a structured decision record from the following context:\\n\\n` +\n `\"${context}\"\\n\\n` +\n `Recent decisions for reference:\\n${recentContext}\\n\\n` +\n `Structure the decision record with:\\n` +\n `1. **Title**: Concise decision statement\\n` +\n `2. **Decided by**: Who made or approved this decision\\n` +\n `3. **Date**: When it was decided\\n` +\n `4. **Status**: decided / proposed / revisited\\n` +\n `5. **Rationale**: Why this decision was made, including trade-offs considered\\n` +\n `6. **Alternatives considered**: What else was on the table\\n` +\n `7. **Related rules or tensions**: Any business rules or tensions this connects to\\n\\n` +\n `After drafting, I can log it using the create-entry tool with collection \"decisions\".`,\n },\n },\n ],\n };\n }\n );\n\n server.prompt(\n \"draft-rule-from-context\",\n \"Draft a new business rule from an observation or discovery made while coding. Fetches existing rules for the domain to ensure consistency.\",\n {\n observation: z.string().describe(\"What you observed or discovered (e.g. 'Suppliers can have multiple org types in Gateway')\"),\n domain: z.string().describe(\"Which domain this rule belongs to (e.g. 'Governance & Decision-Making')\"),\n },\n async ({ observation, domain }) => {\n const allRules = await mcpQuery<any[]>(\"kb.listEntries\", { collectionSlug: \"business-rules\" });\n const existingRules = allRules.filter((r) => r.data?.domain === domain);\n\n const existingContext =\n existingRules.length > 0\n ? existingRules.map((r) => `${r.entryId ?? \"\"}: ${r.name} [${r.status}] — ${r.data?.description ?? \"\"}`).join(\"\\n\")\n : \"No existing rules for this domain.\";\n\n const highestRuleNum = allRules\n .map((r) => parseInt((r.entryId ?? \"\").replace(/^[A-Z]+-/, \"\"), 10))\n .filter((n) => !isNaN(n))\n .sort((a, b) => b - a)[0] || 0;\n const nextRuleId = `SOS-${String(highestRuleNum + 1).padStart(3, \"0\")}`;\n\n return {\n messages: [\n {\n role: \"user\" as const,\n content: {\n type: \"text\" as const,\n text:\n `Draft a business rule based on this observation:\\n\\n` +\n `\"${observation}\"\\n\\n` +\n `Domain: ${domain}\\n` +\n `Suggested rule ID: ${nextRuleId}\\n\\n` +\n `Existing rules in this domain:\\n${existingContext}\\n\\n` +\n `Draft the rule with these fields:\\n` +\n `1. **entryId**: ${nextRuleId}\\n` +\n `2. **name**: Concise rule title\\n` +\n `3. **data.description**: What the rule states\\n` +\n `4. **data.rationale**: Why this rule matters\\n` +\n `5. **data.dataImpact**: How this affects data models, APIs, or storage\\n` +\n `6. **data.severity**: high / medium / low\\n` +\n `7. **data.platforms**: Which platforms are affected\\n` +\n `8. **data.relatedRules**: Any related existing rules\\n\\n` +\n `Make sure the rule is consistent with existing rules and doesn't contradict them. ` +\n `After drafting, I can create it using the create-entry tool with collection \"business-rules\".`,\n },\n },\n ],\n };\n }\n );\n\n // ── Run Workflow Prompt ──────────────────────────────────────────────────\n\n server.prompt(\n \"run-workflow\",\n \"Launch a Chainwork workflow (retro, shape, IDM) in Facilitator Mode. \" +\n \"Returns the full workflow definition, facilitation instructions, and round structure. \" +\n \"The agent enters Facilitator Mode and guides the participant through each round.\",\n {\n workflow: z.string().describe(\n \"Workflow ID to run. Available: \" +\n listWorkflows().map((w) => `'${w.id}' (${w.name})`).join(\", \"),\n ),\n context: z.string().optional().describe(\n \"Optional context from the participant (e.g., 'retro on last sprint', 'shape the Chainwork API bet')\",\n ),\n },\n async ({ workflow: workflowId, context }) => {\n const wf = getWorkflow(workflowId);\n\n if (!wf) {\n const available = listWorkflows()\n .map((w) => `- **${w.id}**: ${w.name} — ${w.shortDescription}`)\n .join(\"\\n\");\n return {\n messages: [\n {\n role: \"user\" as const,\n content: {\n type: \"text\" as const,\n text:\n `Workflow \"${workflowId}\" not found.\\n\\n` +\n `Available workflows:\\n${available}\\n\\n` +\n `Use one of these IDs to run a workflow.`,\n },\n },\n ],\n };\n }\n\n // Fetch recent KB context for richer facilitation\n let kbContext = \"\";\n try {\n const recentDecisions = await mcpQuery<any[]>(\"kb.listEntries\", {\n collectionSlug: wf.kbOutputCollection,\n });\n const sorted = [...recentDecisions]\n .sort((a, b) => ((b.data?.date ?? \"\") > (a.data?.date ?? \"\") ? 1 : -1))\n .slice(0, 5);\n if (sorted.length > 0) {\n kbContext =\n `\\n## Recent ${wf.kbOutputCollection} entries (for context)\\n` +\n sorted.map((d) => `- ${d.entryId ?? \"\"}: ${d.name} [${d.status}]`).join(\"\\n\");\n }\n } catch {\n kbContext = \"\\n_Could not load KB context — proceed without it._\";\n }\n\n const roundsPlan = wf.rounds\n .map((r) =>\n `### Round ${r.num}: ${r.label}\\n` +\n `**Type**: ${r.type} | **Duration**: ~${r.maxDurationHint ?? \"5 min\"}\\n` +\n `**Instruction**: ${r.instruction}\\n` +\n `**Facilitator guidance**: ${r.facilitatorGuidance}\\n` +\n (r.questions\n ? r.questions.map((q) =>\n `**Question**: ${q.prompt}\\n` +\n (q.options\n ? q.options.map((o) => ` - ${o.id}: ${o.label}`).join(\"\\n\")\n : \" _(open response)_\"),\n ).join(\"\\n\")\n : \"\") +\n `\\n**Output**: Capture to \\`${r.outputSchema.field}\\` (${r.outputSchema.format})`,\n )\n .join(\"\\n\\n---\\n\\n\");\n\n const contextLine = context\n ? `\\nThe participant provided this context: \"${context}\"\\nUse it — don't make them repeat themselves.\\n`\n : \"\";\n\n return {\n messages: [\n {\n role: \"user\" as const,\n content: {\n type: \"text\" as const,\n text:\n `# ${wf.icon} ${wf.name} Workflow — Facilitator Mode\\n\\n` +\n `${wf.shortDescription}\\n` +\n contextLine +\n `\\n---\\n\\n` +\n `## Facilitator Instructions\\n\\n${wf.facilitatorPreamble}\\n\\n` +\n `---\\n\\n` +\n `## Rounds\\n\\n${roundsPlan}\\n\\n` +\n `---\\n\\n` +\n `## Error Recovery\\n\\n${wf.errorRecovery}\\n\\n` +\n `---\\n\\n` +\n `## KB Output\\n\\n` +\n `When complete, use \\`smart-capture\\` to create a \\`${wf.kbOutputCollection}\\` entry.\\n` +\n `Name template: ${wf.kbOutputTemplate.nameTemplate}\\n` +\n `Description field: ${wf.kbOutputTemplate.descriptionField}\\n` +\n kbContext +\n `\\n\\n---\\n\\n` +\n `**BEGIN THE WORKFLOW NOW.** Start with Round 01. Create a Plan first.`,\n },\n },\n ],\n };\n },\n );\n}\n"],"mappings":";;;AAAA,SAAS,gBAAAA,qBAAoB;AAC7B,SAAS,WAAAC,gBAAe;AACxB,SAAS,aAAAC,kBAAiB;AAC1B,SAAS,4BAA4B;;;ACFrC,SAAS,SAAS;;;ACKlB,SAAS,gBAAgB;AACzB,SAAS,eAAe;AAExB,IAAI,SAAyB;AAC7B,IAAI,aAAa;AAEjB,IAAM,eAAe;AAMrB,SAAS,IAAI,KAAmB;AAC9B,MAAI,QAAQ,IAAI,cAAc,KAAK;AACjC,YAAQ,OAAO,MAAM,GAAG;AAAA,EAC1B;AACF;AAEO,SAAS,gBAAsB;AACpC,QAAM,SAAS,QAAQ,IAAI,mBAAmB;AAC9C,MAAI,CAAC,QAAQ;AACX,QAAI,iHAA4G;AAChH;AAAA,EACF;AAEA,WAAS,IAAI,QAAQ,QAAQ;AAAA,IAC3B,MAAM;AAAA,IACN,SAAS;AAAA,IACT,eAAe;AAAA,EACjB,CAAC;AACD,eAAa,QAAQ,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","workspaceId","workspace","workspaceId","server","z","server","z","z","log","server","workspaceId","z","z","server","z","z","passed","server","z","extractPreview","existsSync","readFileSync","resolve","z","server","z","resolveProjectRoot","resolve","existsSync","readFileSync","abs","z","server","z","z","server","z","server","z","server","z","resolve","readFileSync","McpServer"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { readFileSync } from \"node:fs\";\nimport { resolve } from \"node:path\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\n\nimport {\n getWorkspaceId,\n bootstrap,\n orphanAgentSession,\n startAgentSession,\n getAgentSessionId,\n recoverSessionState,\n} from \"./client.js\";\nimport { initAnalytics, trackSessionStarted, shutdownAnalytics } from \"./analytics.js\";\nimport { createProductBrainServer, SERVER_VERSION } from \"./server.js\";\n\nif (!process.env.CONVEX_SITE_URL && !process.env.PRODUCTBRAIN_API_KEY) {\n try {\n const envPath = resolve(process.cwd(), \".env.mcp\");\n for (const line of readFileSync(envPath, \"utf-8\").split(\"\\n\")) {\n const trimmed = line.trim();\n if (!trimmed || trimmed.startsWith(\"#\")) continue;\n const eqIdx = trimmed.indexOf(\"=\");\n if (eqIdx === -1) continue;\n process.env[trimmed.slice(0, eqIdx)] ??= trimmed.slice(eqIdx + 1);\n }\n } catch {\n // .env.mcp not found — rely on env vars being set by the launcher\n }\n}\n\nbootstrap();\ninitAnalytics();\n\nconst server = createProductBrainServer();\nconst transport = new StdioServerTransport();\nawait server.connect(transport);\n\ngetWorkspaceId()\n .then(async (wsId) => {\n trackSessionStarted(wsId, SERVER_VERSION);\n try {\n await startAgentSession();\n process.stderr.write(\"[MCP] Agent session started automatically.\\n\");\n } catch (err: any) {\n process.stderr.write(`[MCP] Auto session start failed: ${err.message}. Call agent-start manually.\\n`);\n await recoverSessionState();\n }\n })\n .catch(() => {\n process.stderr.write(\"[MCP] Workspace resolution deferred — will retry on first tool call.\\n\");\n });\n\nasync function gracefulShutdown() {\n if (getAgentSessionId()) {\n await orphanAgentSession();\n }\n await shutdownAnalytics();\n process.exit(0);\n}\n\nprocess.on(\"SIGINT\", gracefulShutdown);\nprocess.on(\"SIGTERM\", gracefulShutdown);\n\nprocess.stdin.on(\"end\", async () => {\n if (getAgentSessionId()) {\n await orphanAgentSession();\n }\n await shutdownAnalytics();\n process.exit(0);\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,SAAS,oBAAoB;AAC7B,SAAS,eAAe;AACxB,SAAS,4BAA4B;AAarC,IAAI,CAAC,QAAQ,IAAI,mBAAmB,CAAC,QAAQ,IAAI,sBAAsB;AACrE,MAAI;AACF,UAAM,UAAU,QAAQ,QAAQ,IAAI,GAAG,UAAU;AACjD,eAAW,QAAQ,aAAa,SAAS,OAAO,EAAE,MAAM,IAAI,GAAG;AAC7D,YAAM,UAAU,KAAK,KAAK;AAC1B,UAAI,CAAC,WAAW,QAAQ,WAAW,GAAG,EAAG;AACzC,YAAM,QAAQ,QAAQ,QAAQ,GAAG;AACjC,UAAI,UAAU,GAAI;AAClB,cAAQ,IAAI,QAAQ,MAAM,GAAG,KAAK,CAAC,MAAM,QAAQ,MAAM,QAAQ,CAAC;AAAA,IAClE;AAAA,EACF,QAAQ;AAAA,EAER;AACF;AAEA,UAAU;AACV,cAAc;AAEd,IAAM,SAAS,yBAAyB;AACxC,IAAM,YAAY,IAAI,qBAAqB;AAC3C,MAAM,OAAO,QAAQ,SAAS;AAE9B,eAAe,EACZ,KAAK,OAAO,SAAS;AACpB,sBAAoB,MAAM,cAAc;AACxC,MAAI;AACF,UAAM,kBAAkB;AACxB,YAAQ,OAAO,MAAM,8CAA8C;AAAA,EACrE,SAAS,KAAU;AACjB,YAAQ,OAAO,MAAM,oCAAoC,IAAI,OAAO;AAAA,CAAgC;AACpG,UAAM,oBAAoB;AAAA,EAC5B;AACF,CAAC,EACA,MAAM,MAAM;AACX,UAAQ,OAAO,MAAM,6EAAwE;AAC/F,CAAC;AAEH,eAAe,mBAAmB;AAChC,MAAI,kBAAkB,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