@productbrain/mcp 0.0.1-beta.12 → 0.0.1-beta.13

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.
@@ -64,9 +64,6 @@ async function writeClientConfig(client, apiKey) {
64
64
  const config = readJsonSafe(client.configPath);
65
65
  const serversKey = "mcpServers";
66
66
  if (!config[serversKey]) config[serversKey] = {};
67
- if (config[serversKey][SERVER_ENTRY_KEY]) {
68
- return false;
69
- }
70
67
  if (config[serversKey][LEGACY_ENTRY_KEY]) {
71
68
  const legacy = config[serversKey][LEGACY_ENTRY_KEY];
72
69
  config[serversKey][SERVER_ENTRY_KEY] = {
@@ -75,7 +72,8 @@ async function writeClientConfig(client, apiKey) {
75
72
  };
76
73
  delete config[serversKey][LEGACY_ENTRY_KEY];
77
74
  } else {
78
- config[serversKey][SERVER_ENTRY_KEY] = buildServerEntry(apiKey);
75
+ const existing = config[serversKey][SERVER_ENTRY_KEY];
76
+ config[serversKey][SERVER_ENTRY_KEY] = existing ? { ...existing, env: { ...existing.env, PRODUCTBRAIN_API_KEY: apiKey } } : buildServerEntry(apiKey);
79
77
  }
80
78
  const dir = dirname(client.configPath);
81
79
  if (!existsSync(dir)) {
@@ -364,4 +362,4 @@ function printClaudeSnippet() {
364
362
  export {
365
363
  runSetup
366
364
  };
367
- //# sourceMappingURL=setup-XXUPPFF2.js.map
365
+ //# sourceMappingURL=setup-GZ5OZ5OP.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cli/setup.ts","../src/cli/config-writer.ts"],"sourcesContent":["#!/usr/bin/env node\n\n/**\n * `npx @productbrain/mcp setup`\n *\n * Guided onboarding: get API key from the app, paste it, write MCP config,\n * and optionally install Cursor rules/skills (additive-only).\n */\n\nimport { execSync } from \"node:child_process\";\nimport { createInterface } from \"node:readline\";\nimport { existsSync, writeFileSync, mkdirSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { resolveClient, writeClientConfig, type McpClientInfo } from \"./config-writer.js\";\nimport { initAnalytics, trackSetupStarted, trackSetupCompleted, shutdownAnalytics } from \"../analytics.js\";\n\nconst APP_URL =\n process.env.PRODUCTBRAIN_APP_URL ?? \"https://productbrain.io\";\n\n// ── Helpers ─────────────────────────────────────────────────────────────\n\nfunction bold(s: string) {\n return `\\x1b[1m${s}\\x1b[0m`;\n}\nfunction green(s: string) {\n return `\\x1b[32m${s}\\x1b[0m`;\n}\nfunction dim(s: string) {\n return `\\x1b[2m${s}\\x1b[0m`;\n}\nfunction orange(s: string) {\n return `\\x1b[33m${s}\\x1b[0m`;\n}\n\nfunction log(msg: string) {\n process.stdout.write(`${msg}\\n`);\n}\n\nfunction openBrowser(url: string) {\n const platform = process.platform;\n try {\n if (platform === \"darwin\") execSync(`open \"${url}\"`);\n else if (platform === \"win32\") execSync(`start \"\" \"${url}\"`);\n else execSync(`xdg-open \"${url}\"`);\n } catch {\n log(dim(` Could not open browser automatically.`));\n log(` Open this URL manually: ${url}`);\n }\n}\n\nfunction prompt(question: string): Promise<string> {\n return new Promise((resolve) => {\n const rl = createInterface({ input: process.stdin, output: process.stdout });\n rl.question(question, (answer) => {\n rl.close();\n resolve(answer.trim());\n });\n });\n}\n\nfunction promptChoice(question: string, choices: string[]): Promise<number> {\n return new Promise((resolve) => {\n log(\"\");\n log(bold(question));\n choices.forEach((c, i) => log(` ${i + 1}) ${c}`));\n const rl = createInterface({ input: process.stdin, output: process.stdout });\n rl.question(`\\n ${dim(\"Choice [1]:\")} `, (line) => {\n rl.close();\n const n = parseInt(line.trim(), 10);\n if (isNaN(n) || n < 1 || n > choices.length) {\n resolve(0);\n } else {\n resolve(n - 1);\n }\n });\n });\n}\n\n// ── Workspace Verification ───────────────────────────────────────────────\n\nconst DEFAULT_CLOUD_URL = \"https://trustworthy-kangaroo-277.convex.site\";\n\nasync function verifyWorkspace(\n apiKey: string,\n): Promise<{ name: string; slug: string } | null> {\n const siteUrl = process.env.CONVEX_SITE_URL\n ?? process.env.PRODUCTBRAIN_URL\n ?? DEFAULT_CLOUD_URL;\n\n try {\n const res = await fetch(`${siteUrl.replace(/\\/$/, \"\")}/api/mcp`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${apiKey}`,\n },\n body: JSON.stringify({ fn: \"resolveWorkspace\", args: {} }),\n });\n\n if (!res.ok) return null;\n const json = (await res.json()) as {\n data?: { name: string; slug: string } | null;\n error?: string;\n };\n return json.data ?? null;\n } catch {\n return null;\n }\n}\n\n// ── Main ────────────────────────────────────────────────────────────────\n\nexport async function runSetup() {\n initAnalytics();\n trackSetupStarted();\n\n log(\"\");\n log(bold(` Product${orange(\"Brain\")} Setup`));\n log(dim(\" Connect your AI assistant to your chain\\n\"));\n\n const apiKeysUrl = `${APP_URL}/settings/api-keys`;\n\n log(` ${dim(\"1. Get your API key from Settings → API Keys\")}`);\n log(` ${dim(apiKeysUrl)}\\n`);\n\n const openNow = await prompt(` Open this URL in your browser? [Y/n]: `);\n if (openNow.toLowerCase() !== \"n\" && openNow.toLowerCase() !== \"no\") {\n openBrowser(apiKeysUrl);\n }\n\n log(\"\");\n log(` ${dim(\"2. Generate a key (if you don't have one), then copy it.\\n\")}`);\n\n const apiKey = await prompt(` Paste your API key (pb_sk_...): `);\n\n if (!apiKey || !apiKey.startsWith(\"pb_sk_\")) {\n log(` ${orange(\"!\")} Invalid key format. Keys start with pb_sk_.`);\n log(` Get one at ${apiKeysUrl}\\n`);\n await shutdownAnalytics();\n process.exit(1);\n }\n\n log(` ${green(\"✓\")} Key received`);\n\n const workspace = await verifyWorkspace(apiKey);\n if (workspace) {\n log(` ${green(\"✓\")} Connected to workspace: ${bold(workspace.name)} ${dim(`(${workspace.slug})`)}`);\n } else {\n log(` ${orange(\"!\")} Could not verify workspace. Check your key at ${apiKeysUrl}`);\n }\n log(\"\");\n\n const CLIENT_NAMES = [\"Cursor\", \"Claude Desktop\"] as const;\n const options = [...CLIENT_NAMES, \"Other\"];\n\n const choice = await promptChoice(\"Where do you want to set up Product Brain?\", options);\n\n if (choice === 2) {\n printConfigSnippet(apiKey);\n trackSetupCompleted(\"Other\", \"snippet_shown\");\n } else {\n const client = resolveClient(CLIENT_NAMES[choice]);\n if (client) {\n const outcome = await writeConfig(client, apiKey);\n trackSetupCompleted(CLIENT_NAMES[choice], outcome);\n } else {\n log(` ${orange(\"!\")} ${CLIENT_NAMES[choice]} config path not available on this platform.`);\n printConfigSnippet(apiKey);\n trackSetupCompleted(CLIENT_NAMES[choice], \"write_error\");\n }\n }\n\n // Cursor-specific: offer to install rule (additive-only)\n if (choice === 0) {\n await offerCursorRulesInstall();\n printDeeplink(apiKey);\n }\n\n // Claude-specific: print snippet (never write to CLAUDE.md)\n if (choice === 1) {\n printClaudeSnippet();\n }\n\n log(\"\");\n log(\n ` ${green(\"✓\")} Done! Restart your AI assistant and try: ${bold('\"Start PB\"')}`,\n );\n printHelpLink();\n await shutdownAnalytics();\n}\n\nasync function writeConfig(\n client: McpClientInfo,\n apiKey: string,\n): Promise<\"config_written\" | \"config_existed\" | \"write_error\"> {\n try {\n const wrote = await writeClientConfig(client, apiKey);\n if (wrote) {\n log(` ${green(\"✓\")} Wrote config to ${dim(client.configPath)}`);\n return \"config_written\";\n } else {\n log(` ${dim(\"ℹ\")} ${client.name} already configured — skipped`);\n return \"config_existed\";\n }\n } catch (err: any) {\n log(` ${orange(\"!\")} Could not write ${client.name} config: ${err.message}`);\n printConfigSnippet(apiKey);\n return \"write_error\";\n }\n}\n\nfunction printHelpLink() {\n log(` ${dim(`Need help? See ${APP_URL}/settings/api-keys`)}`);\n log(\"\");\n}\n\nfunction printConfigSnippet(apiKey: string) {\n log(\"\");\n log(bold(\" Add this to your MCP client config:\\n\"));\n const snippet = JSON.stringify(\n {\n mcpServers: {\n \"Product Brain\": {\n command: \"npx\",\n args: [\"-y\", \"@productbrain/mcp@beta\"],\n env: { PRODUCTBRAIN_API_KEY: apiKey },\n },\n },\n },\n null,\n 2,\n );\n for (const line of snippet.split(\"\\n\")) {\n log(` ${line}`);\n }\n log(\"\");\n}\n\n// ── Cursor Rules/Skills Install (additive-only) ─────────────────────────\n\nconst CURSOR_RULE_FILENAME = \"product-brain.mdc\";\n\nconst CURSOR_RULE_CONTENT = `---\ndescription: Product Brain MCP — single source of truth for product knowledge\nglobs:\nalwaysApply: true\n---\n\n# Product Brain MCP\n\nProduct Brain is your product knowledge base. The Chain is the single source of truth.\n\nEvery entry is either a **draft** (captured but not committed) or **committed** (on the Chain, SSOT).\nCommitting to the Chain is the compounding act.\n\n## Quick Start\n\nSay **\"Start PB\"** or **\"Start Product Brain\"** to begin. This single call:\n- Orients you to the workspace (readiness, gaps, planned work)\n- Unlocks write tools for the session\n- Surfaces your next recommended action\n\n## Tool Workflow\n\n1. **Start here**: \\`orient\\` or \\`start\\` — workspace context + next action\n2. **Search**: \\`search\\` — find entries across all collections\n3. **Drill in**: \\`get-entry\\` — full record with data, labels, relations\n4. **Context**: \\`gather-context\\` — related knowledge around an entry or task\n5. **Capture**: \\`capture\\` — create a draft with auto-linking + quality score\n6. **Commit**: \\`commit-entry\\` — promote draft to SSOT (only when user confirms)\n7. **Connect**: \\`suggest-links\\` then \\`relate-entries\\` to build the graph\n\n## Bulk Knowledge Input\n\nWhen given a document or batch of knowledge to capture:\n1. Scan the input — identify all collections needed\n2. Call \\`list-collections\\` — compare against what exists\n3. Propose missing collections to the user for confirmation\n4. Call \\`create-collection\\` for each confirmed collection\n5. Then capture entries into the correct collections\n\nNever stuff entries into the wrong collection. Never silently skip knowledge.\n\n## Rules\n\n- Always capture as draft first. Only call \\`commit-entry\\` when the user confirms.\n- Use \\`suggest-links\\` after capturing to discover and create relations.\n- Collections are dynamic — use \\`create-collection\\` when the workspace needs new ones.\n- When lost, fetch \\`productbrain://orientation\\` for the full system map.\n`;\n\nfunction isCursorProject(): boolean {\n return existsSync(join(process.cwd(), \".cursor\")) || existsSync(join(process.cwd(), \".cursorignore\"));\n}\n\nasync function offerCursorRulesInstall(): Promise<void> {\n if (!isCursorProject()) return;\n\n const answer = await prompt(`\\n Install Product Brain rule for Cursor? [Y/n]: `);\n if (answer.toLowerCase() === \"n\" || answer.toLowerCase() === \"no\") {\n log(dim(\" Skipped rule install.\"));\n return;\n }\n\n const rulesDir = join(process.cwd(), \".cursor\", \"rules\");\n const rulePath = join(rulesDir, CURSOR_RULE_FILENAME);\n\n if (existsSync(rulePath)) {\n log(` ${dim(\"ℹ\")} Rule already exists at ${dim(rulePath)} — skipped`);\n return;\n }\n\n if (!existsSync(rulesDir)) {\n mkdirSync(rulesDir, { recursive: true });\n }\n\n writeFileSync(rulePath, CURSOR_RULE_CONTENT, \"utf-8\");\n log(` ${green(\"✓\")} Installed rule at ${dim(rulePath)}`);\n}\n\nfunction buildDeeplink(apiKey: string): string {\n const config = JSON.stringify({\n command: \"npx\",\n args: [\"-y\", \"@productbrain/mcp@beta\"],\n env: { PRODUCTBRAIN_API_KEY: apiKey },\n });\n const encoded = Buffer.from(config).toString(\"base64url\");\n return `cursor://anysphere.cursor-deeplink/mcp/install?name=${encodeURIComponent(\"Product Brain\")}&config=${encoded}`;\n}\n\nfunction printDeeplink(apiKey: string): void {\n const link = buildDeeplink(apiKey);\n log(\"\");\n log(` ${dim(\"One-click install for Cursor (paste in browser):\")}`);\n log(` ${link}`);\n}\n\nfunction printClaudeSnippet(): void {\n log(\"\");\n log(bold(\" For Claude Code / CLAUDE.md:\"));\n log(dim(\" Add this line to your ~/.claude/CLAUDE.md:\"));\n log(\"\");\n log(` When Product Brain MCP is available, say \"Start PB\" at the beginning`);\n log(` of each session to orient to the workspace and unlock write tools.`);\n log(` Always capture as draft first; only commit when the user confirms.`);\n log(\"\");\n}\n","/**\n * Multi-client MCP config detection and writer.\n *\n * Supports:\n * - Cursor: .cursor/mcp.json in cwd (project-level)\n * - Claude Desktop: ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)\n * %APPDATA%/Claude/claude_desktop_config.json (Windows)\n *\n * The writer reads existing config, merges the new server entry (never\n * overwrites existing entries), and writes back. Falls back to printing\n * a snippet for unsupported OS or unknown formats.\n */\n\nimport { existsSync, readFileSync, writeFileSync, mkdirSync } from \"node:fs\";\nimport { join, dirname } from \"node:path\";\nimport { homedir, platform } from \"node:os\";\n\nexport interface McpClientInfo {\n name: string;\n configPath: string;\n}\n\nconst SERVER_ENTRY_KEY = \"Product Brain\";\nconst LEGACY_ENTRY_KEY = \"productbrain\";\n\nfunction buildServerEntry(apiKey: string) {\n return {\n command: \"npx\",\n args: [\"-y\", \"@productbrain/mcp@beta\"],\n env: { PRODUCTBRAIN_API_KEY: apiKey },\n };\n}\n\n// ── Detection ───────────────────────────────────────────────────────────\n\nfunction getCursorConfigPath(): string {\n return join(process.cwd(), \".cursor\", \"mcp.json\");\n}\n\nfunction getClaudeDesktopConfigPath(): string | null {\n const os = platform();\n if (os === \"darwin\") {\n return join(\n homedir(),\n \"Library\",\n \"Application Support\",\n \"Claude\",\n \"claude_desktop_config.json\",\n );\n }\n if (os === \"win32\") {\n const appData = process.env.APPDATA ?? join(homedir(), \"AppData\", \"Roaming\");\n return join(appData, \"Claude\", \"claude_desktop_config.json\");\n }\n // Linux: no official Claude Desktop location yet\n return null;\n}\n\nexport function resolveClient(name: \"Cursor\" | \"Claude Desktop\"): McpClientInfo | null {\n if (name === \"Cursor\") {\n return { name, configPath: getCursorConfigPath() };\n }\n const configPath = getClaudeDesktopConfigPath();\n return configPath ? { name, configPath } : null;\n}\n\n// ── Writing ─────────────────────────────────────────────────────────────\n\nfunction readJsonSafe(path: string): Record<string, any> {\n if (!existsSync(path)) return {};\n try {\n return JSON.parse(readFileSync(path, \"utf-8\"));\n } catch {\n return {};\n }\n}\n\n/**\n * Write or merge the Product Brain server entry into a client config file.\n * Migrates legacy \"productbrain\" key to \"Product Brain\" when present.\n * Returns true if the config was written, false if already present.\n */\nexport async function writeClientConfig(\n client: McpClientInfo,\n apiKey: string,\n): Promise<boolean> {\n const config = readJsonSafe(client.configPath);\n\n const serversKey = \"mcpServers\";\n if (!config[serversKey]) config[serversKey] = {};\n\n // Don't overwrite an existing Product Brain entry\n if (config[serversKey][SERVER_ENTRY_KEY]) {\n return false;\n }\n\n // Migrate legacy \"productbrain\" key to \"Product Brain\", preserving existing env\n if (config[serversKey][LEGACY_ENTRY_KEY]) {\n const legacy = config[serversKey][LEGACY_ENTRY_KEY];\n config[serversKey][SERVER_ENTRY_KEY] = {\n ...buildServerEntry(apiKey),\n env: { ...legacy.env, PRODUCTBRAIN_API_KEY: legacy.env?.PRODUCTBRAIN_API_KEY ?? apiKey },\n };\n delete config[serversKey][LEGACY_ENTRY_KEY];\n } else {\n config[serversKey][SERVER_ENTRY_KEY] = buildServerEntry(apiKey);\n }\n\n const dir = dirname(client.configPath);\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n\n writeFileSync(client.configPath, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n return true;\n}\n"],"mappings":";;;;;;;;;AASA,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,cAAAA,aAAY,iBAAAC,gBAAe,aAAAC,kBAAiB;AACrD,SAAS,QAAAC,aAAY;;;ACCrB,SAAS,YAAY,cAAc,eAAe,iBAAiB;AACnE,SAAS,MAAM,eAAe;AAC9B,SAAS,SAAS,gBAAgB;AAOlC,IAAM,mBAAmB;AACzB,IAAM,mBAAmB;AAEzB,SAAS,iBAAiB,QAAgB;AACxC,SAAO;AAAA,IACL,SAAS;AAAA,IACT,MAAM,CAAC,MAAM,wBAAwB;AAAA,IACrC,KAAK,EAAE,sBAAsB,OAAO;AAAA,EACtC;AACF;AAIA,SAAS,sBAA8B;AACrC,SAAO,KAAK,QAAQ,IAAI,GAAG,WAAW,UAAU;AAClD;AAEA,SAAS,6BAA4C;AACnD,QAAM,KAAK,SAAS;AACpB,MAAI,OAAO,UAAU;AACnB,WAAO;AAAA,MACL,QAAQ;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;A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;AAAA,EACtC,CAAC;AACD,QAAM,UAAU,OAAO,KAAK,MAAM,EAAE,SAAS,WAAW;AACxD,SAAO,uDAAuD,mBAAmB,eAAe,CAAC,WAAW,OAAO;AACrH;AAEA,SAAS,cAAc,QAAsB;AAC3C,QAAM,OAAO,cAAc,MAAM;AACjC,MAAI,EAAE;AACN,MAAI,KAAK,IAAI,kDAAkD,CAAC,EAAE;AAClE,MAAI,KAAK,IAAI,EAAE;AACjB;AAEA,SAAS,qBAA2B;AAClC,MAAI,EAAE;AACN,MAAI,KAAK,gCAAgC,CAAC;AAC1C,MAAI,IAAI,8CAA8C,CAAC;AACvD,MAAI,EAAE;AACN,MAAI,0EAA0E;AAC9E,MAAI,wEAAwE;AAC5E,MAAI,wEAAwE;AAC5E,MAAI,EAAE;AACR;","names":["existsSync","writeFileSync","mkdirSync","join","platform","existsSync","join","mkdirSync","writeFileSync"]}
1
+ {"version":3,"sources":["../src/cli/setup.ts","../src/cli/config-writer.ts"],"sourcesContent":["#!/usr/bin/env node\n\n/**\n * `npx @productbrain/mcp setup`\n *\n * Guided onboarding: get API key from the app, paste it, write MCP config,\n * and optionally install Cursor rules/skills (additive-only).\n */\n\nimport { execSync } from \"node:child_process\";\nimport { createInterface } from \"node:readline\";\nimport { existsSync, writeFileSync, mkdirSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { resolveClient, writeClientConfig, type McpClientInfo } from \"./config-writer.js\";\nimport { initAnalytics, trackSetupStarted, trackSetupCompleted, shutdownAnalytics } from \"../analytics.js\";\n\nconst APP_URL =\n process.env.PRODUCTBRAIN_APP_URL ?? \"https://productbrain.io\";\n\n// ── Helpers ─────────────────────────────────────────────────────────────\n\nfunction bold(s: string) {\n return `\\x1b[1m${s}\\x1b[0m`;\n}\nfunction green(s: string) {\n return `\\x1b[32m${s}\\x1b[0m`;\n}\nfunction dim(s: string) {\n return `\\x1b[2m${s}\\x1b[0m`;\n}\nfunction orange(s: string) {\n return `\\x1b[33m${s}\\x1b[0m`;\n}\n\nfunction log(msg: string) {\n process.stdout.write(`${msg}\\n`);\n}\n\nfunction openBrowser(url: string) {\n const platform = process.platform;\n try {\n if (platform === \"darwin\") execSync(`open \"${url}\"`);\n else if (platform === \"win32\") execSync(`start \"\" \"${url}\"`);\n else execSync(`xdg-open \"${url}\"`);\n } catch {\n log(dim(` Could not open browser automatically.`));\n log(` Open this URL manually: ${url}`);\n }\n}\n\nfunction prompt(question: string): Promise<string> {\n return new Promise((resolve) => {\n const rl = createInterface({ input: process.stdin, output: process.stdout });\n rl.question(question, (answer) => {\n rl.close();\n resolve(answer.trim());\n });\n });\n}\n\nfunction promptChoice(question: string, choices: string[]): Promise<number> {\n return new Promise((resolve) => {\n log(\"\");\n log(bold(question));\n choices.forEach((c, i) => log(` ${i + 1}) ${c}`));\n const rl = createInterface({ input: process.stdin, output: process.stdout });\n rl.question(`\\n ${dim(\"Choice [1]:\")} `, (line) => {\n rl.close();\n const n = parseInt(line.trim(), 10);\n if (isNaN(n) || n < 1 || n > choices.length) {\n resolve(0);\n } else {\n resolve(n - 1);\n }\n });\n });\n}\n\n// ── Workspace Verification ───────────────────────────────────────────────\n\nconst DEFAULT_CLOUD_URL = \"https://trustworthy-kangaroo-277.convex.site\";\n\nasync function verifyWorkspace(\n apiKey: string,\n): Promise<{ name: string; slug: string } | null> {\n const siteUrl = process.env.CONVEX_SITE_URL\n ?? process.env.PRODUCTBRAIN_URL\n ?? DEFAULT_CLOUD_URL;\n\n try {\n const res = await fetch(`${siteUrl.replace(/\\/$/, \"\")}/api/mcp`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${apiKey}`,\n },\n body: JSON.stringify({ fn: \"resolveWorkspace\", args: {} }),\n });\n\n if (!res.ok) return null;\n const json = (await res.json()) as {\n data?: { name: string; slug: string } | null;\n error?: string;\n };\n return json.data ?? null;\n } catch {\n return null;\n }\n}\n\n// ── Main ────────────────────────────────────────────────────────────────\n\nexport async function runSetup() {\n initAnalytics();\n trackSetupStarted();\n\n log(\"\");\n log(bold(` Product${orange(\"Brain\")} Setup`));\n log(dim(\" Connect your AI assistant to your chain\\n\"));\n\n const apiKeysUrl = `${APP_URL}/settings/api-keys`;\n\n log(` ${dim(\"1. Get your API key from Settings → API Keys\")}`);\n log(` ${dim(apiKeysUrl)}\\n`);\n\n const openNow = await prompt(` Open this URL in your browser? [Y/n]: `);\n if (openNow.toLowerCase() !== \"n\" && openNow.toLowerCase() !== \"no\") {\n openBrowser(apiKeysUrl);\n }\n\n log(\"\");\n log(` ${dim(\"2. Generate a key (if you don't have one), then copy it.\\n\")}`);\n\n const apiKey = await prompt(` Paste your API key (pb_sk_...): `);\n\n if (!apiKey || !apiKey.startsWith(\"pb_sk_\")) {\n log(` ${orange(\"!\")} Invalid key format. Keys start with pb_sk_.`);\n log(` Get one at ${apiKeysUrl}\\n`);\n await shutdownAnalytics();\n process.exit(1);\n }\n\n log(` ${green(\"✓\")} Key received`);\n\n const workspace = await verifyWorkspace(apiKey);\n if (workspace) {\n log(` ${green(\"✓\")} Connected to workspace: ${bold(workspace.name)} ${dim(`(${workspace.slug})`)}`);\n } else {\n log(` ${orange(\"!\")} Could not verify workspace. Check your key at ${apiKeysUrl}`);\n }\n log(\"\");\n\n const CLIENT_NAMES = [\"Cursor\", \"Claude Desktop\"] as const;\n const options = [...CLIENT_NAMES, \"Other\"];\n\n const choice = await promptChoice(\"Where do you want to set up Product Brain?\", options);\n\n if (choice === 2) {\n printConfigSnippet(apiKey);\n trackSetupCompleted(\"Other\", \"snippet_shown\");\n } else {\n const client = resolveClient(CLIENT_NAMES[choice]);\n if (client) {\n const outcome = await writeConfig(client, apiKey);\n trackSetupCompleted(CLIENT_NAMES[choice], outcome);\n } else {\n log(` ${orange(\"!\")} ${CLIENT_NAMES[choice]} config path not available on this platform.`);\n printConfigSnippet(apiKey);\n trackSetupCompleted(CLIENT_NAMES[choice], \"write_error\");\n }\n }\n\n // Cursor-specific: offer to install rule (additive-only)\n if (choice === 0) {\n await offerCursorRulesInstall();\n printDeeplink(apiKey);\n }\n\n // Claude-specific: print snippet (never write to CLAUDE.md)\n if (choice === 1) {\n printClaudeSnippet();\n }\n\n log(\"\");\n log(\n ` ${green(\"✓\")} Done! Restart your AI assistant and try: ${bold('\"Start PB\"')}`,\n );\n printHelpLink();\n await shutdownAnalytics();\n}\n\nasync function writeConfig(\n client: McpClientInfo,\n apiKey: string,\n): Promise<\"config_written\" | \"config_existed\" | \"write_error\"> {\n try {\n const wrote = await writeClientConfig(client, apiKey);\n if (wrote) {\n log(` ${green(\"✓\")} Wrote config to ${dim(client.configPath)}`);\n return \"config_written\";\n } else {\n log(` ${dim(\"ℹ\")} ${client.name} already configured — skipped`);\n return \"config_existed\";\n }\n } catch (err: any) {\n log(` ${orange(\"!\")} Could not write ${client.name} config: ${err.message}`);\n printConfigSnippet(apiKey);\n return \"write_error\";\n }\n}\n\nfunction printHelpLink() {\n log(` ${dim(`Need help? See ${APP_URL}/settings/api-keys`)}`);\n log(\"\");\n}\n\nfunction printConfigSnippet(apiKey: string) {\n log(\"\");\n log(bold(\" Add this to your MCP client config:\\n\"));\n const snippet = JSON.stringify(\n {\n mcpServers: {\n \"Product Brain\": {\n command: \"npx\",\n args: [\"-y\", \"@productbrain/mcp@beta\"],\n env: { PRODUCTBRAIN_API_KEY: apiKey },\n },\n },\n },\n null,\n 2,\n );\n for (const line of snippet.split(\"\\n\")) {\n log(` ${line}`);\n }\n log(\"\");\n}\n\n// ── Cursor Rules/Skills Install (additive-only) ─────────────────────────\n\nconst CURSOR_RULE_FILENAME = \"product-brain.mdc\";\n\nconst CURSOR_RULE_CONTENT = `---\ndescription: Product Brain MCP — single source of truth for product knowledge\nglobs:\nalwaysApply: true\n---\n\n# Product Brain MCP\n\nProduct Brain is your product knowledge base. The Chain is the single source of truth.\n\nEvery entry is either a **draft** (captured but not committed) or **committed** (on the Chain, SSOT).\nCommitting to the Chain is the compounding act.\n\n## Quick Start\n\nSay **\"Start PB\"** or **\"Start Product Brain\"** to begin. This single call:\n- Orients you to the workspace (readiness, gaps, planned work)\n- Unlocks write tools for the session\n- Surfaces your next recommended action\n\n## Tool Workflow\n\n1. **Start here**: \\`orient\\` or \\`start\\` — workspace context + next action\n2. **Search**: \\`search\\` — find entries across all collections\n3. **Drill in**: \\`get-entry\\` — full record with data, labels, relations\n4. **Context**: \\`gather-context\\` — related knowledge around an entry or task\n5. **Capture**: \\`capture\\` — create a draft with auto-linking + quality score\n6. **Commit**: \\`commit-entry\\` — promote draft to SSOT (only when user confirms)\n7. **Connect**: \\`suggest-links\\` then \\`relate-entries\\` to build the graph\n\n## Bulk Knowledge Input\n\nWhen given a document or batch of knowledge to capture:\n1. Scan the input — identify all collections needed\n2. Call \\`list-collections\\` — compare against what exists\n3. Propose missing collections to the user for confirmation\n4. Call \\`create-collection\\` for each confirmed collection\n5. Then capture entries into the correct collections\n\nNever stuff entries into the wrong collection. Never silently skip knowledge.\n\n## Rules\n\n- Always capture as draft first. Only call \\`commit-entry\\` when the user confirms.\n- Use \\`suggest-links\\` after capturing to discover and create relations.\n- Collections are dynamic — use \\`create-collection\\` when the workspace needs new ones.\n- When lost, fetch \\`productbrain://orientation\\` for the full system map.\n`;\n\nfunction isCursorProject(): boolean {\n return existsSync(join(process.cwd(), \".cursor\")) || existsSync(join(process.cwd(), \".cursorignore\"));\n}\n\nasync function offerCursorRulesInstall(): Promise<void> {\n if (!isCursorProject()) return;\n\n const answer = await prompt(`\\n Install Product Brain rule for Cursor? [Y/n]: `);\n if (answer.toLowerCase() === \"n\" || answer.toLowerCase() === \"no\") {\n log(dim(\" Skipped rule install.\"));\n return;\n }\n\n const rulesDir = join(process.cwd(), \".cursor\", \"rules\");\n const rulePath = join(rulesDir, CURSOR_RULE_FILENAME);\n\n if (existsSync(rulePath)) {\n log(` ${dim(\"ℹ\")} Rule already exists at ${dim(rulePath)} — skipped`);\n return;\n }\n\n if (!existsSync(rulesDir)) {\n mkdirSync(rulesDir, { recursive: true });\n }\n\n writeFileSync(rulePath, CURSOR_RULE_CONTENT, \"utf-8\");\n log(` ${green(\"✓\")} Installed rule at ${dim(rulePath)}`);\n}\n\nfunction buildDeeplink(apiKey: string): string {\n const config = JSON.stringify({\n command: \"npx\",\n args: [\"-y\", \"@productbrain/mcp@beta\"],\n env: { PRODUCTBRAIN_API_KEY: apiKey },\n });\n const encoded = Buffer.from(config).toString(\"base64url\");\n return `cursor://anysphere.cursor-deeplink/mcp/install?name=${encodeURIComponent(\"Product Brain\")}&config=${encoded}`;\n}\n\nfunction printDeeplink(apiKey: string): void {\n const link = buildDeeplink(apiKey);\n log(\"\");\n log(` ${dim(\"One-click install for Cursor (paste in browser):\")}`);\n log(` ${link}`);\n}\n\nfunction printClaudeSnippet(): void {\n log(\"\");\n log(bold(\" For Claude Code / CLAUDE.md:\"));\n log(dim(\" Add this line to your ~/.claude/CLAUDE.md:\"));\n log(\"\");\n log(` When Product Brain MCP is available, say \"Start PB\" at the beginning`);\n log(` of each session to orient to the workspace and unlock write tools.`);\n log(` Always capture as draft first; only commit when the user confirms.`);\n log(\"\");\n}\n","/**\n * Multi-client MCP config detection and writer.\n *\n * Supports:\n * - Cursor: .cursor/mcp.json in cwd (project-level)\n * - Claude Desktop: ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)\n * %APPDATA%/Claude/claude_desktop_config.json (Windows)\n *\n * The writer reads existing config, merges the new server entry (never\n * overwrites existing entries), and writes back. Falls back to printing\n * a snippet for unsupported OS or unknown formats.\n */\n\nimport { existsSync, readFileSync, writeFileSync, mkdirSync } from \"node:fs\";\nimport { join, dirname } from \"node:path\";\nimport { homedir, platform } from \"node:os\";\n\nexport interface McpClientInfo {\n name: string;\n configPath: string;\n}\n\nconst SERVER_ENTRY_KEY = \"Product Brain\";\nconst LEGACY_ENTRY_KEY = \"productbrain\";\n\nfunction buildServerEntry(apiKey: string) {\n return {\n command: \"npx\",\n args: [\"-y\", \"@productbrain/mcp@beta\"],\n env: { PRODUCTBRAIN_API_KEY: apiKey },\n };\n}\n\n// ── Detection ───────────────────────────────────────────────────────────\n\nfunction getCursorConfigPath(): string {\n return join(process.cwd(), \".cursor\", \"mcp.json\");\n}\n\nfunction getClaudeDesktopConfigPath(): string | null {\n const os = platform();\n if (os === \"darwin\") {\n return join(\n homedir(),\n \"Library\",\n \"Application Support\",\n \"Claude\",\n \"claude_desktop_config.json\",\n );\n }\n if (os === \"win32\") {\n const appData = process.env.APPDATA ?? join(homedir(), \"AppData\", \"Roaming\");\n return join(appData, \"Claude\", \"claude_desktop_config.json\");\n }\n // Linux: no official Claude Desktop location yet\n return null;\n}\n\nexport function resolveClient(name: \"Cursor\" | \"Claude Desktop\"): McpClientInfo | null {\n if (name === \"Cursor\") {\n return { name, configPath: getCursorConfigPath() };\n }\n const configPath = getClaudeDesktopConfigPath();\n return configPath ? { name, configPath } : null;\n}\n\n// ── Writing ─────────────────────────────────────────────────────────────\n\nfunction readJsonSafe(path: string): Record<string, any> {\n if (!existsSync(path)) return {};\n try {\n return JSON.parse(readFileSync(path, \"utf-8\"));\n } catch {\n return {};\n }\n}\n\n/**\n * Write or merge the Product Brain server entry into a client config file.\n * Migrates legacy \"productbrain\" key to \"Product Brain\" when present.\n * Returns true if the config was written, false if already present.\n */\nexport async function writeClientConfig(\n client: McpClientInfo,\n apiKey: string,\n): Promise<boolean> {\n const config = readJsonSafe(client.configPath);\n\n const serversKey = \"mcpServers\";\n if (!config[serversKey]) config[serversKey] = {};\n\n // Migrate legacy \"productbrain\" key or update existing Product Brain with new API key\n if (config[serversKey][LEGACY_ENTRY_KEY]) {\n const legacy = config[serversKey][LEGACY_ENTRY_KEY];\n config[serversKey][SERVER_ENTRY_KEY] = {\n ...buildServerEntry(apiKey),\n env: { ...legacy.env, PRODUCTBRAIN_API_KEY: legacy.env?.PRODUCTBRAIN_API_KEY ?? apiKey },\n };\n delete config[serversKey][LEGACY_ENTRY_KEY];\n } else {\n const existing = config[serversKey][SERVER_ENTRY_KEY];\n config[serversKey][SERVER_ENTRY_KEY] = existing\n ? { ...existing, env: { ...existing.env, PRODUCTBRAIN_API_KEY: apiKey } }\n : buildServerEntry(apiKey);\n }\n\n const dir = dirname(client.configPath);\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n\n writeFileSync(client.configPath, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n return true;\n}\n"],"mappings":";;;;;;;;;AASA,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,cAAAA,aAAY,iBAAAC,gBAAe,aAAAC,kBAAiB;AACrD,SAAS,QAAAC,aAAY;;;ACCrB,SAAS,YAAY,cAAc,eAAe,iBAAiB;AACnE,SAAS,MAAM,eAAe;AAC9B,SAAS,SAAS,gBAAgB;AAOlC,IAAM,mBAAmB;AACzB,IAAM,mBAAmB;AAEzB,SAAS,iBAAiB,QAAgB;AACxC,SAAO;AAAA,IACL,SAAS;AAAA,IACT,MAAM,CAAC,MAAM,wBAAwB;AAAA,IACrC,KAAK,EAAE,sBAAsB,OAAO;AAAA,EACtC;AACF;AAIA,SAAS,sBAA8B;AACrC,SAAO,KAAK,QAAQ,IAAI,GAAG,WAAW,UAAU;AAClD;AAEA,SAAS,6BAA4C;AACnD,QAAM,KAAK,SAAS;AACpB,MAAI,OAAO,UAAU;AACnB,WAAO;AAAA,MACL,QAAQ;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACA,MAAI,OAAO,SAAS;AAClB,UAAM,UAAU,QAAQ,IAAI,WAAW,KAAK,QAAQ,GAAG,WAAW,SAAS;AAC3E,WAAO,KAAK,SAAS,UAAU,4BAA4B;AAAA,EAC7D;AAEA,SAAO;AACT;AAEO,SAAS,cAAc,MAAyD;AACrF,MAAI,SAAS,UAAU;AACrB,WAAO,EAAE,MAAM,YAAY,oBAAoB,EAAE;AAAA,EACnD;AACA,QAAM,aAAa,2BAA2B;AAC9C,SAAO,aAAa,EAAE,MAAM,WAAW,IAAI;AAC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AACjB;AAEA,SAAS,qBAA2B;AAClC,MAAI,EAAE;AACN,MAAI,KAAK,gCAAgC,CAAC;AAC1C,MAAI,IAAI,8CAA8C,CAAC;AACvD,MAAI,EAAE;AACN,MAAI,0EAA0E;AAC9E,MAAI,wEAAwE;AAC5E,MAAI,wEAAwE;AAC5E,MAAI,EAAE;AACR;","names":["existsSync","writeFileSync","mkdirSync","join","platform","existsSync","join","mkdirSync","writeFileSync"]}
@@ -3,7 +3,7 @@ import {
3
3
  formatQualityReport,
4
4
  registerSmartCaptureTools,
5
5
  runContradictionCheck
6
- } from "./chunk-KISCDOJ2.js";
6
+ } from "./chunk-4ETXQ24K.js";
7
7
  import "./chunk-XBMI6QHR.js";
8
8
  export {
9
9
  checkEntryQuality,
@@ -11,4 +11,4 @@ export {
11
11
  registerSmartCaptureTools,
12
12
  runContradictionCheck
13
13
  };
14
- //# sourceMappingURL=smart-capture-SR2ETT67.js.map
14
+ //# sourceMappingURL=smart-capture-LD5DEUCX.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@productbrain/mcp",
3
- "version": "0.0.1-beta.12",
3
+ "version": "0.0.1-beta.13",
4
4
  "description": "Product Brain — MCP server for AI-assisted product knowledge management",
5
5
  "type": "module",
6
6
  "engines": {
@@ -19,6 +19,7 @@
19
19
  "scripts": {
20
20
  "build": "tsup",
21
21
  "start": "node dist/index.js",
22
+ "start:http": "node dist/http.js",
22
23
  "dev": "tsx src/index.ts",
23
24
  "typecheck": "tsc --noEmit",
24
25
  "prepublishOnly": "npm run build",
@@ -28,6 +29,8 @@
28
29
  "dependencies": {
29
30
  "@modelcontextprotocol/sdk": "^1.12.1",
30
31
  "convex": "^1.32.0",
32
+ "express": "^5.2.1",
33
+ "express-rate-limit": "^7.5.1",
31
34
  "posthog-node": "^5.24.17",
32
35
  "zod": "^3.23.0"
33
36
  },
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/tools/smart-capture.ts","../src/client.ts"],"sourcesContent":["/**\n * Smart capture — ARCH-node-mcp (Core layer)\n * Chain: 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, getWorkspaceContext, requireWriteAccess, recordSessionActivity, getAgentSessionId } 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 \"Chain\": [\"knowledge\", \"glossary\", \"entry\", \"collection\", \"terminology\", \"drift\", \"graph\", \"chain\", \"commit\"],\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 add more connections.\",\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 \"Chain\": \"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 [\"audiences\", {\n idPrefix: \"AUD\",\n governedDraft: false,\n descriptionField: \"description\",\n defaults: [],\n recommendedRelationTypes: [\"fills_slot\", \"informs\", \"related_to\", \"references\"],\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n COMMON_CHECKS.hasRelations,\n {\n id: \"has-behaviors\",\n label: \"Behaviors described\",\n check: (ctx) => typeof ctx.data.behaviors === \"string\" && ctx.data.behaviors.length > 20,\n suggestion: () => \"Describe how this audience segment behaves — what do they do, what tools do they use?\",\n },\n ],\n }],\n\n [\"strategy\", {\n idPrefix: \"STR\",\n governedDraft: true,\n descriptionField: \"description\",\n defaults: [],\n recommendedRelationTypes: [\"informs\", \"governs\", \"belongs_to\", \"related_to\"],\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n COMMON_CHECKS.hasRelations,\n COMMON_CHECKS.diverseRelations,\n ],\n }],\n\n [\"maps\", {\n idPrefix: \"MAP\",\n governedDraft: false,\n descriptionField: \"description\",\n defaults: [],\n recommendedRelationTypes: [\"fills_slot\", \"references\", \"related_to\"],\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n ],\n }],\n\n [\"chains\", {\n idPrefix: \"CHN\",\n governedDraft: false,\n descriptionField: \"description\",\n defaults: [],\n recommendedRelationTypes: [\"informs\", \"references\", \"related_to\"],\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n ],\n }],\n\n [\"standards\", {\n idPrefix: \"STD\",\n governedDraft: true,\n descriptionField: \"description\",\n defaults: [],\n recommendedRelationTypes: [\"governs\", \"defines_term_for\", \"references\", \"related_to\"],\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n COMMON_CHECKS.hasRelations,\n ],\n }],\n\n [\"tracking-events\", {\n idPrefix: \"TE\",\n governedDraft: false,\n descriptionField: \"description\",\n defaults: [],\n recommendedRelationTypes: [\"references\", \"belongs_to\", \"related_to\"],\n qualityChecks: [\n COMMON_CHECKS.clearName,\n COMMON_CHECKS.hasDescription,\n ],\n }],\n]);\n\nconst FALLBACK_PROFILE: CollectionProfile = {\n idPrefix: \"ENT\",\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 const effectivePrefix = prefix || \"ENT\";\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 `${effectivePrefix}-${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>(\"chain.getEntry\", { entryId });\n if (!entry) {\n return {\n text: `Entry \\`${entryId}\\` not found. Try search to find the right ID.`,\n quality: { score: 0, maxScore: 10, checks: [] },\n };\n }\n\n const collections = await mcpQuery<any[]>(\"chain.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[]>(\"chain.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 \"capture\",\n {\n title: \"Capture\",\n description:\n \"The single tool for creating knowledge entries. Creates an entry, auto-links related entries, \" +\n \"and returns a quality scorecard — all in one call. \" +\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, features, \" +\n \"audiences, strategy, standards, maps, chains, tracking-events.\\n\" +\n \"All other collections get an ENT-{random} ID and sensible defaults.\\n\\n\" +\n \"Always creates as 'draft' for governed collections. Use `update-entry` for post-creation adjustments.\",\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 requireWriteAccess();\n\n const profile = PROFILES.get(collection) ?? FALLBACK_PROFILE;\n\n const col = await mcpQuery<any>(\"chain.getCollection\", { slug: collection });\n if (!col) {\n const displayName = collection.split(\"-\").map((w: string) => w.charAt(0).toUpperCase() + w.slice(1)).join(\" \");\n return {\n content: [{\n type: \"text\" as const,\n text:\n `Collection \\`${collection}\\` not found.\\n\\n` +\n `**To create it**, run:\\n` +\n `\\`\\`\\`\\ncreate-collection slug=\"${collection}\" name=\"${displayName}\" description=\"...\"\\n\\`\\`\\`\\n\\n` +\n `Or use \\`list-collections\\` to see available collections.`,\n }],\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 const agentId = getAgentSessionId();\n internalId = await mcpMutation<string>(\"chain.createEntry\", {\n collectionSlug: collection,\n entryId: finalEntryId || undefined,\n name,\n status,\n data,\n createdBy: agentId ? `agent:${agentId}` : \"capture\",\n });\n\n await recordSessionActivity({ entryCreated: finalEntryId || internalId });\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[]>(\"chain.searchEntries\", { query: searchQuery }),\n mcpQuery<any[]>(\"chain.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(\"chain.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. Keyword contradiction check against governance entries\n const contradictionWarnings = await runContradictionCheck(name, description);\n if (contradictionWarnings.length > 0) {\n await recordSessionActivity({ contradictionWarning: true });\n }\n\n // 9. Format response\n const wsCtx = await getWorkspaceContext();\n const lines: string[] = [\n `# Captured: ${finalEntryId || name}`,\n `**${name}** added to \\`${collection}\\` as \\`${status}\\``,\n `**Workspace:** ${wsCtx.workspaceSlug} (${wsCtx.workspaceId})`,\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 // Contradiction warnings — exact format per spec\n if (contradictionWarnings.length > 0) {\n lines.push(\"\");\n lines.push(\"⚠ Contradiction check: proposed entry matched existing governance entries:\");\n for (const w of contradictionWarnings) {\n lines.push(`- ${w.name} (${w.collection}, ${w.entryId}) — has 'governs' relation to ${w.governsCount} entries`);\n }\n lines.push(\"Run gather-context on these entries before committing.\");\n }\n\n // Next steps: guide the agent to connect and commit\n lines.push(\"\");\n lines.push(\"## Next Steps\");\n const eid = finalEntryId || \"(check entry ID)\";\n lines.push(`1. **Connect it:** \\`suggest-links entryId=\"${eid}\"\\` — discover what this should link to`);\n lines.push(`2. **Commit it:** \\`commit-entry entryId=\"${eid}\"\\` — promote from draft to SSOT on the Chain`);\n if (failedChecks.length > 0) {\n lines.push(`3. **Improve quality:** \\`update-entry entryId=\"${eid}\"\\` — fill missing fields`);\n }\n\n // Advisory: workspace readiness hints (never blocking)\n try {\n const readiness = await mcpQuery<any>(\"chain.workspaceReadiness\");\n if (readiness && readiness.gaps && readiness.gaps.length > 0) {\n const topGaps = readiness.gaps.slice(0, 2);\n lines.push(\"\");\n lines.push(`## Workspace Readiness: ${readiness.score}%`);\n for (const gap of topGaps) {\n lines.push(`- _${gap.label}:_ ${gap.guidance}`);\n }\n }\n } catch {\n // Readiness check is advisory — capture works without it\n }\n\n return { content: [{ type: \"text\" as const, text: lines.join(\"\\n\") }] };\n }\n );\n\n // ── Batch Capture Tool ─────────────────────────────────────────────────\n\n server.registerTool(\n \"batch-capture\",\n {\n title: \"Batch Capture\",\n description:\n \"Create multiple knowledge entries in one call. Ideal for workspace setup, document ingestion, \" +\n \"or any scenario where you need to capture many entries at once.\\n\\n\" +\n \"Each entry is created independently — if one fails, the others still succeed. \" +\n \"Returns a compact summary instead of per-entry quality scorecards.\\n\\n\" +\n \"Auto-linking runs per entry but contradiction checks and readiness hints are skipped for speed. \" +\n \"Use `quality-check` on individual entries afterward if needed.\",\n inputSchema: {\n entries: z.array(z.object({\n collection: z.string().describe(\"Collection slug\"),\n name: z.string().describe(\"Display name\"),\n description: z.string().describe(\"Full context / definition\"),\n entryId: z.string().optional().describe(\"Optional custom entry ID\"),\n })).min(1).max(50).describe(\"Array of entries to capture\"),\n },\n annotations: { destructiveHint: false },\n },\n async ({ entries }) => {\n requireWriteAccess();\n\n const agentId = getAgentSessionId();\n const createdBy = agentId ? `agent:${agentId}` : \"capture\";\n\n const results: Array<{\n name: string;\n collection: string;\n entryId: string;\n ok: boolean;\n autoLinks: number;\n error?: string;\n }> = [];\n\n const allCollections = await mcpQuery<any[]>(\"chain.listCollections\");\n const collCache = new Map<string, any>();\n for (const c of allCollections) collCache.set(c.slug, c);\n const collIdToSlug = new Map<string, string>();\n for (const c of allCollections) collIdToSlug.set(c._id, c.slug);\n\n for (const entry of entries) {\n const profile = PROFILES.get(entry.collection) ?? FALLBACK_PROFILE;\n const col = collCache.get(entry.collection);\n\n if (!col) {\n results.push({ name: entry.name, collection: entry.collection, entryId: \"\", ok: false, autoLinks: 0, error: `Collection \"${entry.collection}\" not found` });\n continue;\n }\n\n const finalEntryId = entry.entryId ?? generateEntryId(profile.idPrefix);\n\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] = entry.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\") data[def.key] = today;\n else if (def.value !== \"infer\") data[def.key] = def.value;\n }\n\n if (profile.inferField) {\n const inferred = profile.inferField({\n collection: entry.collection, name: entry.name, description: entry.description,\n data, entryId: \"\", linksCreated: [], linksSuggested: [], collectionFields: col.fields ?? [],\n });\n for (const [key, val] of Object.entries(inferred)) {\n if (val !== undefined && val !== \"\") data[key] = val;\n }\n }\n\n if (!data[profile.descriptionField] && !data.description && !data.canonical) {\n data[profile.descriptionField || \"description\"] = entry.description;\n }\n\n try {\n await mcpMutation<string>(\"chain.createEntry\", {\n collectionSlug: entry.collection,\n entryId: finalEntryId,\n name: entry.name,\n status: \"draft\",\n data,\n createdBy,\n });\n\n let autoLinkCount = 0;\n const searchQuery = extractSearchTerms(entry.name, entry.description);\n if (searchQuery) {\n try {\n const searchResults = await mcpQuery<any[]>(\"chain.searchEntries\", { query: searchQuery });\n const candidates = (searchResults ?? [])\n .filter((r) => r.entryId !== finalEntryId)\n .map((r) => ({\n ...r,\n collSlug: collIdToSlug.get(r.collectionId) ?? \"unknown\",\n confidence: computeLinkConfidence(r, entry.name, entry.description, entry.collection, collIdToSlug.get(r.collectionId) ?? \"unknown\"),\n }))\n .sort((a, b) => b.confidence - a.confidence);\n\n for (const c of candidates) {\n if (autoLinkCount >= MAX_AUTO_LINKS) break;\n if (c.confidence < AUTO_LINK_CONFIDENCE_THRESHOLD) break;\n if (!c.entryId) continue;\n const relationType = inferRelationType(entry.collection, c.collSlug, profile);\n try {\n await mcpMutation(\"chain.createEntryRelation\", {\n fromEntryId: finalEntryId,\n toEntryId: c.entryId,\n type: relationType,\n });\n autoLinkCount++;\n } catch { /* skip failed link */ }\n }\n } catch { /* search failed; entry still created */ }\n }\n\n results.push({ name: entry.name, collection: entry.collection, entryId: finalEntryId, ok: true, autoLinks: autoLinkCount });\n await recordSessionActivity({ entryCreated: finalEntryId });\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : String(error);\n results.push({ name: entry.name, collection: entry.collection, entryId: finalEntryId, ok: false, autoLinks: 0, error: msg });\n }\n }\n\n const created = results.filter((r) => r.ok);\n const failed = results.filter((r) => !r.ok);\n const totalAutoLinks = created.reduce((sum, r) => sum + r.autoLinks, 0);\n\n const byCollection = new Map<string, number>();\n for (const r of created) {\n byCollection.set(r.collection, (byCollection.get(r.collection) ?? 0) + 1);\n }\n\n const lines: string[] = [\n `# Batch Capture Complete`,\n `**${created.length}** created, **${failed.length}** failed out of ${entries.length} total.`,\n `**Auto-links created:** ${totalAutoLinks}`,\n \"\",\n ];\n\n if (byCollection.size > 0) {\n lines.push(\"## By Collection\");\n for (const [col, count] of byCollection) {\n lines.push(`- \\`${col}\\`: ${count} entries`);\n }\n lines.push(\"\");\n }\n\n if (created.length > 0) {\n lines.push(\"## Created\");\n for (const r of created) {\n const linkNote = r.autoLinks > 0 ? ` (${r.autoLinks} auto-links)` : \"\";\n lines.push(`- **${r.entryId}**: ${r.name} [${r.collection}]${linkNote}`);\n }\n }\n\n if (failed.length > 0) {\n lines.push(\"\");\n lines.push(\"## Failed\");\n for (const r of failed) {\n lines.push(`- ${r.name} [${r.collection}]: _${r.error}_`);\n }\n }\n\n const entryIds = created.map((r) => r.entryId);\n if (entryIds.length > 0) {\n lines.push(\"\");\n lines.push(\"## Next Steps\");\n lines.push(`- **Connect:** Run \\`suggest-links\\` on key entries to build the knowledge graph`);\n lines.push(`- **Commit:** Use \\`commit-entry\\` to promote drafts to SSOT`);\n lines.push(`- **Quality:** Run \\`quality-check\\` on individual entries to assess completeness`);\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 \"including concrete link suggestions from graph analysis when relations are missing.\\n\\n\" +\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\n // If relations are missing, offer graph-aware suggestions\n const needsRelations = result.quality.checks.some(\n (c) => !c.passed && (c.id === \"has-relations\" || c.id === \"diverse-relations\"),\n );\n\n if (needsRelations) {\n try {\n const suggestions = await mcpQuery<any>(\"chain.graphSuggestLinks\", {\n entryId,\n maxHops: 2,\n limit: 3,\n });\n\n if (suggestions?.suggestions?.length > 0) {\n const linkHints = suggestions.suggestions\n .map((s: any) => ` → \\`relate-entries from='${entryId}' to='${s.entryId}' type='${s.recommendedRelationType}'\\` — ${s.name} [${s.collectionSlug}] (${s.score}/100)`)\n .join(\"\\n\");\n\n result.text += `\\n\\n## Suggested Links to Improve Quality\\n${linkHints}`;\n }\n } catch {\n // Graph suggestions failed — quality check still works without them\n }\n }\n\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// Stop words excluded from noun phrase extraction\nconst STOP_WORDS = new Set([\n \"the\", \"and\", \"for\", \"are\", \"but\", \"not\", \"you\", \"all\", \"can\", \"has\", \"her\",\n \"was\", \"one\", \"our\", \"out\", \"day\", \"had\", \"hot\", \"how\", \"its\", \"may\", \"new\",\n \"now\", \"old\", \"see\", \"way\", \"who\", \"did\", \"get\", \"let\", \"say\", \"she\", \"too\",\n \"use\", \"from\", \"have\", \"been\", \"each\", \"that\", \"this\", \"with\", \"will\", \"they\",\n \"what\", \"when\", \"make\", \"like\", \"long\", \"look\", \"many\", \"some\", \"them\", \"than\",\n \"most\", \"only\", \"over\", \"such\", \"into\", \"also\", \"back\", \"just\", \"much\", \"must\",\n \"name\", \"very\", \"your\", \"after\", \"which\", \"their\", \"about\", \"would\", \"there\",\n \"should\", \"could\", \"other\", \"these\", \"first\", \"being\", \"those\", \"still\", \"where\",\n]);\n\n/**\n * Keyword contradiction check against governance entries.\n * Extracts terms 4+ chars, searches architecture and business-rules,\n * checks for 'governs' relations, returns formatted warnings.\n * Non-blocking — never throws, never prevents the operation.\n */\nexport interface ContradictionWarning {\n entryId: string;\n name: string;\n collection: string;\n governsCount: number;\n}\n\nexport async function runContradictionCheck(\n name: string,\n description: string,\n): Promise<ContradictionWarning[]> {\n const warnings: ContradictionWarning[] = [];\n try {\n const text = `${name} ${description}`.toLowerCase();\n const keyTerms = text\n .split(/\\s+/)\n .filter((w) => w.length >= 4 && !STOP_WORDS.has(w))\n .slice(0, 8);\n\n if (keyTerms.length === 0) return warnings;\n\n const searchQuery = keyTerms.slice(0, 3).join(\" \");\n const [govResults, archResults] = await Promise.all([\n mcpQuery<any[]>(\"chain.searchEntries\", { query: searchQuery, collectionSlug: \"business-rules\" }),\n mcpQuery<any[]>(\"chain.searchEntries\", { query: searchQuery, collectionSlug: \"architecture\" }),\n ]);\n\n const allGov = [...(govResults ?? []), ...(archResults ?? [])].slice(0, 5);\n\n for (const entry of allGov) {\n const entryText = `${entry.name} ${entry.data?.description ?? \"\"}`.toLowerCase();\n const matched = keyTerms.filter((t) => entryText.includes(t));\n if (matched.length < 2) continue;\n\n // Check for 'governs' relations\n let governsCount = 0;\n try {\n const relations = await mcpQuery<any[]>(\"chain.listEntryRelations\", {\n entryId: entry.entryId,\n });\n governsCount = (relations ?? []).filter((r: any) => r.type === \"governs\").length;\n } catch { /* non-critical */ }\n\n warnings.push({\n entryId: entry.entryId ?? \"\",\n name: entry.name,\n collection: entry.collectionSlug ?? \"\",\n governsCount,\n });\n }\n } catch {\n // Contradiction check is advisory — never blocks the operation\n }\n return warnings;\n}\n","/**\n * MCP client — communicates with the Convex HTTP Action gateway.\n *\n * Configuration:\n * PRODUCTBRAIN_API_KEY — pb_sk_* key (workspace + user + scope derived server-side)\n * CONVEX_SITE_URL — (optional) Convex deployment URL, defaults to cloud\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 cachedWorkspaceSlug: string | null = null;\nlet cachedWorkspaceName: string | null = null;\nlet cachedWorkspaceCreatedAt: number | null = null;\n\n// ─── Agent Session State ──────────────────────────────────────────────────\n\nlet cachedAgentSessionId: string | null = null;\nlet cachedApiKeyId: string | null = null;\nlet cachedApiKeyScope: \"read\" | \"readwrite\" = \"readwrite\";\nlet sessionOriented = false;\nlet sessionClosed = false;\n\nexport function getAgentSessionId(): string | null {\n return cachedAgentSessionId;\n}\n\nexport function isSessionOriented(): boolean {\n return sessionOriented;\n}\n\nexport function setSessionOriented(value: boolean): void {\n sessionOriented = value;\n}\n\nexport function getApiKeyScope(): \"read\" | \"readwrite\" {\n return cachedApiKeyScope;\n}\n\nexport function isSessionClosed(): boolean {\n return sessionClosed;\n}\n\nexport interface AgentSessionStartResult {\n sessionId: string;\n initiatedBy: string;\n toolsScope: \"read\" | \"readwrite\";\n workspaceName: string;\n superseded: {\n previousSessionId: string;\n startedAt: string;\n initiatedBy: string;\n } | null;\n}\n\n/**\n * Start an agent session. Creates a session record in Convex.\n * toolsScope is derived server-side from the API key — not passed as a parameter.\n * If an active session exists, it gets superseded.\n */\nexport async function startAgentSession(): Promise<AgentSessionStartResult> {\n const workspaceId = await getWorkspaceId();\n if (!cachedApiKeyId) {\n throw new Error(\"Cannot start session: API key ID not resolved. Ensure workspace resolution completed.\");\n }\n\n const result = await mcpCall<AgentSessionStartResult>(\"agent.startSession\", {\n workspaceId,\n apiKeyId: cachedApiKeyId,\n });\n\n cachedAgentSessionId = result.sessionId;\n cachedApiKeyScope = result.toolsScope;\n sessionOriented = false;\n sessionClosed = false;\n\n return result;\n}\n\n/**\n * Close the current agent session. After this, write tools are blocked\n * even if the MCP connection stays open.\n */\nexport async function closeAgentSession(): Promise<void> {\n if (!cachedAgentSessionId) return;\n try {\n await mcpCall(\"agent.closeSession\", {\n sessionId: cachedAgentSessionId,\n status: \"closed\",\n });\n } finally {\n sessionClosed = true;\n cachedAgentSessionId = null;\n sessionOriented = false;\n }\n}\n\n/**\n * Mark current session as orphaned (used on disconnect/crash).\n */\nexport async function orphanAgentSession(): Promise<void> {\n if (!cachedAgentSessionId) return;\n try {\n await mcpCall(\"agent.closeSession\", {\n sessionId: cachedAgentSessionId,\n status: \"orphaned\",\n });\n } catch {\n // Best-effort on disconnect\n } finally {\n cachedAgentSessionId = null;\n sessionOriented = false;\n }\n}\n\n/**\n * Touch the session to update lastToolCallAt. Fire-and-forget.\n */\nexport function touchSessionActivity(): void {\n if (!cachedAgentSessionId) return;\n mcpCall(\"agent.touchSession\", {\n sessionId: cachedAgentSessionId,\n }).catch(() => {\n // Non-critical\n });\n}\n\n/**\n * Record structured activity on the current session.\n */\nexport async function recordSessionActivity(activity: {\n entryCreated?: string;\n entryModified?: string;\n relationCreated?: boolean;\n gateFailure?: boolean;\n contradictionWarning?: boolean;\n}): Promise<void> {\n if (!cachedAgentSessionId) return;\n try {\n await mcpCall(\"agent.recordActivity\", {\n sessionId: cachedAgentSessionId,\n ...activity,\n });\n } catch {\n // Non-critical — don't fail the tool call over activity tracking\n }\n}\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 the client: validate PRODUCTBRAIN_API_KEY and default CONVEX_SITE_URL.\n * Must be called once before any mcpCall.\n */\nexport function bootstrap(): void {\n const pbKey = process.env.PRODUCTBRAIN_API_KEY;\n if (!pbKey?.startsWith(\"pb_sk_\")) {\n throw new Error(\n \"PRODUCTBRAIN_API_KEY is required and must start with 'pb_sk_'. \" +\n \"Generate one at Settings > API Keys in the Product OS UI.\"\n );\n }\n process.env.CONVEX_SITE_URL ??= process.env.PRODUCTBRAIN_URL ?? DEFAULT_CLOUD_URL;\n}\n\n/** @deprecated Use bootstrap() instead. Alias kept for callers in transition. */\nexport const bootstrapCloudMode = bootstrap;\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\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\nexport function getAuditLog(): readonly AuditEntry[] {\n return auditBuffer;\n}\n\n/**\n * Low-level call to the HTTP Action gateway.\n * Workspace scoping is enforced server-side from the API key — callers\n * don't need to (and can't) override the workspace.\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(\"PRODUCTBRAIN_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\n // Touch session activity on every successful call\n if (cachedAgentSessionId && fn !== \"agent.touchSession\" && fn !== \"agent.startSession\") {\n touchSessionActivity();\n }\n\n return json.data as T;\n}\n\nlet resolveInFlight: Promise<string> | null = null;\n\nexport async function getWorkspaceId(): Promise<string> {\n if (cachedWorkspaceId) return cachedWorkspaceId;\n\n if (resolveInFlight) return resolveInFlight;\n\n resolveInFlight = resolveWorkspaceWithRetry().finally(() => {\n resolveInFlight = null;\n });\n return resolveInFlight;\n}\n\nasync function resolveWorkspaceWithRetry(maxRetries = 2): Promise<string> {\n let lastError: Error | null = null;\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n try {\n const workspace = await mcpCall<{\n _id: string;\n name: string;\n slug: string;\n createdAt?: number;\n keyScope?: string;\n keyId?: string;\n } | null>(\"resolveWorkspace\", {});\n\n if (!workspace) {\n throw new Error(\n \"API key is valid but no workspace is associated. \" +\n \"Run `npx productbrain setup` or regenerate your key.\"\n );\n }\n\n cachedWorkspaceId = workspace._id;\n cachedWorkspaceSlug = workspace.slug;\n cachedWorkspaceName = workspace.name;\n cachedWorkspaceCreatedAt = workspace.createdAt ?? null;\n if (workspace.keyScope) cachedApiKeyScope = workspace.keyScope as \"read\" | \"readwrite\";\n if (workspace.keyId) cachedApiKeyId = workspace.keyId;\n return cachedWorkspaceId;\n } catch (err: any) {\n lastError = err;\n const isTransient = /network error|fetch failed|ECONNREFUSED|ETIMEDOUT/i.test(err.message);\n if (!isTransient || attempt === maxRetries) break;\n const delay = 1000 * (attempt + 1);\n process.stderr.write(\n `[MCP] Workspace resolution failed (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${delay}ms...\\n`\n );\n await new Promise((r) => setTimeout(r, delay));\n }\n }\n\n throw lastError!;\n}\n\nexport interface WorkspaceContext {\n workspaceId: string;\n workspaceSlug: string;\n workspaceName: string;\n createdAt: number | null;\n}\n\nexport async function getWorkspaceContext(): Promise<WorkspaceContext> {\n const workspaceId = await getWorkspaceId();\n return {\n workspaceId,\n workspaceSlug: cachedWorkspaceSlug ?? \"unknown\",\n workspaceName: cachedWorkspaceName ?? \"unknown\",\n createdAt: cachedWorkspaceCreatedAt,\n };\n}\n\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\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\n/**\n * Gate check: throws if the agent is not allowed to write.\n *\n * Enforces:\n * 1. Session must exist (always required — no REQUIRE_AGENT_SESSION flag)\n * 2. Session must not be closed\n * 3. Session must be oriented\n * 4. Key scope must be readwrite\n */\nexport function requireWriteAccess(): void {\n if (!cachedAgentSessionId) {\n throw new Error(\n \"Agent session required for write operations. Call `agent-start` first.\"\n );\n }\n\n if (sessionClosed) {\n throw new Error(\n \"Agent session has been closed. Write tools are no longer available.\"\n );\n }\n\n if (!sessionOriented) {\n throw new Error(\n \"Orientation required before writing to the Chain. Call 'orient' first.\"\n );\n }\n\n if (cachedApiKeyScope === \"read\") {\n throw new Error(\n \"This API key has read-only scope. Write tools are not available.\"\n );\n }\n}\n\n/**\n * Recover session orientation state from Convex on restart.\n * If the session is active and oriented in Convex, restore local state.\n */\nexport async function recoverSessionState(): Promise<void> {\n if (!cachedWorkspaceId) return;\n try {\n const session = await mcpCall<{\n _id: string;\n status: string;\n oriented: boolean;\n toolsScope: string;\n } | null>(\"agent.getActiveSession\", { workspaceId: cachedWorkspaceId });\n\n if (session && session.status === \"active\") {\n cachedAgentSessionId = session._id;\n sessionOriented = session.oriented;\n cachedApiKeyScope = session.toolsScope as \"read\" | \"readwrite\";\n sessionClosed = false;\n }\n } catch {\n // Recovery is best-effort\n }\n}\n"],"mappings":";;;;;AAOA,SAAS,SAAS;;;ACGlB,IAAM,oBAAoB;AAE1B,IAAI,oBAAmC;AACvC,IAAI,sBAAqC;AACzC,IAAI,sBAAqC;AACzC,IAAI,2BAA0C;AAI9C,IAAI,uBAAsC;AAC1C,IAAI,iBAAgC;AACpC,IAAI,oBAA0C;AAC9C,IAAI,kBAAkB;AACtB,IAAI,gBAAgB;AAEb,SAAS,oBAAmC;AACjD,SAAO;AACT;AAEO,SAAS,oBAA6B;AAC3C,SAAO;AACT;AAEO,SAAS,mBAAmB,OAAsB;AACvD,oBAAkB;AACpB;AA2BA,eAAsB,oBAAsD;AAC1E,QAAM,cAAc,MAAM,eAAe;AACzC,MAAI,CAAC,gBAAgB;AACnB,UAAM,IAAI,MAAM,uFAAuF;AAAA,EACzG;AAEA,QAAM,SAAS,MAAM,QAAiC,sBAAsB;AAAA,IAC1E;AAAA,IACA,UAAU;AAAA,EACZ,CA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