@theronap/cortex-mcp 0.9.45 → 0.9.46

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.
@@ -50,6 +50,7 @@ if (cmd === '--help' || cmd === '-h' || cmd === 'help') {
50
50
  ` skills install/repair the managed Cortex skills — bundled + org-published (also wired by setup)\n` +
51
51
  ` skills push <file> publish a SKILL.md to your org (owner/manager/admin)\n` +
52
52
  ` docs-scan detect new/changed local docs pending Cortex authoring (used by /cortex-author-docs)\n` +
53
+ ` graphify-sync [path] rebuild the local code graph (graphify) + log an evidence-tier timeline event\n` +
53
54
  ` snapshot-context save the exact startup context Cortex served to a local snapshot\n` +
54
55
  ` capture Stop-hook capturer (invoked by Claude Code)\n` +
55
56
  ` ingest-folder <path> ingest a local markdown folder as your authored records\n` +
@@ -128,6 +129,13 @@ if (cmd === 'setup') {
128
129
  process.exitCode = await runGrep(rest)
129
130
  const { closeFetch } = await import('../lib/diagnose.mjs')
130
131
  await closeFetch()
132
+ } else if (cmd === 'graphify-sync') {
133
+ // Local producer: `graphify update` + log an evidence-tier timeline event. Run from a cron/
134
+ // launchd job per repo, not a git hook (shared-checkout hazard — see reference memory).
135
+ const { runGraphifySync } = await import('../lib/graphify_sync.mjs')
136
+ process.exitCode = await runGraphifySync(rest)
137
+ const { closeFetch } = await import('../lib/diagnose.mjs')
138
+ await closeFetch()
131
139
  } else if (cmd === 'snapshot-context') {
132
140
  const { runSnapshotContext } = await import('../lib/context_log.mjs')
133
141
  process.exitCode = await runSnapshotContext()
@@ -0,0 +1,59 @@
1
+ import { spawnSync } from 'child_process'
2
+ import { existsSync } from 'fs'
3
+ import { join } from 'path'
4
+
5
+ // Thin local wrapper around the `graphify` CLI's read-only query subcommands. Deliberately NOT a
6
+ // fetchCortex client like grep/read_page: this is LOCAL-MACHINE data (a tree-sitter AST graph of
7
+ // whatever repo the session's cwd happens to be in), not org-shared Cortex content, and it never
8
+ // becomes the wiki graph — see cortex-wiki-primary-spec (structural/extracted data is evidence,
9
+ // never auto-promoted into authored pages). No LLM, no network call; graphify already built the
10
+ // graph on disk, this just queries it.
11
+
12
+ function graphPath(cwd) {
13
+ return join(cwd, 'graphify-out', 'graph.json')
14
+ }
15
+
16
+ function hasGraphifyBinary() {
17
+ const r = spawnSync('graphify', ['--version'], { encoding: 'utf8', timeout: 10_000 })
18
+ return !r.error && r.status === 0
19
+ }
20
+
21
+ // Pure-ish: build the argv for a given action, or return an error string if params are missing.
22
+ export function buildArgs({ action, question, from, to, node }) {
23
+ if (action === 'path') {
24
+ if (!from || !to) return { error: 'action:"path" requires both "from" and "to".' }
25
+ return { args: ['path', from, to] }
26
+ }
27
+ if (action === 'explain') {
28
+ if (!node) return { error: 'action:"explain" requires "node".' }
29
+ return { args: ['explain', node] }
30
+ }
31
+ if (!question) return { error: 'action:"query" requires "question".' }
32
+ return { args: ['query', question] }
33
+ }
34
+
35
+ // Effectful: run one of graphify's query/path/explain subcommands against the graph already built
36
+ // for `cwd`. Returns { ok, text } — never throws, always something readable to hand back to the model.
37
+ export function runCodeGraphQuery({ action, question, from, to, node }, cwd = process.cwd()) {
38
+ if (!existsSync(graphPath(cwd))) {
39
+ return {
40
+ ok: false,
41
+ text: `No code graph found at ${graphPath(cwd)}. Run the graphify skill (\`/graphify .\`) in this repo first to build one.`,
42
+ }
43
+ }
44
+ if (!hasGraphifyBinary()) {
45
+ return {
46
+ ok: false,
47
+ text: 'graphify CLI not found on PATH. Install it with `uv tool install graphifyy` (or `pipx install graphifyy`), then run the graphify skill to build a graph.',
48
+ }
49
+ }
50
+ const built = buildArgs({ action, question, from, to, node })
51
+ if (built.error) return { ok: false, text: built.error }
52
+
53
+ const r = spawnSync('graphify', built.args, { cwd, encoding: 'utf8', timeout: 60_000, maxBuffer: 4 * 1024 * 1024 })
54
+ if (r.error) return { ok: false, text: `graphify failed to run: ${r.error.message}` }
55
+ const out = (r.stdout || '').trim()
56
+ const err = (r.stderr || '').trim()
57
+ if (r.status !== 0) return { ok: false, text: err || out || `graphify exited with status ${r.status}` }
58
+ return { ok: true, text: out || '(no results)' }
59
+ }
@@ -0,0 +1,89 @@
1
+ import { spawnSync } from 'child_process'
2
+ import { existsSync, readFileSync } from 'fs'
3
+ import { join } from 'path'
4
+ import { fetchCortex, classify, resolveBase } from './diagnose.mjs'
5
+
6
+ // `cortex-mcp graphify-sync [path]` — local producer: incrementally rebuild a repo's structural
7
+ // code graph (graphify — tree-sitter AST, no LLM) and log an evidence-tier timeline event via
8
+ // POST /api/timeline/graphify. LLM-free, idempotent (server dedupes on repo+commit), additive —
9
+ // never touches the wiki graph (cortex-wiki-primary-spec: extracted/structural data is evidence,
10
+ // never auto-promoted into an authored page).
11
+ //
12
+ // Meant to run from a periodic cron/launchd job per repo you want graphed — NOT a git post-commit
13
+ // hook. A hook fires synchronously inside `git commit`/`git push` and can race another session's
14
+ // git operations in a shared checkout (see reference-cortex-shared-checkout-hazards); a timer-based
15
+ // job just reads whatever the tree looks like at that moment, no hook into git operations at all.
16
+
17
+ function run(cmd, args, cwd) {
18
+ const r = spawnSync(cmd, args, { cwd, encoding: 'utf8', timeout: 120_000 })
19
+ return r.status === 0 ? (r.stdout || '').trim() : null
20
+ }
21
+
22
+ function repoFullNameFromRemote(cwd) {
23
+ const url = run('git', ['remote', 'get-url', 'origin'], cwd)
24
+ if (!url) return null
25
+ const m = /github\.com[:/]([^/]+)\/([^/.]+?)(?:\.git)?$/i.exec(url)
26
+ return m ? `${m[1]}/${m[2]}` : null
27
+ }
28
+
29
+ export async function runGraphifySync(argv = []) {
30
+ const cwd = argv[0] && !argv[0].startsWith('-') ? argv[0] : process.cwd()
31
+ const TOKEN = process.env.CORTEX_TOKEN
32
+ const BASE = resolveBase(process.env.CORTEX_URL)
33
+ if (!TOKEN) {
34
+ process.stderr.write('cortex-mcp graphify-sync: CORTEX_TOKEN is required.\n')
35
+ return 1
36
+ }
37
+ const graphFile = join(cwd, 'graphify-out', 'graph.json')
38
+ if (!existsSync(graphFile)) {
39
+ process.stderr.write(`cortex-mcp graphify-sync: no graph at ${graphFile} — run the graphify skill (\`/graphify .\`) in this repo first.\n`)
40
+ return 1
41
+ }
42
+ if (!run('graphify', ['--version'], cwd)) {
43
+ process.stderr.write('cortex-mcp graphify-sync: graphify CLI not on PATH (`uv tool install graphifyy`).\n')
44
+ return 1
45
+ }
46
+
47
+ const update = spawnSync('graphify', ['update', '.'], { cwd, encoding: 'utf8', timeout: 300_000, maxBuffer: 8 * 1024 * 1024 })
48
+ if (update.status !== 0) {
49
+ process.stderr.write(`cortex-mcp graphify-sync: \`graphify update\` failed:\n${(update.stderr || update.stdout || '').trim()}\n`)
50
+ return 1
51
+ }
52
+ if (update.stdout) process.stdout.write(update.stdout.trim() + '\n')
53
+
54
+ let graph
55
+ try {
56
+ graph = JSON.parse(readFileSync(graphFile, 'utf8'))
57
+ } catch (e) {
58
+ process.stderr.write(`cortex-mcp graphify-sync: could not read ${graphFile}: ${e.message}\n`)
59
+ return 1
60
+ }
61
+ const nodes = Array.isArray(graph.nodes) ? graph.nodes : []
62
+ const nodeCount = nodes.length
63
+ const edgeCount = Array.isArray(graph.links) ? graph.links.length : 0
64
+ const communityCount = new Set(nodes.map((n) => n.community).filter((c) => c !== undefined)).size
65
+ const commitSha = typeof graph.built_at_commit === 'string' ? graph.built_at_commit : null
66
+
67
+ const repo = repoFullNameFromRemote(cwd)
68
+ if (!commitSha || !repo) {
69
+ process.stdout.write('cortex-mcp graphify-sync: graph updated locally; skipping timeline log (no git commit / no github.com origin found).\n')
70
+ return 0
71
+ }
72
+
73
+ const res = await fetchCortex(`${BASE}/api/timeline/graphify`, {
74
+ method: 'POST',
75
+ headers: { Authorization: `Bearer ${TOKEN}`, 'Content-Type': 'application/json' },
76
+ body: JSON.stringify({ repo, commitSha, nodeCount, edgeCount, communityCount }),
77
+ })
78
+ if (!res.ok) {
79
+ const body = await res.text()
80
+ process.stderr.write(classify(res.status, res.headers.get('content-type'), body, res.headers.get('x-vercel-id')).message + '\n')
81
+ return 1
82
+ }
83
+ const payload = await res.json()
84
+ process.stdout.write(
85
+ `Logged to Cortex timeline: ${repo}@${commitSha.slice(0, 7)} (${nodeCount} nodes, ${edgeCount} edges, ${communityCount} communities)` +
86
+ `${payload.inserted ? '' : ' (already logged)'}\n`,
87
+ )
88
+ return 0
89
+ }
package/lib/server.mjs CHANGED
@@ -8,6 +8,7 @@ import { createHash, randomUUID } from 'crypto'
8
8
  import { fetchCortex, classify, resolveBase } from './diagnose.mjs'
9
9
  import { runSendImessage } from './imessage_send.mjs'
10
10
  import { formatGrepHits } from './grep_cli.mjs'
11
+ import { runCodeGraphQuery } from './code_graph_cli.mjs'
11
12
 
12
13
  // The Cortex MCP server (stdio). Serves the signed-in employee's scoped org
13
14
  // context to their AI assistant. CORTEX_TOKEN identifies the user + org.
@@ -284,6 +285,26 @@ export async function runServer(version) {
284
285
  },
285
286
  )
286
287
 
288
+ server.registerTool(
289
+ 'code_graph_query',
290
+ {
291
+ title: 'Query the local code structure graph (graphify)',
292
+ description:
293
+ 'Query a structural code graph for the repo at the CURRENT working directory, built locally by graphify (tree-sitter AST — deterministic, no LLM, no server round-trip; this is LOCAL MACHINE data, not org-shared Cortex content, and reflects a snapshot of one commit, not live files). Use for MULTI-HOP questions a single grep cannot answer: what calls/imports/depends on X, how A structurally reaches B, or a repo-wide overview (hub/community files). Do NOT use for single-hop lookups (does file X import Y) — grep is faster and always current. Structure only — it knows what imports/calls what, never WHY; read the actual files or authored Cortex pages for intent.',
294
+ inputSchema: {
295
+ action: z.enum(['query', 'path', 'explain']).describe('"query" = open-ended natural-language question (graph traversal); "path" = shortest structural path between two named nodes; "explain" = describe one node and list its direct connections'),
296
+ question: z.string().optional().describe('required for action:"query" — the natural-language question'),
297
+ from: z.string().optional().describe('required for action:"path" — the starting node name'),
298
+ to: z.string().optional().describe('required for action:"path" — the target node name'),
299
+ node: z.string().optional().describe('required for action:"explain" — the node name to describe'),
300
+ },
301
+ },
302
+ async ({ action, question, from, to, node }) => {
303
+ const result = runCodeGraphQuery({ action, question, from, to, node })
304
+ return { content: [{ type: 'text', text: result.text }] }
305
+ },
306
+ )
307
+
287
308
  server.registerTool(
288
309
  'project_status',
289
310
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theronap/cortex-mcp",
3
- "version": "0.9.45",
3
+ "version": "0.9.46",
4
4
  "description": "Connect your AI assistant to Cortex — your org's projects, activity, gaps, and directives, scoped to you.",
5
5
  "type": "module",
6
6
  "bin": {