@heuresis/mcp 1.0.0-rc.1 → 1.0.0-rc.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,152 +1,159 @@
1
- # @heuresis/mcp
2
-
3
- A Model Context Protocol (MCP) server that turns the user's Heuresis
4
- workspace into a thinking substrate any MCP-capable agent (Claude
5
- Desktop, Claude Code, Cursor, Windsurf, custom agents) can read and
6
- write the webapp and the MCP become two front-ends to one cloud
7
- workspace. The MCP logs into the user's Heuresis account, talks to the
8
- same Supabase project the webapp talks to, and respects the same RLS.
9
-
10
- ## Install
11
-
12
- ```bash
13
- npm install -g @heuresis/mcp
14
- # or run on demand without installing:
15
- npx -y @heuresis/mcp@latest
16
- ```
17
-
18
- > Not yet published. While in alpha you can run it locally:
19
- >
20
- > ```bash
21
- > cd mcp-server
22
- > npm install
23
- > npm run build
24
- > node dist/index.js --help
25
- > ```
26
-
27
- ## Quickstart
28
-
29
- ### 1. Link this machine to your Heuresis account
30
-
31
- ```bash
32
- npx @heuresis/mcp login
33
- ```
34
-
35
- The CLI prints a short device code (`XXXX-XXXX`) and a URL. Open the URL in your browser, sign into Heuresis if you aren't already, paste the code, and confirm the device. The CLI polls in the background and writes your credentials to `~/.heuresis/credentials.json` (chmod 600 on POSIX) the moment you confirm. Subsequent runs of the MCP are silent.
36
-
37
- To unlink a machine, run `npx @heuresis/mcp logout` locally, or open Settings ▸ Connected devices in the webapp to revoke remotely.
38
-
39
- `npx @heuresis/mcp whoami` confirms which account a machine is currently linked to.
40
-
41
- ### 2. Point your MCP client at it
42
-
43
- **Claude Desktop** edit `~/Library/Application Support/Claude/claude_desktop_config.json`
44
- on macOS, or `%APPDATA%/Claude/claude_desktop_config.json` on Windows:
45
-
46
- ```json
47
- {
48
- "mcpServers": {
49
- "heuresis": { "command": "npx", "args": ["-y", "@heuresis/mcp"] }
50
- }
51
- }
52
- ```
53
-
54
- **Claude Code / Cursor / Windsurf** — drop a `.mcp.json` in the
55
- workspace root:
56
-
57
- ```json
58
- {
59
- "mcpServers": {
60
- "heuresis": { "command": "npx", "args": ["-y", "@heuresis/mcp"] }
61
- }
62
- }
63
- ```
64
-
65
- Restart the client. The Heuresis tools appear in the tool menu.
66
-
67
- ### 3. Optional CLI subcommands
68
-
69
- ```bash
70
- npx @heuresis/mcp whoami # show the linked account + device
71
- npx @heuresis/mcp logout # delete the credentials file
72
- npx @heuresis/mcp --help # all options
73
- npx @heuresis/mcp --no-realtime # boot once with live sync turned off (persisted)
74
- npx @heuresis/mcp --realtime # re-enable live sync
75
- ```
76
-
77
- ## Live sync
78
-
79
- When the MCP boots in cloud mode it subscribes to your workspace over
80
- Supabase Realtime and notifies the client whenever a `nodes`, `edges`,
81
- `projects`, or `ideas` row changes. Edits you make in the webapp show
82
- up in your agent's view without a manual refresh, and writes from one
83
- MCP-connected client reach any other connected client the same way.
84
- Pass `--no-realtime` to disable the subscription (useful if the chatter
85
- is noisy or your client logs every notification). The preference is
86
- saved to `~/.heuresis/config.json` so you only have to pass the flag
87
- once.
88
-
89
- ## Tools
90
-
91
- This is the **Phase 19.1** tool surface. The remaining tools from
92
- `docs/mcp-cloud.md` §4 ship in rolling waves under Phase 19.4.
93
-
94
- ### Reads
95
-
96
- | Tool | Input |
97
- | ------------------- | --------------------------------------------------------------------------------------------------------------------------- |
98
- | `list_projects` | `{}` every project in the workspace with brief, direction, lifecycle, and node count. |
99
- | `get_project_graph` | `{ projectId, includeArchived?, detail? }` — every node + edge inside one project. |
100
- | `get_subtree` | `{ rootId, depth?, detail? }` a node and its descendants up to `depth` generations. |
101
- | `get_concept` | `{ id, includeAncestry?, includeChildren?, includeIdeaMemberships? }` one concept with full detail. |
102
- | `search_concepts` | `{ query, limit?, projectId?, status?, detail? }` — substring search across label / description / partition / tags. Text-only; semantic search lands in 19.4. |
103
-
104
- ### Writes
105
-
106
- | Tool | Input |
107
- | ---------------- | -------------------------------------------------------------------------------------------------------------------- |
108
- | `add_concept` | `{ label, description?, parentId?, projectId?, tags? }` — create a node; partition edge auto-created if parented. |
109
- | `update_concept` | `{ id, label?, description?, tags?, partitionAttribute?, rationale?, status? }` — patch a node. |
110
- | `link_concepts` | `{ fromId, toId, kind }` where `kind ∈ { 'k-ref', 'derived-from', 'semantic-adjacency' }` — create a non-partition edge. |
111
-
112
- Each tool's input shape mirrors its counterpart in the webapp's
113
- `src/agent/tools.ts`, so an agent that uses both surfaces sees a
114
- uniform contract.
115
-
116
- ## Status
117
-
118
- **Cloud-authenticated alpha (v0.2.0-alpha, Phase 19.1).**
119
-
120
- - The 8 tools above hit Supabase live, against the same workspace your
121
- webapp sees, under the same RLS.
122
- - Full tool parity (the other 19 tools — operators, k-ref bulk ops,
123
- validation, standing, ideas, projects-as-targets) ships in waves
124
- under Phase 19.4 and is **not** included in this build.
125
- - The `mcp-device-poll` / `mcp-device-grant` Edge Functions that make
126
- `login` automatic ship in Phase 19.3. Until then the `login`
127
- subcommand uses the manual paste workaround described above.
128
- - The Settings Account Connected devices UI (where you'd revoke
129
- this device) ships in Phase 19.6. Until then, revoke at the database
130
- level.
131
-
132
- ## Legacy snapshot mode (deprecated)
133
-
134
- The v0 read-only snapshot reader still works as a fallback while users
135
- migrate. With no `~/.heuresis/credentials.json` and the
136
- `HEURESIS_SNAPSHOT` env var set, the server reads a JSON export from
137
- disk and exposes the v0 read-only tool set (`get_workspace_summary`,
138
- `list_projects`, `search_concepts`, `get_concept`, `get_subtree`,
139
- `get_project_graph`, `list_recent_decisions`).
140
-
141
- ```bash
142
- export HEURESIS_SNAPSHOT="/absolute/path/to/your-export.json"
143
- npx @heuresis/mcp
144
- ```
145
-
146
- This path is **deprecated** and will be removed after Phase 19.7
147
- (cloud-auth mandatory). It's here so the current install path keeps
148
- working through the migration.
149
-
150
- ## License
151
-
152
- AGPL-3.0-or-later.
1
+ # @heuresis/mcp
2
+
3
+ A Model Context Protocol (MCP) server that exposes a Heuresis workspace
4
+ to any MCP-capable client (Claude Desktop, Claude Code, Cursor,
5
+ Windsurf, custom agents). The server logs into the user's Heuresis
6
+ account, talks to the same Supabase project the webapp talks to, and
7
+ respects the same RLS. Webapp and MCP are two front-ends to one cloud
8
+ workspace.
9
+
10
+ Current version: `1.0.0-rc.11`.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install -g @heuresis/mcp
16
+ # or on demand without installing:
17
+ npx -y @heuresis/mcp
18
+ ```
19
+
20
+ > **Package name vs. command name.** The npm package is `@heuresis/mcp`; the
21
+ > command it installs is `heuresis-mcp`. A bare `npx -y @heuresis/mcp` (no
22
+ > subcommand) starts the MCP server fine, but `npx @heuresis/mcp login` can
23
+ > fail with `heuresis-mcp: not found` because npx derives the command name
24
+ > from the scope-stripped package name (`mcp`), which doesn't match. To run a
25
+ > subcommand reliably on every npm/OS, name the binary explicitly with `-p`:
26
+ >
27
+ > ```bash
28
+ > npx -y -p @heuresis/mcp heuresis-mcp login
29
+ > ```
30
+
31
+ ## Quickstart
32
+
33
+ ### 1. Link this machine to your Heuresis account
34
+
35
+ ```bash
36
+ npx -y -p @heuresis/mcp heuresis-mcp login
37
+ ```
38
+
39
+ The CLI prints a device code and a one-click URL of the form
40
+ `https://heuresis.app/device?code=XXXX-XXXX`. Open it in your browser,
41
+ sign in if you aren't already, and confirm the device. The CLI polls
42
+ in the background and writes credentials to
43
+ `~/.heuresis/credentials.json` (chmod 600 on POSIX) the moment you
44
+ confirm. Subsequent runs of the MCP are silent.
45
+
46
+ The login flow rides three Supabase Edge Functions:
47
+ `mcp-device-init`, `mcp-device-grant`, and `mcp-device-poll`.
48
+
49
+ To unlink a machine: `npx -y -p @heuresis/mcp heuresis-mcp logout`, or open
50
+ Settings ▸ Connected devices in the webapp to revoke remotely.
51
+
52
+ `npx -y -p @heuresis/mcp heuresis-mcp whoami` confirms which account a machine
53
+ is currently linked to.
54
+
55
+ ### 2. Point your MCP client at it
56
+
57
+ **Claude Desktop.** Edit
58
+ `~/Library/Application Support/Claude/claude_desktop_config.json` on
59
+ macOS, or `%APPDATA%/Claude/claude_desktop_config.json` on Windows:
60
+
61
+ ```json
62
+ {
63
+ "mcpServers": {
64
+ "heuresis": { "command": "npx", "args": ["-y", "@heuresis/mcp"] }
65
+ }
66
+ }
67
+ ```
68
+
69
+ **Claude Code / Cursor / Windsurf.** Drop a `.mcp.json` in the
70
+ workspace root:
71
+
72
+ ```json
73
+ {
74
+ "mcpServers": {
75
+ "heuresis": { "command": "npx", "args": ["-y", "@heuresis/mcp"] }
76
+ }
77
+ }
78
+ ```
79
+
80
+ Restart the client. The Heuresis tools appear in the tool menu.
81
+
82
+ ### 3. CLI subcommands
83
+
84
+ ```bash
85
+ npx -y -p @heuresis/mcp heuresis-mcp whoami # show the linked account + device
86
+ npx -y -p @heuresis/mcp heuresis-mcp logout # delete the credentials file
87
+ npx -y -p @heuresis/mcp heuresis-mcp --help # all options
88
+ npx -y @heuresis/mcp --no-realtime # boot the server with live sync off (persisted)
89
+ npx -y @heuresis/mcp --realtime # re-enable live sync
90
+ ```
91
+
92
+ ## Live sync
93
+
94
+ When the MCP boots in cloud mode it subscribes to the workspace over
95
+ Supabase Realtime and notifies the client whenever a `nodes`, `edges`,
96
+ `projects`, or `ideas` row changes. Edits made in the webapp show up
97
+ in the agent's view without a manual refresh, and writes from one
98
+ MCP-connected client reach any other connected client the same way.
99
+ Pass `--no-realtime` to disable the subscription (useful if the
100
+ chatter is noisy or the client logs every notification). The
101
+ preference is saved to `~/.heuresis/config.json` so the flag only
102
+ needs to be passed once.
103
+
104
+ ## Tools
105
+
106
+ 34 tools total: 31 data tools against the cloud workspace, plus 3
107
+ operator tools that drive the same ideation operators the webapp uses.
108
+
109
+ **Reads (10).** `get_workspace_summary`, `list_projects`,
110
+ `get_project_graph`, `list_concepts`, `list_edges`, `get_subtree`,
111
+ `get_concept`, `search_concepts`, `find_concepts`,
112
+ `list_recent_decisions`. Most agent sessions start with
113
+ `get_workspace_summary` or `list_projects`.
114
+
115
+ **Writes (21).** Concepts: `add_concept`, `update_concept`,
116
+ `bulk_add_concepts`, `set_parent`, `validate_concept`, `set_standing`,
117
+ `archive_concept`, `unarchive_concept`, `star_concept`,
118
+ `remove_concept`. Edges: `link_concepts`, `add_kref`. Ideas:
119
+ `create_idea`, `rename_idea`, `recolor_idea`, `set_idea_members`,
120
+ `add_to_idea`, `delete_idea`. Projects: `create_project`,
121
+ `update_project`, `delete_project`. Every write stamps a row in
122
+ `public.provenance` with `origin='mcp'` so the webapp's session log
123
+ shows which surface made the change.
124
+
125
+ **Operator runs (3).** `run_operator` (generate candidates with
126
+ Branch / Matrix / ASIT / TRIZ / Combine / Free / Contradiction),
127
+ `run_operator_and_commit` (same, plus commit the result in one
128
+ round-trip), and `expand_concept` (recursive Branch, capped at depth ×
129
+ breadth ≤ 60).
130
+
131
+ Tool input shapes mirror their counterparts in the webapp's
132
+ `src/agent/tools.ts`, so an agent that uses both surfaces sees a
133
+ uniform contract.
134
+
135
+ Wave-shipping: `find_in_files` (in-browser embedding search) is in the
136
+ webapp but not yet on the MCP.
137
+
138
+ ## Legacy snapshot mode (deprecated)
139
+
140
+ The original read-only snapshot reader still works as a fallback while
141
+ users migrate to cloud auth. With no `~/.heuresis/credentials.json`
142
+ and the `HEURESIS_SNAPSHOT` env var set, the server reads a JSON
143
+ export from disk and exposes the original read-only tool set
144
+ (`get_workspace_summary`, `list_projects`, `search_concepts`,
145
+ `get_concept`, `get_subtree`, `get_project_graph`,
146
+ `list_recent_decisions`).
147
+
148
+ ```bash
149
+ export HEURESIS_SNAPSHOT="/absolute/path/to/your-export.json"
150
+ npx @heuresis/mcp
151
+ ```
152
+
153
+ This path is deprecated and will be removed in a later release. It is
154
+ here so existing setups keep working through the migration to cloud
155
+ auth.
156
+
157
+ ## License
158
+
159
+ AGPL-3.0-or-later.
package/dist/cli.js CHANGED
@@ -25,14 +25,15 @@
25
25
  // to attach the user's identity to the pending grant row.
26
26
  import { createInterface } from 'node:readline/promises';
27
27
  import { stdin as input, stdout as output } from 'node:process';
28
- import { createClient } from '@supabase/supabase-js';
29
28
  import { credentialsPath, defaultDeviceName, deleteCredentials, readCredentials, writeCredentials, } from './credentials.js';
29
+ import { exchangeRefreshToken, RefreshTokenError } from './gotrue.js';
30
+ import { ensureProxyAgent } from './proxy.js';
30
31
  // Where the device pairing UI lives. Production default; can be overridden
31
32
  // for staging / self-hosted deploys via HEURESIS_DEVICE_BASE_URL. We also
32
33
  // allow HEURESIS_SUPABASE_URL to override which Supabase project the CLI
33
34
  // talks to (e.g. a staging instance). Both default to production.
34
35
  const DEFAULT_DEVICE_BASE_URL = 'https://heuresis.app';
35
- const DEFAULT_SUPABASE_URL = 'https://heuresis.supabase.co';
36
+ const DEFAULT_SUPABASE_URL = 'https://wpgniquyuppljeqkedqh.supabase.co';
36
37
  const POLL_INTERVAL_MS = 5_000;
37
38
  const POLL_TIMEOUT_MS = 15 * 60 * 1_000;
38
39
  function log(...args) {
@@ -45,12 +46,12 @@ function printHelp() {
45
46
  'heuresis-mcp — Heuresis MCP server (cloud-authenticated, alpha)',
46
47
  '',
47
48
  'Usage:',
48
- ' npx @heuresis/mcp Start the MCP stdio server (run by Claude Desktop, Cursor, etc.)',
49
- ' npx @heuresis/mcp login Link this machine to your Heuresis account',
50
- ' --device-name <name> Override the default device name (hostname-shortRand).',
51
- ' npx @heuresis/mcp logout Remove the saved credentials',
52
- ' npx @heuresis/mcp whoami Show the linked account',
53
- ' npx @heuresis/mcp --help Show this message',
49
+ ' npx -y @heuresis/mcp Start the MCP stdio server (run by Claude Desktop, Cursor, etc.)',
50
+ ' npx -y -p @heuresis/mcp heuresis-mcp login Link this machine to your Heuresis account',
51
+ ' --device-name <name> Override the default device name (hostname-shortRand).',
52
+ ' npx -y -p @heuresis/mcp heuresis-mcp logout Remove the saved credentials',
53
+ ' npx -y -p @heuresis/mcp heuresis-mcp whoami Show the linked account',
54
+ ' npx -y -p @heuresis/mcp heuresis-mcp --help Show this message',
54
55
  '',
55
56
  'Credentials are stored at:',
56
57
  ` ${credentialsPath()}`,
@@ -99,6 +100,7 @@ function sleep(ms) {
99
100
  return new Promise((resolve) => setTimeout(resolve, ms));
100
101
  }
101
102
  async function postJson(url, body) {
103
+ await ensureProxyAgent(log);
102
104
  const res = await fetch(url, {
103
105
  method: 'POST',
104
106
  headers: { 'Content-Type': 'application/json' },
@@ -147,16 +149,15 @@ export async function loginCommand(argv = []) {
147
149
  log('Pairing init returned no code. Aborting.');
148
150
  process.exit(1);
149
151
  }
150
- // 2. Tell the user where to go.
152
+ // 2. Tell the user where to go. The URL has the code baked in as a query
153
+ // param so the device page can pre-fill it; the user just clicks Confirm.
154
+ const confirmUrl = `${deviceBaseUrl}/device?code=${encodeURIComponent(init.code)}`;
151
155
  log('');
152
- log(`1. Open this page in your browser:`);
153
- log(` ${deviceBaseUrl}/device`);
156
+ log(`Open this URL to link this machine to your Heuresis account:`);
154
157
  log('');
155
- log(`2. Enter this code (case-insensitive):`);
156
- log(` ${init.code}`);
158
+ log(` ${confirmUrl}`);
157
159
  log('');
158
- log(`3. This window will finish on its own once you confirm. The code`);
159
- log(` expires in 15 minutes.`);
160
+ log(`(Code: ${init.code}, in case the page needs it manually. Expires in 15 min.)`);
160
161
  log('');
161
162
  log(`Waiting for confirmation…`);
162
163
  // 3. Poll until ok / 410 / timeout.
@@ -176,7 +177,7 @@ export async function loginCommand(argv = []) {
176
177
  }
177
178
  if (pollRes.status === 410) {
178
179
  log('');
179
- log('That code expired or was already used. Run `npx @heuresis/mcp login` again to start over.');
180
+ log('That code expired or was already used. Run `npx -y -p @heuresis/mcp heuresis-mcp login` again to start over.');
180
181
  process.exit(1);
181
182
  }
182
183
  if (pollRes.status === 202) {
@@ -198,36 +199,31 @@ export async function loginCommand(argv = []) {
198
199
  }
199
200
  if (!success) {
200
201
  log('');
201
- log('Timed out waiting for confirmation. Run `npx @heuresis/mcp login` again.');
202
+ log('Timed out waiting for confirmation. Run `npx -y -p @heuresis/mcp heuresis-mcp login` again.');
202
203
  process.exit(1);
203
204
  }
204
205
  // 4. Verify the refresh token works + fetch the user's email to print.
205
- const client = createClient(success.supabase_url, success.anon_key, {
206
- auth: {
207
- persistSession: false,
208
- autoRefreshToken: false,
209
- detectSessionInUrl: false,
210
- },
211
- });
206
+ // We exchange the refresh token straight against the GoTrue token endpoint
207
+ // rather than going through supabase-js's stored-session machinery (which
208
+ // throws "Auth session missing!" headlessly — see gotrue.ts for why).
212
209
  let email = '(no email on record)';
213
210
  try {
214
- const { data, error } = await client.auth.setSession({
215
- access_token: '',
216
- refresh_token: success.refresh_token,
217
- });
218
- if (error || !data.session || !data.user) {
219
- log('');
220
- log(`Pairing returned a token, but it failed to refresh: ${error?.message ?? 'no session'}`);
221
- log('Run `npx @heuresis/mcp login` again to retry.');
222
- process.exit(1);
223
- }
224
- email = data.user.email ?? email;
225
- // Use whatever supabase-js handed us back — it may have already rotated
226
- // the refresh token at the first refresh.
227
- success.refresh_token = data.session.refresh_token ?? success.refresh_token;
211
+ const session = await exchangeRefreshToken(success.supabase_url, success.anon_key, success.refresh_token);
212
+ email = session.user?.email ?? email;
213
+ // GoTrue rotates the refresh token on every exchange — persist the NEW
214
+ // one so the credentials we write are immediately usable. The token we
215
+ // got from the poll is now spent.
216
+ success.refresh_token = session.refresh_token;
228
217
  }
229
218
  catch (err) {
230
- log(`Verification of the new refresh token failed: ${err instanceof Error ? err.message : String(err)}`);
219
+ log('');
220
+ if (err instanceof RefreshTokenError) {
221
+ log(`Pairing returned a token, but it failed to refresh: ${err.message}`);
222
+ }
223
+ else {
224
+ log(`Verification of the new refresh token failed: ${err instanceof Error ? err.message : String(err)}`);
225
+ }
226
+ log('Run `npx -y -p @heuresis/mcp heuresis-mcp login` again to retry.');
231
227
  process.exit(1);
232
228
  }
233
229
  const creds = {
@@ -257,7 +253,7 @@ export async function logoutCommand() {
257
253
  export async function whoamiCommand() {
258
254
  const creds = await readCredentials();
259
255
  if (!creds) {
260
- log('Not linked. Run `npx @heuresis/mcp login` to pair this machine.');
256
+ log('Not linked. Run `npx -y -p @heuresis/mcp heuresis-mcp login` to pair this machine.');
261
257
  process.exit(1);
262
258
  }
263
259
  log(`Heuresis MCP — linked`);
@@ -4,18 +4,20 @@
4
4
  // session to localStorage (no such thing in Node) — instead we manage the
5
5
  // session manually:
6
6
  //
7
- // 1. At process start: read ~/.heuresis/credentials.json → call
8
- // `client.auth.setSession({ access_token: '', refresh_token })`.
9
- // supabase-js immediately refreshes the access token from the refresh
10
- // token and stores both in memory.
7
+ // 1. At process start: read ~/.heuresis/credentials.json → exchange the
8
+ // refresh token for a fresh session by hitting the GoTrue token endpoint
9
+ // directly (see gotrue.ts), then hand the resulting access_token +
10
+ // refresh_token to `client.auth.setSession(...)`. We deliberately do NOT
11
+ // use the old `setSession({ access_token: '', refresh_token })` trick:
12
+ // auth-js 2.x rejects an empty access_token with "Auth session missing!"
13
+ // before it ever refreshes. With a real access_token the guard passes
14
+ // and supabase-js stores both tokens in memory.
11
15
  // 2. supabase-js handles silent re-refresh in the background while the
12
16
  // process runs. We don't have to do anything per-tool-call.
13
- // 3. If the refresh fails (revoked, expired), tool calls 401 — the wrapper
14
- // catches that and surfaces a "re-run `npx @heuresis/mcp login`" message.
15
- //
16
- // Phase 19.3 will swap the refresh-token issuance to come from the
17
- // `mcp-device-poll` Edge Function, but the client-side shape doesn't change.
17
+ // 3. If the refresh fails (revoked, expired), the bootstrap throws — the
18
+ // wrapper surfaces a "re-run login" message.
18
19
  import { createClient } from '@supabase/supabase-js';
20
+ import { exchangeRefreshToken } from './gotrue.js';
19
21
  let cached = null;
20
22
  export class CloudAuthError extends Error {
21
23
  constructor(msg) {
@@ -41,14 +43,25 @@ export async function getCloudClient(creds) {
41
43
  detectSessionInUrl: false,
42
44
  },
43
45
  });
44
- // Bootstrap the session from the refresh token. setSession with an empty
45
- // access_token forces an immediate refresh against the refresh_token.
46
- const { data, error } = await client.auth.setSession({
47
- access_token: '',
48
- refresh_token: creds.refresh_token,
49
- });
50
- if (error || !data.session) {
51
- throw new CloudAuthError(`Failed to refresh Heuresis session: ${error?.message ?? 'no session returned'}. Run \`npx @heuresis/mcp login\` to re-authenticate.`);
46
+ // Bootstrap the session: exchange the stored refresh token for a fresh
47
+ // session directly against GoTrue, then seed supabase-js with the REAL
48
+ // tokens so every subsequent PostgREST call carries the user's JWT and
49
+ // auto-refresh keeps it alive.
50
+ try {
51
+ const session = await exchangeRefreshToken(creds.supabase_url, creds.anon_key, creds.refresh_token);
52
+ const { error } = await client.auth.setSession({
53
+ access_token: session.access_token,
54
+ refresh_token: session.refresh_token,
55
+ });
56
+ if (error) {
57
+ throw new CloudAuthError(`Failed to seed Heuresis session: ${error.message}. ` +
58
+ 'Run `npx -y -p @heuresis/mcp heuresis-mcp login` to re-authenticate.');
59
+ }
60
+ }
61
+ catch (err) {
62
+ if (err instanceof CloudAuthError)
63
+ throw err;
64
+ throw new CloudAuthError(`Failed to refresh Heuresis session: ${err instanceof Error ? err.message : String(err)}. Run \`npx -y -p @heuresis/mcp heuresis-mcp login\` to re-authenticate.`);
52
65
  }
53
66
  cached = { client, userId: creds.user_id };
54
67
  return cached;
@@ -37,6 +37,7 @@ import { COMBINE_OPERATOR } from './operators/combine.js';
37
37
  import { EXPLORE_OPERATOR } from './operators/explore.js';
38
38
  import { TRIZ_PARAMETERS, TRIZ_PRINCIPLES, lookupPrinciples, } from './operators/triz-matrix.js';
39
39
  import { composePrompt } from './prompt/compose.js';
40
+ import { composeOperatorSystemPrefix } from './llm/operatorFraming.js';
40
41
  import { parseLlmResponse } from './prompt/parse.js';
41
42
  import { defaultModelFor, runLlm, } from './llm/client.js';
42
43
  import { estimateCost } from './llm/cost.js';
@@ -293,7 +294,14 @@ export async function runOperator(client, args) {
293
294
  model: config.model,
294
295
  promptChars: prompt.length,
295
296
  });
296
- const llmResult = await runLlm(config, { prompt });
297
+ // Prompt-caching Step 2 mirror pass a stable operator-framing system
298
+ // prefix so the Anthropic prompt cache catches every repeat operator
299
+ // call within the 5-minute TTL. The MCP `runLlm` already wraps
300
+ // systemPrefix with `cache_control: ephemeral` for the Anthropic path.
301
+ const llmResult = await runLlm(config, {
302
+ prompt,
303
+ systemPrefix: composeOperatorSystemPrefix(),
304
+ });
297
305
  const parsed = parseLlmResponse(llmResult.text);
298
306
  if (!parsed.ok) {
299
307
  throw new Error(`Operator run produced output the parser rejected: ${parsed.error}`);
@@ -853,17 +853,14 @@ export async function listEdges(client, args) {
853
853
  // ---------------------------------------------------------------------------
854
854
  // find_concepts
855
855
  // ---------------------------------------------------------------------------
856
- // Mirrors the webapp `find_concepts` tool. Webapp version supports
857
- // by='meaning' (in-browser embeddings) and by='label'. The MCP runs server-
858
- // side without a local embedding model, so by='meaning' transparently falls
859
- // back to the same substring search as by='label' and the response carries a
860
- // `note` explaining the degradation. Phase 19.5 swaps in pgvector when the
861
- // schema picks it up.
856
+ // Mirrors the webapp `find_concepts` tool, label/substring path only. The
857
+ // webapp's by='meaning' variant relies on in-browser embeddings; that path is
858
+ // not wired on the MCP side, so this tool accepts by='label' only.
862
859
  export const findConceptsInput = z
863
860
  .object({
864
861
  query: z.string().min(1),
865
862
  k: z.number().int().positive().max(20).default(10),
866
- by: z.enum(['meaning', 'label']).default('meaning'),
863
+ by: z.enum(['label']).default('label'),
867
864
  projectId: z.string().optional(),
868
865
  })
869
866
  .strict();
@@ -885,13 +882,6 @@ export async function findConcepts(client, args) {
885
882
  status: r.status,
886
883
  score: 1,
887
884
  }));
888
- if (args.by === 'meaning') {
889
- return {
890
- hits,
891
- mode: 'label',
892
- note: 'Semantic search not yet wired in MCP (no in-browser embedding model); fell back to label/substring match.',
893
- };
894
- }
895
885
  return { hits, mode: 'label' };
896
886
  }
897
887
  // ===========================================================================
@@ -1591,7 +1581,7 @@ export const CLOUD_TOOLS = [
1591
1581
  },
1592
1582
  {
1593
1583
  name: 'find_concepts',
1594
- description: "Find concepts by label/substring match. (Webapp parity: by='meaning' is accepted but currently falls back to label match on the MCP — semantic search ships in Phase 19.5.) Returns up to k hits, each with id, label, status.",
1584
+ description: 'Find concepts by label/substring match. Returns up to k hits, each with id, label, status.',
1595
1585
  inputSchema: findConceptsInput,
1596
1586
  handler: async (client, args) => findConcepts(client, findConceptsInput.parse(args)),
1597
1587
  },
package/dist/gotrue.js ADDED
@@ -0,0 +1,84 @@
1
+ // Heuresis MCP — direct GoTrue refresh-token exchange.
2
+ //
3
+ // Why not `supabase-js` setSession/refreshSession?
4
+ // ------------------------------------------------
5
+ // We run headless in Node — there is no localStorage and no persisted
6
+ // session for supabase-js to read. The previous code tried to bootstrap a
7
+ // session with `client.auth.setSession({ access_token: '', refresh_token })`,
8
+ // relying on the old behavior where an empty access_token forced an immediate
9
+ // refresh from the refresh token. That trick is dead as of @supabase/auth-js
10
+ // 2.x: `_setSession` now guards at the very top —
11
+ //
12
+ // if (!currentSession.access_token || !currentSession.refresh_token) {
13
+ // throw new AuthSessionMissingError(); // → "Auth session missing!"
14
+ // }
15
+ //
16
+ // so an empty `access_token` throws `AuthSessionMissingError` ("Auth session
17
+ // missing!") BEFORE it ever attempts the refresh. `refreshSession()` has the
18
+ // same problem in a different shape — it leans on a stored session that does
19
+ // not exist here.
20
+ //
21
+ // The robust path is to hit the GoTrue token endpoint directly. It exchanges
22
+ // a bare refresh token for a fresh session with zero stored-state
23
+ // assumptions, and it is exactly what supabase-js does internally once it has
24
+ // a session — including refresh-token rotation.
25
+ export class RefreshTokenError extends Error {
26
+ constructor(msg) {
27
+ super(msg);
28
+ this.name = 'RefreshTokenError';
29
+ }
30
+ }
31
+ /**
32
+ * Exchange a refresh token for a fresh session via the GoTrue token endpoint:
33
+ *
34
+ * POST `${supabaseUrl}/auth/v1/token?grant_type=refresh_token`
35
+ * headers: { apikey, Authorization: Bearer <anon>, Content-Type: application/json }
36
+ * body: { refresh_token }
37
+ *
38
+ * Returns the full session (access_token + the rotated refresh_token + the
39
+ * user). Throws `RefreshTokenError` with an actionable message — including the
40
+ * response's keys when the expected token fields are missing — on any failure.
41
+ */
42
+ export async function exchangeRefreshToken(supabaseUrl, anonKey, refreshToken) {
43
+ if (!refreshToken) {
44
+ throw new RefreshTokenError('No refresh token to exchange (the value was empty/undefined). ' +
45
+ 'The pairing response did not carry a usable token.');
46
+ }
47
+ const url = `${supabaseUrl.replace(/\/$/, '')}/auth/v1/token?grant_type=refresh_token`;
48
+ let res;
49
+ try {
50
+ res = await fetch(url, {
51
+ method: 'POST',
52
+ headers: {
53
+ apikey: anonKey,
54
+ Authorization: `Bearer ${anonKey}`,
55
+ 'Content-Type': 'application/json',
56
+ },
57
+ body: JSON.stringify({ refresh_token: refreshToken }),
58
+ });
59
+ }
60
+ catch (err) {
61
+ throw new RefreshTokenError(`Could not reach the auth endpoint at ${url}: ${err instanceof Error ? err.message : String(err)}`);
62
+ }
63
+ let payload = null;
64
+ try {
65
+ payload = await res.json();
66
+ }
67
+ catch {
68
+ /* leave null — handled below */
69
+ }
70
+ if (!res.ok) {
71
+ const err = payload;
72
+ const detail = err?.error_description ?? err?.msg ?? err?.error ?? err?.message ?? `HTTP ${res.status}`;
73
+ throw new RefreshTokenError(`Token refresh failed (HTTP ${res.status}): ${detail}`);
74
+ }
75
+ const session = payload;
76
+ if (!session || !session.access_token || !session.refresh_token) {
77
+ const keys = session && typeof session === 'object'
78
+ ? Object.keys(session).join(', ') || '(empty object)'
79
+ : '(no JSON body)';
80
+ throw new RefreshTokenError(`Token refresh succeeded (HTTP ${res.status}) but the response is missing ` +
81
+ `access_token/refresh_token. Response keys: [${keys}].`);
82
+ }
83
+ return session;
84
+ }
package/dist/index.js CHANGED
@@ -24,6 +24,7 @@ import { getConcept as legacyGetConcept, getConceptInput as legacyGetConceptInpu
24
24
  import { CLOUD_TOOLS } from './cloudTools.js';
25
25
  import { readCredentials } from './credentials.js';
26
26
  import { CloudAuthError, getCloudClient } from './cloudClient.js';
27
+ import { ensureProxyAgent } from './proxy.js';
27
28
  import { helpCommand, loginCommand, logoutCommand, whoamiCommand, } from './cli.js';
28
29
  import { readRealtimeFlag, resolveSubscriptionWorkspaceId, startRealtimeSubscription, stripRealtimeFlags, } from './realtime.js';
29
30
  const VERSION = '0.2.0-alpha';
@@ -93,6 +94,9 @@ function makeLegacySnapshotTools(store) {
93
94
  ];
94
95
  }
95
96
  async function runServer() {
97
+ // Route outbound fetch (GoTrue refresh + PostgREST queries) through
98
+ // HTTPS_PROXY / HTTP_PROXY before any cloud call. No-op when unset.
99
+ await ensureProxyAgent(console.error);
96
100
  const creds = await readCredentials();
97
101
  const snapshotEnv = process.env.HEURESIS_SNAPSHOT;
98
102
  let tools;
@@ -141,7 +145,7 @@ async function runServer() {
141
145
  '[heuresis-mcp] Not configured.',
142
146
  '',
143
147
  'To use cloud mode (recommended):',
144
- ' npx @heuresis/mcp login',
148
+ ' npx -y -p @heuresis/mcp heuresis-mcp login',
145
149
  '',
146
150
  'To use legacy snapshot mode (deprecated, removed after 19.7):',
147
151
  ' HEURESIS_SNAPSHOT=/path/to/export.json npx @heuresis/mcp',
@@ -0,0 +1,53 @@
1
+ // VENDORED MIRROR of src/llm/operatorFraming.ts — keep in sync with the
2
+ // webapp. The mcp-server is independently buildable (its own tsconfig,
3
+ // its own deps), so we vendor the framing block here rather than reaching
4
+ // across the repo boundary.
5
+ //
6
+ // Heuresis's smaller operator doctrines (ASIT ~500-800 tok, Branch ~600-1200
7
+ // tok) sit right around or below Sonnet's 1,024-token minimum-cacheable-size
8
+ // threshold. Below that floor the Anthropic API silently does NOT cache the
9
+ // prefix — `cache_creation_input_tokens` comes back as 0 and the next call
10
+ // re-tokenizes everything at full price.
11
+ //
12
+ // IMPORTANT: this block is the cache prefix for the ENTIRE MCP operator
13
+ // path. Editing it invalidates every operator's cache. Keep BYTE-IDENTICAL
14
+ // with the webapp copy at src/llm/operatorFraming.ts.
15
+ export const OPERATOR_FRAMING_PREAMBLE = [
16
+ 'You are Heuresis\'s operator engine — a stateless transform that takes a parent CONCEPT plus structured context, applies one named OPERATOR (an ASIT tool, a TRIZ inventive principle, a C-K partition rule, a free-form lab prompt, etc.), and emits a JSON object proposing 1–8 child concepts.',
17
+ '',
18
+ 'CONTRACT.',
19
+ ' • You receive: the project brief, the concept-path from root to target, the target concept, a pool of validated knowledge (K), an operator definition (family / key / name / doctrine / prompt fragment), an optional graph-awareness <context> block (ancestry, sibling axes, existing labels), and optionally COMBINE inputs, an EXPLORE <branch>, or a free-form <angle>.',
20
+ ' • You return: ONE JSON object matching the requested schema. NEVER prose before or after, NEVER markdown fences, NEVER trailing commas, NEVER comments inside the JSON. The caller parses with strict-mode zod and rejects anything that does not match.',
21
+ ' • If the schema asks for partitions[], emit between 3 and 5 top-level partitions unless the operator explicitly says otherwise (EXPLORE allows 4–8). Each partition is a STANDALONE concept title (2–5 words, ≤60 chars, no parent-prefix, no trailing period), a 1–2 sentence description, a ≤5-word partitionAttribute naming the distinguishing AXIS, a 1–3 sentence rationale citing the operator and any K used, and a kReferences[] of K-ids you actually used.',
22
+ '',
23
+ 'DOCTRINE.',
24
+ ' • Stay faithful to the operator. ASIT operators MUST stay inside the closed world — never introduce alien components. TRIZ operators MUST honor the named inventive principle. C-K operators MUST treat C-nodes as undecidable noun-phrases (never solutions in disguise — verbs like "build", "add", "use", "implement", "create" at the start of a label belong in the parking lot, not in the C-tree).',
25
+ ' • Be additive, not redundant. The <context> block (when present) lists existing canvas labels and sibling axes — do not propose paraphrases of either. Where possible, partition on an axis NOT yet present in <sibling_axes>.',
26
+ ' • Be honest. Use the optional selfCritique field to surface the strongest assumption or risk in each partition. Do not flatter.',
27
+ ' • Cite K. If a knowledge item informed a partition, list its id in kReferences. If you needed a fact you do not have, propose it via newKnowledgeProposed (1–3 items, framed as questions, never invented numbers).',
28
+ '',
29
+ 'VOICE.',
30
+ ' • Plain, sharp, conversational. Active voice, short sentences. No sales hype, no poetry, no exclamation points. Never use an em dash ("—"); use a period, comma, colon, or parentheses instead.',
31
+ ' • Labels are concept titles, not sentences. Put long-form prose in description / rationale, never in label.',
32
+ '',
33
+ 'FAILURE MODES TO AVOID.',
34
+ ' • Restating the parent concept as a child.',
35
+ ' • Two partitions that decompose the same axis (collapse them or pick the stronger).',
36
+ ' • Children whose label contains the immediate parent partition\'s label.',
37
+ ' • Nesting children deeper than one level — a child MUST NOT carry its own children[].',
38
+ ' • Markdown / code fences around the JSON.',
39
+ ].join('\n');
40
+ /**
41
+ * Stable system prefix shared by every operator run on the MCP side.
42
+ * Composed of the framing preamble plus a short prologue paragraph so the
43
+ * cached prefix carries both the framing and the per-call lead-in.
44
+ */
45
+ export function composeOperatorSystemPrefix() {
46
+ return (OPERATOR_FRAMING_PREAMBLE +
47
+ '\n\n' +
48
+ 'You are assisting an inventive design session structured by C-K theory. ' +
49
+ 'The user grows a graph of concepts (C) drawing on a pool of validated ' +
50
+ 'knowledge (K). When asked to apply an operator from ASIT, TRIZ, or a ' +
51
+ 'free-form lab prompt, propose between 3 and 5 partitions of the TARGET ' +
52
+ 'concept (3–8 for EXPLORE) unless the operator instructions say otherwise.');
53
+ }
package/dist/proxy.js ADDED
@@ -0,0 +1,43 @@
1
+ // Heuresis MCP — proxy wiring for Node's global fetch dispatcher.
2
+ //
3
+ // Node 18-22's undici does NOT auto-honor HTTPS_PROXY / HTTP_PROXY (Node 24+
4
+ // does). Without this, every outbound fetch fails with "fetch failed" on
5
+ // networks that require an egress proxy — both the device-pairing + GoTrue
6
+ // refresh calls in the CLI and the SupabaseClient's PostgREST queries in the
7
+ // running MCP server. We install an undici ProxyAgent as the global
8
+ // dispatcher once, at process start.
9
+ //
10
+ // Idempotent across the whole process (module-level flag) and a no-op when no
11
+ // proxy var is set, so it is safe to call from every entry point.
12
+ //
13
+ // NOTE: this only covers `fetch` (PostgREST + auth). The Realtime websocket
14
+ // uses a separate transport that the global dispatcher does not touch; live
15
+ // sync behind a strict proxy is a known follow-up, but it is best-effort and
16
+ // fire-and-forget, so it never blocks tool calls.
17
+ let proxyAgentInstalled = false;
18
+ /**
19
+ * Route Node's global `fetch` through HTTPS_PROXY / HTTP_PROXY when set.
20
+ * Pass a logger to surface the routing decision (the CLI logs to stderr; the
21
+ * server passes console.error). Resolves once the dispatcher is in place.
22
+ */
23
+ export async function ensureProxyAgent(log = () => { }) {
24
+ if (proxyAgentInstalled)
25
+ return;
26
+ proxyAgentInstalled = true;
27
+ const proxyUrl = process.env.HTTPS_PROXY ||
28
+ process.env.https_proxy ||
29
+ process.env.HTTP_PROXY ||
30
+ process.env.http_proxy;
31
+ if (!proxyUrl)
32
+ return;
33
+ try {
34
+ // Dynamic import keeps undici off the cold-start path when no proxy is in
35
+ // play. It's a direct dependency, so this resolves.
36
+ const { ProxyAgent, setGlobalDispatcher } = await import('undici');
37
+ setGlobalDispatcher(new ProxyAgent(proxyUrl));
38
+ log(`(routing through proxy ${proxyUrl})`);
39
+ }
40
+ catch (err) {
41
+ log(`(could not configure proxy ${proxyUrl}: ${err instanceof Error ? err.message : String(err)})`);
42
+ }
43
+ }
package/package.json CHANGED
@@ -1,54 +1,56 @@
1
- {
2
- "name": "@heuresis/mcp",
3
- "version": "1.0.0-rc.1",
4
- "description": "Cloud-authenticated Model Context Protocol server for a Heuresis workspace. Logs into the user's Heuresis account and lets any MCP client (Claude Desktop, Claude Code, Cursor, custom agents) read and write the same workspace the webapp uses. 31 data tools, 3 operator tools (Branch/Matrix/C-K/ASIT/TRIZ/Free/Combine/Explore), and live Realtime change subscriptions.",
5
- "type": "module",
6
- "bin": {
7
- "heuresis-mcp": "dist/index.js"
8
- },
9
- "files": [
10
- "dist",
11
- "README.md"
12
- ],
13
- "scripts": {
14
- "build": "tsc",
15
- "start": "node dist/index.js",
16
- "dev": "tsc --watch",
17
- "prepublishOnly": "npm run build"
18
- },
19
- "publishConfig": {
20
- "access": "public"
21
- },
22
- "homepage": "https://heuresis.app/mcp",
23
- "repository": {
24
- "type": "git",
25
- "url": "git+https://github.com/ToremLabs/Heuresis.git",
26
- "directory": "mcp-server"
27
- },
28
- "keywords": [
29
- "mcp",
30
- "model-context-protocol",
31
- "heuresis",
32
- "ideation",
33
- "knowledge-graph",
34
- "claude-code",
35
- "claude-desktop",
36
- "cursor"
37
- ],
38
- "dependencies": {
39
- "@anthropic-ai/sdk": "^0.40.0",
40
- "@google/generative-ai": "^0.21.0",
41
- "@modelcontextprotocol/sdk": "^1.0.0",
42
- "@supabase/supabase-js": "^2.45.0",
43
- "openai": "^4.71.0",
44
- "zod": "^3.23.0"
45
- },
46
- "devDependencies": {
47
- "typescript": "^5.6.0",
48
- "@types/node": "^22.0.0"
49
- },
50
- "engines": {
51
- "node": ">=18"
52
- },
53
- "license": "AGPL-3.0-or-later"
54
- }
1
+ {
2
+ "name": "@heuresis/mcp",
3
+ "version": "1.0.0-rc.11",
4
+ "mcpName": "io.github.ToremLabs/heuresis",
5
+ "description": "Cloud-authenticated Model Context Protocol server for a Heuresis workspace. Logs into the user's Heuresis account and lets any MCP client (Claude Desktop, Claude Code, Cursor, custom agents) read and write the same workspace the webapp uses. 31 data tools, 3 operator tools (Branch/Matrix/C-K/ASIT/TRIZ/Free/Combine/Explore), and live Realtime change subscriptions.",
6
+ "type": "module",
7
+ "bin": {
8
+ "heuresis-mcp": "dist/index.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "README.md"
13
+ ],
14
+ "scripts": {
15
+ "build": "tsc",
16
+ "start": "node dist/index.js",
17
+ "dev": "tsc --watch",
18
+ "prepublishOnly": "npm run build"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "homepage": "https://heuresis.app/mcp",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/ToremLabs/Heuresis.git",
27
+ "directory": "mcp-server"
28
+ },
29
+ "keywords": [
30
+ "mcp",
31
+ "model-context-protocol",
32
+ "heuresis",
33
+ "ideation",
34
+ "knowledge-graph",
35
+ "claude-code",
36
+ "claude-desktop",
37
+ "cursor"
38
+ ],
39
+ "dependencies": {
40
+ "@anthropic-ai/sdk": "^0.40.0",
41
+ "@google/generative-ai": "^0.21.0",
42
+ "@modelcontextprotocol/sdk": "^1.0.0",
43
+ "@supabase/supabase-js": "^2.45.0",
44
+ "openai": "^4.71.0",
45
+ "undici": "^6.25.0",
46
+ "zod": "^3.23.0"
47
+ },
48
+ "devDependencies": {
49
+ "@types/node": "^22.0.0",
50
+ "typescript": "^5.6.0"
51
+ },
52
+ "engines": {
53
+ "node": ">=18"
54
+ },
55
+ "license": "AGPL-3.0-or-later"
56
+ }