@deepsql/mcp 0.21.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,293 +0,0 @@
1
- "use strict";
2
-
3
- // Lazy bootstrap for the DeepSQL Agent (Hermes-based TUI): detect/install the
4
- // Hermes runtime and provision the `deepsql` profile (model → the backend LLM
5
- // proxy with the user's token; DeepSQL MCP tools; DBA persona/skills; subtle
6
- // DeepSQL skin; read-only sandbox). Provisioning is idempotent; the per-launch
7
- // config rewrite keeps the token/connection fresh.
8
-
9
- const fs = require("node:fs");
10
- const os = require("node:os");
11
- const path = require("node:path");
12
- const { spawnSync } = require("node:child_process");
13
- const { userHome } = require("../user-home");
14
-
15
- const HOME = userHome();
16
- const HERMES_HOME = path.join(HOME, ".hermes");
17
- const AGENT_DIR = path.join(HERMES_HOME, "hermes-agent");
18
- const PROFILE = "deepsql";
19
- const PROFILE_HOME = path.join(HERMES_HOME, "profiles", PROFILE);
20
-
21
- const INSTALL_URL = "https://hermes-agent.nousresearch.com/install.sh";
22
- // Pin the Hermes runtime to the exact commit our bundled TUI (agent-profile/
23
- // tui/entry.js, HERMES_VERSION 0.17.0) was built against. This makes installs
24
- // reproducible AND guarantees the branded-TUI overlay's version guard matches,
25
- // so end users get the DeepSQL wordmark/welcome — not a "latest main" build
26
- // that drifts in behavior and skips our overlay. Bump alongside the bundle.
27
- const HERMES_COMMIT = "92d40c2553961243991376bf889d833e8326caf7";
28
-
29
- // Extra dirs the Hermes runtime + its installer rely on but a bare GUI/login
30
- // shell PATH (as seen by a spawned Node process) often lacks: the venv, the
31
- // installer's `hermes` symlink targets, and Homebrew (uv/node/git).
32
- function extraPathDirs() {
33
- return [
34
- path.join(AGENT_DIR, ".venv", "bin"),
35
- path.join(HOME, ".local", "bin"),
36
- "/usr/local/bin",
37
- "/opt/homebrew/bin",
38
- "/usr/local/lib/hermes-agent/.venv/bin",
39
- ];
40
- }
41
-
42
- // Env for any spawn that runs (or installs) Hermes: augmented PATH so node/uv/
43
- // git resolve, UV_NO_CONFIG so uv ignores stray project configs, and a pinned
44
- // HERMES_HOME so the install/profile lands where we expect.
45
- function hermesEnv(extra = {}) {
46
- const sep = path.delimiter;
47
- const have = (process.env.PATH || "").split(sep);
48
- const merged = [...extraPathDirs().filter((d) => !have.includes(d)), ...have].join(sep);
49
- return { ...process.env, PATH: merged, UV_NO_CONFIG: "1", HERMES_HOME, ...extra };
50
- }
51
-
52
- // All the places the official installer may drop a runnable `hermes` for a
53
- // non-root install (venv) or an FHS/root install (/usr/local), plus the PATH.
54
- function findHermes() {
55
- const candidates = [];
56
- if (process.env.HERMES_INSTALL_DIR) {
57
- candidates.push(path.join(process.env.HERMES_INSTALL_DIR, ".venv", "bin", "hermes"));
58
- }
59
- candidates.push(
60
- path.join(AGENT_DIR, ".venv", "bin", "hermes"),
61
- "/usr/local/lib/hermes-agent/.venv/bin/hermes",
62
- path.join(HOME, ".local", "bin", "hermes"),
63
- "/usr/local/bin/hermes"
64
- );
65
- for (const bin of candidates) {
66
- if (!fs.existsSync(bin)) continue;
67
- if (spawnSync(bin, ["--version"], { stdio: "ignore", env: hermesEnv() }).status === 0) return bin;
68
- }
69
- // Last resort: a `hermes` already on the (augmented) PATH.
70
- if (spawnSync("hermes", ["--version"], { stdio: "ignore", env: hermesEnv() }).status === 0) return "hermes";
71
- return null;
72
- }
73
-
74
- function hermesBin() {
75
- return findHermes() || path.join(AGENT_DIR, ".venv", "bin", "hermes");
76
- }
77
-
78
- function hermesInstalled() {
79
- return findHermes() !== null;
80
- }
81
-
82
- function mcpServerPath() {
83
- return path.resolve(__dirname, "..", "..", "deepsql-phase1-server.js");
84
- }
85
-
86
- // Profile distribution (SOUL.md + skills/ + skins/): the bundled copy shipped in
87
- // the package, else the repo `hermes/` dir during local development.
88
- function distSource() {
89
- const bundled = path.resolve(__dirname, "..", "..", "agent-profile");
90
- if (fs.existsSync(path.join(bundled, "distribution.yaml"))) return bundled;
91
- const repoHermes = path.resolve(__dirname, "..", "..", "..", "hermes");
92
- if (fs.existsSync(path.join(repoHermes, "distribution.yaml"))) return repoHermes;
93
- return null;
94
- }
95
-
96
- // Install the Hermes runtime, pinned + non-interactive, with the output teed to
97
- // a log so a failure surfaces a real cause instead of a vague "can't set up".
98
- // `quiet` (postinstall) hides the live stream; interactive first-run shows it.
99
- function ensureHermes(io, { quiet = false } = {}) {
100
- const err = (io && io.stderr) || process.stderr;
101
- if (hermesInstalled()) return true;
102
-
103
- err.write("DeepSQL Agent: installing the agent runtime (first run; ~1–2 min)…\n");
104
- const logFile = path.join(os.tmpdir(), `deepsql-agent-install-${process.pid}.log`);
105
- // --commit pins the checkout; --skip-setup/--non-interactive avoid the
106
- // wizard + any TTY prompt. tee keeps a persisted log for diagnostics.
107
- const script =
108
- `curl -fsSL ${INSTALL_URL} | bash -s -- ` +
109
- `--skip-setup --non-interactive --commit ${HERMES_COMMIT} 2>&1 | tee ${JSON.stringify(logFile)}`;
110
- const r = spawnSync("bash", ["-lc", script], {
111
- stdio: quiet ? ["ignore", "ignore", "ignore"] : "inherit",
112
- env: hermesEnv(),
113
- });
114
-
115
- if (hermesInstalled()) {
116
- err.write("✓ DeepSQL Agent runtime installed. (Ignore any \"run hermes setup\" note above — DeepSQL configures it for you.)\n");
117
- return true;
118
- }
119
- err.write(
120
- "DeepSQL Agent: the runtime install did not complete.\n" +
121
- ` Install log: ${logFile}\n` +
122
- ` Exit code: ${r.status == null ? "n/a" : r.status}\n` +
123
- " You can retry, or install Hermes manually: " +
124
- `curl -fsSL ${INSTALL_URL} | bash -s -- --commit ${HERMES_COMMIT}\n`
125
- );
126
- return false;
127
- }
128
-
129
- function q(s) {
130
- return `"${String(s).replace(/"/g, '\\"')}"`;
131
- }
132
-
133
- function writeRuntimeConfig(session) {
134
- fs.mkdirSync(PROFILE_HOME, { recursive: true });
135
- const base = session.baseUrl.replace(/\/?$/, "/"); // ensure single trailing /
136
- const proxy = base + "api/llm/v1";
137
- const apiBase = base + "api/";
138
- const token = session.token;
139
- const mcp = mcpServerPath();
140
- const conn = session.defaultConnection || "";
141
-
142
- const config =
143
- `model:
144
- default: gpt-5.4
145
- provider: custom
146
- base_url: ${q(proxy)}
147
- api_key: ${q(token)}
148
- api_mode: chat_completions
149
- context_length: 272000
150
- mcp_servers:
151
- deepsql:
152
- command: node
153
- args:
154
- - ${q(mcp)}
155
- env:
156
- DEEPSQL_API_BASE_URL: ${q(apiBase)}
157
- DEEPSQL_MCP_USER_ID: deepsql-cli
158
- DEEPSQL_AUTH_TOKEN: ${q(token)}
159
- approvals:
160
- mode: smart
161
- display:
162
- skin: deepsql
163
- interface: tui
164
- `;
165
- fs.writeFileSync(path.join(PROFILE_HOME, "config.yaml"), config);
166
-
167
- // OPENAI_BASE_URL/OPENAI_API_KEY are belt-and-suspenders: Hermes' first-run
168
- // guard (_has_any_provider_configured) treats either as "a provider is
169
- // configured", so the "run hermes setup" gate can never fire on a fresh
170
- // machine even if config-load timing differs. They point at the SAME backend
171
- // proxy as the model block, authed by the user's DeepSQL token.
172
- const env =
173
- `DEEPSQL_API_BASE_URL=${apiBase}
174
- DEEPSQL_AUTH_TOKEN=${token}
175
- DEEPSQL_MCP_SERVER=${mcp}
176
- DEEPSQL_DEFAULT_CONNECTION_ID=${conn}
177
- OPENAI_BASE_URL=${proxy}
178
- OPENAI_API_KEY=${token}
179
- HERMES_REVISION=deepsql-cli
180
- `;
181
- fs.writeFileSync(path.join(PROFILE_HOME, ".env"), env, { mode: 0o600 });
182
- }
183
-
184
- // Env for spawning the TUI: augmented PATH (so the TUI's node + the MCP
185
- // server's node resolve), the provider vars that defeat Hermes' first-run
186
- // guard regardless of config state, and HERMES_TUI_DIR pointing at our prebuilt
187
- // branded TUI (when the Hermes version matches) so Hermes runs it directly with
188
- // no esbuild — branded on the first launch.
189
- function launchEnv(session) {
190
- const base = session.baseUrl.replace(/\/?$/, "/");
191
- const extra = {
192
- OPENAI_BASE_URL: base + "api/llm/v1",
193
- OPENAI_API_KEY: session.token,
194
- };
195
- const tui = brandedTuiDir();
196
- if (tui) extra.HERMES_TUI_DIR = tui;
197
- return hermesEnv(extra);
198
- }
199
-
200
- function installProfileAssets(io) {
201
- const err = (io && io.stderr) || process.stderr;
202
- const src = distSource();
203
- if (!src) {
204
- err.write("DeepSQL agent profile assets not found in this install.\n");
205
- return false;
206
- }
207
- const bin = hermesBin();
208
- const baseEnv = hermesEnv();
209
- const inst = spawnSync(
210
- bin,
211
- ["profile", "install", src, "--name", PROFILE, "--force", "--yes"],
212
- { encoding: "utf8", env: baseEnv }
213
- );
214
- if (inst.status !== 0) {
215
- err.write(
216
- "DeepSQL Agent: provisioning the profile failed.\n" +
217
- ` ${(inst.stderr || inst.stdout || "(no output)").trim().split("\n").slice(-6).join("\n ")}\n`
218
- );
219
- return false;
220
- }
221
- // Skin loads from <profile>/skins/.
222
- const skin = path.join(src, "skins", "deepsql.yaml");
223
- if (fs.existsSync(skin)) {
224
- const dst = path.join(PROFILE_HOME, "skins");
225
- fs.mkdirSync(dst, { recursive: true });
226
- fs.copyFileSync(skin, path.join(dst, "deepsql.yaml"));
227
- }
228
- // Read-only sandbox: only deepsql + skills/memory remain. Non-fatal — the
229
- // shipped distribution already scopes tools; this is defense in depth.
230
- spawnSync(
231
- bin,
232
- ["tools", "disable", "terminal", "file", "code_execution", "browser", "computer_use",
233
- "image_gen", "tts", "vision", "web", "delegation", "cronjob"],
234
- { stdio: "ignore", env: hermesEnv({ HERMES_HOME: PROFILE_HOME }) }
235
- );
236
- return fs.existsSync(PROFILE_HOME);
237
- }
238
-
239
- function ensureProfile(session, io) {
240
- if (!fs.existsSync(path.join(PROFILE_HOME, "config.yaml"))) {
241
- if (!installProfileAssets(io)) return false;
242
- }
243
- writeRuntimeConfig(session); // always refresh the token/connection for this launch
244
- return true;
245
- }
246
-
247
- // Installed Hermes version (parsed from the runtime's __init__.py). Null if the
248
- // runtime layout isn't what we expect (e.g. PATH-only install).
249
- function installedHermesVersion() {
250
- try {
251
- const init = path.join(AGENT_DIR, "hermes_cli", "__init__.py");
252
- const m = fs.readFileSync(init, "utf8").match(/__version__\s*=\s*["']([^"']+)["']/);
253
- return m ? m[1] : null;
254
- } catch {
255
- return null;
256
- }
257
- }
258
-
259
- // The DeepSQL-branded prebuilt TUI bundle shipped in the package, as a directory
260
- // laid out the way Hermes' `HERMES_TUI_DIR` mechanism expects: `<dir>/dist/entry.js`.
261
- // When that env var points here, Hermes runs our entry.js DIRECTLY — no npm
262
- // install, no esbuild — so the custom DEEPSQL wordmark / welcome panel / DBA
263
- // placeholders show on the very FIRST launch. (The earlier copy-into-the-clone
264
- // approach lost every launch because Hermes "always esbuild"s the normal path.)
265
- //
266
- // Guarded by the Hermes version our bundle was built against: a prebuilt entry.js
267
- // speaks one release's TUI↔CLI protocol, so we only point at it on an exact
268
- // version match (the runtime is pinned to HERMES_COMMIT, so it matches on our
269
- // installs). On any mismatch / missing bundle we return null and let Hermes use
270
- // its own (stock) TUI, which still renders our skin (DeepSQL name + maroon).
271
- function brandedTuiDir() {
272
- try {
273
- const tuiDir = path.resolve(__dirname, "..", "..", "agent-profile", "tui");
274
- if (!fs.existsSync(path.join(tuiDir, "dist", "entry.js"))) return null;
275
- const builtFor = fs.readFileSync(path.join(tuiDir, "HERMES_VERSION"), "utf8").trim();
276
- const installed = installedHermesVersion();
277
- if (!builtFor || !installed || builtFor !== installed) return null;
278
- return tuiDir;
279
- } catch {
280
- return null;
281
- }
282
- }
283
-
284
- module.exports = {
285
- ensureHermes,
286
- ensureProfile,
287
- brandedTuiDir,
288
- hermesBin,
289
- hermesInstalled,
290
- launchEnv,
291
- PROFILE,
292
- PROFILE_HOME,
293
- };